fast-uri 3.1.4 → 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
  }
@@ -206,6 +211,15 @@ const URI_PARSE = /^(?:([^#/:?]+):)?(?:\/\/((?:([^#/?@]*)@)?(\[[^#/?\]]+\]|[^#/:
206
211
  // with or without a scheme prefix, for the literal-backslash rejection below.
207
212
  const AUTHORITY_PREFIX = /^(?:[^#/:?]+:)?\/\/([^/?#]*)/
208
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
+
209
223
  /**
210
224
  * @param {import('./types/index').URIComponent} parsed
211
225
  * @param {RegExpMatchArray} matches
@@ -265,6 +279,28 @@ function parseWithStatus (uri, opts) {
265
279
  malformedAuthorityOrPort = true
266
280
  }
267
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
+
268
304
  const matches = uri.match(URI_PARSE)
269
305
 
270
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.4",
4
+ "version": "3.1.5",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
7
7
  "types": "types/index.d.ts",
@@ -221,3 +221,139 @@ test('parse does not reject a literal backslash in the query or fragment', (t) =
221
221
  t.notOk(parsed.error, 'backslash in query/fragment does not mark the URI malformed')
222
222
  t.equal(parsed.host, 'host.example.com', 'host parsed normally')
223
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
+ })