Skip to content

Commit 1eb3ce4

Browse files
chuanghiduocmcollina
authored andcommitted
fix: treat unterminated bracket hosts as reg-names again (#214)
(cherry picked from commit e008152)
1 parent 6f970b2 commit 1eb3ce4

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎index.js‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,18 @@ function hasMalformedPercentEncoding (component) {
324324
return false
325325
}
326326

327+
/**
328+
* Whether the host is a bracketed IP literal (RFC 3986 `IP-literal`).
329+
* An unterminated `[` is not a literal, so it must still be validated as a
330+
* reg-name instead of being waved through as an IP.
331+
*
332+
* @param {string} host
333+
* @returns {boolean}
334+
*/
335+
function isIPLiteral (host) {
336+
return host[0] === '[' && host[host.length - 1] === ']'
337+
}
338+
327339
/**
328340
* @param {RegExpMatchArray} matches
329341
* @returns {boolean}
@@ -333,7 +345,7 @@ function hasMalformedComponentPercentEncoding (matches) {
333345
// compatibility. Their parsing is intentionally left to normalizeIPv6.
334346
const host = matches[4]
335347
return hasMalformedPercentEncoding(matches[3]) ||
336-
(host !== undefined && !(host[0] === '[' && host[host.length - 1] === ']') && hasMalformedPercentEncoding(host)) ||
348+
(host !== undefined && !isIPLiteral(host) && hasMalformedPercentEncoding(host)) ||
337349
hasMalformedPercentEncoding(matches[6]) ||
338350
hasMalformedPercentEncoding(matches[7]) ||
339351
hasMalformedPercentEncoding(matches[8])
@@ -351,7 +363,7 @@ function canonicalizeHost (parsed, options, schemeHandler, isIP) {
351363
!options.unicodeSupport &&
352364
(!schemeHandler || !schemeHandler.unicodeSupport) &&
353365
parsed.host &&
354-
parsed.host[0] !== '[' &&
366+
!isIPLiteral(parsed.host) &&
355367
(options.domainHost || (schemeHandler && schemeHandler.domainHost)) &&
356368
isIP === false &&
357369
nonSimpleDomain(parsed.host)

‎test/ipv6-validation.test.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,19 @@ test('valid IPv6, IPvFuture, embedded IPv4, and zone forms normalize safely', (t
8888
}
8989
t.end()
9090
})
91+
92+
test('unterminated bracket hosts are not treated as IP literals', (t) => {
93+
const unterminated = [
94+
'http://[fe80',
95+
'http://[',
96+
'http://[not-an-ip',
97+
'http://[\u65e5\u672c'
98+
]
99+
100+
for (const uri of unterminated) {
101+
const parsed = fastURI.parse(uri)
102+
t.ok(parsed.error, `parse rejects ${uri}`)
103+
t.equal(fastURI.normalize(uri), uri, `normalize preserves ${uri}`)
104+
}
105+
t.end()
106+
})

0 commit comments

Comments
 (0)