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
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const test = require('tape')
|
|
4
|
+
const fastURI = require('..')
|
|
5
|
+
|
|
6
|
+
test('userinfo serialization cannot terminate the authority', (t) => {
|
|
7
|
+
const uri = fastURI.serialize({
|
|
8
|
+
scheme: 'http',
|
|
9
|
+
userinfo: 'attacker.example/',
|
|
10
|
+
host: 'trusted.example',
|
|
11
|
+
path: '/'
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
t.equal(uri, 'http://attacker.example%2F@trusted.example/', 'slash is encoded as userinfo data')
|
|
15
|
+
|
|
16
|
+
const reparsed = fastURI.parse(uri)
|
|
17
|
+
t.equal(reparsed.host, 'trusted.example', 'reparsing retains the supplied host')
|
|
18
|
+
t.equal(reparsed.userinfo, 'attacker.example%2F', 'encoded slash remains in userinfo')
|
|
19
|
+
t.equal(
|
|
20
|
+
fastURI.serialize({ userinfo: 'user@/?:#[]\\', host: 'example.test' }),
|
|
21
|
+
'//user%40%2F%3F:%23%5B%5D%5C@example.test',
|
|
22
|
+
'all userinfo component delimiters are encoded as data'
|
|
23
|
+
)
|
|
24
|
+
t.end()
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('query and fragment serialization cannot inject component delimiters', (t) => {
|
|
28
|
+
const uri = fastURI.serialize({
|
|
29
|
+
scheme: 'x',
|
|
30
|
+
path: '/resource',
|
|
31
|
+
query: 'key=value#still-query',
|
|
32
|
+
fragment: 'first#still-fragment'
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
t.equal(
|
|
36
|
+
uri,
|
|
37
|
+
'x:/resource?key=value%23still-query#first%23still-fragment',
|
|
38
|
+
'raw hashes are encoded within their supplied components'
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
const reparsed = fastURI.parse(uri)
|
|
42
|
+
t.equal(reparsed.query, 'key=value%23still-query', 'hash remains query data')
|
|
43
|
+
t.equal(reparsed.fragment, 'first%23still-fragment', 'hash remains fragment data')
|
|
44
|
+
t.end()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test('component serializers preserve each RFC 3986 literal character set', (t) => {
|
|
48
|
+
const unreservedAndSubDelims = "AZaz09-._~!$&'()*+,;="
|
|
49
|
+
const userinfo = unreservedAndSubDelims + ':'
|
|
50
|
+
const queryOrFragment = unreservedAndSubDelims + ':@/?'
|
|
51
|
+
|
|
52
|
+
t.equal(
|
|
53
|
+
fastURI.serialize({ userinfo, host: 'example.test' }),
|
|
54
|
+
'//' + userinfo + '@example.test',
|
|
55
|
+
'userinfo literals are preserved'
|
|
56
|
+
)
|
|
57
|
+
t.equal(
|
|
58
|
+
fastURI.serialize({ query: queryOrFragment }),
|
|
59
|
+
'?' + queryOrFragment,
|
|
60
|
+
'query literals are preserved'
|
|
61
|
+
)
|
|
62
|
+
t.equal(
|
|
63
|
+
fastURI.serialize({ fragment: queryOrFragment }),
|
|
64
|
+
'#' + queryOrFragment,
|
|
65
|
+
'fragment literals are preserved'
|
|
66
|
+
)
|
|
67
|
+
t.end()
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
test('component serializers preserve escapes and encode Unicode as UTF-8', (t) => {
|
|
71
|
+
t.equal(
|
|
72
|
+
fastURI.serialize({ userinfo: 'café/%2f%GG', host: 'example.test' }),
|
|
73
|
+
'//caf%C3%A9%2F%2F%25GG@example.test',
|
|
74
|
+
'userinfo preserves valid escapes, uppercases hex, and escapes malformed percent data'
|
|
75
|
+
)
|
|
76
|
+
t.equal(
|
|
77
|
+
fastURI.serialize({ query: '日本/%2f#%GG' }),
|
|
78
|
+
'?%E6%97%A5%E6%9C%AC/%2F%23%25GG',
|
|
79
|
+
'query uses UTF-8 and does not double encode valid escapes'
|
|
80
|
+
)
|
|
81
|
+
t.equal(
|
|
82
|
+
fastURI.serialize({ fragment: '😀/%3f#%GG' }),
|
|
83
|
+
'#%F0%9F%98%80/%3F%23%25GG',
|
|
84
|
+
'fragment uses UTF-8 and does not double encode valid escapes'
|
|
85
|
+
)
|
|
86
|
+
t.equal(
|
|
87
|
+
fastURI.serialize({ query: 'Kſ' }),
|
|
88
|
+
'?%E2%84%AA%C5%BF',
|
|
89
|
+
'Unicode characters that case-fold to ASCII are still UTF-8 encoded'
|
|
90
|
+
)
|
|
91
|
+
t.end()
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test('parsed URI serialization remains stable', (t) => {
|
|
95
|
+
const input = 'x://user:pass@example.test/a%2Fb?x=a/b?c&y=%23#frag/a?b%23'
|
|
96
|
+
const serialized = fastURI.serialize(fastURI.parse(input))
|
|
97
|
+
|
|
98
|
+
t.equal(serialized, input, 'an already normalized parsed URI round-trips unchanged')
|
|
99
|
+
t.equal(
|
|
100
|
+
fastURI.serialize(fastURI.parse(serialized)),
|
|
101
|
+
serialized,
|
|
102
|
+
'repeated parse and serialize cycles are stable'
|
|
103
|
+
)
|
|
104
|
+
t.end()
|
|
105
|
+
})
|
package/test/equal.test.js
CHANGED
|
@@ -22,6 +22,36 @@ test('URI Equals', (t) => {
|
|
|
22
22
|
t.end()
|
|
23
23
|
})
|
|
24
24
|
|
|
25
|
+
test('URI Equals rejects malformed object input even when identical', (t) => {
|
|
26
|
+
const malformed = { scheme: 'http', host: 'example.com', port: 99999, path: '/x' }
|
|
27
|
+
t.equal(fn(malformed, malformed), false)
|
|
28
|
+
t.end()
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
test('URI Equals preserves case-sensitive components', (t) => {
|
|
32
|
+
const suite = [
|
|
33
|
+
{ pair: ['http://example.com/Admin', 'http://example.com/admin'], result: false },
|
|
34
|
+
{ pair: ['http://example.com/?token=SECRET', 'http://example.com/?token=secret'], result: false },
|
|
35
|
+
{ pair: ['http://example.com/#Section', 'http://example.com/#section'], result: false },
|
|
36
|
+
{ pair: ['http://User@example.com/', 'http://user@example.com/'], result: false },
|
|
37
|
+
{ pair: ['urn:foo:CaseSensitive', 'urn:foo:casesensitive'], result: false },
|
|
38
|
+
{ pair: ['ws://example.com/Chat', 'ws://example.com/chat'], result: false },
|
|
39
|
+
{ pair: ['ws://example.com/?token=SECRET', 'ws://example.com/?token=secret'], result: false },
|
|
40
|
+
{ pair: [{ scheme: 'http', host: 'example.com', path: '/Admin' }, { scheme: 'http', host: 'example.com', path: '/admin' }], result: false }
|
|
41
|
+
]
|
|
42
|
+
runTest(t, suite)
|
|
43
|
+
t.end()
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('URI Equals folds normalized scheme and host case', (t) => {
|
|
47
|
+
const suite = [
|
|
48
|
+
{ pair: ['HTTP://EXAMPLE.COM/resource', 'http://example.com/resource'], result: true },
|
|
49
|
+
{ pair: [{ scheme: 'HTTP', host: 'EXAMPLE.COM', path: '/resource' }, { scheme: 'http', host: 'example.com', path: '/resource' }], result: true }
|
|
50
|
+
]
|
|
51
|
+
runTest(t, suite)
|
|
52
|
+
t.end()
|
|
53
|
+
})
|
|
54
|
+
|
|
25
55
|
// test('IRI Equals', (t) => {
|
|
26
56
|
// // example from RFC 3987
|
|
27
57
|
// t.equal(URI.equal('example://a/b/c/%7Bfoo%7D/ros\xE9', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d/ros%C3%A9', IRI_OPTION), true)
|
|
@@ -65,6 +95,8 @@ test('URN Equals', (t) => {
|
|
|
65
95
|
// t.equal(URI.equal('urn:foo:a123,456', 'urn:foo:A123,456'), false)
|
|
66
96
|
// t.equal(URI.equal('urn:foo:a123%2C456', 'URN:FOO:a123%2c456'), true)
|
|
67
97
|
|
|
98
|
+
t.equal(fn('urn:', 'urn:FOO:a123,456'), false, 'malformed URN fails equality safely')
|
|
99
|
+
|
|
68
100
|
runTest(t, suite)
|
|
69
101
|
t.end()
|
|
70
102
|
})
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const test = require('tape')
|
|
4
|
+
const fastURI = require('..')
|
|
5
|
+
|
|
6
|
+
const MALFORMED_PERCENT_ERROR = 'URI contains malformed percent-encoding.'
|
|
7
|
+
|
|
8
|
+
const malformedComponents = [
|
|
9
|
+
['path', 'x://example.test/a%'],
|
|
10
|
+
['path with one trailing hex digit', 'x://example.test/a%2'],
|
|
11
|
+
['path with non-hex digits', 'x://example.test/a%GG'],
|
|
12
|
+
['host', 'x://exa%mple.test/path'],
|
|
13
|
+
['userinfo', 'x://us%er@example.test/path'],
|
|
14
|
+
['query', 'x://example.test/path?q=%G0'],
|
|
15
|
+
['fragment', 'x://example.test/path#frag%1']
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
test('parse reports malformed percent syntax in generic URI components', (t) => {
|
|
19
|
+
for (const [component, uri] of malformedComponents) {
|
|
20
|
+
t.equal(fastURI.parse(uri).error, MALFORMED_PERCENT_ERROR, component)
|
|
21
|
+
}
|
|
22
|
+
t.end()
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
test('normalize preserves malformed percent input unchanged', (t) => {
|
|
26
|
+
for (const [component, uri] of malformedComponents) {
|
|
27
|
+
t.equal(fastURI.normalize(uri), uri, component)
|
|
28
|
+
}
|
|
29
|
+
t.end()
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
test('equal does not equate malformed input with repaired valid input', (t) => {
|
|
33
|
+
const cases = [
|
|
34
|
+
['x://example.test/a%', 'x://example.test/a%25'],
|
|
35
|
+
['x://exa%mple.test/path', 'x://exa%25mple.test/path'],
|
|
36
|
+
['x://us%er@example.test/path', 'x://us%25er@example.test/path'],
|
|
37
|
+
['x://example.test/path?q=%', 'x://example.test/path?q=%25'],
|
|
38
|
+
['x://example.test/path#%', 'x://example.test/path#%25']
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
for (const [malformed, repaired] of cases) {
|
|
42
|
+
t.equal(fastURI.equal(malformed, repaired, {}), false, malformed)
|
|
43
|
+
}
|
|
44
|
+
t.end()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test('resolve rejects malformed percent syntax in either input', (t) => {
|
|
48
|
+
t.throws(
|
|
49
|
+
() => fastURI.resolve('x://example.test/base%', 'child'),
|
|
50
|
+
/URI contains malformed percent-encoding\./,
|
|
51
|
+
'malformed base'
|
|
52
|
+
)
|
|
53
|
+
t.throws(
|
|
54
|
+
() => fastURI.resolve('x://example.test/base', 'child%'),
|
|
55
|
+
/URI contains malformed percent-encoding\./,
|
|
56
|
+
'malformed relative reference'
|
|
57
|
+
)
|
|
58
|
+
t.end()
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('valid percent octets remain valid without UTF-8 validation', (t) => {
|
|
62
|
+
const uri = '/%ff?q=%80#%fe'
|
|
63
|
+
const parsed = fastURI.parse(uri)
|
|
64
|
+
|
|
65
|
+
t.equal(parsed.error, undefined, 'non-UTF-8 octets are valid percent syntax')
|
|
66
|
+
t.equal(parsed.path, '/%FF', 'path octet is preserved and hex is uppercased')
|
|
67
|
+
t.equal(parsed.query, 'q=%80', 'query octet is preserved')
|
|
68
|
+
t.equal(parsed.fragment, '%FE', 'fragment octet is preserved and hex is uppercased')
|
|
69
|
+
t.equal(fastURI.normalize(uri), '/%FF?q=%80#%FE', 'normalization preserves the octets')
|
|
70
|
+
|
|
71
|
+
const allGenericComponents = fastURI.parse('x://u%2f@exa%2fmple.test/%2f?q=%2f#%2f')
|
|
72
|
+
t.equal(allGenericComponents.error, undefined, 'valid escapes are accepted in every generic component')
|
|
73
|
+
|
|
74
|
+
const ipv6Zone = fastURI.parse('//[2001:db8::7%en0]')
|
|
75
|
+
t.equal(ipv6Zone.error, undefined, 'historically accepted raw IPv6 zone separator is unchanged')
|
|
76
|
+
t.end()
|
|
77
|
+
})
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const test = require('tape')
|
|
4
|
+
const fastURI = require('..')
|
|
5
|
+
|
|
6
|
+
const malformedURNs = [
|
|
7
|
+
'urn:',
|
|
8
|
+
'URN:',
|
|
9
|
+
'urn:foo',
|
|
10
|
+
'urn::foo',
|
|
11
|
+
'urn:foo:',
|
|
12
|
+
'urn:%66oo:bar'
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
test('parse reports malformed ordinary URNs', (t) => {
|
|
16
|
+
for (const uri of malformedURNs) {
|
|
17
|
+
t.match(fastURI.parse(uri).error, /^URN can not be parsed\.?$/, uri)
|
|
18
|
+
}
|
|
19
|
+
t.end()
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
test('normalize preserves malformed ordinary URNs without throwing', (t) => {
|
|
23
|
+
for (const uri of malformedURNs) {
|
|
24
|
+
t.doesNotThrow(() => fastURI.normalize(uri), `${uri} does not throw`)
|
|
25
|
+
t.equal(fastURI.normalize(uri), uri, `${uri} is preserved`)
|
|
26
|
+
}
|
|
27
|
+
t.equal(
|
|
28
|
+
fastURI.normalize('urn:foo', { reference: 'relative' }),
|
|
29
|
+
'urn:foo',
|
|
30
|
+
'an earlier parse error does not hide the missing URN nid'
|
|
31
|
+
)
|
|
32
|
+
t.end()
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test('equal returns false for malformed ordinary URNs', (t) => {
|
|
36
|
+
for (const uri of malformedURNs) {
|
|
37
|
+
t.equal(fastURI.equal(uri, uri, {}), false, `${uri} is not equal to itself as malformed input`)
|
|
38
|
+
t.equal(fastURI.equal(uri, 'urn:foo:bar', {}), false, `${uri} is not equal to a valid URN`)
|
|
39
|
+
}
|
|
40
|
+
t.end()
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('resolve handles malformed ordinary URNs without throwing', (t) => {
|
|
44
|
+
for (const uri of malformedURNs) {
|
|
45
|
+
t.doesNotThrow(() => fastURI.resolve('uri://base/', uri), `${uri} does not throw as a relative reference`)
|
|
46
|
+
t.doesNotThrow(() => fastURI.resolve(uri, ''), `${uri} does not throw as a base URI`)
|
|
47
|
+
}
|
|
48
|
+
// resolve preserves the malformed scheme-specific input rather than surfacing
|
|
49
|
+
// an uncaught 'URN without nid cannot be serialized' error (matches upstream uri-js)
|
|
50
|
+
t.equal(fastURI.resolve('uri://base/', 'urn:'), 'urn:', 'malformed relative URN is preserved')
|
|
51
|
+
t.equal(fastURI.resolve('URN:', ''), 'urn:', 'scheme case is normalized')
|
|
52
|
+
t.equal(fastURI.resolve('uri://base/', 'urn:%66oo:bar'), 'urn:foo:bar', 'percent-encoding is decoded')
|
|
53
|
+
t.end()
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('valid URNs retain their existing behavior', (t) => {
|
|
57
|
+
t.equal(fastURI.normalize('URN:FOO:a123,456'), 'urn:foo:a123,456')
|
|
58
|
+
t.equal(fastURI.equal('urn:foo:a123,456', 'URN:FOO:a123,456', {}), true)
|
|
59
|
+
t.equal(fastURI.resolve('uri://base/', 'urn:'), 'urn:', 'default resolve behavior is unchanged')
|
|
60
|
+
t.end()
|
|
61
|
+
})
|
package/test/parse.test.js
CHANGED
|
@@ -174,12 +174,12 @@ test('URI parse', (t) => {
|
|
|
174
174
|
t.equal(components.query, undefined, 'query')
|
|
175
175
|
t.equal(components.fragment, undefined, 'fragment')
|
|
176
176
|
|
|
177
|
-
// IPv4address with unformated 0
|
|
178
|
-
components = URI.parse('//10.10.000.10')
|
|
177
|
+
// IPv4address with unformated 0 stay as-is
|
|
178
|
+
components = URI.parse('//10.10.000.10') // not valid as per https://datatracker.ietf.org/doc/html/rfc5954#section-4.1
|
|
179
179
|
t.equal(components.error, undefined, 'IPv4address errors')
|
|
180
180
|
t.equal(components.scheme, undefined, 'scheme')
|
|
181
181
|
t.equal(components.userinfo, undefined, 'userinfo')
|
|
182
|
-
t.equal(components.host, '10.10.
|
|
182
|
+
t.equal(components.host, '10.10.000.10', 'host')
|
|
183
183
|
t.equal(components.port, undefined, 'port')
|
|
184
184
|
t.equal(components.path, '', 'path')
|
|
185
185
|
t.equal(components.query, undefined, 'query')
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const test = require('tape')
|
|
4
|
+
const fastURI = require('..')
|
|
5
|
+
|
|
6
|
+
test('query percent-encoding is normalized (RFC 3986 §6.2.2)', (t) => {
|
|
7
|
+
// hex digits are uppercased, like the path component already does
|
|
8
|
+
t.equal(fastURI.normalize('x://h/p?a=%2a'), 'x://h/p?a=%2A', 'uppercases hex in query')
|
|
9
|
+
// unreserved characters are decoded
|
|
10
|
+
t.equal(fastURI.normalize('x://h/p?a=%7e%2e'), 'x://h/p?a=~.', 'decodes unreserved in query')
|
|
11
|
+
// reserved characters stay percent-encoded
|
|
12
|
+
t.equal(fastURI.normalize('x://h/p?a=%2F'), 'x://h/p?a=%2F', 'keeps reserved encoded in query')
|
|
13
|
+
// raw characters that are not allowed are percent-encoded
|
|
14
|
+
t.equal(fastURI.normalize('x://h/p?a b'), 'x://h/p?a%20b', 'encodes a raw space in query')
|
|
15
|
+
t.equal(fastURI.normalize('x://h/p?café'), 'x://h/p?caf%C3%A9', 'encodes raw non-ASCII in query')
|
|
16
|
+
// `?` is allowed unencoded in a query
|
|
17
|
+
t.equal(fastURI.normalize('x://h/p?a?b'), 'x://h/p?a?b', 'keeps `?` raw in query')
|
|
18
|
+
|
|
19
|
+
t.end()
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
test('fragment percent-encoding is normalized without decoding reserved characters', (t) => {
|
|
23
|
+
// reserved characters must NOT be decoded — `%2F` is not `/`
|
|
24
|
+
t.equal(fastURI.normalize('x://h/p#a%2Fb%2A'), 'x://h/p#a%2Fb%2A', 'keeps reserved encoded in fragment')
|
|
25
|
+
// hex is uppercased and unreserved is decoded
|
|
26
|
+
t.equal(fastURI.normalize('x://h/p#a%2a%7e'), 'x://h/p#a%2A~', 'uppercases hex and decodes unreserved in fragment')
|
|
27
|
+
// raw characters are still encoded
|
|
28
|
+
t.equal(fastURI.normalize('x://h/p#a b'), 'x://h/p#a%20b', 'encodes a raw space in fragment')
|
|
29
|
+
// `?` and `/` are allowed unencoded in a fragment
|
|
30
|
+
t.equal(fastURI.normalize('x://h/p#f?x/y'), 'x://h/p#f?x/y', 'keeps `?` and `/` raw in fragment')
|
|
31
|
+
|
|
32
|
+
t.end()
|
|
33
|
+
})
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const test = require('tape')
|
|
4
|
+
const fastURI = require('..')
|
|
5
|
+
|
|
6
|
+
const PATH_RESERVED = "!$&'()*+,;=:@/"
|
|
7
|
+
|
|
8
|
+
function percentEncode (character, lowerCase) {
|
|
9
|
+
const hex = character.charCodeAt(0).toString(16).padStart(2, '0')
|
|
10
|
+
return '%' + (lowerCase ? hex : hex.toUpperCase())
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
test('normalize preserves literal and escaped reserved path characters', (t) => {
|
|
14
|
+
t.equal(
|
|
15
|
+
fastURI.normalize('http://example.com/a;b'),
|
|
16
|
+
'http://example.com/a;b',
|
|
17
|
+
'literal semicolon remains literal'
|
|
18
|
+
)
|
|
19
|
+
t.equal(
|
|
20
|
+
fastURI.normalize('http://example.com/a%3ab'),
|
|
21
|
+
'http://example.com/a%3Ab',
|
|
22
|
+
'escaped colon remains escaped and its hex is uppercased'
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
for (const character of PATH_RESERVED) {
|
|
26
|
+
const literal = `http://example.com/a${character}b`
|
|
27
|
+
const escaped = `http://example.com/a${percentEncode(character, true)}b`
|
|
28
|
+
const normalizedEscape = `http://example.com/a${percentEncode(character, false)}b`
|
|
29
|
+
|
|
30
|
+
t.equal(fastURI.normalize(literal), literal, `preserves literal ${character}`)
|
|
31
|
+
t.equal(fastURI.normalize(escaped), normalizedEscape, `preserves escaped ${character}`)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
t.equal(
|
|
35
|
+
fastURI.normalize('http://example.com/a/./café'),
|
|
36
|
+
'http://example.com/a/caf%C3%A9',
|
|
37
|
+
'removes real dot segments and UTF-8 encodes raw non-ASCII'
|
|
38
|
+
)
|
|
39
|
+
t.equal(
|
|
40
|
+
fastURI.normalize('http://example.com/a/%2e%2e/b'),
|
|
41
|
+
'http://example.com/a/%2E%2E/b',
|
|
42
|
+
'preserves escaped dots as path data'
|
|
43
|
+
)
|
|
44
|
+
t.equal(
|
|
45
|
+
fastURI.normalize('http://example.com/Kſ'),
|
|
46
|
+
'http://example.com/%E2%84%AA%C5%BF',
|
|
47
|
+
'UTF-8 encodes Unicode characters that case-fold to ASCII'
|
|
48
|
+
)
|
|
49
|
+
t.end()
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('serialize uses the RFC 3986 path character set without opening escapes', (t) => {
|
|
53
|
+
const rawPath = `/a${PATH_RESERVED}b`
|
|
54
|
+
t.equal(
|
|
55
|
+
fastURI.serialize({ scheme: 'http', host: 'example.com', path: rawPath }),
|
|
56
|
+
`http://example.com${rawPath}`,
|
|
57
|
+
'keeps all legal raw path characters'
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
const escapedPath = Array.from(PATH_RESERVED, (character) => percentEncode(character, true)).join('')
|
|
61
|
+
const normalizedEscapedPath = Array.from(PATH_RESERVED, (character) => percentEncode(character, false)).join('')
|
|
62
|
+
t.equal(
|
|
63
|
+
fastURI.serialize({ scheme: 'http', host: 'example.com', path: '/' + escapedPath }),
|
|
64
|
+
'http://example.com/' + normalizedEscapedPath,
|
|
65
|
+
'uppercases valid escapes without decoding reserved characters'
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
t.equal(
|
|
69
|
+
fastURI.serialize({ scheme: 'http', host: 'example.com', path: '/a?b#c[d]' }),
|
|
70
|
+
'http://example.com/a%3Fb%23c%5Bd%5D',
|
|
71
|
+
'encodes characters that would leave the path component'
|
|
72
|
+
)
|
|
73
|
+
t.equal(
|
|
74
|
+
fastURI.serialize({ path: 'a:b/c:d' }),
|
|
75
|
+
'a%3Ab/c:d',
|
|
76
|
+
'only escapes a colon where path-noscheme requires it'
|
|
77
|
+
)
|
|
78
|
+
t.equal(
|
|
79
|
+
fastURI.serialize({ path: './a:b/c:d' }),
|
|
80
|
+
'a%3Ab/c:d',
|
|
81
|
+
'escapes a first-segment colon exposed by dot-segment removal'
|
|
82
|
+
)
|
|
83
|
+
t.equal(
|
|
84
|
+
fastURI.normalize('./a:b'),
|
|
85
|
+
'a%3Ab',
|
|
86
|
+
'normalization keeps a relative path from becoming a scheme'
|
|
87
|
+
)
|
|
88
|
+
t.equal(
|
|
89
|
+
fastURI.serialize({ path: '/Kſ' }),
|
|
90
|
+
'/%E2%84%AA%C5%BF',
|
|
91
|
+
'UTF-8 encodes Unicode characters that case-fold to ASCII'
|
|
92
|
+
)
|
|
93
|
+
t.end()
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
test('equal distinguishes escaped reserved path data from literal syntax', (t) => {
|
|
97
|
+
for (const character of PATH_RESERVED) {
|
|
98
|
+
const literal = `http://example.com/a${character}b`
|
|
99
|
+
const escaped = `http://example.com/a${percentEncode(character, false)}b`
|
|
100
|
+
t.equal(fastURI.equal(literal, escaped, {}), false, `distinguishes literal and escaped ${character}`)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
t.equal(
|
|
104
|
+
fastURI.equal('./a:b', 'a:b', {}),
|
|
105
|
+
false,
|
|
106
|
+
'does not equate a relative path with an absolute URI after removing dot segments'
|
|
107
|
+
)
|
|
108
|
+
t.end()
|
|
109
|
+
})
|
|
@@ -0,0 +1,124 @@
|
|
|
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
|
+
// 3.x has no mailto scheme handler (added in v4); use the ws handler to
|
|
53
|
+
// verify that a percent-encoded scheme still selects its scheme handler
|
|
54
|
+
const ws = fastURI.parse('we%62socket://example.com/')
|
|
55
|
+
t.equal(ws.scheme, 'websocket', 'ws parsing runs after decoding the scheme')
|
|
56
|
+
t.end()
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('normalize preserves schemes that decode to invalid identifiers', (t) => {
|
|
60
|
+
for (const uri of malformedSchemes) {
|
|
61
|
+
t.equal(fastURI.normalize(uri), uri, uri)
|
|
62
|
+
}
|
|
63
|
+
t.end()
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('scheme normalization cannot introduce authority or control delimiters', (t) => {
|
|
67
|
+
const authority = '%2f%2fevil.example:/pwn'
|
|
68
|
+
const crlf = '%0d%0aSet-Cookie:%20sid=attacker:/p'
|
|
69
|
+
|
|
70
|
+
t.equal(fastURI.parse(authority).host, undefined, 'original input has no authority')
|
|
71
|
+
t.equal(fastURI.normalize(authority), authority, 'normalization does not create an authority')
|
|
72
|
+
t.equal(fastURI.normalize(crlf), crlf, 'normalization does not emit raw CRLF')
|
|
73
|
+
t.equal(fastURI.normalize(crlf).includes('\r\n'), false, 'normalized output contains no raw CRLF')
|
|
74
|
+
t.end()
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('equal returns false for malformed decoded schemes', (t) => {
|
|
78
|
+
for (const uri of malformedSchemes) {
|
|
79
|
+
t.equal(fastURI.equal(uri, uri, {}), false, uri)
|
|
80
|
+
}
|
|
81
|
+
t.end()
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('resolve rejects malformed decoded schemes in either input', (t) => {
|
|
85
|
+
t.throws(
|
|
86
|
+
() => fastURI.resolve('%2f%2fevil.example:/base', 'child'),
|
|
87
|
+
/URI scheme is malformed\./,
|
|
88
|
+
'malformed base'
|
|
89
|
+
)
|
|
90
|
+
t.throws(
|
|
91
|
+
() => fastURI.resolve('https://allowed.example/app/', '%2f%2fevil.example:/pwn'),
|
|
92
|
+
/URI scheme is malformed\./,
|
|
93
|
+
'malformed relative reference'
|
|
94
|
+
)
|
|
95
|
+
t.end()
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
test('serialize validates decoded component schemes', (t) => {
|
|
99
|
+
t.equal(
|
|
100
|
+
fastURI.serialize({ scheme: 'foo%2Bbar', path: 'value' }),
|
|
101
|
+
'foo+bar:value',
|
|
102
|
+
'valid decoded scheme is serialized'
|
|
103
|
+
)
|
|
104
|
+
t.throws(
|
|
105
|
+
() => fastURI.serialize({ scheme: '//evil.example', path: '/pwn' }),
|
|
106
|
+
/URI scheme is malformed\./,
|
|
107
|
+
'raw invalid scheme'
|
|
108
|
+
)
|
|
109
|
+
t.throws(
|
|
110
|
+
() => fastURI.serialize({ scheme: '%2f%2fevil.example', path: '/pwn' }),
|
|
111
|
+
/URI scheme is malformed\./,
|
|
112
|
+
'encoded invalid scheme'
|
|
113
|
+
)
|
|
114
|
+
t.equal(
|
|
115
|
+
fastURI.equal(
|
|
116
|
+
{ scheme: '%2f%2fevil.example', path: '/pwn' },
|
|
117
|
+
{ scheme: '%2f%2fevil.example', path: '/pwn' },
|
|
118
|
+
{}
|
|
119
|
+
),
|
|
120
|
+
false,
|
|
121
|
+
'equality fails closed for malformed component objects'
|
|
122
|
+
)
|
|
123
|
+
t.end()
|
|
124
|
+
})
|