@fastify/express 4.0.8 → 4.0.10
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/README.md +15 -3
- package/index.js +417 -43
- package/package.json +4 -2
- package/test/absolute-form-url.test.js +237 -0
- package/test/enhance-request.test.js +57 -6
- package/test/form-data.test.js +3 -3
- package/test/middleware.test.js +1099 -35
- package/test/path-normalization.test.js +158 -0
- package/test/url-encoding.test.js +144 -0
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const net = require('node:net')
|
|
4
|
+
const { test } = require('node:test')
|
|
5
|
+
const Fastify = require('fastify')
|
|
6
|
+
|
|
7
|
+
const expressPlugin = require('../index')
|
|
8
|
+
|
|
9
|
+
function requestTarget (address, target, method = 'GET') {
|
|
10
|
+
const { hostname, port } = new URL(address)
|
|
11
|
+
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
const socket = net.createConnection({ host: hostname, port }, () => {
|
|
14
|
+
// Do not half-close the connection: Node.js aborts a request still in
|
|
15
|
+
// flight when the client ends its side, so its response would be lost.
|
|
16
|
+
// The server closes the connection after responding (Connection: close).
|
|
17
|
+
socket.write(`${method} ${target} HTTP/1.1\r\nHost: ${hostname}:${port}\r\nConnection: close\r\n\r\n`)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
let response = ''
|
|
21
|
+
socket.setEncoding('utf8')
|
|
22
|
+
socket.on('data', chunk => { response += chunk })
|
|
23
|
+
socket.on('end', () => {
|
|
24
|
+
const status = /^HTTP\/1\.1 (\d{3})/.exec(response)
|
|
25
|
+
resolve({
|
|
26
|
+
statusCode: Number(status[1]),
|
|
27
|
+
payload: response.slice(response.indexOf('\r\n\r\n') + 4)
|
|
28
|
+
})
|
|
29
|
+
})
|
|
30
|
+
socket.on('error', reject)
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
test('absolute-form targets do not bypass path-scoped middleware', async t => {
|
|
35
|
+
const instance = Fastify({
|
|
36
|
+
routerOptions: {
|
|
37
|
+
ignoreDuplicateSlashes: true,
|
|
38
|
+
useSemicolonDelimiter: true
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
t.after(() => instance.close())
|
|
42
|
+
|
|
43
|
+
await instance.register(expressPlugin)
|
|
44
|
+
|
|
45
|
+
const seen = []
|
|
46
|
+
let middlewareCalls = 0
|
|
47
|
+
|
|
48
|
+
instance.use((req, res, next) => {
|
|
49
|
+
seen.push({ url: req.url, originalUrl: req.originalUrl })
|
|
50
|
+
next()
|
|
51
|
+
})
|
|
52
|
+
instance.use('/admin', (req, res) => {
|
|
53
|
+
middlewareCalls++
|
|
54
|
+
res.statusCode = 403
|
|
55
|
+
res.end('blocked')
|
|
56
|
+
})
|
|
57
|
+
instance.get('/admin/:section', async () => ({ secret: 'classified' }))
|
|
58
|
+
|
|
59
|
+
const address = await instance.listen({ port: 0, host: '127.0.0.1' })
|
|
60
|
+
const targets = [
|
|
61
|
+
'http://localhost/admin/dashboard',
|
|
62
|
+
'HTTP://localhost/admin/dashboard',
|
|
63
|
+
'https://user:pass@example.test:443/admin/dashboard?test=%76alue',
|
|
64
|
+
'http://example.test//admin//dashboard',
|
|
65
|
+
'http://localhost/admin/dashboard;bypass'
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
for (const target of targets) {
|
|
69
|
+
const response = await requestTarget(address, target)
|
|
70
|
+
t.assert.strictEqual(response.statusCode, 403, target)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
t.assert.strictEqual(middlewareCalls, targets.length)
|
|
74
|
+
t.assert.deepStrictEqual(seen, [
|
|
75
|
+
{ url: '/admin/dashboard', originalUrl: targets[0] },
|
|
76
|
+
{ url: '/admin/dashboard', originalUrl: targets[1] },
|
|
77
|
+
{ url: '/admin/dashboard?test=%76alue', originalUrl: targets[2] },
|
|
78
|
+
{ url: '/admin/dashboard', originalUrl: targets[3] },
|
|
79
|
+
{ url: '/admin/dashboard?bypass', originalUrl: targets[4] }
|
|
80
|
+
])
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test('absolute-form targets match middleware in prefixed plugins', async t => {
|
|
84
|
+
const instance = Fastify({
|
|
85
|
+
routerOptions: {
|
|
86
|
+
ignoreDuplicateSlashes: true
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
t.after(() => instance.close())
|
|
90
|
+
|
|
91
|
+
await instance.register(expressPlugin)
|
|
92
|
+
await instance.register(async child => {
|
|
93
|
+
child.use('/admin', (req, res) => {
|
|
94
|
+
res.statusCode = 403
|
|
95
|
+
res.end('blocked')
|
|
96
|
+
})
|
|
97
|
+
child.get('/admin/:id', async () => ({ secret: 'classified' }))
|
|
98
|
+
}, { prefix: '/api' })
|
|
99
|
+
|
|
100
|
+
const address = await instance.listen({ port: 0, host: '127.0.0.1' })
|
|
101
|
+
const response = await requestTarget(address, 'http://localhost/api//admin/42')
|
|
102
|
+
|
|
103
|
+
t.assert.strictEqual(response.statusCode, 403)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('absolute-form targets are resolved when duplicate slash normalization is disabled', async t => {
|
|
107
|
+
const instance = Fastify()
|
|
108
|
+
t.after(() => instance.close())
|
|
109
|
+
|
|
110
|
+
await instance.register(expressPlugin)
|
|
111
|
+
|
|
112
|
+
instance.use('/admin', (req, res) => {
|
|
113
|
+
res.statusCode = 403
|
|
114
|
+
res.end('blocked')
|
|
115
|
+
})
|
|
116
|
+
instance.get('/admin/dashboard', async () => ({ secret: 'classified' }))
|
|
117
|
+
|
|
118
|
+
const address = await instance.listen({ port: 0, host: '127.0.0.1' })
|
|
119
|
+
const response = await requestTarget(address, 'http://localhost/admin/dashboard')
|
|
120
|
+
|
|
121
|
+
t.assert.strictEqual(response.statusCode, 403)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
test('absolute-form targets without a path resolve to root and preserve the query', async t => {
|
|
125
|
+
const instance = Fastify()
|
|
126
|
+
t.after(() => instance.close())
|
|
127
|
+
|
|
128
|
+
await instance.register(expressPlugin)
|
|
129
|
+
|
|
130
|
+
const seen = []
|
|
131
|
+
instance.use((req, res, next) => {
|
|
132
|
+
seen.push(req.url)
|
|
133
|
+
next()
|
|
134
|
+
})
|
|
135
|
+
instance.get('/', async () => ({ ok: true }))
|
|
136
|
+
instance.setNotFoundHandler(async () => ({ ok: true }))
|
|
137
|
+
|
|
138
|
+
const address = await instance.listen({ port: 0, host: '127.0.0.1' })
|
|
139
|
+
|
|
140
|
+
t.assert.strictEqual((await requestTarget(address, 'http://example.test')).statusCode, 200)
|
|
141
|
+
t.assert.strictEqual((await requestTarget(address, 'http://example.test?next=/admin&x=%76alue')).statusCode, 200)
|
|
142
|
+
t.assert.strictEqual((await requestTarget(address, '*', 'OPTIONS')).statusCode, 200)
|
|
143
|
+
t.assert.deepStrictEqual(seen, ['/', '/?next=/admin&x=%76alue', '/'])
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
test('request targets that do not start with a slash do not bypass path-scoped middleware', async t => {
|
|
147
|
+
const instance = Fastify()
|
|
148
|
+
t.after(() => instance.close())
|
|
149
|
+
|
|
150
|
+
await instance.register(expressPlugin)
|
|
151
|
+
|
|
152
|
+
const deny = (req, res) => {
|
|
153
|
+
res.statusCode = 403
|
|
154
|
+
res.end('blocked')
|
|
155
|
+
}
|
|
156
|
+
instance.use('/admin', deny)
|
|
157
|
+
instance.use('/tenant/:id/admin', deny)
|
|
158
|
+
instance.get('/admin', async () => ({ secret: 'classified' }))
|
|
159
|
+
instance.get('/admin/:id', async () => ({ secret: 'classified' }))
|
|
160
|
+
instance.get('/tenant/:id/admin', async () => ({ secret: 'classified' }))
|
|
161
|
+
|
|
162
|
+
const address = await instance.listen({ port: 0, host: '127.0.0.1' })
|
|
163
|
+
|
|
164
|
+
for (const target of ['*admin', '*admin/42', '*tenant/x/admin']) {
|
|
165
|
+
const response = await requestTarget(address, target)
|
|
166
|
+
t.assert.strictEqual(response.statusCode, 400, target)
|
|
167
|
+
}
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
test('the asterisk-form request target is matched as the root path', async t => {
|
|
171
|
+
const instance = Fastify()
|
|
172
|
+
t.after(() => instance.close())
|
|
173
|
+
|
|
174
|
+
await instance.register(expressPlugin)
|
|
175
|
+
|
|
176
|
+
instance.use(/^\/$/, (req, res) => {
|
|
177
|
+
res.statusCode = 403
|
|
178
|
+
res.end('blocked')
|
|
179
|
+
})
|
|
180
|
+
instance.get('/', async () => ({ secret: 'classified' }))
|
|
181
|
+
instance.options('/', async () => ({ secret: 'classified' }))
|
|
182
|
+
|
|
183
|
+
const address = await instance.listen({ port: 0, host: '127.0.0.1' })
|
|
184
|
+
|
|
185
|
+
for (const method of ['GET', 'OPTIONS']) {
|
|
186
|
+
const response = await requestTarget(address, '*', method)
|
|
187
|
+
t.assert.strictEqual(response.statusCode, 403, method)
|
|
188
|
+
}
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
test('malformed request targets are rejected without affecting later requests', async t => {
|
|
192
|
+
const instance = Fastify({
|
|
193
|
+
routerOptions: {
|
|
194
|
+
ignoreDuplicateSlashes: true
|
|
195
|
+
}
|
|
196
|
+
})
|
|
197
|
+
t.after(() => instance.close())
|
|
198
|
+
|
|
199
|
+
await instance.register(expressPlugin, {
|
|
200
|
+
createProxyHandler: request => ({
|
|
201
|
+
get (target, property, receiver) {
|
|
202
|
+
if (property === 'url' && request.headers['x-test-url']) {
|
|
203
|
+
return request.headers['x-test-url']
|
|
204
|
+
}
|
|
205
|
+
return Reflect.get(target, property, receiver)
|
|
206
|
+
}
|
|
207
|
+
})
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
instance.use((req, res, next) => next())
|
|
211
|
+
instance.get('/admin', async () => ({ ok: true }))
|
|
212
|
+
|
|
213
|
+
const address = await instance.listen({ port: 0, host: '127.0.0.1' })
|
|
214
|
+
const malformedTargets = [
|
|
215
|
+
'http:///admin',
|
|
216
|
+
'http://example.test:invalid/admin',
|
|
217
|
+
'http://example.test#fragment/admin',
|
|
218
|
+
'/admin?value=%zz'
|
|
219
|
+
]
|
|
220
|
+
|
|
221
|
+
for (const target of malformedTargets) {
|
|
222
|
+
const response = await requestTarget(address, target)
|
|
223
|
+
t.assert.strictEqual(response.statusCode, 400, target)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
for (const target of ['http:///admin', '/admin?value=%zz', '*admin']) {
|
|
227
|
+
const response = await instance.inject({
|
|
228
|
+
method: 'GET',
|
|
229
|
+
url: '/admin',
|
|
230
|
+
headers: { 'x-test-url': target }
|
|
231
|
+
})
|
|
232
|
+
t.assert.strictEqual(response.statusCode, 400, target)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const validResponse = await requestTarget(address, '/admin')
|
|
236
|
+
t.assert.strictEqual(validResponse.statusCode, 200)
|
|
237
|
+
})
|
|
@@ -57,23 +57,30 @@ test('trust proxy protocol', async (t) => {
|
|
|
57
57
|
})
|
|
58
58
|
|
|
59
59
|
test('query params still in in the request url after decodeURI', async (t) => {
|
|
60
|
-
t.plan(2)
|
|
61
60
|
const fastify = Fastify()
|
|
62
61
|
|
|
63
62
|
t.after(() => fastify.close())
|
|
64
63
|
|
|
64
|
+
// Record what the middleware sees and assert afterwards: Express would
|
|
65
|
+
// swallow an assertion thrown inside the middleware.
|
|
66
|
+
let seen
|
|
65
67
|
fastify.register(expressPlugin).after(() => {
|
|
66
|
-
fastify.use(
|
|
67
|
-
|
|
68
|
-
t.assert.deepStrictEqual(req.url, '/admin?test=value&test2=value', 'url is decoded')
|
|
69
|
-
|
|
68
|
+
fastify.use(function (req, res) {
|
|
69
|
+
seen = { originalUrl: req.originalUrl, url: req.url, query: { ...req.query } }
|
|
70
70
|
res.sendStatus(200)
|
|
71
71
|
})
|
|
72
72
|
})
|
|
73
73
|
|
|
74
74
|
const address = await fastify.listen({ port: 0 })
|
|
75
75
|
|
|
76
|
-
await fetch(`${address}/%61dmin?test=%76alue&test2=%76alue`)
|
|
76
|
+
const response = await fetch(`${address}/%61dmin?test=%76alue&test2=%76alue`)
|
|
77
|
+
t.assert.strictEqual(response.status, 200)
|
|
78
|
+
t.assert.deepStrictEqual(seen, {
|
|
79
|
+
originalUrl: '/%61dmin?test=%76alue&test2=%76alue',
|
|
80
|
+
// Only the path is decoded; the query string is decoded once, by Express.
|
|
81
|
+
url: '/admin?test=%76alue&test2=%76alue',
|
|
82
|
+
query: { test: 'value', test2: 'value' }
|
|
83
|
+
})
|
|
77
84
|
})
|
|
78
85
|
|
|
79
86
|
test('passing createProxyHandler sets up a Proxy with Express req', async t => {
|
|
@@ -182,3 +189,47 @@ test('createProxyHandler has access to Fastify request object', async t => {
|
|
|
182
189
|
t.assert.deepStrictEqual(response.headers.get('content-length'), '' + responseText.length)
|
|
183
190
|
t.assert.deepStrictEqual(JSON.parse(responseText), { hello: 'world' })
|
|
184
191
|
})
|
|
192
|
+
|
|
193
|
+
test('createProxyHandler keeps the original request on the response', async t => {
|
|
194
|
+
t.plan(5)
|
|
195
|
+
|
|
196
|
+
const fastify = Fastify()
|
|
197
|
+
t.after(() => fastify.close())
|
|
198
|
+
|
|
199
|
+
let rawRequest
|
|
200
|
+
fastify.register(expressPlugin, {
|
|
201
|
+
createProxyHandler: fastifyReq => {
|
|
202
|
+
rawRequest = fastifyReq.raw
|
|
203
|
+
return {}
|
|
204
|
+
}
|
|
205
|
+
})
|
|
206
|
+
.after(() => {
|
|
207
|
+
fastify.use(function (req, res, next) {
|
|
208
|
+
// Express still works with the Proxy while it handles the request
|
|
209
|
+
t.assert.notStrictEqual(req, rawRequest)
|
|
210
|
+
t.assert.strictEqual(res.req, req)
|
|
211
|
+
next()
|
|
212
|
+
})
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
// Node.js reads `res.req` when the response finishes and expects the
|
|
216
|
+
// request it created there, not the Proxy that Express assigned
|
|
217
|
+
const finished = new Promise(resolve => {
|
|
218
|
+
fastify.addHook('onResponse', function (_request, reply, done) {
|
|
219
|
+
resolve(reply.raw.req)
|
|
220
|
+
done()
|
|
221
|
+
})
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
fastify.get('/', function (_request, reply) {
|
|
225
|
+
reply.send({ hello: 'world' })
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
const address = await fastify.listen({ port: 0 })
|
|
229
|
+
|
|
230
|
+
const response = await fetch(address)
|
|
231
|
+
|
|
232
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
233
|
+
t.assert.deepStrictEqual(await response.json(), { hello: 'world' })
|
|
234
|
+
t.assert.strictEqual(await finished, rawRequest)
|
|
235
|
+
})
|
package/test/form-data.test.js
CHANGED
|
@@ -34,7 +34,7 @@ test('POST request without form body works', async t => {
|
|
|
34
34
|
|
|
35
35
|
const result = await fetch(address + '/hello', {
|
|
36
36
|
method: 'post',
|
|
37
|
-
signal: AbortSignal.timeout(
|
|
37
|
+
signal: AbortSignal.timeout(5000)
|
|
38
38
|
})
|
|
39
39
|
|
|
40
40
|
t.assert.deepStrictEqual(result.status, 200)
|
|
@@ -67,7 +67,7 @@ test('POST request with form body and without body-parser works', async t => {
|
|
|
67
67
|
const result = await fetch(address + '/hello', {
|
|
68
68
|
method: 'post',
|
|
69
69
|
body: new URLSearchParams({ input: 'test' }),
|
|
70
|
-
signal: AbortSignal.timeout(
|
|
70
|
+
signal: AbortSignal.timeout(5000)
|
|
71
71
|
})
|
|
72
72
|
|
|
73
73
|
t.assert.deepStrictEqual(result.status, 200)
|
|
@@ -133,7 +133,7 @@ test('POST request with form body and body-parser hangs up, compatibility case',
|
|
|
133
133
|
const result = await fetch(address + '/hello', {
|
|
134
134
|
method: 'post',
|
|
135
135
|
body: new URLSearchParams({ input: 'test' }),
|
|
136
|
-
signal: AbortSignal.timeout(
|
|
136
|
+
signal: AbortSignal.timeout(5000)
|
|
137
137
|
})
|
|
138
138
|
|
|
139
139
|
t.assert.deepStrictEqual(result.status, 200)
|