Skip to content

Commit abb00cc

Browse files
committed
url: throw for invalid values to url.format
`'use strict'` changes the behavior for `Function.prototype.call` when the context is `undefined`. In earlier versions of node the value `undefined` would make `url.format` look for fields in the global scope. The docs states that `url.format` takes a parsed URL object and returns a formatted URL string. So with this change it will now throw for other values. The exception is if the input is a string. Then it will call `url.parse` on the string and then format it. The reason for that is that you can call `url.format` on strings to clean up potentially wonky urls. Fixes: #1033 PR-URL: #1036 Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Brian White <[email protected]> Reviewed-By: Julian Duque <[email protected]>
1 parent 3114241 commit abb00cc

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/url.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,13 @@ function urlFormat(obj) {
353353
// this way, you can call url_format() on strings
354354
// to clean up potentially wonky urls.
355355
if (typeof obj === 'string') obj = urlParse(obj);
356-
if (!(obj instanceof Url)) return Url.prototype.format.call(obj);
356+
357+
else if (typeof obj !== 'object' || obj === null)
358+
throw new TypeError("Parameter 'urlObj' must be an object, not " +
359+
obj === null ? 'null' : typeof obj);
360+
361+
else if (!(obj instanceof Url)) return Url.prototype.format.call(obj);
362+
357363
return obj.format();
358364
}
359365

test/parallel/test-url.js

+17
Original file line numberDiff line numberDiff line change
@@ -1557,3 +1557,20 @@ relativeTests2.forEach(function(relativeTest) {
15571557
'format(' + relativeTest[1] + ') == ' + expected +
15581558
'\nactual:' + actual);
15591559
});
1560+
1561+
1562+
1563+
// https://github.com/iojs/io.js/pull/1036
1564+
var throws = [
1565+
undefined,
1566+
null,
1567+
true,
1568+
false,
1569+
0,
1570+
function () {}
1571+
];
1572+
for (var i = 0; i < throws.length; i++) {
1573+
assert.throws(function () { url.format(throws[i]); }, TypeError);
1574+
};
1575+
assert(url.format('') === '');
1576+
assert(url.format({}) === '');

0 commit comments

Comments
 (0)