Skip to content

Commit ae92a4c

Browse files
committed
Merge commit from fork
1 parent 444ecda commit ae92a4c

4 files changed

Lines changed: 93 additions & 20 deletions

File tree

‎index.js‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,25 @@ function resolve (baseURI, relativeURI, options) {
3030
parsed: baseParsed,
3131
malformedAuthorityOrPort: baseMalformed,
3232
malformedPercentEncoding: baseMalformedPercentEncoding,
33-
malformedSchemeSpecific: baseMalformedSchemeSpecific
33+
malformedSchemeSpecific: baseMalformedSchemeSpecific,
34+
malformedHost: baseMalformedHost
3435
} = parseWithStatus(baseURI, schemelessOptions)
3536
const {
3637
parsed: relativeParsed,
3738
malformedAuthorityOrPort: relativeMalformed,
3839
malformedPercentEncoding: relativeMalformedPercentEncoding,
39-
malformedSchemeSpecific: relativeMalformedSchemeSpecific
40+
malformedSchemeSpecific: relativeMalformedSchemeSpecific,
41+
malformedHost: relativeMalformedHost
4042
} = parseWithStatus(relativeURI, schemelessOptions)
4143
if (
4244
baseMalformed ||
4345
relativeMalformed ||
4446
baseMalformedPercentEncoding ||
4547
relativeMalformedPercentEncoding ||
4648
baseMalformedSchemeSpecific ||
47-
relativeMalformedSchemeSpecific
49+
relativeMalformedSchemeSpecific ||
50+
baseMalformedHost ||
51+
relativeMalformedHost
4852
) {
4953
throw new Error(baseParsed.error || relativeParsed.error || 'URI is malformed.')
5054
}
@@ -315,6 +319,7 @@ function hasMalformedComponentPercentEncoding (matches) {
315319
* @param {import('./types/index').Options} options
316320
* @param {{ domainHost?: boolean, unicodeSupport?: boolean }|undefined} schemeHandler
317321
* @param {boolean} isIP
322+
* @returns {boolean} whether host conversion failed
318323
*/
319324
function canonicalizeHost (parsed, options, schemeHandler, isIP) {
320325
if (
@@ -329,14 +334,16 @@ function canonicalizeHost (parsed, options, schemeHandler, isIP) {
329334
parsed.host = new URL('http://' + parsed.host).hostname
330335
} catch (e) {
331336
parsed.error = parsed.error || "Host's domain name can not be converted to ASCII: " + e
337+
return true
332338
}
333339
}
340+
return false
334341
}
335342

336343
/**
337344
* @param {string} uri
338345
* @param {import('./types/index').Options} [opts]
339-
* @returns {{ parsed: import('./types/index').URIComponent, malformedAuthorityOrPort: boolean, malformedPercentEncoding: boolean, malformedSchemeSpecific: boolean }}
346+
* @returns {{ parsed: import('./types/index').URIComponent, malformedAuthorityOrPort: boolean, malformedPercentEncoding: boolean, malformedSchemeSpecific: boolean, malformedHost: boolean }}
340347
*/
341348
function parseWithStatus (uri, opts) {
342349
const options = Object.assign({}, opts)
@@ -354,6 +361,7 @@ function parseWithStatus (uri, opts) {
354361
let malformedAuthorityOrPort = false
355362
let malformedPercentEncoding = false
356363
let malformedSchemeSpecific = false
364+
let malformedHost = false
357365

358366
let isIP = false
359367
if (options.reference === 'suffix') {
@@ -456,15 +464,15 @@ function parseWithStatus (uri, opts) {
456464
const schemeHandler = getSchemeHandler(options.scheme || parsed.scheme)
457465

458466
// convert Unicode IDN -> ASCII IDN when the effective scheme uses domain hosts
459-
canonicalizeHost(parsed, options, schemeHandler, isIP)
467+
malformedHost = canonicalizeHost(parsed, options, schemeHandler, isIP)
460468

461469
if (!schemeHandler || (schemeHandler && !schemeHandler.skipNormalize)) {
462470
if (uri.indexOf('%') !== -1) {
463471
if (parsed.scheme !== undefined) {
464472
parsed.scheme = unescape(parsed.scheme)
465473
}
466474
if (parsed.host !== undefined) {
467-
parsed.host = reescapeHostDelimiters(unescape(parsed.host), isIP)
475+
parsed.host = reescapeHostDelimiters(normalizePercentEncoding(parsed.host, true), isIP)
468476
}
469477
}
470478
if (parsed.path) {
@@ -488,7 +496,7 @@ function parseWithStatus (uri, opts) {
488496
} else {
489497
parsed.error = parsed.error || 'URI can not be parsed.'
490498
}
491-
return { parsed, malformedAuthorityOrPort, malformedPercentEncoding, malformedSchemeSpecific }
499+
return { parsed, malformedAuthorityOrPort, malformedPercentEncoding, malformedSchemeSpecific, malformedHost }
492500
}
493501

494502
/**
@@ -512,15 +520,16 @@ function normalizeString (uri, opts) {
512520
/**
513521
* @param {string} uri
514522
* @param {import('./types/index').Options} [opts]
515-
* @returns {{ normalized: string, malformedAuthorityOrPort: boolean, malformedPercentEncoding: boolean, malformedSchemeSpecific: boolean }}
523+
* @returns {{ normalized: string, malformedAuthorityOrPort: boolean, malformedPercentEncoding: boolean, malformedSchemeSpecific: boolean, malformedHost: boolean }}
516524
*/
517525
function normalizeStringWithStatus (uri, opts) {
518-
const { parsed, malformedAuthorityOrPort, malformedPercentEncoding, malformedSchemeSpecific } = parseWithStatus(uri, opts)
526+
const { parsed, malformedAuthorityOrPort, malformedPercentEncoding, malformedSchemeSpecific, malformedHost } = parseWithStatus(uri, opts)
519527
return {
520-
normalized: malformedAuthorityOrPort || malformedPercentEncoding || malformedSchemeSpecific ? uri : serialize(parsed, opts),
528+
normalized: malformedAuthorityOrPort || malformedPercentEncoding || malformedSchemeSpecific || malformedHost ? uri : serialize(parsed, opts),
521529
malformedAuthorityOrPort,
522530
malformedPercentEncoding,
523-
malformedSchemeSpecific
531+
malformedSchemeSpecific,
532+
malformedHost
524533
}
525534
}
526535

@@ -535,8 +544,8 @@ function normalizeComparableURI (uri, opts) {
535544
}
536545

537546
const value = typeof uri === 'string' ? uri : serialize(uri, opts)
538-
const { normalized, malformedAuthorityOrPort, malformedPercentEncoding, malformedSchemeSpecific } = normalizeStringWithStatus(value, opts)
539-
return malformedAuthorityOrPort || malformedPercentEncoding || malformedSchemeSpecific ? undefined : normalized
547+
const { normalized, malformedAuthorityOrPort, malformedPercentEncoding, malformedSchemeSpecific, malformedHost } = normalizeStringWithStatus(value, opts)
548+
return malformedAuthorityOrPort || malformedPercentEncoding || malformedSchemeSpecific || malformedHost ? undefined : normalized
540549
}
541550

542551
const fastUri = {

‎lib/utils.js‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,12 @@ function normalizeIPv6 (host) {
192192
let newHost = ipv6.address
193193
let escapedHost = ipv6.address
194194
if (ipv6.zone) {
195-
newHost += '%' + ipv6.zone
196-
escapedHost += '%25' + ipv6.zone
195+
// RFC 6874 encodes the zone separator as "%25" in a URI. Accept both
196+
// component forms used by this API ("%zone" and "%25zone") while
197+
// consuming at most that one separator escape.
198+
const zone = ipv6.zone.startsWith('25') ? ipv6.zone.slice(2) : ipv6.zone
199+
newHost += '%' + zone
200+
escapedHost += '%25' + zone
197201
}
198202
return { host: newHost, isIPV6: true, escapedHost }
199203
} else {
@@ -323,7 +327,7 @@ function reescapeHostDelimiters (host, isIP) {
323327

324328
/**
325329
* Normalizes percent escapes and optionally decodes only unreserved ASCII bytes.
326-
* Reserved delimiters such as `%2F` and `%2E` stay escaped.
330+
* Reserved delimiters such as `%2F` stay escaped; `%2E` is unreserved.
327331
*
328332
* @param {string} input
329333
* @param {boolean} [decodeUnreserved=false]
@@ -653,7 +657,9 @@ function recomposeAuthority (component) {
653657
}
654658

655659
if (component.host !== undefined) {
656-
let host = unescape(component.host)
660+
// Decode only unreserved bytes, once. In particular, keep %25 encoded so
661+
// it cannot become the introducer for a second escape during recomposition.
662+
let host = normalizePercentEncoding(component.host, true)
657663
if (!isIPv4(host)) {
658664
const ipV6res = normalizeIPv6(host)
659665
if (ipV6res.isIPV6 === true) {

‎test/security-normalization.test.js‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,59 @@ test('serialize keeps path-noscheme colon escaping', (t) => {
8282
t.equal(fastURI.serialize({ path: 'a%3Ab' }), 'a%3Ab')
8383
t.end()
8484
})
85+
86+
test('hostname normalization never decodes an escape more than once', (t) => {
87+
const encodedLocalhost = 'http://%256c%256f%2563%2561%256c%2568%256f%2573%2574/'
88+
const encodedLoopback = '//127%252e0%252e0%252e1/private'
89+
const encodedMetadataAddress = '//169%252E254%252E169%252E254/latest/meta-data/'
90+
91+
t.equal(fastURI.normalize(encodedLocalhost), encodedLocalhost, 'nested hostname letters remain encoded')
92+
t.equal(fastURI.normalize(encodedLoopback), encodedLoopback, 'nested IPv4 dots remain encoded')
93+
t.equal(
94+
fastURI.resolve('https://safe.example/', encodedLoopback),
95+
'https://127%252e0%252e0%252e1/private',
96+
'resolve does not turn nested dots into a loopback address'
97+
)
98+
t.equal(
99+
fastURI.resolve('https://allowed.com/api/v1/', encodedMetadataAddress),
100+
'https://169%252e254%252e169%252e254/latest/meta-data/',
101+
'resolve does not turn nested dots into a metadata address'
102+
)
103+
t.equal(
104+
fastURI.normalize('http://allowed.com%255Cevil.com/'),
105+
'http://allowed.com%255Cevil.com/',
106+
'normalize does not activate a nested backslash'
107+
)
108+
t.equal(
109+
fastURI.serialize({ scheme: 'http', host: '%256cocalhost', path: '/' }),
110+
'http://%256cocalhost/',
111+
'component serialization preserves an encoded percent sign'
112+
)
113+
t.equal(
114+
fastURI.equal(encodedLocalhost, 'http://localhost/', {}),
115+
false,
116+
'nested escapes do not compare equal to their twice-decoded target'
117+
)
118+
t.end()
119+
})
120+
121+
test('hostname normalization decodes only current unreserved escapes', (t) => {
122+
t.equal(fastURI.normalize('x://%6cocalhost/'), 'x://localhost/', 'a current unreserved escape is decoded')
123+
t.equal(fastURI.normalize('x://%256cocalhost/'), 'x://%256cocalhost/', 'an encoded percent is preserved')
124+
t.equal(fastURI.normalize('x://host%2540evil/'), 'x://host%2540evil/', 'a nested authority delimiter stays inert')
125+
t.equal(fastURI.normalize('x://%2525/'), 'x://%2525/', 'nested encoded percent signs stay encoded')
126+
t.end()
127+
})
128+
129+
test('host conversion failures are not treated as comparable URLs', (t) => {
130+
const malformedHost = 'http://trusted.test%2540evil.test/'
131+
132+
t.equal(fastURI.normalize(malformedHost), malformedHost, 'normalization preserves the failing input')
133+
t.equal(fastURI.equal(malformedHost, malformedHost, {}), false, 'equal rejects a failed host conversion')
134+
t.throws(
135+
() => fastURI.resolve(malformedHost, 'child', { domainHost: true }),
136+
/Host's domain name can not be converted to ASCII/,
137+
'resolve propagates a host conversion failure'
138+
)
139+
t.end()
140+
})

‎test/security.test.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,13 @@ test('serialize escapes authority delimiters in host field', (t) => {
125125
})
126126

127127
test('normalize does not double-decode %2540 into a live @', (t) => {
128-
const result = fastURI.normalize('http://trusted.com%2540evil.com/')
128+
const input = 'http://trusted.com%2540evil.com/'
129+
const result = fastURI.normalize(input)
129130
const parsed = fastURI.parse(result)
130131

131-
t.plan(1)
132-
t.notEqual(parsed.host, '[email protected]', 'http://trusted.com%2540evil.com/')
132+
t.plan(2)
133+
t.equal(result, input, 'the encoded percent sign is preserved')
134+
t.notEqual(parsed.host, '[email protected]', input)
133135
})
134136

135137
test('parse canonicalises IDN / Unicode hosts to their ASCII form', (t) => {

0 commit comments

Comments
 (0)