@fastify/express 4.0.9 → 4.0.11

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.
@@ -11,7 +11,10 @@ function requestTarget (address, target, method = 'GET') {
11
11
 
12
12
  return new Promise((resolve, reject) => {
13
13
  const socket = net.createConnection({ host: hostname, port }, () => {
14
- socket.end(`${method} ${target} HTTP/1.1\r\nHost: ${hostname}:${port}\r\nConnection: close\r\n\r\n`)
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`)
15
18
  })
16
19
 
17
20
  let response = ''
@@ -71,7 +74,7 @@ test('absolute-form targets do not bypass path-scoped middleware', async t => {
71
74
  t.assert.deepStrictEqual(seen, [
72
75
  { url: '/admin/dashboard', originalUrl: targets[0] },
73
76
  { url: '/admin/dashboard', originalUrl: targets[1] },
74
- { url: '/admin/dashboard?test=value', originalUrl: targets[2] },
77
+ { url: '/admin/dashboard?test=%76alue', originalUrl: targets[2] },
75
78
  { url: '/admin/dashboard', originalUrl: targets[3] },
76
79
  { url: '/admin/dashboard?bypass', originalUrl: targets[4] }
77
80
  ])
@@ -137,7 +140,52 @@ test('absolute-form targets without a path resolve to root and preserve the quer
137
140
  t.assert.strictEqual((await requestTarget(address, 'http://example.test')).statusCode, 200)
138
141
  t.assert.strictEqual((await requestTarget(address, 'http://example.test?next=/admin&x=%76alue')).statusCode, 200)
139
142
  t.assert.strictEqual((await requestTarget(address, '*', 'OPTIONS')).statusCode, 200)
140
- t.assert.deepStrictEqual(seen, ['/', '/?next=/admin&x=value', '*'])
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
+ }
141
189
  })
142
190
 
143
191
  test('malformed request targets are rejected without affecting later requests', async t => {
@@ -175,7 +223,7 @@ test('malformed request targets are rejected without affecting later requests',
175
223
  t.assert.strictEqual(response.statusCode, 400, target)
176
224
  }
177
225
 
178
- for (const target of ['http:///admin', '/admin?value=%zz']) {
226
+ for (const target of ['http:///admin', '/admin?value=%zz', '*admin']) {
179
227
  const response = await instance.inject({
180
228
  method: 'GET',
181
229
  url: '/admin',
@@ -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('/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
-
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
+ })
@@ -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(100)
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(100)
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(100)
136
+ signal: AbortSignal.timeout(5000)
137
137
  })
138
138
 
139
139
  t.assert.deepStrictEqual(result.status, 200)