From 227e749888bda940237bd7045fae07910dbb9be5 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 1cb733e1ae2490..9e26e42b27a229 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -623,6 +623,10 @@ function isURL(self) { class URL { constructor(input, base = undefined) { + if (arguments.length === 0) { + throw new ERR_MISSING_ARGS('url'); + } + // toUSVString is not needed. input = `${input}`; this[context] = new URLContext(); 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', +});