From 180d3654394d6e25cf0435f30f56b7daf0d83c3f Mon Sep 17 00:00:00 2001 From: Khafra Date: Tue, 11 Apr 2023 11:35:46 -0400 Subject: [PATCH] url: validate URL constructor arg length PR-URL: https://github.com/nodejs/node/pull/47513 Reviewed-By: Yagiz Nizipli Reviewed-By: Benjamin Gruenbaum Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Luigi Pinca Reviewed-By: Filip Skokan --- lib/internal/url.js | 4 ++++ test/parallel/test-url-canParse-whatwg.js | 24 +++++++++---------- .../test-whatwg-url-custom-parsing.js | 7 ++++++ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index b6412dd6a5480b..a6dc1272b0ce6b 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -706,6 +706,10 @@ class URL { #searchParams; constructor(input, base = undefined) { + if (arguments.length === 0) { + throw new ERR_MISSING_ARGS('url'); + } + // toUSVString is not needed. input = `${input}`; diff --git a/test/parallel/test-url-canParse-whatwg.js b/test/parallel/test-url-canParse-whatwg.js index 997c90c343c2f2..7f7d5d40aa7a28 100644 --- a/test/parallel/test-url-canParse-whatwg.js +++ b/test/parallel/test-url-canParse-whatwg.js @@ -1,12 +1,12 @@ -'use strict'; - -require('../common'); -const assert = require('assert'); - -// One argument is required -assert.throws(() => { - URL.canParse(); -}, { - code: 'ERR_MISSING_ARGS', - name: 'TypeError' -}); +'use strict'; + +require('../common'); +const assert = require('assert'); + +// One argument is required +assert.throws(() => { + URL.canParse(); +}, { + code: 'ERR_MISSING_ARGS', + name: 'TypeError', +}); diff --git a/test/parallel/test-whatwg-url-custom-parsing.js b/test/parallel/test-whatwg-url-custom-parsing.js index a3532374ca684e..905028fee3812c 100644 --- a/test/parallel/test-whatwg-url-custom-parsing.js +++ b/test/parallel/test-whatwg-url-custom-parsing.js @@ -78,3 +78,10 @@ for (const test of additional_tests) { if (test.search) assert.strictEqual(url.search, test.search); if (test.hash) assert.strictEqual(url.hash, test.hash); } + +assert.throws(() => { + new URL(); +}, { + name: 'TypeError', + code: 'ERR_MISSING_ARGS', +});