fast-uri 4.0.1 → 4.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +22 -6
- package/lib/utils.js +62 -1
- package/package.json +1 -1
- package/test/parse.test.js +5 -3
- package/test/query-fragment-normalization.test.js +33 -0
- package/test/security.test.js +62 -0
- package/test/util.test.js +9 -0
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { normalizeIPv6, removeDotSegments, recomposeAuthority, normalizePercentEncoding, normalizePathEncoding, escapePreservingEscapes, reescapeHostDelimiters, isIPv4, nonSimpleDomain } = require('./lib/utils')
|
|
3
|
+
const { normalizeIPv6, removeDotSegments, recomposeAuthority, normalizePercentEncoding, normalizePathEncoding, normalizeQueryFragmentEncoding, escapePreservingEscapes, reescapeHostDelimiters, isIPv4, nonSimpleDomain } = require('./lib/utils')
|
|
4
4
|
const { SCHEMES, getSchemeHandler } = require('./lib/schemes')
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -202,6 +202,10 @@ function serialize (cmpts, opts) {
|
|
|
202
202
|
|
|
203
203
|
const URI_PARSE = /^(?:([^#/:?]+):)?(?:\/\/((?:([^#/?@]*)@)?(\[[^#/?\]]+\]|[^#/:?]*)(?::(\d*))?))?([^#?]*)(?:\?([^#]*))?(?:#((?:.|[\n\r])*))?/u
|
|
204
204
|
|
|
205
|
+
// Captures the authority component (between "//" and the next "/", "?" or "#"),
|
|
206
|
+
// with or without a scheme prefix, for the literal-backslash rejection below.
|
|
207
|
+
const AUTHORITY_PREFIX = /^(?:[^#/:?]+:)?\/\/([^/?#]*)/
|
|
208
|
+
|
|
205
209
|
/**
|
|
206
210
|
* @param {import('./types/index').URIComponent} parsed
|
|
207
211
|
* @param {RegExpMatchArray} matches
|
|
@@ -248,6 +252,19 @@ function parseWithStatus (uri, opts) {
|
|
|
248
252
|
}
|
|
249
253
|
}
|
|
250
254
|
|
|
255
|
+
// A literal backslash (U+005C) is not a valid RFC 3986 URI character and is
|
|
256
|
+
// not an authority delimiter. Reject it in the authority rather than
|
|
257
|
+
// rewriting it: normalizing "\" -> "/" (WHATWG error recovery) could silently
|
|
258
|
+
// change the resource identified by an otherwise-invalid input, and lets "\"
|
|
259
|
+
// act as a host delimiter here while Node's native URL parses a different
|
|
260
|
+
// host (SSRF / redirect / origin-allowlist bypass). Percent-encoded %5C is
|
|
261
|
+
// untouched and remains valid encoded data.
|
|
262
|
+
const authorityMatch = uri.match(AUTHORITY_PREFIX)
|
|
263
|
+
if (authorityMatch !== null && authorityMatch[1].indexOf('\\') !== -1) {
|
|
264
|
+
parsed.error = 'URI authority must not contain a literal backslash.'
|
|
265
|
+
malformedAuthorityOrPort = true
|
|
266
|
+
}
|
|
267
|
+
|
|
251
268
|
const matches = uri.match(URI_PARSE)
|
|
252
269
|
|
|
253
270
|
if (matches) {
|
|
@@ -325,12 +342,11 @@ function parseWithStatus (uri, opts) {
|
|
|
325
342
|
if (parsed.path) {
|
|
326
343
|
parsed.path = normalizePathEncoding(parsed.path)
|
|
327
344
|
}
|
|
345
|
+
if (parsed.query) {
|
|
346
|
+
parsed.query = normalizeQueryFragmentEncoding(parsed.query)
|
|
347
|
+
}
|
|
328
348
|
if (parsed.fragment) {
|
|
329
|
-
|
|
330
|
-
parsed.fragment = encodeURI(decodeURIComponent(parsed.fragment))
|
|
331
|
-
} catch {
|
|
332
|
-
parsed.error = parsed.error || 'URI malformed'
|
|
333
|
-
}
|
|
349
|
+
parsed.fragment = normalizeQueryFragmentEncoding(parsed.fragment)
|
|
334
350
|
}
|
|
335
351
|
}
|
|
336
352
|
|
package/lib/utils.js
CHANGED
|
@@ -15,6 +15,9 @@ const isUnreserved = RegExp.prototype.test.bind(/^[\da-z\-._~]$/iu)
|
|
|
15
15
|
/** @type {(value: string) => boolean} */
|
|
16
16
|
const isPathCharacter = RegExp.prototype.test.bind(/^[\da-z\-._~!$&'()*+,;=:@/]$/iu)
|
|
17
17
|
|
|
18
|
+
/** @type {(value: string) => boolean} */
|
|
19
|
+
const isQueryFragmentCharacter = RegExp.prototype.test.bind(/^[\da-z\-._~!$&'()*+,;=:@/?]$/iu)
|
|
20
|
+
|
|
18
21
|
/**
|
|
19
22
|
* @param {Array<string>} input
|
|
20
23
|
* @returns {string}
|
|
@@ -218,7 +221,7 @@ function removeDotSegments (path) {
|
|
|
218
221
|
continue
|
|
219
222
|
}
|
|
220
223
|
} else if (input[0] === '/') {
|
|
221
|
-
if (input[1] === '.'
|
|
224
|
+
if (input[1] === '.') {
|
|
222
225
|
output.push('/')
|
|
223
226
|
break
|
|
224
227
|
}
|
|
@@ -435,6 +438,63 @@ function normalizePathEncoding (input) {
|
|
|
435
438
|
return output
|
|
436
439
|
}
|
|
437
440
|
|
|
441
|
+
/**
|
|
442
|
+
* Normalizes the percent-encoding of a query or fragment component.
|
|
443
|
+
*
|
|
444
|
+
* Like `normalizePathEncoding`, but uses the query/fragment character set
|
|
445
|
+
* (which additionally allows `?`) and decodes `.` since it has no dot-segment
|
|
446
|
+
* meaning outside of a path.
|
|
447
|
+
*
|
|
448
|
+
* @param {string} input
|
|
449
|
+
* @returns {string}
|
|
450
|
+
*/
|
|
451
|
+
function normalizeQueryFragmentEncoding (input) {
|
|
452
|
+
let output = ''
|
|
453
|
+
|
|
454
|
+
for (let i = 0; i < input.length; i++) {
|
|
455
|
+
const ch = input[i]
|
|
456
|
+
if (ch === '%' && i + 2 < input.length) {
|
|
457
|
+
const hex = input.slice(i + 1, i + 3)
|
|
458
|
+
if (isHexPair(hex)) {
|
|
459
|
+
const normalizedHex = hex.toUpperCase()
|
|
460
|
+
const decoded = String.fromCharCode(parseInt(normalizedHex, 16))
|
|
461
|
+
|
|
462
|
+
if (isUnreserved(decoded)) {
|
|
463
|
+
output += decoded
|
|
464
|
+
} else {
|
|
465
|
+
output += '%' + normalizedHex
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
i += 2
|
|
469
|
+
continue
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
if (isQueryFragmentCharacter(ch)) {
|
|
474
|
+
output += ch
|
|
475
|
+
} else {
|
|
476
|
+
const code = input.charCodeAt(i)
|
|
477
|
+
if (code < 0x80) {
|
|
478
|
+
output += isEscapeSafe(code) ? ch : BYTE_HEX[code]
|
|
479
|
+
} else if (code < 0xD800 || code > 0xDFFF) {
|
|
480
|
+
output += percentEncodeNonAscii(code)
|
|
481
|
+
} else if (code <= 0xDBFF && i + 1 < input.length) {
|
|
482
|
+
const low = input.charCodeAt(i + 1)
|
|
483
|
+
if (low >= 0xDC00 && low <= 0xDFFF) {
|
|
484
|
+
output += percentEncodeNonAscii(0x10000 + ((code - 0xD800) << 10) + (low - 0xDC00))
|
|
485
|
+
i++
|
|
486
|
+
} else {
|
|
487
|
+
output += percentEncodeNonAscii(0xFFFD)
|
|
488
|
+
}
|
|
489
|
+
} else {
|
|
490
|
+
output += percentEncodeNonAscii(0xFFFD)
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
return output
|
|
496
|
+
}
|
|
497
|
+
|
|
438
498
|
/**
|
|
439
499
|
* Escapes a component while preserving existing valid percent escapes.
|
|
440
500
|
*
|
|
@@ -515,6 +575,7 @@ module.exports = {
|
|
|
515
575
|
reescapeHostDelimiters,
|
|
516
576
|
normalizePercentEncoding,
|
|
517
577
|
normalizePathEncoding,
|
|
578
|
+
normalizeQueryFragmentEncoding,
|
|
518
579
|
escapePreservingEscapes,
|
|
519
580
|
removeDotSegments,
|
|
520
581
|
isIPv4,
|
package/package.json
CHANGED
package/test/parse.test.js
CHANGED
|
@@ -150,10 +150,12 @@ test('URI parse', (t) => {
|
|
|
150
150
|
t.equal(components.query, undefined, 'query')
|
|
151
151
|
t.equal(components.fragment, '%0D', 'fragment')
|
|
152
152
|
|
|
153
|
-
//
|
|
153
|
+
// a fragment whose decoded bytes are not valid UTF-8 is still valid
|
|
154
|
+
// percent-encoding (RFC 3986 §2.1 is byte-level), so it is preserved as-is
|
|
155
|
+
// rather than being flagged as malformed
|
|
154
156
|
components = fastURI.parse('http://example.com/#%E0%A4A')
|
|
155
|
-
t.equal(components.error,
|
|
156
|
-
t.equal(components.fragment, '%E0%A4A', '
|
|
157
|
+
t.equal(components.error, undefined, 'valid percent-encoding is not flagged as malformed')
|
|
158
|
+
t.equal(components.fragment, '%E0%A4A', 'fragment is preserved')
|
|
157
159
|
|
|
158
160
|
// all
|
|
159
161
|
components = fastURI.parse('uri://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body')
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const test = require('tape')
|
|
4
|
+
const fastURI = require('..')
|
|
5
|
+
|
|
6
|
+
test('query percent-encoding is normalized (RFC 3986 §6.2.2)', (t) => {
|
|
7
|
+
// hex digits are uppercased, like the path component already does
|
|
8
|
+
t.equal(fastURI.normalize('x://h/p?a=%2a'), 'x://h/p?a=%2A', 'uppercases hex in query')
|
|
9
|
+
// unreserved characters are decoded
|
|
10
|
+
t.equal(fastURI.normalize('x://h/p?a=%7e%2e'), 'x://h/p?a=~.', 'decodes unreserved in query')
|
|
11
|
+
// reserved characters stay percent-encoded
|
|
12
|
+
t.equal(fastURI.normalize('x://h/p?a=%2F'), 'x://h/p?a=%2F', 'keeps reserved encoded in query')
|
|
13
|
+
// raw characters that are not allowed are percent-encoded
|
|
14
|
+
t.equal(fastURI.normalize('x://h/p?a b'), 'x://h/p?a%20b', 'encodes a raw space in query')
|
|
15
|
+
t.equal(fastURI.normalize('x://h/p?café'), 'x://h/p?caf%C3%A9', 'encodes raw non-ASCII in query')
|
|
16
|
+
// `?` is allowed unencoded in a query
|
|
17
|
+
t.equal(fastURI.normalize('x://h/p?a?b'), 'x://h/p?a?b', 'keeps `?` raw in query')
|
|
18
|
+
|
|
19
|
+
t.end()
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
test('fragment percent-encoding is normalized without decoding reserved characters', (t) => {
|
|
23
|
+
// reserved characters must NOT be decoded — `%2F` is not `/`
|
|
24
|
+
t.equal(fastURI.normalize('x://h/p#a%2Fb%2A'), 'x://h/p#a%2Fb%2A', 'keeps reserved encoded in fragment')
|
|
25
|
+
// hex is uppercased and unreserved is decoded
|
|
26
|
+
t.equal(fastURI.normalize('x://h/p#a%2a%7e'), 'x://h/p#a%2A~', 'uppercases hex and decodes unreserved in fragment')
|
|
27
|
+
// raw characters are still encoded
|
|
28
|
+
t.equal(fastURI.normalize('x://h/p#a b'), 'x://h/p#a%20b', 'encodes a raw space in fragment')
|
|
29
|
+
// `?` and `/` are allowed unencoded in a fragment
|
|
30
|
+
t.equal(fastURI.normalize('x://h/p#f?x/y'), 'x://h/p#f?x/y', 'keeps `?` and `/` raw in fragment')
|
|
31
|
+
|
|
32
|
+
t.end()
|
|
33
|
+
})
|
package/test/security.test.js
CHANGED
|
@@ -159,3 +159,65 @@ test('parse canonicalises IDN / Unicode hosts to their ASCII form', (t) => {
|
|
|
159
159
|
t.equal(parsed.host, expectedHost, `host canonicalised to ASCII: ${description}`)
|
|
160
160
|
})
|
|
161
161
|
})
|
|
162
|
+
|
|
163
|
+
test('parse rejects a literal backslash in the authority as malformed (RFC 3986)', (t) => {
|
|
164
|
+
// Regression for the host-confusion bypass: a literal "\" is invalid RFC 3986
|
|
165
|
+
// syntax and must be flagged malformed, not silently rewritten. Otherwise "\"
|
|
166
|
+
// acts as a host delimiter here while Node's native URL parses a different
|
|
167
|
+
// host, defeating a host-based SSRF/redirect/origin allowlist.
|
|
168
|
+
const cases = [
|
|
169
|
+
'http://evil.com\\@allowed.com',
|
|
170
|
+
'https://169.254.169.254\\@trusted.example.com',
|
|
171
|
+
'http://127.0.0.1\\@public.example.com',
|
|
172
|
+
'https://attacker.com\\@api.internal',
|
|
173
|
+
'http://a\\@b',
|
|
174
|
+
'ws://evil.com\\@allowed.com/chat',
|
|
175
|
+
'wss://evil.com\\@allowed.com/chat',
|
|
176
|
+
'http://evil.com\\%40allowed.com',
|
|
177
|
+
'//evil.com\\@allowed.com'
|
|
178
|
+
]
|
|
179
|
+
|
|
180
|
+
t.plan(cases.length)
|
|
181
|
+
|
|
182
|
+
cases.forEach((input) => {
|
|
183
|
+
t.equal(
|
|
184
|
+
fastURI.parse(input).error,
|
|
185
|
+
'URI authority must not contain a literal backslash.',
|
|
186
|
+
input
|
|
187
|
+
)
|
|
188
|
+
})
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
test('normalize does not canonicalize a literal-backslash URI into a different valid URL', (t) => {
|
|
192
|
+
const cases = [
|
|
193
|
+
'http://evil.com\\@allowed.com',
|
|
194
|
+
'https://attacker.com\\@api.internal'
|
|
195
|
+
]
|
|
196
|
+
|
|
197
|
+
t.plan(cases.length)
|
|
198
|
+
|
|
199
|
+
cases.forEach((input) => {
|
|
200
|
+
t.equal(fastURI.normalize(input), input, input)
|
|
201
|
+
})
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
test('parse leaves percent-encoded %5C untouched as encoded data (not rejected)', (t) => {
|
|
205
|
+
// Only the literal "\" byte is rejected; %5C stays valid encoded data and
|
|
206
|
+
// does not diverge from the native URL parser, so it must not be flagged.
|
|
207
|
+
const input = 'http://evil.com%5C@allowed.com'
|
|
208
|
+
const parsed = fastURI.parse(input)
|
|
209
|
+
|
|
210
|
+
t.plan(2)
|
|
211
|
+
t.notOk(parsed.error, '%5C is valid encoded data, not malformed')
|
|
212
|
+
t.equal(parsed.host, new URL(input).hostname, '%5C host matches native URL (no divergence)')
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
test('parse does not reject a literal backslash in the query or fragment', (t) => {
|
|
216
|
+
// The rejection is scoped to the authority/path (the host-confusion surface);
|
|
217
|
+
// a backslash after "?"/"#" is normalized as encoded data as before.
|
|
218
|
+
const parsed = fastURI.parse('http://host.example.com/?x=\\y#z\\w')
|
|
219
|
+
|
|
220
|
+
t.plan(2)
|
|
221
|
+
t.notOk(parsed.error, 'backslash in query/fragment does not mark the URI malformed')
|
|
222
|
+
t.equal(parsed.host, 'host.example.com', 'host parsed normally')
|
|
223
|
+
})
|
package/test/util.test.js
CHANGED
|
@@ -29,6 +29,15 @@ test('removeDotSegments', (t) => {
|
|
|
29
29
|
// https://github.com/fastify/fast-uri/issues/139
|
|
30
30
|
testCases.push(['WS:/WS://1305G130505:1&%0D:1&C(XXXXX*)))))))XXX130505:UUVUaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa$aaaaaaaaaaaa13a',
|
|
31
31
|
'WS:/WS://1305G130505:1&%0D:1&C(XXXXX*)))))))XXX130505:UUVUaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa$aaaaaaaaaaaa13a'])
|
|
32
|
+
// trailing empty segments must be preserved: RFC 3986 5.2.4 only removes
|
|
33
|
+
// complete "." / ".." segments, never an empty segment
|
|
34
|
+
testCases.push(['//', '//'])
|
|
35
|
+
testCases.push(['/foo//', '/foo//'])
|
|
36
|
+
testCases.push(['/a/b//', '/a/b//'])
|
|
37
|
+
testCases.push(['/x//y//z', '/x//y//z'])
|
|
38
|
+
// "." / ".." segments still collapse
|
|
39
|
+
testCases.push(['/a/b/c/./../../g', '/a/g'])
|
|
40
|
+
testCases.push(['/.', '/'])
|
|
32
41
|
|
|
33
42
|
t.plan(testCases.length)
|
|
34
43
|
|