From edd8f6e9989ab05d819f664a1130e51bd4970385 Mon Sep 17 00:00:00 2001 From: Priyank Singh Date: Fri, 17 Jul 2020 20:12:34 +0530 Subject: [PATCH 1/9] http: fixed invalid path with connect method Fixes: https://github.com/nodejs/node/issues/34347 --- lib/_http_client.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/_http_client.js b/lib/_http_client.js index 9e2ebca8ee5c..0c66fd737325 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -197,6 +197,9 @@ function ClientRequest(input, options, cb) { } this.insecureHTTPParser = insecureHTTPParser; + options.path = method === 'CONNECT' && options.path.charAt(0) === '/' ? + options.path.slice(1) : options.path; + this.path = options.path || '/'; if (cb) { this.once('response', cb); From 0825564bfad6669682d45bf0dc7f1d3ac2254e1f Mon Sep 17 00:00:00 2001 From: Priyank Singh Date: Fri, 17 Jul 2020 21:02:08 +0530 Subject: [PATCH 2/9] http: fixed invalid path with connect method Added tests Fixes: https://github.com/nodejs/node/issues/34347 --- .../test-http-request-connect-method.js | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 test/parallel/test-http-request-connect-method.js diff --git a/test/parallel/test-http-request-connect-method.js b/test/parallel/test-http-request-connect-method.js new file mode 100644 index 000000000000..92e1b4c89e46 --- /dev/null +++ b/test/parallel/test-http-request-connect-method.js @@ -0,0 +1,25 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const http = require('http'); + +const server = http.createServer(); + +server.on('connect', common.mustCall((req, stream) => { + assert.notStrictEqual(req.url.charAt(0), '/'); + stream.end(); +})); + +server.listen(0); + +server.on('listening', common.mustCall(() => { + const url = new URL(`http://localhost:${server.address().port}/example.com`); + + const req = http.request(url, {method: 'CONNECT'}).end(); + req.once('connect', common.mustCall(res => { + res.destroy(); + server.close(); + })) +})); + From 247401a98db089aba9521f76dfc1b7081b12bdf5 Mon Sep 17 00:00:00 2001 From: Priyank Singh Date: Fri, 17 Jul 2020 21:21:57 +0530 Subject: [PATCH 3/9] Fixed linting --- lib/_http_client.js | 1 - .../test-http-request-connect-method.js | 17 ++++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index 0c66fd737325..a82e418b7e52 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -199,7 +199,6 @@ function ClientRequest(input, options, cb) { options.path = method === 'CONNECT' && options.path.charAt(0) === '/' ? options.path.slice(1) : options.path; - this.path = options.path || '/'; if (cb) { this.once('response', cb); diff --git a/test/parallel/test-http-request-connect-method.js b/test/parallel/test-http-request-connect-method.js index 92e1b4c89e46..2ecebe3595f4 100644 --- a/test/parallel/test-http-request-connect-method.js +++ b/test/parallel/test-http-request-connect-method.js @@ -7,19 +7,18 @@ const http = require('http'); const server = http.createServer(); server.on('connect', common.mustCall((req, stream) => { - assert.notStrictEqual(req.url.charAt(0), '/'); - stream.end(); + assert.notStrictEqual(req.url.charAt(0), '/'); + stream.end(); })); server.listen(0); server.on('listening', common.mustCall(() => { - const url = new URL(`http://localhost:${server.address().port}/example.com`); + const url = new URL(`http://localhost:${server.address().port}/example.com`); - const req = http.request(url, {method: 'CONNECT'}).end(); - req.once('connect', common.mustCall(res => { - res.destroy(); - server.close(); - })) + const req = http.request(url, { method: 'CONNECT' }).end(); + req.once('connect', common.mustCall((res) => { + res.destroy(); + server.close(); + })); })); - From 989cecd1b3ebf0270bd8b73002b9edc315f74997 Mon Sep 17 00:00:00 2001 From: Priyank Singh Date: Fri, 17 Jul 2020 21:38:02 +0530 Subject: [PATCH 4/9] checked for undefined options.path --- lib/_http_client.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index a82e418b7e52..3f23ebfa2647 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -197,7 +197,8 @@ function ClientRequest(input, options, cb) { } this.insecureHTTPParser = insecureHTTPParser; - options.path = method === 'CONNECT' && options.path.charAt(0) === '/' ? + options.path = options.path && method === 'CONNECT' && + options.path.charAt(0) === '/' ? options.path.slice(1) : options.path; this.path = options.path || '/'; if (cb) { From e1283be09dfa1064c3ccfc4dd82591c6de2bf5ba Mon Sep 17 00:00:00 2001 From: Priyank Singh Date: Wed, 22 Jul 2020 02:27:32 +0530 Subject: [PATCH 5/9] Added check for valid host:port path --- doc/api/errors.md | 5 +++++ lib/_http_client.js | 12 +++++++---- lib/internal/errors.js | 2 ++ lib/internal/http.js | 16 ++++++++++++++- .../test-http-request-connect-method.js | 20 ++++++++++++++++--- 5 files changed, 47 insertions(+), 8 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index db3ea5af4be9..58d272ab0551 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1362,6 +1362,11 @@ An attempt was made to send an unsupported "handle" over an IPC communication channel to a child process. See [`subprocess.send()`][] and [`process.send()`][] for more information. + +### `ERR_INVALID_HOST_PORT_COMBO` + +The provided path is not a valid host:port combo. + ### `ERR_INVALID_HTTP_TOKEN` diff --git a/lib/_http_client.js b/lib/_http_client.js index 3f23ebfa2647..0b33f79cc8b1 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -49,7 +49,7 @@ const Agent = require('_http_agent'); const { Buffer } = require('buffer'); const { defaultTriggerAsyncIdScope } = require('internal/async_hooks'); const { URL, urlToOptions, searchParamsSymbol } = require('internal/url'); -const { kOutHeaders, kNeedDrain } = require('internal/http'); +const { kOutHeaders, kNeedDrain, validatePath } = require('internal/http'); const { connResetException, codes } = require('internal/errors'); const { ERR_HTTP_HEADERS_SENT, @@ -197,10 +197,14 @@ function ClientRequest(input, options, cb) { } this.insecureHTTPParser = insecureHTTPParser; - options.path = options.path && method === 'CONNECT' && + this.path = (options.path && method === 'CONNECT' && options.path.charAt(0) === '/' ? - options.path.slice(1) : options.path; - this.path = options.path || '/'; + options.path.slice(1) : options.path) || '/'; + + if (method === 'CONNECT' && this.path !== '/') { + validatePath(this.path); + } + if (cb) { this.once('response', cb); } diff --git a/lib/internal/errors.js b/lib/internal/errors.js index a9c69eda7b0e..7173678c9dee 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -1108,6 +1108,8 @@ E('ERR_INVALID_FILE_URL_HOST', 'File URL host must be "localhost" or empty on %s', TypeError); E('ERR_INVALID_FILE_URL_PATH', 'File URL path %s', TypeError); E('ERR_INVALID_HANDLE_TYPE', 'This handle type cannot be sent', TypeError); +E('ERR_INVALID_HOST_PORT_COMBO', + 'Path must be a valid : combo', TypeError); E('ERR_INVALID_HTTP_TOKEN', '%s must be a valid HTTP token ["%s"]', TypeError); E('ERR_INVALID_IP_ADDRESS', 'Invalid IP address: %s', TypeError); E('ERR_INVALID_MODULE_SPECIFIER', (request, reason, base = undefined) => { diff --git a/lib/internal/http.js b/lib/internal/http.js index aab4170a2f0e..26ea12507180 100644 --- a/lib/internal/http.js +++ b/lib/internal/http.js @@ -6,6 +6,11 @@ const { const { setUnrefTimeout } = require('internal/timers'); const { PerformanceEntry, notify } = internalBinding('performance'); +const { + codes: { + ERR_INVALID_HOST_PORT_COMBO + } +} = require('internal/errors'); let nowCache; let utcCache; @@ -32,6 +37,14 @@ function resetCache() { utcCache = undefined; } +function validatePath(path) { + const pathRegex = /^[_0-9A-Za-z]+(\.[_0-9A-Za-z]+)*\.?:([1-9]|[1-9][0-9]{1,2}|[1-5][0-9]{3}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/; + if (!pathRegex.test(path)) { + throw new ERR_INVALID_HOST_PORT_COMBO(); + } + return path; +} + class HttpRequestTiming extends PerformanceEntry { constructor(statistics) { super(); @@ -53,5 +66,6 @@ module.exports = { kNeedDrain: Symbol('kNeedDrain'), nowDate, utcDate, - emitStatistics + emitStatistics, + validatePath }; diff --git a/test/parallel/test-http-request-connect-method.js b/test/parallel/test-http-request-connect-method.js index 2ecebe3595f4..17c092e69c58 100644 --- a/test/parallel/test-http-request-connect-method.js +++ b/test/parallel/test-http-request-connect-method.js @@ -1,20 +1,23 @@ +// Flags: --expose-internals + 'use strict'; const common = require('../common'); const assert = require('assert'); const http = require('http'); +const { validatePath } = require('internal/http'); const server = http.createServer(); server.on('connect', common.mustCall((req, stream) => { - assert.notStrictEqual(req.url.charAt(0), '/'); - stream.end(); + assert.strictEqual(req.url, 'example.com'); + stream.end('HTTP/1.1 501 Not Implemented\r\n\r\n'); })); server.listen(0); server.on('listening', common.mustCall(() => { - const url = new URL(`http://localhost:${server.address().port}/example.com`); + const url = new URL(`http://localhost:${server.address().port}/example.com:80`); const req = http.request(url, { method: 'CONNECT' }).end(); req.once('connect', common.mustCall((res) => { @@ -22,3 +25,14 @@ server.on('listening', common.mustCall(() => { server.close(); })); })); + +['example.com', 'example.com:0', 'example.com:65536'].forEach((path) => { + assert.throws( + () => validatePath(path), + { + code: 'ERR_INVALID_HOST_PORT_COMBO', + name: 'TypeError', + message: 'Path must be a valid : combo' + } + ); +}); From 669ee6507a732b948b0e1a332c5083d5bc8fa8bb Mon Sep 17 00:00:00 2001 From: Priyank Singh Date: Wed, 22 Jul 2020 20:21:47 +0530 Subject: [PATCH 6/9] Fixes --- doc/api/errors.md | 5 ----- lib/internal/errors.js | 2 -- lib/internal/http.js | 5 +++-- .../test-http-request-connect-method.js | 21 ++++++++++++++----- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index 58d272ab0551..db3ea5af4be9 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1362,11 +1362,6 @@ An attempt was made to send an unsupported "handle" over an IPC communication channel to a child process. See [`subprocess.send()`][] and [`process.send()`][] for more information. - -### `ERR_INVALID_HOST_PORT_COMBO` - -The provided path is not a valid host:port combo. - ### `ERR_INVALID_HTTP_TOKEN` diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 7173678c9dee..a9c69eda7b0e 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -1108,8 +1108,6 @@ E('ERR_INVALID_FILE_URL_HOST', 'File URL host must be "localhost" or empty on %s', TypeError); E('ERR_INVALID_FILE_URL_PATH', 'File URL path %s', TypeError); E('ERR_INVALID_HANDLE_TYPE', 'This handle type cannot be sent', TypeError); -E('ERR_INVALID_HOST_PORT_COMBO', - 'Path must be a valid : combo', TypeError); E('ERR_INVALID_HTTP_TOKEN', '%s must be a valid HTTP token ["%s"]', TypeError); E('ERR_INVALID_IP_ADDRESS', 'Invalid IP address: %s', TypeError); E('ERR_INVALID_MODULE_SPECIFIER', (request, reason, base = undefined) => { diff --git a/lib/internal/http.js b/lib/internal/http.js index 26ea12507180..9139f2049265 100644 --- a/lib/internal/http.js +++ b/lib/internal/http.js @@ -8,7 +8,7 @@ const { setUnrefTimeout } = require('internal/timers'); const { PerformanceEntry, notify } = internalBinding('performance'); const { codes: { - ERR_INVALID_HOST_PORT_COMBO + ERR_INVALID_ARG_VALUE } } = require('internal/errors'); @@ -40,7 +40,8 @@ function resetCache() { function validatePath(path) { const pathRegex = /^[_0-9A-Za-z]+(\.[_0-9A-Za-z]+)*\.?:([1-9]|[1-9][0-9]{1,2}|[1-5][0-9]{3}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/; if (!pathRegex.test(path)) { - throw new ERR_INVALID_HOST_PORT_COMBO(); + throw new ERR_INVALID_ARG_VALUE('options.path', + path, 'must be a valid host:port combo'); } return path; } diff --git a/test/parallel/test-http-request-connect-method.js b/test/parallel/test-http-request-connect-method.js index 17c092e69c58..578cf0ae6e99 100644 --- a/test/parallel/test-http-request-connect-method.js +++ b/test/parallel/test-http-request-connect-method.js @@ -10,7 +10,7 @@ const { validatePath } = require('internal/http'); const server = http.createServer(); server.on('connect', common.mustCall((req, stream) => { - assert.strictEqual(req.url, 'example.com'); + assert.strictEqual(req.url, 'example.com:80'); stream.end('HTTP/1.1 501 Not Implemented\r\n\r\n'); })); @@ -18,8 +18,19 @@ server.listen(0); server.on('listening', common.mustCall(() => { const url = new URL(`http://localhost:${server.address().port}/example.com:80`); - - const req = http.request(url, { method: 'CONNECT' }).end(); + let req = http.request(url, { method: 'CONNECT' }).end(); + // invalid path + const invalidPathURL = new URL(`http://localhost:${server.address().port}/example.com`); + assert.throws( + () => { + req = http.request(invalidPathURL, { method: 'CONNECT' }).end(); + }, + { + code: 'ERR_INVALID_ARG_VALUE', + name: 'TypeError', + message: /^The argument 'options\.path' must be a valid host:port combo\. Received .+$/ + } + ); req.once('connect', common.mustCall((res) => { res.destroy(); server.close(); @@ -30,9 +41,9 @@ server.on('listening', common.mustCall(() => { assert.throws( () => validatePath(path), { - code: 'ERR_INVALID_HOST_PORT_COMBO', + code: 'ERR_INVALID_ARG_VALUE', name: 'TypeError', - message: 'Path must be a valid : combo' + message: /^The argument 'options\.path' must be a valid host:port combo\. Received .+$/ } ); }); From c9f15978da4dbb406be72c8379a4c76626f149a0 Mon Sep 17 00:00:00 2001 From: Priyank Singh Date: Wed, 5 Aug 2020 20:34:17 +0530 Subject: [PATCH 7/9] fixes --- lib/_http_client.js | 33 ++++++++++++++----- lib/internal/http.js | 18 ++++------ .../test-http-request-connect-method.js | 11 ++----- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index 0b33f79cc8b1..8e7280b5e4ed 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -49,14 +49,18 @@ const Agent = require('_http_agent'); const { Buffer } = require('buffer'); const { defaultTriggerAsyncIdScope } = require('internal/async_hooks'); const { URL, urlToOptions, searchParamsSymbol } = require('internal/url'); -const { kOutHeaders, kNeedDrain, validatePath } = require('internal/http'); +const { kOutHeaders, + kNeedDrain, + isValidCONNECTPath +} = require('internal/http'); const { connResetException, codes } = require('internal/errors'); const { ERR_HTTP_HEADERS_SENT, ERR_INVALID_ARG_TYPE, ERR_INVALID_HTTP_TOKEN, ERR_INVALID_PROTOCOL, - ERR_UNESCAPED_CHARACTERS + ERR_UNESCAPED_CHARACTERS, + ERR_INVALID_ARG_VALUE } = codes; const { validateInteger } = require('internal/validators'); const { getTimerDuration } = require('internal/timers'); @@ -197,12 +201,25 @@ function ClientRequest(input, options, cb) { } this.insecureHTTPParser = insecureHTTPParser; - this.path = (options.path && method === 'CONNECT' && - options.path.charAt(0) === '/' ? - options.path.slice(1) : options.path) || '/'; - - if (method === 'CONNECT' && this.path !== '/') { - validatePath(this.path); + path = options.path; + if (path) { + if (method === 'CONNECT') { + if (path.charAt(0) === '/') { + path = path.slice(1) || '/'; + if (!isValidCONNECTPath(path)) { + throw new ERR_INVALID_ARG_VALUE('options.path', + path, + 'must be a valid host:port combo'); + } + this.path = path; + } else { + this.path = path; + } + } else { + this.path = path; + } + } else { + this.path = '/'; } if (cb) { diff --git a/lib/internal/http.js b/lib/internal/http.js index 9139f2049265..3df558ed4b2c 100644 --- a/lib/internal/http.js +++ b/lib/internal/http.js @@ -6,12 +6,8 @@ const { const { setUnrefTimeout } = require('internal/timers'); const { PerformanceEntry, notify } = internalBinding('performance'); -const { - codes: { - ERR_INVALID_ARG_VALUE - } -} = require('internal/errors'); +const VALID_PATH_REGEX = /^[_0-9A-Za-z]+(?:\.[_0-9A-Za-z]+)*\.?:(?:[1-9]|[1-9][0-9]{1,2}|[1-5][0-9]{3}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/; let nowCache; let utcCache; @@ -37,13 +33,11 @@ function resetCache() { utcCache = undefined; } -function validatePath(path) { - const pathRegex = /^[_0-9A-Za-z]+(\.[_0-9A-Za-z]+)*\.?:([1-9]|[1-9][0-9]{1,2}|[1-5][0-9]{3}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/; - if (!pathRegex.test(path)) { - throw new ERR_INVALID_ARG_VALUE('options.path', - path, 'must be a valid host:port combo'); +function isValidCONNECTPath(path) { + if (!VALID_PATH_REGEX.test(path)) { + return false; } - return path; + return true; } class HttpRequestTiming extends PerformanceEntry { @@ -68,5 +62,5 @@ module.exports = { nowDate, utcDate, emitStatistics, - validatePath + isValidCONNECTPath }; diff --git a/test/parallel/test-http-request-connect-method.js b/test/parallel/test-http-request-connect-method.js index 578cf0ae6e99..21576988dfe4 100644 --- a/test/parallel/test-http-request-connect-method.js +++ b/test/parallel/test-http-request-connect-method.js @@ -5,7 +5,7 @@ const common = require('../common'); const assert = require('assert'); const http = require('http'); -const { validatePath } = require('internal/http'); +const { isValidCONNECTPath } = require('internal/http'); const server = http.createServer(); @@ -38,12 +38,5 @@ server.on('listening', common.mustCall(() => { })); ['example.com', 'example.com:0', 'example.com:65536'].forEach((path) => { - assert.throws( - () => validatePath(path), - { - code: 'ERR_INVALID_ARG_VALUE', - name: 'TypeError', - message: /^The argument 'options\.path' must be a valid host:port combo\. Received .+$/ - } - ); + assert.strictEqual(isValidCONNECTPath(path), false); }); From d8039e3036033ca77c5c07e162d8dc9fe1e6c36d Mon Sep 17 00:00:00 2001 From: Priyank Singh Date: Thu, 6 Aug 2020 16:01:23 +0530 Subject: [PATCH 8/9] minor fixes --- lib/_http_client.js | 18 +++++++----------- lib/internal/http.js | 5 +---- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index 8e7280b5e4ed..361f54de1384 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -49,9 +49,10 @@ const Agent = require('_http_agent'); const { Buffer } = require('buffer'); const { defaultTriggerAsyncIdScope } = require('internal/async_hooks'); const { URL, urlToOptions, searchParamsSymbol } = require('internal/url'); -const { kOutHeaders, - kNeedDrain, - isValidCONNECTPath +const { + kOutHeaders, + kNeedDrain, + isValidCONNECTPath } = require('internal/http'); const { connResetException, codes } = require('internal/errors'); const { @@ -204,23 +205,18 @@ function ClientRequest(input, options, cb) { path = options.path; if (path) { if (method === 'CONNECT') { - if (path.charAt(0) === '/') { + if (path.startsWith('/')) { + // if path is '/' path = path.slice(1) || '/'; if (!isValidCONNECTPath(path)) { throw new ERR_INVALID_ARG_VALUE('options.path', path, 'must be a valid host:port combo'); } - this.path = path; - } else { - this.path = path; } - } else { - this.path = path; } - } else { - this.path = '/'; } + this.path = path || '/'; if (cb) { this.once('response', cb); diff --git a/lib/internal/http.js b/lib/internal/http.js index 3df558ed4b2c..585e6148d060 100644 --- a/lib/internal/http.js +++ b/lib/internal/http.js @@ -34,10 +34,7 @@ function resetCache() { } function isValidCONNECTPath(path) { - if (!VALID_PATH_REGEX.test(path)) { - return false; - } - return true; + return VALID_PATH_REGEX.test(path); } class HttpRequestTiming extends PerformanceEntry { From 9426cbb95d5f17b08cde5bff8cc11dc6898816f4 Mon Sep 17 00:00:00 2001 From: Priyank Singh Date: Sun, 6 Sep 2020 23:05:02 +0530 Subject: [PATCH 9/9] removed regex --- lib/_http_client.js | 4 ++-- lib/internal/http.js | 11 +++++++---- test/parallel/test-http-request-connect-method.js | 6 +++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index 361f54de1384..7450dd8e4805 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -52,7 +52,7 @@ const { URL, urlToOptions, searchParamsSymbol } = require('internal/url'); const { kOutHeaders, kNeedDrain, - isValidCONNECTPath + isValidConnectPath } = require('internal/http'); const { connResetException, codes } = require('internal/errors'); const { @@ -208,7 +208,7 @@ function ClientRequest(input, options, cb) { if (path.startsWith('/')) { // if path is '/' path = path.slice(1) || '/'; - if (!isValidCONNECTPath(path)) { + if (!isValidConnectPath(path)) { throw new ERR_INVALID_ARG_VALUE('options.path', path, 'must be a valid host:port combo'); diff --git a/lib/internal/http.js b/lib/internal/http.js index 585e6148d060..6b7cfe9a19e6 100644 --- a/lib/internal/http.js +++ b/lib/internal/http.js @@ -6,8 +6,8 @@ const { const { setUnrefTimeout } = require('internal/timers'); const { PerformanceEntry, notify } = internalBinding('performance'); +const { URL } = require('internal/url'); -const VALID_PATH_REGEX = /^[_0-9A-Za-z]+(?:\.[_0-9A-Za-z]+)*\.?:(?:[1-9]|[1-9][0-9]{1,2}|[1-5][0-9]{3}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/; let nowCache; let utcCache; @@ -33,8 +33,11 @@ function resetCache() { utcCache = undefined; } -function isValidCONNECTPath(path) { - return VALID_PATH_REGEX.test(path); +function isValidConnectPath(path) { + const url = new URL(`http://${path}`); + if (url.hostname && url.port && `http://${url.host}/` === url.href) + return true; + return false; } class HttpRequestTiming extends PerformanceEntry { @@ -59,5 +62,5 @@ module.exports = { nowDate, utcDate, emitStatistics, - isValidCONNECTPath + isValidConnectPath }; diff --git a/test/parallel/test-http-request-connect-method.js b/test/parallel/test-http-request-connect-method.js index 21576988dfe4..f92fbf6415a5 100644 --- a/test/parallel/test-http-request-connect-method.js +++ b/test/parallel/test-http-request-connect-method.js @@ -5,7 +5,7 @@ const common = require('../common'); const assert = require('assert'); const http = require('http'); -const { isValidCONNECTPath } = require('internal/http'); +const { isValidConnectPath } = require('internal/http'); const server = http.createServer(); @@ -37,6 +37,6 @@ server.on('listening', common.mustCall(() => { })); })); -['example.com', 'example.com:0', 'example.com:65536'].forEach((path) => { - assert.strictEqual(isValidCONNECTPath(path), false); +['example.com', 'example.com:8080/example'].forEach((path) => { + assert.strictEqual(isValidConnectPath(path), false); });