Skip to content

Commit 6c82c2d

Browse files
simon-idtargos
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 2049b82 commit 6c82c2d

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
@@ -91,6 +91,8 @@ const { Buffer } = require('buffer');
9191

9292
const {
9393
validateFunction,
94+
validateObject,
95+
kValidateObjectAllowObjects,
9496
} = require('internal/validators');
9597

9698
const { percentDecode } = require('internal/data_url');
@@ -1431,6 +1433,7 @@ function domainToUnicode(domain) {
14311433
* @returns {Record<string, unknown>}
14321434
*/
14331435
function urlToHttpOptions(url) {
1436+
validateObject(url, 'url', kValidateObjectAllowObjects);
14341437
const { hostname, pathname, port, username, password, search } = url;
14351438
const options = {
14361439
__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)