From 7e1e56ac783330fef02e27b0f306abe0cfb744ed Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Mon, 7 Mar 2022 21:00:06 -0800 Subject: [PATCH] url: trim leading and trailing C0 control chars Emulate the WHATWHG URL parse behavior of trimming leading and trailing C0 control characters. This moves url.parse() slightly closer to WHATWHG URL behavior. The current behavior is possibly insecure for some uses. (The url.parse() API is marked as Legacy and the documentation specifically says it has known bugs and insecure behaviors. Still this change makes a lot of sense.) This issue was reported by P0cas. https://github.com/P0cas PR-URL: https://github.com/nodejs/node/pull/42196 Reviewed-By: Luigi Pinca Reviewed-By: Darshan Sen Reviewed-By: Matteo Collina Reviewed-By: Mestery Reviewed-By: Anto Aravinth Reviewed-By: Anna Henningsen --- lib/url.js | 7 +------ test/parallel/test-url-parse-format.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/url.js b/lib/url.js index 63d24bef7bf0bd..06321eecfa3be3 100644 --- a/lib/url.js +++ b/lib/url.js @@ -117,7 +117,6 @@ const { CHAR_TAB, CHAR_CARRIAGE_RETURN, CHAR_LINE_FEED, - CHAR_FORM_FEED, CHAR_NO_BREAK_SPACE, CHAR_ZERO_WIDTH_NOBREAK_SPACE, CHAR_HASH, @@ -196,11 +195,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) { const code = url.charCodeAt(i); // Find first and last non-whitespace characters for trimming - const isWs = code === CHAR_SPACE || - code === CHAR_TAB || - code === CHAR_CARRIAGE_RETURN || - code === CHAR_LINE_FEED || - code === CHAR_FORM_FEED || + const isWs = code < 33 || code === CHAR_NO_BREAK_SPACE || code === CHAR_ZERO_WIDTH_NOBREAK_SPACE; if (start === -1) { diff --git a/test/parallel/test-url-parse-format.js b/test/parallel/test-url-parse-format.js index a4bb141b49bfc7..3914c13548377d 100644 --- a/test/parallel/test-url-parse-format.js +++ b/test/parallel/test-url-parse-format.js @@ -992,6 +992,21 @@ const parseTests = { path: '/', href: 'http://localhost/', }, + + '\bhttp://example.com/\b': { + protocol: 'http:', + slashes: true, + auth: null, + host: 'example.com', + port: null, + hostname: 'example.com', + hash: null, + search: null, + query: null, + pathname: '/', + path: '/', + href: 'http://example.com/' + } }; for (const u in parseTests) {