Skip to content

Commit 444ecda

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

2 files changed

Lines changed: 108 additions & 13 deletions

File tree

‎index.js‎

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ function resolve (baseURI, relativeURI, options) {
4949
throw new Error(baseParsed.error || relativeParsed.error || 'URI is malformed.')
5050
}
5151
const resolved = resolveComponent(baseParsed, relativeParsed, schemelessOptions, true)
52+
const resolvedSchemeHandler = getSchemeHandler((options && options.scheme) || resolved.scheme)
53+
const resolvedHost = resolved.host
54+
const resolvedHostIsIP = resolvedHost !== undefined && resolvedHost !== '' &&
55+
(isIPv4(resolvedHost) || normalizeIPv6(resolvedHost).isIPV6)
56+
canonicalizeHost(resolved, options || {}, resolvedSchemeHandler, resolvedHostIsIP)
57+
// Percent escapes in an ASCII reg-name are encoded data. The WHATWG hostname
58+
// parser can reject them even though fast-uri preserves them safely as RFC
59+
// 3986 data. A raw non-ASCII host must still fail closed if conversion fails.
60+
const encodedASCIIHost = resolvedHost && resolvedHost.indexOf('%') !== -1 &&
61+
!/\P{ASCII}/u.test(resolvedHost)
62+
if (resolved.error && !encodedASCIIHost) {
63+
throw new Error(resolved.error)
64+
}
5265
schemelessOptions.skipEscape = true
5366
return serialize(resolved, schemelessOptions)
5467
}
@@ -297,6 +310,29 @@ function hasMalformedComponentPercentEncoding (matches) {
297310
hasMalformedPercentEncoding(matches[8])
298311
}
299312

313+
/**
314+
* @param {import('./types/index').URIComponent} parsed
315+
* @param {import('./types/index').Options} options
316+
* @param {{ domainHost?: boolean, unicodeSupport?: boolean }|undefined} schemeHandler
317+
* @param {boolean} isIP
318+
*/
319+
function canonicalizeHost (parsed, options, schemeHandler, isIP) {
320+
if (
321+
!options.unicodeSupport &&
322+
(!schemeHandler || !schemeHandler.unicodeSupport) &&
323+
parsed.host &&
324+
(options.domainHost || (schemeHandler && schemeHandler.domainHost)) &&
325+
isIP === false &&
326+
nonSimpleDomain(parsed.host)
327+
) {
328+
try {
329+
parsed.host = new URL('http://' + parsed.host).hostname
330+
} catch (e) {
331+
parsed.error = parsed.error || "Host's domain name can not be converted to ASCII: " + e
332+
}
333+
}
334+
}
335+
300336
/**
301337
* @param {string} uri
302338
* @param {import('./types/index').Options} [opts]
@@ -419,19 +455,8 @@ function parseWithStatus (uri, opts) {
419455
// find scheme handler
420456
const schemeHandler = getSchemeHandler(options.scheme || parsed.scheme)
421457

422-
// check if scheme can't handle IRIs
423-
if (!options.unicodeSupport && (!schemeHandler || !schemeHandler.unicodeSupport)) {
424-
// if host component is a domain name
425-
if (parsed.host && (options.domainHost || (schemeHandler && schemeHandler.domainHost)) && isIP === false && nonSimpleDomain(parsed.host)) {
426-
// convert Unicode IDN -> ASCII IDN
427-
try {
428-
parsed.host = new URL('http://' + parsed.host).hostname
429-
} catch (e) {
430-
parsed.error = parsed.error || "Host's domain name can not be converted to ASCII: " + e
431-
}
432-
}
433-
// convert IRI -> URI
434-
}
458+
// convert Unicode IDN -> ASCII IDN when the effective scheme uses domain hosts
459+
canonicalizeHost(parsed, options, schemeHandler, isIP)
435460

436461
if (!schemeHandler || (schemeHandler && !schemeHandler.skipNormalize)) {
437462
if (uri.indexOf('%') !== -1) {

‎test/security.test.js‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,76 @@ test('parse canonicalises IDN / Unicode hosts to their ASCII form', (t) => {
160160
})
161161
})
162162

163+
test('resolve canonicalises the host using the final resolved scheme', (t) => {
164+
const cases = [
165+
{
166+
base: 'http://trusted.example/base',
167+
relative: '//127\u30020\u30020\u30021/private',
168+
expected: 'http://127.0.0.1/private',
169+
expectedHost: '127.0.0.1',
170+
description: 'scheme-relative loopback host'
171+
},
172+
{
173+
base: 'https://ex\u00ADample.com/base',
174+
relative: 'child',
175+
expected: 'https://example.com/child',
176+
expectedHost: 'example.com',
177+
description: 'host inherited from the base'
178+
},
179+
{
180+
base: 'http://trusted.example/base',
181+
relative: 'http://ex\u200Bample.com/',
182+
expected: 'http://example.com/',
183+
expectedHost: 'example.com',
184+
description: 'absolute relative reference'
185+
}
186+
]
187+
188+
t.plan(cases.length * 2)
189+
190+
cases.forEach(({ base, relative, expected, expectedHost, description }) => {
191+
const resolved = fastURI.resolve(base, relative)
192+
t.equal(resolved, expected, description)
193+
t.equal(fastURI.parse(resolved).host, expectedHost, `${description} reparses consistently`)
194+
})
195+
})
196+
197+
test('resolve applies domain canonicalisation only when the effective scheme opts in', (t) => {
198+
const host = 'ex\u00ADample.com'
199+
200+
t.plan(2)
201+
t.equal(
202+
fastURI.resolve('uri://trusted.example/', `//${host}/`),
203+
`uri://${host}/`,
204+
'an unsupported scheme preserves the host'
205+
)
206+
t.equal(
207+
fastURI.resolve('http://trusted.example/', `//${host}/`, { unicodeSupport: true }),
208+
`http://${host}/`,
209+
'unicodeSupport preserves the Unicode host'
210+
)
211+
})
212+
213+
test('resolve throws when the final scheme cannot canonicalise the host', (t) => {
214+
const invalidHost = '\u200D.example'
215+
const cases = [
216+
['http://trusted.example/', `//${invalidHost}/`],
217+
[`https://${invalidHost}/base`, 'child'],
218+
['http://trusted.example/', `http://${invalidHost}/`],
219+
['http://trusted.example/', `//${invalidHost}%2Etest/`]
220+
]
221+
222+
t.plan(cases.length)
223+
224+
cases.forEach(([base, relative]) => {
225+
t.throws(
226+
() => fastURI.resolve(base, relative),
227+
/Host's domain name can not be converted to ASCII/,
228+
`${base} + ${relative}`
229+
)
230+
})
231+
})
232+
163233
test('parse rejects a literal backslash in the authority as malformed (RFC 3986)', (t) => {
164234
// Regression for the host-confusion bypass: a literal "\" is invalid RFC 3986
165235
// syntax and must be flagged malformed, not silently rewritten. Otherwise "\"

0 commit comments

Comments
 (0)