Skip to content

Commit 24c2249

Browse files
simon-idaduh95
authored andcommitted
url: add type checking to urlToHttpOptions()
PR-URL: #59753 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Bryan English <[email protected]>
1 parent 9833dc6 commit 24c2249

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/internal/url.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ const { Buffer } = require('buffer');
9090

9191
const {
9292
validateFunction,
93+
validateObject,
94+
kValidateObjectAllowObjects,
9395
} = require('internal/validators');
9496

9597
const { percentDecode } = require('internal/data_url');
@@ -1430,6 +1432,7 @@ function domainToUnicode(domain) {
14301432
* @returns {Record<string, unknown>}
14311433
*/
14321434
function urlToHttpOptions(url) {
1435+
validateObject(url, 'url', kValidateObjectAllowObjects);
14331436
const { hostname, pathname, port, username, password, search } = url;
14341437
const options = {
14351438
__proto__: null,

test/parallel/test-url-urltooptions.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,12 @@ assert.strictEqual(copiedOpts.pathname, undefined);
3535
assert.strictEqual(copiedOpts.search, undefined);
3636
assert.strictEqual(copiedOpts.hash, undefined);
3737
assert.strictEqual(copiedOpts.href, undefined);
38+
39+
// Test when passed an invalid argument
40+
assert.throws(() => {
41+
urlToHttpOptions('http://127.0.0.1');
42+
}, {
43+
code: 'ERR_INVALID_ARG_TYPE',
44+
message: 'The "url" argument must be of type object. Received type string (\'http://127.0.0.1\')',
45+
name: 'TypeError'
46+
});

0 commit comments

Comments
 (0)