Skip to content

Commit a95158a

Browse files
authored
Handle malformed fragment decoding without throwing (#171)
1 parent cea547c commit a95158a

4 files changed

Lines changed: 28 additions & 1 deletion

File tree

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,11 @@ function parse (uri, opts) {
309309
parsed.path = normalizePathEncoding(parsed.path)
310310
}
311311
if (parsed.fragment) {
312-
parsed.fragment = encodeURI(decodeURIComponent(parsed.fragment))
312+
try {
313+
parsed.fragment = encodeURI(decodeURIComponent(parsed.fragment))
314+
} catch {
315+
parsed.error = parsed.error || 'URI malformed'
316+
}
313317
}
314318
}
315319

test/equal.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,12 @@ test('WSS Equal', (t) => {
106106
runTest(t, suite)
107107
t.end()
108108
})
109+
110+
test('URI Equals tolerates malformed fragments', (t) => {
111+
t.equal(
112+
fastURI.equal('http://example.com/#%E0%A4A', 'http://example.com/#%E0%A4A'),
113+
true,
114+
'malformed fragment does not throw during equality checks'
115+
)
116+
t.end()
117+
})

test/parse.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ test('URI parse', (t) => {
150150
t.equal(components.query, undefined, 'query')
151151
t.equal(components.fragment, '%0D', 'fragment')
152152

153+
// malformed percent-encoded fragment must not throw
154+
components = fastURI.parse('http://example.com/#%E0%A4A')
155+
t.equal(components.error, 'URI malformed', 'malformed fragment errors')
156+
t.equal(components.fragment, '%E0%A4A', 'malformed fragment is preserved')
157+
153158
// all
154159
components = fastURI.parse('uri://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body')
155160
t.equal(components.error, undefined, 'all errors')

test/resolve.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,12 @@ test('URN Resolving', (t) => {
7676
t.equal(fastURI.resolve('urn:some:other:prop', 'urn:some:ip:prop'), 'urn:some:ip:prop', 'urn:some:ip:prop')
7777
t.end()
7878
})
79+
80+
test('URI Resolving tolerates malformed fragments', (t) => {
81+
t.equal(
82+
fastURI.resolve('http://base.com/', 'http://example.com/#%E0%A4A'),
83+
'http://example.com/#%E0%A4A',
84+
'malformed fragment does not throw during resolve'
85+
)
86+
t.end()
87+
})

0 commit comments

Comments
 (0)