Skip to content

Commit b89f4d5

Browse files
Trottbengl
authored andcommitted
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: #42196 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Mestery <mestery@protonmail.com> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent f27bcec commit b89f4d5

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

lib/url.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ const {
117117
CHAR_TAB,
118118
CHAR_CARRIAGE_RETURN,
119119
CHAR_LINE_FEED,
120-
CHAR_FORM_FEED,
121120
CHAR_NO_BREAK_SPACE,
122121
CHAR_ZERO_WIDTH_NOBREAK_SPACE,
123122
CHAR_HASH,
@@ -196,11 +195,7 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
196195
const code = url.charCodeAt(i);
197196

198197
// Find first and last non-whitespace characters for trimming
199-
const isWs = code === CHAR_SPACE ||
200-
code === CHAR_TAB ||
201-
code === CHAR_CARRIAGE_RETURN ||
202-
code === CHAR_LINE_FEED ||
203-
code === CHAR_FORM_FEED ||
198+
const isWs = code < 33 ||
204199
code === CHAR_NO_BREAK_SPACE ||
205200
code === CHAR_ZERO_WIDTH_NOBREAK_SPACE;
206201
if (start === -1) {

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,21 @@ const parseTests = {
977977
path: '/everybody',
978978
href: '//fhqwhgads@example.com/everybody#to-the-limit'
979979
},
980+
981+
'\bhttp://example.com/\b': {
982+
protocol: 'http:',
983+
slashes: true,
984+
auth: null,
985+
host: 'example.com',
986+
port: null,
987+
hostname: 'example.com',
988+
hash: null,
989+
search: null,
990+
query: null,
991+
pathname: '/',
992+
path: '/',
993+
href: 'http://example.com/'
994+
}
980995
};
981996

982997
for (const u in parseTests) {

0 commit comments

Comments
 (0)