fast-uri 4.1.1 → 4.1.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.
@@ -0,0 +1,122 @@
1
+ 'use strict'
2
+
3
+ const test = require('tape')
4
+ const fastURI = require('..')
5
+
6
+ const MALFORMED_SCHEME_ERROR = 'URI scheme is malformed.'
7
+
8
+ const malformedSchemes = [
9
+ '%2f%2fevil.example:/pwn',
10
+ '%u002f%u002fevil.example:/pwn',
11
+ '%0d%0aSet-Cookie:%20sid=attacker:/p',
12
+ 'foo%3Abar:value',
13
+ 'foo%2Fbar:value',
14
+ '1http://example.com/',
15
+ 'foo_bar:value',
16
+ 'éxample:value',
17
+ 'Kttp://example.com/',
18
+ 'ſcheme:value'
19
+ ]
20
+
21
+ test('parse validates the decoded scheme against RFC 3986', (t) => {
22
+ const validSchemes = [
23
+ ['a:value', 'a'],
24
+ ['HTTP://example.com/', 'http'],
25
+ ['a1+.-:value', 'a1+.-'],
26
+ ['%4Aavascript:alert(1)', 'javascript'],
27
+ ['foo%2Bbar:value', 'foo+bar'],
28
+ ['%u006Aavascript:1', 'javascript'],
29
+ ['ht%74ps://example.com/', 'https']
30
+ ]
31
+
32
+ for (const [uri, scheme] of validSchemes) {
33
+ const parsed = fastURI.parse(uri)
34
+ t.equal(parsed.error, undefined, uri)
35
+ t.equal(parsed.scheme, scheme, uri + ' scheme')
36
+ }
37
+
38
+ for (const uri of malformedSchemes) {
39
+ const parsed = fastURI.parse(uri)
40
+ t.equal(parsed.error, MALFORMED_SCHEME_ERROR, uri)
41
+ }
42
+ t.end()
43
+ })
44
+
45
+ test('decoded schemes select their scheme handlers', (t) => {
46
+ t.equal(
47
+ fastURI.normalize('ht%74ps://example.com:443'),
48
+ 'https://example.com/',
49
+ 'HTTP normalization runs after decoding the scheme'
50
+ )
51
+
52
+ const mailto = fastURI.parse('ma%69lto:user@example.org')
53
+ t.deepEqual(mailto.to, ['user@example.org'], 'mailto parsing runs after decoding the scheme')
54
+ t.end()
55
+ })
56
+
57
+ test('normalize preserves schemes that decode to invalid identifiers', (t) => {
58
+ for (const uri of malformedSchemes) {
59
+ t.equal(fastURI.normalize(uri), uri, uri)
60
+ }
61
+ t.end()
62
+ })
63
+
64
+ test('scheme normalization cannot introduce authority or control delimiters', (t) => {
65
+ const authority = '%2f%2fevil.example:/pwn'
66
+ const crlf = '%0d%0aSet-Cookie:%20sid=attacker:/p'
67
+
68
+ t.equal(fastURI.parse(authority).host, undefined, 'original input has no authority')
69
+ t.equal(fastURI.normalize(authority), authority, 'normalization does not create an authority')
70
+ t.equal(fastURI.normalize(crlf), crlf, 'normalization does not emit raw CRLF')
71
+ t.equal(fastURI.normalize(crlf).includes('\r\n'), false, 'normalized output contains no raw CRLF')
72
+ t.end()
73
+ })
74
+
75
+ test('equal returns false for malformed decoded schemes', (t) => {
76
+ for (const uri of malformedSchemes) {
77
+ t.equal(fastURI.equal(uri, uri, {}), false, uri)
78
+ }
79
+ t.end()
80
+ })
81
+
82
+ test('resolve rejects malformed decoded schemes in either input', (t) => {
83
+ t.throws(
84
+ () => fastURI.resolve('%2f%2fevil.example:/base', 'child'),
85
+ /URI scheme is malformed\./,
86
+ 'malformed base'
87
+ )
88
+ t.throws(
89
+ () => fastURI.resolve('https://allowed.example/app/', '%2f%2fevil.example:/pwn'),
90
+ /URI scheme is malformed\./,
91
+ 'malformed relative reference'
92
+ )
93
+ t.end()
94
+ })
95
+
96
+ test('serialize validates decoded component schemes', (t) => {
97
+ t.equal(
98
+ fastURI.serialize({ scheme: 'foo%2Bbar', path: 'value' }),
99
+ 'foo+bar:value',
100
+ 'valid decoded scheme is serialized'
101
+ )
102
+ t.throws(
103
+ () => fastURI.serialize({ scheme: '//evil.example', path: '/pwn' }),
104
+ /URI scheme is malformed\./,
105
+ 'raw invalid scheme'
106
+ )
107
+ t.throws(
108
+ () => fastURI.serialize({ scheme: '%2f%2fevil.example', path: '/pwn' }),
109
+ /URI scheme is malformed\./,
110
+ 'encoded invalid scheme'
111
+ )
112
+ t.equal(
113
+ fastURI.equal(
114
+ { scheme: '%2f%2fevil.example', path: '/pwn' },
115
+ { scheme: '%2f%2fevil.example', path: '/pwn' },
116
+ {}
117
+ ),
118
+ false,
119
+ 'equality fails closed for malformed component objects'
120
+ )
121
+ t.end()
122
+ })
@@ -37,3 +37,104 @@ test('equal does not treat reserved path escapes as live path syntax', (t) => {
37
37
 
38
38
  t.end()
39
39
  })
40
+
41
+ test('serialize preserves literal RFC 3986 reserved path characters', (t) => {
42
+ // GHSA-7mh8-fcmq-x23c: literal reserved path chars must not be rewritten to
43
+ // percent escapes by the escape()-style safe set.
44
+ const cases = [
45
+ 'http://example.com/a;b',
46
+ 'http://example.com/a=b',
47
+ 'http://example.com/a&b',
48
+ 'http://example.com/a$b',
49
+ 'http://example.com/a:b',
50
+ 'http://example.com/a@b'
51
+ ]
52
+
53
+ t.plan(cases.length)
54
+
55
+ cases.forEach((uri) => {
56
+ t.equal(fastURI.serialize(fastURI.parse(uri)), uri, uri)
57
+ })
58
+ t.end()
59
+ })
60
+
61
+ test('serialize preserves existing reserved path escapes as data', (t) => {
62
+ // GHSA-7mh8-fcmq-x23c: an existing %3A escape must stay %3A instead of being
63
+ // rewritten into a live colon.
64
+ const cases = [
65
+ 'http://example.com/a%3Ab',
66
+ 'http://example.com/a%3Bb',
67
+ 'http://example.com/a%3D%3D'
68
+ ]
69
+
70
+ t.plan(cases.length)
71
+
72
+ cases.forEach((uri) => {
73
+ t.equal(fastURI.serialize(fastURI.parse(uri)), uri, uri)
74
+ })
75
+ t.end()
76
+ })
77
+
78
+ test('serialize keeps path-noscheme colon escaping', (t) => {
79
+ // Without a scheme a literal colon would be reparsed as a scheme separator,
80
+ // so it must stay percent-escaped while an existing %3A is preserved.
81
+ t.equal(fastURI.serialize({ path: 'foo:bar' }), 'foo%3Abar')
82
+ t.equal(fastURI.serialize({ path: 'a%3Ab' }), 'a%3Ab')
83
+ t.end()
84
+ })
85
+
86
+ test('hostname normalization never decodes an escape more than once', (t) => {
87
+ const encodedLocalhost = 'http://%256c%256f%2563%2561%256c%2568%256f%2573%2574/'
88
+ const encodedLoopback = '//127%252e0%252e0%252e1/private'
89
+ const encodedMetadataAddress = '//169%252E254%252E169%252E254/latest/meta-data/'
90
+
91
+ t.equal(fastURI.normalize(encodedLocalhost), encodedLocalhost, 'nested hostname letters remain encoded')
92
+ t.equal(fastURI.normalize(encodedLoopback), encodedLoopback, 'nested IPv4 dots remain encoded')
93
+ t.equal(
94
+ fastURI.resolve('https://safe.example/', encodedLoopback),
95
+ 'https://127%252e0%252e0%252e1/private',
96
+ 'resolve does not turn nested dots into a loopback address'
97
+ )
98
+ t.equal(
99
+ fastURI.resolve('https://allowed.com/api/v1/', encodedMetadataAddress),
100
+ 'https://169%252e254%252e169%252e254/latest/meta-data/',
101
+ 'resolve does not turn nested dots into a metadata address'
102
+ )
103
+ t.equal(
104
+ fastURI.normalize('http://allowed.com%255Cevil.com/'),
105
+ 'http://allowed.com%255Cevil.com/',
106
+ 'normalize does not activate a nested backslash'
107
+ )
108
+ t.equal(
109
+ fastURI.serialize({ scheme: 'http', host: '%256cocalhost', path: '/' }),
110
+ 'http://%256cocalhost/',
111
+ 'component serialization preserves an encoded percent sign'
112
+ )
113
+ t.equal(
114
+ fastURI.equal(encodedLocalhost, 'http://localhost/', {}),
115
+ false,
116
+ 'nested escapes do not compare equal to their twice-decoded target'
117
+ )
118
+ t.end()
119
+ })
120
+
121
+ test('hostname normalization decodes only current unreserved escapes', (t) => {
122
+ t.equal(fastURI.normalize('x://%6cocalhost/'), 'x://localhost/', 'a current unreserved escape is decoded')
123
+ t.equal(fastURI.normalize('x://%256cocalhost/'), 'x://%256cocalhost/', 'an encoded percent is preserved')
124
+ t.equal(fastURI.normalize('x://host%2540evil/'), 'x://host%2540evil/', 'a nested authority delimiter stays inert')
125
+ t.equal(fastURI.normalize('x://%2525/'), 'x://%2525/', 'nested encoded percent signs stay encoded')
126
+ t.end()
127
+ })
128
+
129
+ test('host conversion failures are not treated as comparable URLs', (t) => {
130
+ const malformedHost = 'http://trusted.test%2540evil.test/'
131
+
132
+ t.equal(fastURI.normalize(malformedHost), malformedHost, 'normalization preserves the failing input')
133
+ t.equal(fastURI.equal(malformedHost, malformedHost, {}), false, 'equal rejects a failed host conversion')
134
+ t.throws(
135
+ () => fastURI.resolve(malformedHost, 'child', { domainHost: true }),
136
+ /Host's domain name can not be converted to ASCII/,
137
+ 'resolve propagates a host conversion failure'
138
+ )
139
+ t.end()
140
+ })
Binary file
@@ -125,11 +125,13 @@ test('serialize escapes authority delimiters in host field', (t) => {
125
125
  })
126
126
 
127
127
  test('normalize does not double-decode %2540 into a live @', (t) => {
128
- const result = fastURI.normalize('http://trusted.com%2540evil.com/')
128
+ const input = 'http://trusted.com%2540evil.com/'
129
+ const result = fastURI.normalize(input)
129
130
  const parsed = fastURI.parse(result)
130
131
 
131
- t.plan(1)
132
- t.notEqual(parsed.host, 'trusted.com@evil.com', 'http://trusted.com%2540evil.com/')
132
+ t.plan(2)
133
+ t.equal(result, input, 'the encoded percent sign is preserved')
134
+ t.notEqual(parsed.host, 'trusted.com@evil.com', input)
133
135
  })
134
136
 
135
137
  test('parse canonicalises IDN / Unicode hosts to their ASCII form', (t) => {
@@ -160,6 +162,76 @@ test('parse canonicalises IDN / Unicode hosts to their ASCII form', (t) => {
160
162
  })
161
163
  })
162
164
 
165
+ test('resolve canonicalises the host using the final resolved scheme', (t) => {
166
+ const cases = [
167
+ {
168
+ base: 'http://trusted.example/base',
169
+ relative: '//127\u30020\u30020\u30021/private',
170
+ expected: 'http://127.0.0.1/private',
171
+ expectedHost: '127.0.0.1',
172
+ description: 'scheme-relative loopback host'
173
+ },
174
+ {
175
+ base: 'https://ex\u00ADample.com/base',
176
+ relative: 'child',
177
+ expected: 'https://example.com/child',
178
+ expectedHost: 'example.com',
179
+ description: 'host inherited from the base'
180
+ },
181
+ {
182
+ base: 'http://trusted.example/base',
183
+ relative: 'http://ex\u200Bample.com/',
184
+ expected: 'http://example.com/',
185
+ expectedHost: 'example.com',
186
+ description: 'absolute relative reference'
187
+ }
188
+ ]
189
+
190
+ t.plan(cases.length * 2)
191
+
192
+ cases.forEach(({ base, relative, expected, expectedHost, description }) => {
193
+ const resolved = fastURI.resolve(base, relative)
194
+ t.equal(resolved, expected, description)
195
+ t.equal(fastURI.parse(resolved).host, expectedHost, `${description} reparses consistently`)
196
+ })
197
+ })
198
+
199
+ test('resolve applies domain canonicalisation only when the effective scheme opts in', (t) => {
200
+ const host = 'ex\u00ADample.com'
201
+
202
+ t.plan(2)
203
+ t.equal(
204
+ fastURI.resolve('uri://trusted.example/', `//${host}/`),
205
+ `uri://${host}/`,
206
+ 'an unsupported scheme preserves the host'
207
+ )
208
+ t.equal(
209
+ fastURI.resolve('http://trusted.example/', `//${host}/`, { unicodeSupport: true }),
210
+ `http://${host}/`,
211
+ 'unicodeSupport preserves the Unicode host'
212
+ )
213
+ })
214
+
215
+ test('resolve throws when the final scheme cannot canonicalise the host', (t) => {
216
+ const invalidHost = '\u200D.example'
217
+ const cases = [
218
+ ['http://trusted.example/', `//${invalidHost}/`],
219
+ [`https://${invalidHost}/base`, 'child'],
220
+ ['http://trusted.example/', `http://${invalidHost}/`],
221
+ ['http://trusted.example/', `//${invalidHost}%2Etest/`]
222
+ ]
223
+
224
+ t.plan(cases.length)
225
+
226
+ cases.forEach(([base, relative]) => {
227
+ t.throws(
228
+ () => fastURI.resolve(base, relative),
229
+ /Host's domain name can not be converted to ASCII/,
230
+ `${base} + ${relative}`
231
+ )
232
+ })
233
+ })
234
+
163
235
  test('parse rejects a literal backslash in the authority as malformed (RFC 3986)', (t) => {
164
236
  // Regression for the host-confusion bypass: a literal "\" is invalid RFC 3986
165
237
  // syntax and must be flagged malformed, not silently rewritten. Otherwise "\"
@@ -221,3 +293,139 @@ test('parse does not reject a literal backslash in the query or fragment', (t) =
221
293
  t.notOk(parsed.error, 'backslash in query/fragment does not mark the URI malformed')
222
294
  t.equal(parsed.host, 'host.example.com', 'host parsed normally')
223
295
  })
296
+
297
+ test('parse rejects a malformed authority introducer (\\\\, /\\, \\/) in place of //', (t) => {
298
+ // Regression: "\\", "/\\", "\\/" after the scheme colon are not valid authority
299
+ // introducers. Node's URL treats "\\" as interchangeable with "/" on special
300
+ // schemes, so "http:\\\\evil.com/path" would be parsed as host "evil.com" by
301
+ // Node, but fast-uri must reject it as malformed to prevent SSRF/redirect bypass.
302
+ const cases = [
303
+ 'http:\\\\evil.com/path',
304
+ 'http:/\\evil.com/path',
305
+ 'http:\\/evil.com/path',
306
+ 'ws:\\\\evil.com/chat',
307
+ 'wss:\\\\evil.com/chat',
308
+ 'ftp:\\\\evil.com/',
309
+ '\\\\evil.com/path'
310
+ ]
311
+
312
+ t.plan(cases.length)
313
+
314
+ cases.forEach((input) => {
315
+ t.equal(
316
+ fastURI.parse(input).error,
317
+ 'URI authority must not contain a literal backslash.',
318
+ input
319
+ )
320
+ })
321
+ })
322
+
323
+ test('normalize does not canonicalize a malformed-authority-introducer URI', (t) => {
324
+ const cases = [
325
+ 'http:\\\\evil.com/path',
326
+ 'http:/\\evil.com/path'
327
+ ]
328
+
329
+ t.plan(cases.length)
330
+
331
+ cases.forEach((input) => {
332
+ t.equal(fastURI.normalize(input), input, input)
333
+ })
334
+ })
335
+
336
+ test('equal returns false for malformed-authority-introducer URIs', (t) => {
337
+ const pairs = [
338
+ ['http:\\\\evil.com/path', 'http://evil.com/path'],
339
+ ['http:/\\evil.com/path', 'http://evil.com/path']
340
+ ]
341
+
342
+ t.plan(pairs.length)
343
+
344
+ pairs.forEach(([left, right]) => {
345
+ t.equal(fastURI.equal(left, right), false, `${left} != ${right}`)
346
+ })
347
+ })
348
+
349
+ test('resolve throws on malformed authority introducer', (t) => {
350
+ // resolve() returns a plain string with no error field, so the only safe
351
+ // behavior is to throw when either component has a malformed authority.
352
+ const pairs = [
353
+ ['https://allowed.com/', '\\\\evil.com/path'],
354
+ ['\\\\evil.com/path', 'https://allowed.com/'],
355
+ ['https://allowed.com/', 'http:/\\evil.com/path'],
356
+ ['https://allowed.com/', 'http:\\/evil.com/path']
357
+ ]
358
+
359
+ t.plan(pairs.length)
360
+
361
+ pairs.forEach(([base, rel]) => {
362
+ t.throws(
363
+ () => fastURI.resolve(base, rel),
364
+ /URI authority must not contain a literal backslash/,
365
+ `${base} + ${rel}`
366
+ )
367
+ })
368
+ })
369
+
370
+ test('parse rejects a whitespace-split authority introducer (TAB, LF, CR)', (t) => {
371
+ // The WHATWG URL parser removes TAB (U+0009), LF (U+000A) and CR (U+000D) from
372
+ // the input before parsing, so a stripped character wedged into the introducer
373
+ // ("/<TAB>\\", "/<TAB>/", or a leading "<TAB>//") reaches an authority in Node
374
+ // while fast-uri would otherwise fold it into the path. These must be rejected
375
+ // like the adjacent "\\", "/\\", "\\/" forms.
376
+ const cases = [
377
+ { input: '/\t\\evil.com/path', expectedError: 'URI authority must not contain a literal backslash.' },
378
+ { input: '/\t/evil.com/path', expectedError: 'URI authority introducer must not contain whitespace.' },
379
+ { input: '/\n\\evil.com/path', expectedError: 'URI authority must not contain a literal backslash.' },
380
+ { input: '/\r\\evil.com/path', expectedError: 'URI authority must not contain a literal backslash.' },
381
+ { input: '\t//evil.com/path', expectedError: 'URI authority introducer must not contain whitespace.' },
382
+ { input: '\t/\\evil.com/path', expectedError: 'URI authority must not contain a literal backslash.' },
383
+ { input: 'https:/\t/evil.com/path', expectedError: 'URI authority introducer must not contain whitespace.' }
384
+ ]
385
+
386
+ t.plan(cases.length)
387
+
388
+ cases.forEach(({ input, expectedError }) => {
389
+ t.equal(fastURI.parse(input).error, expectedError, JSON.stringify(input))
390
+ })
391
+ })
392
+
393
+ test('resolve throws on a whitespace-split authority introducer', (t) => {
394
+ const pairs = [
395
+ ['https://allowed.com/', '/\t\\evil.com/path'],
396
+ ['https://allowed.com/', '/\t/evil.com/path'],
397
+ ['https://allowed.com/', '/\n\\evil.com/path'],
398
+ ['/\t/evil.com/path', 'https://allowed.com/']
399
+ ]
400
+
401
+ t.plan(pairs.length)
402
+
403
+ pairs.forEach(([base, rel]) => {
404
+ t.throws(
405
+ () => fastURI.resolve(base, rel),
406
+ /URI authority (must not contain a literal backslash|introducer must not contain whitespace)/,
407
+ `${JSON.stringify(base)} + ${JSON.stringify(rel)}`
408
+ )
409
+ })
410
+ })
411
+
412
+ test('parse does not reject valid authority introducer patterns', (t) => {
413
+ // No false positives: "//" introducer and scheme-less "//" must be valid.
414
+ const cases = [
415
+ 'http://good.com/',
416
+ 'https://good.com/',
417
+ 'ws://good.com/chat',
418
+ 'wss://good.com/chat',
419
+ 'ftp://good.com/',
420
+ '//good.com/path',
421
+ '/absolute/path',
422
+ 'relative/path'
423
+ ]
424
+
425
+ t.plan(cases.length)
426
+
427
+ cases.forEach((input) => {
428
+ const parsed = fastURI.parse(input)
429
+ t.notOk(parsed.error, input)
430
+ })
431
+ })
@@ -149,3 +149,147 @@ test('URN NID Override', (t) => {
149
149
  t.equal(fastURI.serialize(components, { nid: 'uuid' }), 'urn:foo:f81d4fae-7dec-11d0-a765-00a0c91e6bf6')
150
150
  t.end()
151
151
  })
152
+
153
+ test('Mailto serialization preserves delimiters, escapes, and input state', (t) => {
154
+ const valueComponent = {
155
+ scheme: 'mailto',
156
+ to: ['user@example.org'],
157
+ headers: { x: 'a&b' }
158
+ }
159
+ const valueURI = fastURI.serialize(valueComponent)
160
+ t.equal(valueURI, 'mailto:user@example.org?x=a%26b', 'ampersand in header value is encoded')
161
+ // spread: `headers` has a null prototype, which t.deepEqual compares strictly
162
+ t.deepEqual({ ...fastURI.parse(valueURI).headers }, { x: 'a&b' }, 'header value round-trips')
163
+
164
+ const nameComponent = {
165
+ scheme: 'mailto',
166
+ to: ['user@example.org'],
167
+ headers: { 'x&y': 'z' }
168
+ }
169
+ const nameURI = fastURI.serialize(nameComponent)
170
+ t.equal(nameURI, 'mailto:user@example.org?x%26y=z', 'ampersand in header name is encoded')
171
+ t.deepEqual({ ...fastURI.parse(nameURI).headers }, { 'x&y': 'z' }, 'header name round-trips')
172
+
173
+ t.equal(
174
+ fastURI.serialize({ scheme: 'mailto', to: ['user@example.org'], subject: 'a%2fb' }),
175
+ 'mailto:user@example.org?subject=a%2Fb',
176
+ 'valid percent escape is preserved and uppercased'
177
+ )
178
+ t.equal(
179
+ fastURI.serialize({ scheme: 'mailto', to: ['user@example.org'], subject: 'a%GGb' }),
180
+ 'mailto:user@example.org?subject=a%25GGb',
181
+ 'malformed percent escape is encoded'
182
+ )
183
+
184
+ const immutableComponent = {
185
+ scheme: 'mailto',
186
+ to: ['café@EXAMPLE.ORG'],
187
+ subject: 'hello',
188
+ headers: { cc: 'other@example.org' }
189
+ }
190
+ const expected = JSON.parse(JSON.stringify(immutableComponent))
191
+ fastURI.serialize(immutableComponent)
192
+ t.deepEqual(immutableComponent, expected, 'recipient and header containers are not mutated')
193
+ t.end()
194
+ })
195
+
196
+ test('Mailto serialization does not let recipient data inject URI structure', (t) => {
197
+ // A recipient domain must never reach the output with a delimiter intact:
198
+ // "?" would inject header fields and "#" a fragment.
199
+ const cases = [
200
+ ['user@example.org?subject=Injected', 'mailto:user@example.org%3Fsubject%3Dinjected'],
201
+ ['user@example.org#frag', 'mailto:user@example.org%23frag'],
202
+ // domain literals accept the full RFC 5321 dcontent range, which includes "?"
203
+ ['user@[x?subject=Injected]', 'mailto:user@[x%3Fsubject%3Dinjected]'],
204
+ // "," is the recipient delimiter, so it must be encoded inside an address
205
+ ['a,b@example.org', 'mailto:a%2Cb@example.org']
206
+ ]
207
+
208
+ for (const [recipient, expected] of cases) {
209
+ const uri = fastURI.serialize({ scheme: 'mailto', to: [recipient] })
210
+ t.equal(uri, expected, 'encodes delimiters in ' + recipient)
211
+ const reparsed = fastURI.parse(uri)
212
+ t.deepEqual(reparsed.to, [recipient.toLowerCase()], 'round-trips ' + recipient)
213
+ t.equal(reparsed.subject, undefined, 'no subject injected by ' + recipient)
214
+ t.equal(reparsed.headers, undefined, 'no headers injected by ' + recipient)
215
+ t.equal(reparsed.fragment, undefined, 'no fragment injected by ' + recipient)
216
+ }
217
+
218
+ t.equal(
219
+ fastURI.serialize({ scheme: 'mailto', to: ['a@x.test', 'b@y.test'] }),
220
+ 'mailto:a@x.test,b@y.test',
221
+ 'the joining comma between recipients stays literal'
222
+ )
223
+ t.equal(
224
+ fastURI.serialize({ scheme: 'mailto', to: ['user@[IPv6:2001:db8::1]'] }),
225
+ 'mailto:user@[ipv6:2001:db8::1]',
226
+ 'a valid domain literal keeps its delimiters'
227
+ )
228
+ t.equal(
229
+ fastURI.serialize({ scheme: 'mailto', to: ['user@納豆.example.org'] }),
230
+ 'mailto:user@xn--99zt52a.example.org',
231
+ 'Unicode domain is still converted to ASCII'
232
+ )
233
+ t.equal(
234
+ fastURI.serialize({ scheme: 'mailto', to: ['user@納豆.example.org'] }, { unicodeSupport: true }),
235
+ 'mailto:user@納豆.example.org',
236
+ 'Unicode domain is still preserved when requested'
237
+ )
238
+ t.end()
239
+ })
240
+
241
+ test('Mailto encoding is consistent across the fast/slow path boundary', (t) => {
242
+ // The encoder returns the input untouched when every character is allowed,
243
+ // and only then falls back to the per-character loop. Both branches must
244
+ // agree on what needs escaping.
245
+ const base = { scheme: 'mailto', to: ['user@example.org'] }
246
+
247
+ t.equal(
248
+ fastURI.serialize({ ...base, headers: { 'In-Reply-To': 'plain-value' } }),
249
+ 'mailto:user@example.org?In-Reply-To=plain-value',
250
+ 'fully allowed name and value are emitted verbatim'
251
+ )
252
+ t.equal(
253
+ fastURI.serialize({ ...base, headers: { 'x y': '<a>&b c' } }),
254
+ 'mailto:user@example.org?x%20y=%3Ca%3E%26b%20c',
255
+ 'disallowed characters are escaped in both name and value'
256
+ )
257
+ t.equal(
258
+ fastURI.serialize({ ...base, body: 'send current-issue\r\nsend index' }),
259
+ 'mailto:user@example.org?body=send%20current-issue%0D%0Asend%20index',
260
+ 'space and CRLF are escaped in a body'
261
+ )
262
+ t.equal(
263
+ fastURI.serialize({ ...base, subject: 'a%2fb%GGc' }),
264
+ 'mailto:user@example.org?subject=a%2Fb%25GGc',
265
+ 'valid escapes are uppercased and invalid ones escaped in one pass'
266
+ )
267
+ t.equal(
268
+ fastURI.serialize({ scheme: 'mailto', to: ['a.b-c_d~e!f@example.org'] }),
269
+ 'mailto:a.b-c_d~e!f@example.org',
270
+ 'an entirely allowed local part takes the fast path unchanged'
271
+ )
272
+ t.end()
273
+ })
274
+
275
+ test('Mailto serialization ignores a caller-supplied path', (t) => {
276
+ // `to` is the only recipient source. A raw path has not been through this
277
+ // handler's encoder, and the handler sets `skipEscape`, so emitting it would
278
+ // put unescaped data (including CR/LF) straight into the URI.
279
+ t.equal(
280
+ fastURI.serialize({ scheme: 'mailto', path: 'a@b.test\r\nBcc: evil@evil.test' }),
281
+ 'mailto:',
282
+ 'a path containing CRLF is dropped, not emitted raw'
283
+ )
284
+ t.equal(
285
+ fastURI.serialize({ scheme: 'mailto', path: 'user@example.org?subject=Injected' }),
286
+ 'mailto:',
287
+ 'a path containing header fields is dropped'
288
+ )
289
+ t.equal(
290
+ fastURI.serialize({ scheme: 'mailto', to: ['a@b.test'], path: 'ignored?x=1' }),
291
+ 'mailto:a@b.test',
292
+ 'a path is ignored when recipients are present'
293
+ )
294
+ t.end()
295
+ })
@@ -13,9 +13,6 @@ test('uri-js compatibility Parse', (t) => {
13
13
  if (value === '//10.10.000.10') {
14
14
  return t.skip('Skipping //10.10.000.10 as it is not a valid URI per URI spec: https://datatracker.ietf.org/doc/html/rfc5954#section-4.1')
15
15
  }
16
- if (value.slice(0, 6) === 'mailto') {
17
- return t.skip('Skipping mailto schema test as it is not supported by fastifyURI')
18
- }
19
16
  t.same(JSON.parse(JSON.stringify(fastURI.parse(value))), expected, 'Compatibility parse: ' + value)
20
17
  })
21
18
  t.end()
@@ -692,7 +692,7 @@ if (fastURI.SCHEMES.mailto) {
692
692
 
693
693
  components = fastURI.parse('mailto:list@example.org?In-Reply-To=%3C3469A91.D10AF4C@example.com%3E')
694
694
  t.deepEqual(components.to, ['list@example.org'], 'to')
695
- t.deepEqual(components.headers, { 'In-Reply-To': '<3469A91.D10AF4C@example.com>' }, 'headers')
695
+ t.deepEqual({ ...components.headers }, { 'In-Reply-To': '<3469A91.D10AF4C@example.com>' }, 'headers')
696
696
 
697
697
  components = fastURI.parse('mailto:majordomo@example.com?body=subscribe%20bamboo-l')
698
698
  t.deepEqual(components.to, ['majordomo@example.com'], 'to')
@@ -701,7 +701,7 @@ if (fastURI.SCHEMES.mailto) {
701
701
  components = fastURI.parse('mailto:joe@example.com?cc=bob@example.com&body=hello')
702
702
  t.deepEqual(components.to, ['joe@example.com'], 'to')
703
703
  t.equal(components.body, 'hello', 'body')
704
- t.deepEqual(components.headers, { cc: 'bob@example.com' }, 'headers')
704
+ t.deepEqual({ ...components.headers }, { cc: 'bob@example.com' }, 'headers')
705
705
 
706
706
  components = fastURI.parse('mailto:joe@example.com?cc=bob@example.com?body=hello')
707
707
  if (fastURI.VALIDATE_SUPPORT) t.ok(components.error, 'invalid header fields')
@@ -711,7 +711,7 @@ if (fastURI.SCHEMES.mailto) {
711
711
 
712
712
  components = fastURI.parse('mailto:unlikely%3Faddress@example.com?blat=foop')
713
713
  t.deepEqual(components.to, ['unlikely?address@example.com'], 'to unlikely?address@example.com')
714
- t.deepEqual(components.headers, { blat: 'foop' }, 'headers')
714
+ t.deepEqual({ ...components.headers }, { blat: 'foop' }, 'headers')
715
715
 
716
716
  components = fastURI.parse('mailto:Mike%26family@example.org')
717
717
  t.deepEqual(components.to, ['Mike&family@example.org'], 'to Mike&family@example.org')
@@ -0,0 +1,29 @@
1
+ 'use strict'
2
+
3
+ const test = require('tape')
4
+ const fastURI = require('..')
5
+
6
+ test('URN parsing validates the complete scheme-specific input', (t) => {
7
+ const embedded = fastURI.parse('urn:x|foo:bar')
8
+ t.equal(embedded.error, 'URN can not be parsed.', 'does not search for a later nid:nss substring')
9
+ t.equal(embedded.nid, undefined, 'does not adopt the later nid')
10
+ t.equal(embedded.nss, undefined, 'does not adopt the later nss')
11
+
12
+ const trailing = fastURI.parse('urn:foo:bar|ignored')
13
+ t.equal(trailing.error, 'URN can not be parsed.', 'rejects trailing input outside the RFC 2141 NSS grammar')
14
+ t.equal(trailing.nid, undefined, 'does not return an nid from a partial match')
15
+ t.equal(trailing.nss, undefined, 'does not return a truncated nss')
16
+
17
+ t.end()
18
+ })
19
+
20
+ test('URN parsing preserves the complete RFC 2141 NSS', (t) => {
21
+ const parsed = fastURI.parse('urn:foo:allowed/evil')
22
+ t.equal(parsed.error, undefined, 'accepts slash in an RFC 2141 NSS')
23
+ t.equal(parsed.nid, 'foo', 'parses the nid')
24
+ t.equal(parsed.nss, 'allowed/evil', 'preserves the complete NSS')
25
+ t.equal(fastURI.normalize('urn:foo:allowed/evil'), 'urn:foo:allowed/evil', 'normalization retains slash data')
26
+ t.equal(fastURI.equal('urn:foo:allowed/evil', 'urn:foo:allowed'), false, 'distinct NSS values do not compare equal')
27
+
28
+ t.end()
29
+ })