fast-uri 2.4.1 → 2.4.3
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/.github/workflows/ci.yml +3 -0
- package/index.js +18 -1
- package/package.json +1 -1
- package/test/security.test.js +90 -0
package/.github/workflows/ci.yml
CHANGED
package/index.js
CHANGED
|
@@ -181,6 +181,10 @@ function nonSimpleDomain (value) {
|
|
|
181
181
|
|
|
182
182
|
const URI_PARSE = /^(?:([^#/:?]+):)?(?:\/\/((?:([^#/?@]*)@)?(\[[^#/?\]]+\]|[^#/:?]*)(?::(\d*))?))?([^#?]*)(?:\?([^#]*))?(?:#((?:.|[\n\r])*))?/u
|
|
183
183
|
|
|
184
|
+
// Captures the authority component (between "//" and the next "/", "?" or "#"),
|
|
185
|
+
// with or without a scheme prefix, for the literal-backslash rejection below.
|
|
186
|
+
const AUTHORITY_PREFIX = /^(?:[^#/:?]+:)?\/\/([^/?#]*)/
|
|
187
|
+
|
|
184
188
|
function getParseError (parsed, matches) {
|
|
185
189
|
if (matches[2] !== undefined && parsed.path && parsed.path[0] !== '/') {
|
|
186
190
|
return 'URI path must start with "/" when authority is present.'
|
|
@@ -209,6 +213,19 @@ function parseWithStatus (uri, opts) {
|
|
|
209
213
|
let isIP = false
|
|
210
214
|
if (options.reference === 'suffix') uri = (options.scheme ? options.scheme + ':' : '') + '//' + uri
|
|
211
215
|
|
|
216
|
+
// A literal backslash (U+005C) is not a valid RFC 3986 URI character and is
|
|
217
|
+
// not an authority delimiter. Reject it in the authority rather than
|
|
218
|
+
// rewriting it: normalizing "\" -> "/" (WHATWG error recovery) could silently
|
|
219
|
+
// change the resource identified by an otherwise-invalid input, and lets "\"
|
|
220
|
+
// act as a host delimiter here while Node's native URL parses a different
|
|
221
|
+
// host (SSRF / redirect / origin-allowlist bypass). Percent-encoded %5C is
|
|
222
|
+
// untouched and remains valid encoded data.
|
|
223
|
+
const authorityMatch = uri.match(AUTHORITY_PREFIX)
|
|
224
|
+
if (authorityMatch !== null && authorityMatch[1].indexOf('\\') !== -1) {
|
|
225
|
+
parsed.error = 'URI authority must not contain a literal backslash.'
|
|
226
|
+
malformedAuthorityOrPort = true
|
|
227
|
+
}
|
|
228
|
+
|
|
212
229
|
const matches = uri.match(URI_PARSE)
|
|
213
230
|
|
|
214
231
|
if (matches) {
|
|
@@ -267,7 +284,7 @@ function parseWithStatus (uri, opts) {
|
|
|
267
284
|
if (parsed.host && (options.domainHost || (schemeHandler && schemeHandler.domainHost)) && isIP === false && nonSimpleDomain(parsed.host)) {
|
|
268
285
|
// convert Unicode IDN -> ASCII IDN
|
|
269
286
|
try {
|
|
270
|
-
parsed.host = URL
|
|
287
|
+
parsed.host = new URL('http://' + parsed.host).hostname
|
|
271
288
|
} catch (e) {
|
|
272
289
|
parsed.error = parsed.error || "Host's domain name can not be converted to ASCII: " + e
|
|
273
290
|
}
|
package/package.json
CHANGED
package/test/security.test.js
CHANGED
|
@@ -132,3 +132,93 @@ test('normalize does not double-decode %2540 into a live @', (t) => {
|
|
|
132
132
|
t.plan(1)
|
|
133
133
|
t.not(parsed.host, 'trusted.com@evil.com', 'http://trusted.com%2540evil.com/')
|
|
134
134
|
})
|
|
135
|
+
|
|
136
|
+
test('parse canonicalises IDN / Unicode hosts to their ASCII form', (t) => {
|
|
137
|
+
const cases = [
|
|
138
|
+
{
|
|
139
|
+
input: 'http://127。0。0。1/',
|
|
140
|
+
expectedHost: '127.0.0.1',
|
|
141
|
+
description: 'full-width ideographic stops as octet separators'
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
input: 'http://example.com/',
|
|
145
|
+
expectedHost: 'example.com',
|
|
146
|
+
description: 'fullwidth e as first letter'
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
input: 'http://納豆.example.org/',
|
|
150
|
+
expectedHost: 'xn--99zt52a.example.org',
|
|
151
|
+
description: 'CJK label requiring punycode'
|
|
152
|
+
}
|
|
153
|
+
]
|
|
154
|
+
|
|
155
|
+
t.plan(cases.length * 2)
|
|
156
|
+
|
|
157
|
+
cases.forEach(({ input, expectedHost, description }) => {
|
|
158
|
+
const parsed = fastURI.parse(input)
|
|
159
|
+
t.notOk(parsed.error, `parse should not set error: ${description}`)
|
|
160
|
+
t.equal(parsed.host, expectedHost, `host canonicalised to ASCII: ${description}`)
|
|
161
|
+
})
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
test('parse rejects a literal backslash in the authority as malformed (RFC 3986)', (t) => {
|
|
165
|
+
// Regression for the host-confusion bypass: a literal "\" is invalid RFC 3986
|
|
166
|
+
// syntax and must be flagged malformed, not silently rewritten. Otherwise "\"
|
|
167
|
+
// acts as a host delimiter here while Node's native URL parses a different
|
|
168
|
+
// host, defeating a host-based SSRF/redirect/origin allowlist.
|
|
169
|
+
const cases = [
|
|
170
|
+
'http://evil.com\\@allowed.com',
|
|
171
|
+
'https://169.254.169.254\\@trusted.example.com',
|
|
172
|
+
'http://127.0.0.1\\@public.example.com',
|
|
173
|
+
'https://attacker.com\\@api.internal',
|
|
174
|
+
'http://a\\@b',
|
|
175
|
+
'ws://evil.com\\@allowed.com/chat',
|
|
176
|
+
'wss://evil.com\\@allowed.com/chat',
|
|
177
|
+
'http://evil.com\\%40allowed.com',
|
|
178
|
+
'//evil.com\\@allowed.com'
|
|
179
|
+
]
|
|
180
|
+
|
|
181
|
+
t.plan(cases.length)
|
|
182
|
+
|
|
183
|
+
cases.forEach((input) => {
|
|
184
|
+
t.equal(
|
|
185
|
+
fastURI.parse(input).error,
|
|
186
|
+
'URI authority must not contain a literal backslash.',
|
|
187
|
+
input
|
|
188
|
+
)
|
|
189
|
+
})
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
test('normalize does not canonicalize a literal-backslash URI into a different valid URL', (t) => {
|
|
193
|
+
const cases = [
|
|
194
|
+
'http://evil.com\\@allowed.com',
|
|
195
|
+
'https://attacker.com\\@api.internal'
|
|
196
|
+
]
|
|
197
|
+
|
|
198
|
+
t.plan(cases.length)
|
|
199
|
+
|
|
200
|
+
cases.forEach((input) => {
|
|
201
|
+
t.equal(fastURI.normalize(input), input, input)
|
|
202
|
+
})
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
test('parse leaves percent-encoded %5C untouched as encoded data (not rejected)', (t) => {
|
|
206
|
+
// Only the literal "\" byte is rejected; %5C stays valid encoded data and
|
|
207
|
+
// does not diverge from the native URL parser, so it must not be flagged.
|
|
208
|
+
const input = 'http://evil.com%5C@allowed.com'
|
|
209
|
+
const parsed = fastURI.parse(input)
|
|
210
|
+
|
|
211
|
+
t.plan(2)
|
|
212
|
+
t.notOk(parsed.error, '%5C is valid encoded data, not malformed')
|
|
213
|
+
t.equal(parsed.host, new URL(input).hostname, '%5C host matches native URL (no divergence)')
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
test('parse does not reject a literal backslash in the query or fragment', (t) => {
|
|
217
|
+
// The rejection is scoped to the authority/path (the host-confusion surface);
|
|
218
|
+
// a backslash after "?"/"#" is normalized as encoded data as before.
|
|
219
|
+
const parsed = fastURI.parse('http://host.example.com/?x=\\y#z\\w')
|
|
220
|
+
|
|
221
|
+
t.plan(2)
|
|
222
|
+
t.notOk(parsed.error, 'backslash in query/fragment does not mark the URI malformed')
|
|
223
|
+
t.equal(parsed.host, 'host.example.com', 'host parsed normally')
|
|
224
|
+
})
|