fast-uri 3.1.3 → 3.1.5

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 CHANGED
@@ -26,7 +26,12 @@ function normalize (uri, options) {
26
26
  */
27
27
  function resolve (baseURI, relativeURI, options) {
28
28
  const schemelessOptions = options ? Object.assign({ scheme: 'null' }, options) : { scheme: 'null' }
29
- const resolved = resolveComponent(parse(baseURI, schemelessOptions), parse(relativeURI, schemelessOptions), schemelessOptions, true)
29
+ const { parsed: baseParsed, malformedAuthorityOrPort: baseMalformed } = parseWithStatus(baseURI, schemelessOptions)
30
+ const { parsed: relativeParsed, malformedAuthorityOrPort: relativeMalformed } = parseWithStatus(relativeURI, schemelessOptions)
31
+ if (baseMalformed || relativeMalformed) {
32
+ throw new Error(baseParsed.error || relativeParsed.error || 'URI is malformed.')
33
+ }
34
+ const resolved = resolveComponent(baseParsed, relativeParsed, schemelessOptions, true)
30
35
  schemelessOptions.skipEscape = true
31
36
  return serialize(resolved, schemelessOptions)
32
37
  }
@@ -202,6 +207,19 @@ function serialize (cmpts, opts) {
202
207
 
203
208
  const URI_PARSE = /^(?:([^#/:?]+):)?(?:\/\/((?:([^#/?@]*)@)?(\[[^#/?\]]+\]|[^#/:?]*)(?::(\d*))?))?([^#?]*)(?:\?([^#]*))?(?:#((?:.|[\n\r])*))?/u
204
209
 
210
+ // Captures the authority component (between "//" and the next "/", "?" or "#"),
211
+ // with or without a scheme prefix, for the literal-backslash rejection below.
212
+ const AUTHORITY_PREFIX = /^(?:[^#/:?]+:)?\/\/([^/?#]*)/
213
+
214
+ // Captures the leading authority-introducer region after an optional scheme: a
215
+ // run of forward slashes, backslashes, and the characters the WHATWG URL parser
216
+ // removes before parsing (TAB U+0009, LF U+000A, CR U+000D). A valid introducer
217
+ // is exactly "//". Node treats "\" as "/" on special schemes and strips those
218
+ // characters first, so forms like "\\", "/\", "\/", "/<TAB>/", or a leading
219
+ // "<TAB>//" reach an authority in Node while fast-uri's URI_PARSE folds them into
220
+ // the path group (host confusion / SSRF / redirect bypass).
221
+ const AUTHORITY_INTRODUCER_REGION = /^(?:[^#/:?]+:)?([/\\\t\n\r]*)/
222
+
205
223
  /**
206
224
  * @param {import('./types/index').URIComponent} parsed
207
225
  * @param {RegExpMatchArray} matches
@@ -248,6 +266,41 @@ function parseWithStatus (uri, opts) {
248
266
  }
249
267
  }
250
268
 
269
+ // A literal backslash (U+005C) is not a valid RFC 3986 URI character and is
270
+ // not an authority delimiter. Reject it in the authority rather than
271
+ // rewriting it: normalizing "\" -> "/" (WHATWG error recovery) could silently
272
+ // change the resource identified by an otherwise-invalid input, and lets "\"
273
+ // act as a host delimiter here while Node's native URL parses a different
274
+ // host (SSRF / redirect / origin-allowlist bypass). Percent-encoded %5C is
275
+ // untouched and remains valid encoded data.
276
+ const authorityMatch = uri.match(AUTHORITY_PREFIX)
277
+ if (authorityMatch !== null && authorityMatch[1].indexOf('\\') !== -1) {
278
+ parsed.error = 'URI authority must not contain a literal backslash.'
279
+ malformedAuthorityOrPort = true
280
+ }
281
+
282
+ // Reject a malformed or whitespace-smuggled authority introducer. fast-uri
283
+ // only recognizes a literal "//"; anything else in the leading separator run
284
+ // (a backslash, or a "//" that appears only after removing the TAB/LF/CR that
285
+ // Node strips) means the authority fast-uri parses differs from the one Node's
286
+ // URL resolves. Reject rather than rewrite, mirroring the literal-backslash
287
+ // guard above. Percent-encoded forms (%5C, %09) are untouched, valid data.
288
+ const introducerMatch = uri.match(AUTHORITY_INTRODUCER_REGION)
289
+ if (introducerMatch !== null) {
290
+ const region = introducerMatch[1]
291
+ const normalizedRegion = region.replace(/[\t\n\r]/g, '')
292
+ // Two or more leading separators introduce an authority.
293
+ if (normalizedRegion.length >= 2) {
294
+ if (normalizedRegion.slice(0, 2) !== '//') {
295
+ parsed.error = parsed.error || 'URI authority must not contain a literal backslash.'
296
+ malformedAuthorityOrPort = true
297
+ } else if (region.length !== normalizedRegion.length) {
298
+ parsed.error = parsed.error || 'URI authority introducer must not contain whitespace.'
299
+ malformedAuthorityOrPort = true
300
+ }
301
+ }
302
+ }
303
+
251
304
  const matches = uri.match(URI_PARSE)
252
305
 
253
306
  if (matches) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "fast-uri",
3
3
  "description": "Dependency-free RFC 3986 URI toolbox",
4
- "version": "3.1.3",
4
+ "version": "3.1.5",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
7
7
  "types": "types/index.d.ts",
@@ -159,3 +159,201 @@ 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
+ })
224
+
225
+ test('parse rejects a malformed authority introducer (\\\\, /\\, \\/) in place of //', (t) => {
226
+ // Regression: "\\", "/\\", "\\/" after the scheme colon are not valid authority
227
+ // introducers. Node's URL treats "\\" as interchangeable with "/" on special
228
+ // schemes, so "http:\\\\evil.com/path" would be parsed as host "evil.com" by
229
+ // Node, but fast-uri must reject it as malformed to prevent SSRF/redirect bypass.
230
+ const cases = [
231
+ 'http:\\\\evil.com/path',
232
+ 'http:/\\evil.com/path',
233
+ 'http:\\/evil.com/path',
234
+ 'ws:\\\\evil.com/chat',
235
+ 'wss:\\\\evil.com/chat',
236
+ 'ftp:\\\\evil.com/',
237
+ '\\\\evil.com/path'
238
+ ]
239
+
240
+ t.plan(cases.length)
241
+
242
+ cases.forEach((input) => {
243
+ t.equal(
244
+ fastURI.parse(input).error,
245
+ 'URI authority must not contain a literal backslash.',
246
+ input
247
+ )
248
+ })
249
+ })
250
+
251
+ test('normalize does not canonicalize a malformed-authority-introducer URI', (t) => {
252
+ const cases = [
253
+ 'http:\\\\evil.com/path',
254
+ 'http:/\\evil.com/path'
255
+ ]
256
+
257
+ t.plan(cases.length)
258
+
259
+ cases.forEach((input) => {
260
+ t.equal(fastURI.normalize(input), input, input)
261
+ })
262
+ })
263
+
264
+ test('equal returns false for malformed-authority-introducer URIs', (t) => {
265
+ const pairs = [
266
+ ['http:\\\\evil.com/path', 'http://evil.com/path'],
267
+ ['http:/\\evil.com/path', 'http://evil.com/path']
268
+ ]
269
+
270
+ t.plan(pairs.length)
271
+
272
+ pairs.forEach(([left, right]) => {
273
+ t.equal(fastURI.equal(left, right), false, `${left} != ${right}`)
274
+ })
275
+ })
276
+
277
+ test('resolve throws on malformed authority introducer', (t) => {
278
+ // resolve() returns a plain string with no error field, so the only safe
279
+ // behavior is to throw when either component has a malformed authority.
280
+ const pairs = [
281
+ ['https://allowed.com/', '\\\\evil.com/path'],
282
+ ['\\\\evil.com/path', 'https://allowed.com/'],
283
+ ['https://allowed.com/', 'http:/\\evil.com/path'],
284
+ ['https://allowed.com/', 'http:\\/evil.com/path']
285
+ ]
286
+
287
+ t.plan(pairs.length)
288
+
289
+ pairs.forEach(([base, rel]) => {
290
+ t.throws(
291
+ () => fastURI.resolve(base, rel),
292
+ /URI authority must not contain a literal backslash/,
293
+ `${base} + ${rel}`
294
+ )
295
+ })
296
+ })
297
+
298
+ test('parse rejects a whitespace-split authority introducer (TAB, LF, CR)', (t) => {
299
+ // The WHATWG URL parser removes TAB (U+0009), LF (U+000A) and CR (U+000D) from
300
+ // the input before parsing, so a stripped character wedged into the introducer
301
+ // ("/<TAB>\\", "/<TAB>/", or a leading "<TAB>//") reaches an authority in Node
302
+ // while fast-uri would otherwise fold it into the path. These must be rejected
303
+ // like the adjacent "\\", "/\\", "\\/" forms.
304
+ const cases = [
305
+ { input: '/\t\\evil.com/path', expectedError: 'URI authority must not contain a literal backslash.' },
306
+ { input: '/\t/evil.com/path', expectedError: 'URI authority introducer must not contain whitespace.' },
307
+ { input: '/\n\\evil.com/path', expectedError: 'URI authority must not contain a literal backslash.' },
308
+ { input: '/\r\\evil.com/path', expectedError: 'URI authority must not contain a literal backslash.' },
309
+ { input: '\t//evil.com/path', expectedError: 'URI authority introducer must not contain whitespace.' },
310
+ { input: '\t/\\evil.com/path', expectedError: 'URI authority must not contain a literal backslash.' },
311
+ { input: 'https:/\t/evil.com/path', expectedError: 'URI authority introducer must not contain whitespace.' }
312
+ ]
313
+
314
+ t.plan(cases.length)
315
+
316
+ cases.forEach(({ input, expectedError }) => {
317
+ t.equal(fastURI.parse(input).error, expectedError, JSON.stringify(input))
318
+ })
319
+ })
320
+
321
+ test('resolve throws on a whitespace-split authority introducer', (t) => {
322
+ const pairs = [
323
+ ['https://allowed.com/', '/\t\\evil.com/path'],
324
+ ['https://allowed.com/', '/\t/evil.com/path'],
325
+ ['https://allowed.com/', '/\n\\evil.com/path'],
326
+ ['/\t/evil.com/path', 'https://allowed.com/']
327
+ ]
328
+
329
+ t.plan(pairs.length)
330
+
331
+ pairs.forEach(([base, rel]) => {
332
+ t.throws(
333
+ () => fastURI.resolve(base, rel),
334
+ /URI authority (must not contain a literal backslash|introducer must not contain whitespace)/,
335
+ `${JSON.stringify(base)} + ${JSON.stringify(rel)}`
336
+ )
337
+ })
338
+ })
339
+
340
+ test('parse does not reject valid authority introducer patterns', (t) => {
341
+ // No false positives: "//" introducer and scheme-less "//" must be valid.
342
+ const cases = [
343
+ 'http://good.com/',
344
+ 'https://good.com/',
345
+ 'ws://good.com/chat',
346
+ 'wss://good.com/chat',
347
+ 'ftp://good.com/',
348
+ '//good.com/path',
349
+ '/absolute/path',
350
+ 'relative/path'
351
+ ]
352
+
353
+ t.plan(cases.length)
354
+
355
+ cases.forEach((input) => {
356
+ const parsed = fastURI.parse(input)
357
+ t.notOk(parsed.error, input)
358
+ })
359
+ })