Skip to content

Commit b34abae

Browse files
authored
fix: decode url with query params (#178)
* fix: decode url with query params * fix: refacto form data test to be idempotent
1 parent d1c061d commit b34abae

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const fp = require('fastify-plugin')
44
const Express = require('express')
5-
const FindMyWay = require('find-my-way')
65
const kMiddlewares = Symbol('fastify-express-middlewares')
76

87
function fastifyExpress (fastify, options, next) {
@@ -43,9 +42,7 @@ function fastifyExpress (fastify, options, next) {
4342

4443
const { url } = req.raw
4544

46-
const decodedUrl = FindMyWay.sanitizeUrlPath(url)
47-
// Decode URL before Express matches middleware to prevent encoded path bypass
48-
// e.g., /%61dmin should match middleware registered on /admin
45+
const decodedUrl = decodeURI(url)
4946
req.raw.url = decodedUrl
5047
req.raw.originalUrl = url
5148
req.raw.id = req.id

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@
7676
},
7777
"dependencies": {
7878
"express": "^4.18.3",
79-
"fastify-plugin": "^5.0.0",
80-
"find-my-way": "^9.4.0"
79+
"fastify-plugin": "^5.0.0"
8180
},
8281
"tsd": {
8382
"directory": "test/types"

test/enhance-request.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ test('trust proxy protocol', async (t) => {
5656
})
5757
})
5858

59+
test('query params still in in the request url after decodeURI', async (t) => {
60+
t.plan(2)
61+
const fastify = Fastify()
62+
63+
t.after(() => fastify.close())
64+
65+
fastify.register(expressPlugin).after(() => {
66+
fastify.use('/admin', function (req, res) {
67+
t.assert.deepStrictEqual(req.originalUrl, '/%61dmin?test=%76alue&test2=%76alue', 'originalUrl is not decoded')
68+
t.assert.deepStrictEqual(req.url, '/admin?test=value&test2=value', 'url is decoded')
69+
70+
res.sendStatus(200)
71+
})
72+
})
73+
74+
const address = await fastify.listen({ port: 0 })
75+
76+
await fetch(`${address}/%61dmin?test=%76alue&test2=%76alue`)
77+
})
78+
5979
test('passing createProxyHandler sets up a Proxy with Express req', async t => {
6080
t.plan(6)
6181
const testString = 'test proxy'

test/form-data.test.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ test('POST request with form body and without body-parser works', async t => {
7575
})
7676

7777
test('POST request with form body and body-parser hangs up', async t => {
78-
t.plan(2)
78+
t.plan(3)
79+
7980
const fastify = Fastify()
8081
const express = Express()
8182
t.after(() => fastify.close())
@@ -85,24 +86,25 @@ test('POST request with form body and body-parser hangs up', async t => {
8586
.after(() => {
8687
express.use(bodyParser.urlencoded({ extended: false }))
8788
fastify.use(express)
88-
fastify.use((req, _res, next) => {
89-
// body-parser result
90-
t.assert.deepStrictEqual(req.body, { input: 'test' })
91-
next()
92-
})
93-
})
9489

95-
fastify.post('/hello', () => {
96-
return { hello: 'world' }
97-
})
90+
fastify.use((_req, _res, next) => next())
91+
})
9892

93+
fastify.post('/hello', () => ({ hello: 'world' }))
9994
const address = await fastify.listen({ port: 0 })
10095

101-
await t.assert.rejects(() => fetch(address + '/hello', {
102-
method: 'post',
103-
body: new URLSearchParams({ input: 'test' }),
104-
signal: AbortSignal.timeout(5)
105-
}), 'Request timed out')
96+
await t.assert.rejects(
97+
() => fetch(address + '/hello', {
98+
method: 'post',
99+
body: new URLSearchParams({ input: 'test' }),
100+
signal: AbortSignal.timeout(200)
101+
}),
102+
(err) => {
103+
t.assert.equal(err?.name, 'TimeoutError')
104+
t.assert.ok(err)
105+
return true
106+
}
107+
)
106108
})
107109

108110
test('POST request with form body and body-parser hangs up, compatibility case', async t => {

0 commit comments

Comments
 (0)