Skip to content

Commit fda9aba

Browse files
Trottdanielleadams
authored andcommitted
url: remove \t \n \r in url.parse() similar to WHATWG
WHATWG URL removes tab, new line, and carraige return characters before processing URL strings. To narrow the differences between WHATWG URL and url.parse(), and thus reduce opportunities for host spoofing etc. due to differences between the two APIs, let's do the same with url.parse(). PR-URL: #45116 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent f390de9 commit fda9aba

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

lib/url.js

+4
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,10 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
319319
case CHAR_TAB:
320320
case CHAR_LINE_FEED:
321321
case CHAR_CARRIAGE_RETURN:
322+
// WHATWG URL removes tabs, newlines, and carriage returns. Let's do that too.
323+
rest = rest.slice(0, i) + rest.slice(i + 1);
324+
i -= 1;
325+
break;
322326
case CHAR_SPACE:
323327
case CHAR_DOUBLE_QUOTE:
324328
case CHAR_PERCENT:

test/parallel/test-url-parse-format.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -853,16 +853,16 @@ const parseTests = {
853853
'http://a\r" \t\n<\'b:b@c\r\nd/e?f': {
854854
protocol: 'http:',
855855
slashes: true,
856-
auth: 'a\r" \t\n<\'b:b',
857-
host: 'c',
856+
auth: 'a" <\'b:b',
857+
host: 'cd',
858858
port: null,
859-
hostname: 'c',
859+
hostname: 'cd',
860860
hash: null,
861861
search: '?f',
862862
query: 'f',
863-
pathname: '%0D%0Ad/e',
864-
path: '%0D%0Ad/e?f',
865-
href: 'http://a%0D%22%20%09%0A%3C\'b:b@c/%0D%0Ad/e?f'
863+
pathname: '/e',
864+
path: '/e?f',
865+
href: 'http://a%22%20%3C\'b:b@cd/e?f'
866866
},
867867

868868
// Git urls used by npm
@@ -1023,7 +1023,7 @@ for (const u in parseTests) {
10231023
assert.deepStrictEqual(
10241024
actual,
10251025
expected,
1026-
`expected ${inspect(expected)}, got ${inspect(actual)}`
1026+
`parsing ${u} and expected ${inspect(expected)} but got ${inspect(actual)}`
10271027
);
10281028
assert.deepStrictEqual(
10291029
spaced,

0 commit comments

Comments
 (0)