fast-uri 2.4.4 → 2.4.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 +202 -63
- package/lib/schemes.js +9 -4
- package/lib/utils.js +424 -116
- package/package.json +2 -1
- package/test/compatibility.test.js +12 -0
- package/test/component-safe-serialization.test.js +105 -0
- package/test/equal.test.js +32 -0
- package/test/malformed-percent.test.js +77 -0
- package/test/malformed-urn.test.js +61 -0
- package/test/parse.test.js +3 -3
- package/test/query-fragment-normalization.test.js +33 -0
- package/test/reserved-path-normalization.test.js +109 -0
- package/test/scheme-validation.test.js +124 -0
- package/test/security-normalization.test.js +102 -2
- package/test/security.test.js +78 -7
- package/test/urn-full-input.test.js +29 -0
- package/test/websocket-query-preservation.test.js +24 -0
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const test = tap.test
|
|
3
|
+
const test = require('tape')
|
|
5
4
|
const fastURI = require('..')
|
|
6
5
|
|
|
7
6
|
test('parse preserves reserved path escapes as data', (t) => {
|
|
@@ -38,3 +37,104 @@ test('equal does not treat reserved path escapes as live path syntax', (t) => {
|
|
|
38
37
|
|
|
39
38
|
t.end()
|
|
40
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
|
+
})
|
package/test/security.test.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const test = tap.test
|
|
3
|
+
const test = require('tape')
|
|
5
4
|
const fastURI = require('..')
|
|
6
5
|
|
|
7
6
|
test('parse marks malformed authority and port inputs as errors', (t) => {
|
|
@@ -114,7 +113,7 @@ test('resolve preserves encoded authority delimiters', (t) => {
|
|
|
114
113
|
const parsed = fastURI.parse(result)
|
|
115
114
|
|
|
116
115
|
t.plan(1)
|
|
117
|
-
t.
|
|
116
|
+
t.notEqual(parsed.host, 'evil.com', '//trusted.com%40evil.com/path')
|
|
118
117
|
})
|
|
119
118
|
|
|
120
119
|
test('serialize escapes authority delimiters in host field', (t) => {
|
|
@@ -122,15 +121,17 @@ test('serialize escapes authority delimiters in host field', (t) => {
|
|
|
122
121
|
const parsed = fastURI.parse(result)
|
|
123
122
|
|
|
124
123
|
t.plan(1)
|
|
125
|
-
t.
|
|
124
|
+
t.notEqual(parsed.host, 'evil.com', 'host: trusted.com@evil.com')
|
|
126
125
|
})
|
|
127
126
|
|
|
128
127
|
test('normalize does not double-decode %2540 into a live @', (t) => {
|
|
129
|
-
const
|
|
128
|
+
const input = 'http://trusted.com%2540evil.com/'
|
|
129
|
+
const result = fastURI.normalize(input)
|
|
130
130
|
const parsed = fastURI.parse(result)
|
|
131
131
|
|
|
132
|
-
t.plan(
|
|
133
|
-
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)
|
|
134
135
|
})
|
|
135
136
|
|
|
136
137
|
test('parse canonicalises IDN / Unicode hosts to their ASCII form', (t) => {
|
|
@@ -161,6 +162,76 @@ test('parse canonicalises IDN / Unicode hosts to their ASCII form', (t) => {
|
|
|
161
162
|
})
|
|
162
163
|
})
|
|
163
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
|
+
|
|
164
235
|
test('parse rejects a literal backslash in the authority as malformed (RFC 3986)', (t) => {
|
|
165
236
|
// Regression for the host-confusion bypass: a literal "\" is invalid RFC 3986
|
|
166
237
|
// syntax and must be flagged malformed, not silently rewritten. Otherwise "\"
|
|
@@ -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
|
+
})
|