fast-uri 4.1.2 → 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.
- package/.github/workflows/ci.yml +1 -1
- package/.github/workflows/lock-threads.yml +1 -1
- package/SECURITY.md +6 -1
- package/benchmark/benchmark.mjs +71 -0
- package/index.js +202 -44
- package/lib/schemes.js +384 -7
- package/lib/utils.js +302 -88
- package/package.json +1 -1
- package/test/component-safe-serialization.test.js +105 -0
- package/test/equal.test.js +43 -3
- package/test/fixtures/uri-js-parse.json +24 -1
- package/test/fixtures/uri-js-serialize.json +23 -1
- package/test/ipv6-canonical.test.js +34 -0
- package/test/ipv6-validation.test.js +90 -0
- package/test/malformed-percent.test.js +77 -0
- package/test/malformed-urn.test.js +61 -0
- package/test/parse.test.js +145 -0
- package/test/reserved-path-normalization.test.js +109 -0
- package/test/scheme-validation.test.js +122 -0
- package/test/security-normalization.test.js +101 -0
- package/test/security-utf8-encoding.test.js +0 -0
- package/test/security.test.js +75 -3
- package/test/serialize.test.js +144 -0
- package/test/uri-js-compatibility.test.js +0 -3
- package/test/uri-js.test.js +3 -3
- package/test/urn-full-input.test.js +29 -0
- package/test/websocket-query-preservation.test.js +24 -0
- package/types/index.d.ts +4 -0
- package/types/index.tst.ts +16 -0
|
@@ -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
|
package/test/security.test.js
CHANGED
|
@@ -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
|
|
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(
|
|
132
|
-
t.
|
|
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 "\"
|
package/test/serialize.test.js
CHANGED
|
@@ -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()
|
package/test/uri-js.test.js
CHANGED
|
@@ -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
|
+
})
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const test = require('tape')
|
|
4
|
+
const fastURI = require('..')
|
|
5
|
+
|
|
6
|
+
test('WebSocket queries preserve additional question marks', (t) => {
|
|
7
|
+
for (const scheme of ['ws', 'wss']) {
|
|
8
|
+
const uri = `${scheme}://example.com/chat?a?b`
|
|
9
|
+
const truncatedURI = `${scheme}://example.com/chat?a`
|
|
10
|
+
const parsed = fastURI.parse(uri)
|
|
11
|
+
|
|
12
|
+
t.equal(parsed.resourceName, '/chat?a?b', `${scheme} parse preserves the resource name`)
|
|
13
|
+
t.equal(fastURI.serialize(parsed), uri, `${scheme} parsed components round-trip`)
|
|
14
|
+
t.equal(
|
|
15
|
+
fastURI.serialize({ scheme, host: 'example.com', resourceName: '/chat?a?b' }),
|
|
16
|
+
uri,
|
|
17
|
+
`${scheme} resource name serializes without truncation`
|
|
18
|
+
)
|
|
19
|
+
t.equal(fastURI.normalize(uri), uri, `${scheme} normalization preserves the full query`)
|
|
20
|
+
t.equal(fastURI.equal(uri, truncatedURI), false, `${scheme} equality distinguishes the full query`)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
t.end()
|
|
24
|
+
})
|
package/types/index.d.ts
CHANGED
package/types/index.tst.ts
CHANGED
|
@@ -13,3 +13,19 @@ const parsed2 = uri.parse('foo', {
|
|
|
13
13
|
})
|
|
14
14
|
|
|
15
15
|
expect(parsed2).type.toBe<URIComponent>()
|
|
16
|
+
|
|
17
|
+
const mailtoComponent: URIComponent = {
|
|
18
|
+
scheme: 'mailto',
|
|
19
|
+
to: ['user@example.org'],
|
|
20
|
+
subject: 'Hello',
|
|
21
|
+
body: 'Message',
|
|
22
|
+
headers: { cc: 'other@example.org' }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
expect(uri.serialize(mailtoComponent, { unicodeSupport: true })).type.toBe<string>()
|
|
26
|
+
|
|
27
|
+
const parsedMailto = uri.parse('mailto:user@example.org')
|
|
28
|
+
expect(parsedMailto.to).type.toBe<string[] | undefined>()
|
|
29
|
+
expect(parsedMailto.subject).type.toBe<string | undefined>()
|
|
30
|
+
expect(parsedMailto.body).type.toBe<string | undefined>()
|
|
31
|
+
expect(parsedMailto.headers).type.toBe<{ [hfname: string]: string } | undefined>()
|