Skip to content

Commit 2cad02d

Browse files
Merge commit from fork
* fix: reject malformed authority introducer (\, /\, \/) to prevent host-confusion Adds MALFORMED_AUTHORITY_INTRODUCER regex and check in parseWithStatus() to detect "\\", "/\", and "\/" used as authority introducers in place of "//" after the scheme colon. Node's URL treats "\" as interchangeable with "/" on special schemes, so these inputs would parse as a different host — an SSRF/redirect bypass gap distinct from the previously-patched literal-backslash-in-authority fix (GHSA-v2hh-gcrm-f6hx). Also makes resolve() throw on malformed authority or port, since it returns a plain string with no error-signal mechanism. Adds 23 regression tests covering rejection, normalization, equal(), resolve(), and no false positives. * fix: reject whitespace-split authority introducer (TAB/LF/CR) host-confusion bypass --------- Co-authored-by: Ulises Gascon <[email protected]>
1 parent 6aeece6 commit 2cad02d

2 files changed

Lines changed: 173 additions & 1 deletion

File tree

‎index.js‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ function normalize (uri, options) {
2626
*/
2727
function resolve (baseURI, relativeURI, options) {
2828
const schemelessOptions = options ? Object.assign({ scheme: 'null' }, options) : { scheme: 'null' }
29-
const resolved = resolveComponent(parse(baseURI, schemelessOptions), parse(relativeURI, schemelessOptions), schemelessOptions, true)
29+
const { parsed: baseParsed, malformedAuthorityOrPort: baseMalformed } = parseWithStatus(baseURI, schemelessOptions)
30+
const { parsed: relativeParsed, malformedAuthorityOrPort: relativeMalformed } = parseWithStatus(relativeURI, schemelessOptions)
31+
if (baseMalformed || relativeMalformed) {
32+
throw new Error(baseParsed.error || relativeParsed.error || 'URI is malformed.')
33+
}
34+
const resolved = resolveComponent(baseParsed, relativeParsed, schemelessOptions, true)
3035
schemelessOptions.skipEscape = true
3136
return serialize(resolved, schemelessOptions)
3237
}
@@ -206,6 +211,15 @@ const URI_PARSE = /^(?:([^#/:?]+):)?(?:\/\/((?:([^#/?@]*)@)?(\[[^#/?\]]+\]|[^#/:
206211
// with or without a scheme prefix, for the literal-backslash rejection below.
207212
const AUTHORITY_PREFIX = /^(?:[^#/:?]+:)?\/\/([^/?#]*)/
208213

214+
// Captures the leading authority-introducer region after an optional scheme: a
215+
// run of forward slashes, backslashes, and the characters the WHATWG URL parser
216+
// removes before parsing (TAB U+0009, LF U+000A, CR U+000D). A valid introducer
217+
// is exactly "//". Node treats "\" as "/" on special schemes and strips those
218+
// characters first, so forms like "\\", "/\", "\/", "/<TAB>/", or a leading
219+
// "<TAB>//" reach an authority in Node while fast-uri's URI_PARSE folds them into
220+
// the path group (host confusion / SSRF / redirect bypass).
221+
const AUTHORITY_INTRODUCER_REGION = /^(?:[^#/:?]+:)?([/\\\t\n\r]*)/
222+
209223
/**
210224
* @param {import('./types/index').URIComponent} parsed
211225
* @param {RegExpMatchArray} matches
@@ -265,6 +279,28 @@ function parseWithStatus (uri, opts) {
265279
malformedAuthorityOrPort = true
266280
}
267281

282+
// Reject a malformed or whitespace-smuggled authority introducer. fast-uri
283+
// only recognizes a literal "//"; anything else in the leading separator run
284+
// (a backslash, or a "//" that appears only after removing the TAB/LF/CR that
285+
// Node strips) means the authority fast-uri parses differs from the one Node's
286+
// URL resolves. Reject rather than rewrite, mirroring the literal-backslash
287+
// guard above. Percent-encoded forms (%5C, %09) are untouched, valid data.
288+
const introducerMatch = uri.match(AUTHORITY_INTRODUCER_REGION)
289+
if (introducerMatch !== null) {
290+
const region = introducerMatch[1]
291+
const normalizedRegion = region.replace(/[\t\n\r]/g, '')
292+
// Two or more leading separators introduce an authority.
293+
if (normalizedRegion.length >= 2) {
294+
if (normalizedRegion.slice(0, 2) !== '//') {
295+
parsed.error = parsed.error || 'URI authority must not contain a literal backslash.'
296+
malformedAuthorityOrPort = true
297+
} else if (region.length !== normalizedRegion.length) {
298+
parsed.error = parsed.error || 'URI authority introducer must not contain whitespace.'
299+
malformedAuthorityOrPort = true
300+
}
301+
}
302+
}
303+
268304
const matches = uri.match(URI_PARSE)
269305

270306
if (matches) {

‎test/security.test.js‎

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,139 @@ test('parse does not reject a literal backslash in the query or fragment', (t) =
221221
t.notOk(parsed.error, 'backslash in query/fragment does not mark the URI malformed')
222222
t.equal(parsed.host, 'host.example.com', 'host parsed normally')
223223
})
224+
225+
test('parse rejects a malformed authority introducer (\\\\, /\\, \\/) in place of //', (t) => {
226+
// Regression: "\\", "/\\", "\\/" after the scheme colon are not valid authority
227+
// introducers. Node's URL treats "\\" as interchangeable with "/" on special
228+
// schemes, so "http:\\\\evil.com/path" would be parsed as host "evil.com" by
229+
// Node, but fast-uri must reject it as malformed to prevent SSRF/redirect bypass.
230+
const cases = [
231+
'http:\\\\evil.com/path',
232+
'http:/\\evil.com/path',
233+
'http:\\/evil.com/path',
234+
'ws:\\\\evil.com/chat',
235+
'wss:\\\\evil.com/chat',
236+
'ftp:\\\\evil.com/',
237+
'\\\\evil.com/path'
238+
]
239+
240+
t.plan(cases.length)
241+
242+
cases.forEach((input) => {
243+
t.equal(
244+
fastURI.parse(input).error,
245+
'URI authority must not contain a literal backslash.',
246+
input
247+
)
248+
})
249+
})
250+
251+
test('normalize does not canonicalize a malformed-authority-introducer URI', (t) => {
252+
const cases = [
253+
'http:\\\\evil.com/path',
254+
'http:/\\evil.com/path'
255+
]
256+
257+
t.plan(cases.length)
258+
259+
cases.forEach((input) => {
260+
t.equal(fastURI.normalize(input), input, input)
261+
})
262+
})
263+
264+
test('equal returns false for malformed-authority-introducer URIs', (t) => {
265+
const pairs = [
266+
['http:\\\\evil.com/path', 'http://evil.com/path'],
267+
['http:/\\evil.com/path', 'http://evil.com/path']
268+
]
269+
270+
t.plan(pairs.length)
271+
272+
pairs.forEach(([left, right]) => {
273+
t.equal(fastURI.equal(left, right), false, `${left} != ${right}`)
274+
})
275+
})
276+
277+
test('resolve throws on malformed authority introducer', (t) => {
278+
// resolve() returns a plain string with no error field, so the only safe
279+
// behavior is to throw when either component has a malformed authority.
280+
const pairs = [
281+
['https://allowed.com/', '\\\\evil.com/path'],
282+
['\\\\evil.com/path', 'https://allowed.com/'],
283+
['https://allowed.com/', 'http:/\\evil.com/path'],
284+
['https://allowed.com/', 'http:\\/evil.com/path']
285+
]
286+
287+
t.plan(pairs.length)
288+
289+
pairs.forEach(([base, rel]) => {
290+
t.throws(
291+
() => fastURI.resolve(base, rel),
292+
/URI authority must not contain a literal backslash/,
293+
`${base} + ${rel}`
294+
)
295+
})
296+
})
297+
298+
test('parse rejects a whitespace-split authority introducer (TAB, LF, CR)', (t) => {
299+
// The WHATWG URL parser removes TAB (U+0009), LF (U+000A) and CR (U+000D) from
300+
// the input before parsing, so a stripped character wedged into the introducer
301+
// ("/<TAB>\\", "/<TAB>/", or a leading "<TAB>//") reaches an authority in Node
302+
// while fast-uri would otherwise fold it into the path. These must be rejected
303+
// like the adjacent "\\", "/\\", "\\/" forms.
304+
const cases = [
305+
{ input: '/\t\\evil.com/path', expectedError: 'URI authority must not contain a literal backslash.' },
306+
{ input: '/\t/evil.com/path', expectedError: 'URI authority introducer must not contain whitespace.' },
307+
{ input: '/\n\\evil.com/path', expectedError: 'URI authority must not contain a literal backslash.' },
308+
{ input: '/\r\\evil.com/path', expectedError: 'URI authority must not contain a literal backslash.' },
309+
{ input: '\t//evil.com/path', expectedError: 'URI authority introducer must not contain whitespace.' },
310+
{ input: '\t/\\evil.com/path', expectedError: 'URI authority must not contain a literal backslash.' },
311+
{ input: 'https:/\t/evil.com/path', expectedError: 'URI authority introducer must not contain whitespace.' }
312+
]
313+
314+
t.plan(cases.length)
315+
316+
cases.forEach(({ input, expectedError }) => {
317+
t.equal(fastURI.parse(input).error, expectedError, JSON.stringify(input))
318+
})
319+
})
320+
321+
test('resolve throws on a whitespace-split authority introducer', (t) => {
322+
const pairs = [
323+
['https://allowed.com/', '/\t\\evil.com/path'],
324+
['https://allowed.com/', '/\t/evil.com/path'],
325+
['https://allowed.com/', '/\n\\evil.com/path'],
326+
['/\t/evil.com/path', 'https://allowed.com/']
327+
]
328+
329+
t.plan(pairs.length)
330+
331+
pairs.forEach(([base, rel]) => {
332+
t.throws(
333+
() => fastURI.resolve(base, rel),
334+
/URI authority (must not contain a literal backslash|introducer must not contain whitespace)/,
335+
`${JSON.stringify(base)} + ${JSON.stringify(rel)}`
336+
)
337+
})
338+
})
339+
340+
test('parse does not reject valid authority introducer patterns', (t) => {
341+
// No false positives: "//" introducer and scheme-less "//" must be valid.
342+
const cases = [
343+
'http://good.com/',
344+
'https://good.com/',
345+
'ws://good.com/chat',
346+
'wss://good.com/chat',
347+
'ftp://good.com/',
348+
'//good.com/path',
349+
'/absolute/path',
350+
'relative/path'
351+
]
352+
353+
t.plan(cases.length)
354+
355+
cases.forEach((input) => {
356+
const parsed = fastURI.parse(input)
357+
t.notOk(parsed.error, input)
358+
})
359+
})

0 commit comments

Comments
 (0)