Skip to content

Commit a796553

Browse files
authored
fix: encode URIs before setting them as Location header (#58)
1 parent 4001371 commit a796553

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

lib/middleware.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = function (options) {
3131
const toUrl = decodedBaseUrl.replace(foundRule.from, toTarget)
3232

3333
try {
34-
res.setHeader('Location', toUrl)
34+
res.setHeader('Location', encodeURI(toUrl))
3535
} catch (error) {
3636
// Not passing the error as it's caused by URL that was user-provided so we
3737
// can't do anything about the error.

test/fixture/redirects.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports = [
22
{ from: '^/redirected', to: '/' },
33
{ from: /^\/äßU</, to: '/' },
4+
{ from: '^/äöü$', to: '/äßU<' },
45
{ from: '^/many/(.*)$', to: '/posts/abcde' },
56
{ from: '^/mapped/(.*)$', to: '/posts/$1' },
67
{ from: '^/function$', to: () => '/' },
@@ -17,6 +18,7 @@ module.exports = [
1718
setTimeout(() => resolve(`/posts/${param}`), 2000)
1819
})
1920
},
21+
{ from: '^/errorInTo$', to: '/mapped/\uD800ab\u0001/' },
2022
{
2123
from: '^/errorInToFunction$',
2224
to: () => Promise.reject(new Error('forced error'))

test/module.test.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,19 @@ const testSuite = () => {
4242
expect(html).toContain('Works!')
4343
})
4444

45-
test('redirect error with control character', async () => {
45+
test('non-ascii redirect to another non-ascii url', async () => {
46+
const html = await get('/äöü')
47+
expect(html).toContain('Works!')
48+
})
49+
50+
test('redirect with control character', async () => {
51+
const html = await get(encodeURI('/mapped/ab\u0001'))
52+
expect(html).toContain('ab')
53+
})
54+
55+
test('redirect error due to malformatted target url', async () => {
4656
const requestOptions = {
47-
uri: url(encodeURI('/mapped/ab\u0001')),
57+
uri: url('/errorInTo'),
4858
resolveWithFullResponse: true
4959
}
5060

0 commit comments

Comments
 (0)