File size: 4,690 Bytes
edaa6b9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | 'use strict'
const test = require('tape')
const fastURI = require('..')
test('userinfo serialization cannot terminate the authority', (t) => {
const uri = fastURI.serialize({
scheme: 'http',
userinfo: 'attacker.example/',
host: 'trusted.example',
path: '/'
})
t.equal(uri, 'http://attacker.example%2F@trusted.example/', 'slash is encoded as userinfo data')
const reparsed = fastURI.parse(uri)
t.equal(reparsed.host, 'trusted.example', 'reparsing retains the supplied host')
t.equal(reparsed.userinfo, 'attacker.example%2F', 'encoded slash remains in userinfo')
t.equal(
fastURI.serialize({ userinfo: 'user@/?:#[]\\', host: 'example.test' }),
'//user%40%2F%3F:%23%5B%5D%5C@example.test',
'all userinfo component delimiters are encoded as data'
)
t.end()
})
test('port serialization rejects non-digit values', (t) => {
const malformedPorts = [
'@127.0.0.1:8124',
'8080@evil.example',
'8080/path',
'8080?query',
'8080#fragment',
'8080:9000',
'-1',
'1.5',
1.5,
NaN,
Infinity,
'\u0661'
]
for (const port of malformedPorts) {
t.throws(
() => fastURI.serialize({ scheme: 'http', host: 'trusted.example', port, path: '/app' }),
/URI port is malformed\./,
String(port)
)
}
t.throws(
() => fastURI.normalize({ scheme: 'http', host: 'trusted.example', port: '@evil.example' }),
/URI port is malformed\./,
'object normalization rejects a malformed port'
)
t.equal(
fastURI.equal(
{ scheme: 'http', host: 'trusted.example', port: '@evil.example' },
{ scheme: 'http', host: 'trusted.example', port: '@evil.example' }
),
false,
'object equality fails closed for a malformed port'
)
t.end()
})
test('port serialization preserves RFC 3986 digit values', (t) => {
const validPorts = [
[8080, '8080'],
['8080', '8080'],
['00080', '00080'],
['', '']
]
for (const [port, expected] of validPorts) {
t.equal(
fastURI.serialize({ scheme: 'uri', host: 'example.test', port }),
`uri://example.test:${expected}`,
JSON.stringify(port)
)
}
t.end()
})
test('query and fragment serialization cannot inject component delimiters', (t) => {
const uri = fastURI.serialize({
scheme: 'x',
path: '/resource',
query: 'key=value#still-query',
fragment: 'first#still-fragment'
})
t.equal(
uri,
'x:/resource?key=value%23still-query#first%23still-fragment',
'raw hashes are encoded within their supplied components'
)
const reparsed = fastURI.parse(uri)
t.equal(reparsed.query, 'key=value%23still-query', 'hash remains query data')
t.equal(reparsed.fragment, 'first%23still-fragment', 'hash remains fragment data')
t.end()
})
test('component serializers preserve each RFC 3986 literal character set', (t) => {
const unreservedAndSubDelims = "AZaz09-._~!$&'()*+,;="
const userinfo = unreservedAndSubDelims + ':'
const queryOrFragment = unreservedAndSubDelims + ':@/?'
t.equal(
fastURI.serialize({ userinfo, host: 'example.test' }),
'//' + userinfo + '@example.test',
'userinfo literals are preserved'
)
t.equal(
fastURI.serialize({ query: queryOrFragment }),
'?' + queryOrFragment,
'query literals are preserved'
)
t.equal(
fastURI.serialize({ fragment: queryOrFragment }),
'#' + queryOrFragment,
'fragment literals are preserved'
)
t.end()
})
test('component serializers preserve escapes and encode Unicode as UTF-8', (t) => {
t.equal(
fastURI.serialize({ userinfo: 'café/%2f%GG', host: 'example.test' }),
'//caf%C3%A9%2F%2F%25GG@example.test',
'userinfo preserves valid escapes, uppercases hex, and escapes malformed percent data'
)
t.equal(
fastURI.serialize({ query: '日本/%2f#%GG' }),
'?%E6%97%A5%E6%9C%AC/%2F%23%25GG',
'query uses UTF-8 and does not double encode valid escapes'
)
t.equal(
fastURI.serialize({ fragment: '😀/%3f#%GG' }),
'#%F0%9F%98%80/%3F%23%25GG',
'fragment uses UTF-8 and does not double encode valid escapes'
)
t.equal(
fastURI.serialize({ query: 'Kſ' }),
'?%E2%84%AA%C5%BF',
'Unicode characters that case-fold to ASCII are still UTF-8 encoded'
)
t.end()
})
test('parsed URI serialization remains stable', (t) => {
const input = 'x://user:pass@example.test/a%2Fb?x=a/b?c&y=%23#frag/a?b%23'
const serialized = fastURI.serialize(fastURI.parse(input))
t.equal(serialized, input, 'an already normalized parsed URI round-trips unchanged')
t.equal(
fastURI.serialize(fastURI.parse(serialized)),
serialized,
'repeated parse and serialize cycles are stable'
)
t.end()
})
|