@fastify/express 4.0.3 → 4.0.5

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.
@@ -27,7 +27,7 @@ jobs:
27
27
  permissions:
28
28
  contents: write
29
29
  pull-requests: write
30
- uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5
30
+ uses: fastify/workflows/.github/workflows/plugins-ci.yml@v6
31
31
  with:
32
32
  license-check: true
33
33
  lint: true
@@ -0,0 +1,19 @@
1
+ name: Lock Threads
2
+
3
+ on:
4
+ schedule:
5
+ - cron: '0 0 1 * *'
6
+ workflow_dispatch:
7
+
8
+ concurrency:
9
+ group: lock
10
+
11
+ permissions:
12
+ contents: read
13
+
14
+ jobs:
15
+ lock-threads:
16
+ permissions:
17
+ issues: write
18
+ pull-requests: write
19
+ uses: fastify/workflows/.github/workflows/lock-threads.yml@v6
package/LICENSE CHANGED
@@ -1,8 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2019-present The Fastify team
4
-
5
- The Fastify team members are listed at https://github.com/fastify/fastify#team.
3
+ Copyright (c) 2019-present The Fastify team <https://github.com/fastify/fastify#team>
6
4
 
7
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
8
6
  of this software and associated documentation files (the "Software"), to deal
package/index.js CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  const fp = require('fastify-plugin')
4
4
  const Express = require('express')
5
- const FindMyWay = require('find-my-way')
6
5
  const kMiddlewares = Symbol('fastify-express-middlewares')
7
6
 
8
7
  function fastifyExpress (fastify, options, next) {
@@ -11,6 +10,10 @@ function fastifyExpress (fastify, options, next) {
11
10
  createProxyHandler
12
11
  } = options
13
12
 
13
+ const routerOptions = fastify.initialConfig.routerOptions || {}
14
+ const ignoreDuplicateSlashes = routerOptions.ignoreDuplicateSlashes ?? fastify.initialConfig.ignoreDuplicateSlashes
15
+ const useSemicolonDelimiter = routerOptions.useSemicolonDelimiter ?? fastify.initialConfig.useSemicolonDelimiter
16
+
14
17
  fastify.decorate('use', use)
15
18
  fastify[kMiddlewares] = []
16
19
  fastify.decorate('express', Express())
@@ -43,10 +46,13 @@ function fastifyExpress (fastify, options, next) {
43
46
 
44
47
  const { url } = req.raw
45
48
 
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
49
- req.raw.url = decodedUrl
49
+ const decodedUrl = decodeURI(url)
50
+ const normalizedUrl = normalizeUrl(decodedUrl, {
51
+ ignoreDuplicateSlashes,
52
+ useSemicolonDelimiter
53
+ })
54
+
55
+ req.raw.url = normalizedUrl
50
56
  req.raw.originalUrl = url
51
57
  req.raw.id = req.id
52
58
  req.raw.hostname = req.hostname
@@ -56,7 +62,7 @@ function fastifyExpress (fastify, options, next) {
56
62
  reply.raw.log = req.log
57
63
  reply.raw.send = function send (...args) {
58
64
  // Restore req.raw.url to its original value https://github.com/fastify/fastify-express/issues/11
59
- req.raw.url = decodedUrl
65
+ req.raw.url = normalizedUrl
60
66
  return reply.send.apply(reply, args)
61
67
  }
62
68
 
@@ -94,18 +100,71 @@ function fastifyExpress (fastify, options, next) {
94
100
 
95
101
  function onRegister (instance) {
96
102
  const middlewares = instance[kMiddlewares].slice()
97
- instance[kMiddlewares] = []
103
+ instance[kMiddlewares] = middlewares.slice()
98
104
  instance.decorate('express', Express())
99
105
  instance.express.disable('x-powered-by')
100
106
  instance.decorate('use', use)
101
- for (const middleware of middlewares) {
102
- instance.use(...middleware)
107
+ for (const [path, fn] of middlewares) {
108
+ if (fn == null) {
109
+ instance.express.use(path)
110
+ } else {
111
+ instance.express.use(path, fn)
112
+ }
103
113
  }
104
114
  }
105
115
 
106
116
  next()
107
117
  }
108
118
 
119
+ function normalizeUrl (url, options) {
120
+ const {
121
+ ignoreDuplicateSlashes,
122
+ useSemicolonDelimiter
123
+ } = options
124
+
125
+ const { path, query } = splitPathAndQuery(url, useSemicolonDelimiter)
126
+ const normalizedPath = ignoreDuplicateSlashes ? removeDuplicateSlashes(path) : path
127
+
128
+ return normalizedPath + query
129
+ }
130
+
131
+ function splitPathAndQuery (url, useSemicolonDelimiter) {
132
+ const queryIndex = url.indexOf('?')
133
+
134
+ if (!useSemicolonDelimiter) {
135
+ if (queryIndex === -1) {
136
+ return { path: url, query: '' }
137
+ }
138
+
139
+ return {
140
+ path: url.slice(0, queryIndex),
141
+ query: url.slice(queryIndex)
142
+ }
143
+ }
144
+
145
+ const semicolonIndex = url.indexOf(';')
146
+
147
+ if (semicolonIndex === -1 || (queryIndex !== -1 && queryIndex < semicolonIndex)) {
148
+ if (queryIndex === -1) {
149
+ return { path: url, query: '' }
150
+ }
151
+
152
+ return {
153
+ path: url.slice(0, queryIndex),
154
+ query: url.slice(queryIndex)
155
+ }
156
+ }
157
+
158
+ return {
159
+ path: url.slice(0, semicolonIndex),
160
+ query: `?${url.slice(semicolonIndex + 1)}`
161
+ }
162
+ }
163
+
164
+ function removeDuplicateSlashes (path) {
165
+ return path.replace(/\/{2,}/g, '/')
166
+ }
167
+
109
168
  module.exports = fp(fastifyExpress, {
110
169
  fastify: '5.x',
111
170
  name: '@fastify/express'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/express",
3
- "version": "4.0.3",
3
+ "version": "4.0.5",
4
4
  "description": "Express compatibility layer for Fastify",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -63,21 +63,20 @@
63
63
  "@types/express": "^5.0.0",
64
64
  "@types/node": "*",
65
65
  "body-parser": "^1.20.2",
66
- "c8": "^10.1.3",
66
+ "c8": "^11.0.0",
67
67
  "cors": "^2.8.5",
68
68
  "eslint": "^9.17.0",
69
69
  "fastify": "^5.0.0",
70
70
  "helmet": "^8.0.0",
71
- "neostandard": "^0.12.0",
71
+ "neostandard": "^0.13.0",
72
72
  "passport": "^0.7.0",
73
73
  "passport-http-bearer": "^1.0.1",
74
74
  "serve-static": "^2.2.0",
75
75
  "tsd": "^0.33.0"
76
76
  },
77
77
  "dependencies": {
78
- "express": "^4.18.3",
79
- "fastify-plugin": "^5.0.0",
80
- "find-my-way": "^9.4.0"
78
+ "express": "^5.2.1",
79
+ "fastify-plugin": "^5.0.0"
81
80
  },
82
81
  "tsd": {
83
82
  "directory": "test/types"
@@ -56,6 +56,26 @@ test('trust proxy protocol', async (t) => {
56
56
  })
57
57
  })
58
58
 
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
+
59
79
  test('passing createProxyHandler sets up a Proxy with Express req', async t => {
60
80
  t.plan(6)
61
81
  const testString = 'test proxy'
@@ -75,7 +75,8 @@ test('POST request with form body and without body-parser works', async t => {
75
75
  })
76
76
 
77
77
  test('POST request with form body and body-parser hangs up', async t => {
78
- t.plan(2)
78
+ t.plan(3)
79
+
79
80
  const fastify = Fastify()
80
81
  const express = Express()
81
82
  t.after(() => fastify.close())
@@ -85,24 +86,25 @@ test('POST request with form body and body-parser hangs up', async t => {
85
86
  .after(() => {
86
87
  express.use(bodyParser.urlencoded({ extended: false }))
87
88
  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
- })
94
89
 
95
- fastify.post('/hello', () => {
96
- return { hello: 'world' }
97
- })
90
+ fastify.use((_req, _res, next) => next())
91
+ })
98
92
 
93
+ fastify.post('/hello', () => ({ hello: 'world' }))
99
94
  const address = await fastify.listen({ port: 0 })
100
95
 
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
+ )
106
108
  })
107
109
 
108
110
  test('POST request with form body and body-parser hangs up, compatibility case', async t => {
@@ -221,6 +221,52 @@ test('middlewares with prefix', async t => {
221
221
  })
222
222
  })
223
223
 
224
+ test('should not double-prefix inherited middleware paths in child scopes', async t => {
225
+ t.plan(4)
226
+
227
+ const instance = fastify()
228
+ t.after(() => instance.close())
229
+
230
+ await instance.register(expressPlugin)
231
+
232
+ instance.use('/admin', function (req, res, next) {
233
+ if (req.headers.authorization == null) {
234
+ res.statusCode = 403
235
+ res.end('forbidden')
236
+ return
237
+ }
238
+
239
+ next()
240
+ })
241
+
242
+ instance.get('/admin/root-data', async function () {
243
+ return { data: 'root-secret' }
244
+ })
245
+
246
+ await instance.register(async function (child) {
247
+ child.get('/secret', async function () {
248
+ return { data: 'child-secret' }
249
+ })
250
+ }, { prefix: '/admin' })
251
+
252
+ const address = await instance.listen({ port: 0 })
253
+
254
+ const rootNoAuth = await fetch(address + '/admin/root-data')
255
+ t.assert.deepStrictEqual(rootNoAuth.status, 403)
256
+
257
+ const childNoAuth = await fetch(address + '/admin/secret')
258
+ t.assert.deepStrictEqual(childNoAuth.status, 403)
259
+
260
+ const childWithAuth = await fetch(address + '/admin/secret', {
261
+ headers: {
262
+ authorization: 'Bearer test'
263
+ }
264
+ })
265
+
266
+ t.assert.deepStrictEqual(childWithAuth.status, 200)
267
+ t.assert.deepStrictEqual(await childWithAuth.json(), { data: 'child-secret' })
268
+ })
269
+
224
270
  test('res.end should block middleware execution', async t => {
225
271
  t.plan(4)
226
272
 
@@ -0,0 +1,98 @@
1
+ 'use strict'
2
+
3
+ const { test } = require('node:test')
4
+ const fastify = require('fastify')
5
+
6
+ const expressPlugin = require('../index')
7
+
8
+ test('ignoreDuplicateSlashes does not bypass path-scoped middleware', async t => {
9
+ t.plan(3)
10
+
11
+ const instance = fastify({
12
+ routerOptions: {
13
+ ignoreDuplicateSlashes: true
14
+ }
15
+ })
16
+
17
+ t.after(() => instance.close())
18
+
19
+ await instance.register(expressPlugin)
20
+
21
+ let middlewareCalls = 0
22
+
23
+ instance.use('/admin', function (req, res, next) {
24
+ middlewareCalls++
25
+
26
+ if (req.headers.authorization !== 'Bearer admin-secret-token') {
27
+ res.statusCode = 403
28
+ res.setHeader('content-type', 'application/json')
29
+ res.end(JSON.stringify({ error: 'Forbidden by Express middleware' }))
30
+ return
31
+ }
32
+
33
+ next()
34
+ })
35
+
36
+ instance.get('/admin/dashboard', async () => {
37
+ return { secret: 'classified' }
38
+ })
39
+
40
+ const address = await instance.listen({ port: 0 })
41
+
42
+ const normalRequest = await fetch(address + '/admin/dashboard')
43
+ t.assert.deepStrictEqual(normalRequest.status, 403)
44
+
45
+ const bypassAttempt = await fetch(address + '//admin/dashboard')
46
+ t.assert.deepStrictEqual(bypassAttempt.status, 403)
47
+
48
+ t.assert.deepStrictEqual(middlewareCalls, 2)
49
+ })
50
+
51
+ test('useSemicolonDelimiter does not bypass path-scoped middleware', async t => {
52
+ t.plan(5)
53
+
54
+ const instance = fastify({
55
+ routerOptions: {
56
+ useSemicolonDelimiter: true
57
+ }
58
+ })
59
+
60
+ t.after(() => instance.close())
61
+
62
+ await instance.register(expressPlugin)
63
+
64
+ let middlewareCalls = 0
65
+
66
+ instance.use('/admin', function (req, res, next) {
67
+ middlewareCalls++
68
+
69
+ if (req.headers.authorization !== 'Bearer admin-secret-token') {
70
+ res.statusCode = 403
71
+ res.setHeader('content-type', 'application/json')
72
+ res.end(JSON.stringify({ error: 'Forbidden by Express middleware' }))
73
+ return
74
+ }
75
+
76
+ next()
77
+ })
78
+
79
+ instance.get('/admin', async () => {
80
+ return { secret: 'classified' }
81
+ })
82
+
83
+ const address = await instance.listen({ port: 0 })
84
+
85
+ const normalRequest = await fetch(address + '/admin')
86
+ t.assert.deepStrictEqual(normalRequest.status, 403)
87
+
88
+ const withQueryString = await fetch(address + '/admin?foo=bar')
89
+ t.assert.deepStrictEqual(withQueryString.status, 403)
90
+
91
+ const withSemicolonInQuery = await fetch(address + '/admin?foo=bar;baz=1')
92
+ t.assert.deepStrictEqual(withSemicolonInQuery.status, 403)
93
+
94
+ const bypassAttempt = await fetch(address + '/admin;bypass')
95
+ t.assert.deepStrictEqual(bypassAttempt.status, 403)
96
+
97
+ t.assert.deepStrictEqual(middlewareCalls, 4)
98
+ })
package/.github/stale.yml DELETED
@@ -1,21 +0,0 @@
1
- # Number of days of inactivity before an issue becomes stale
2
- daysUntilStale: 15
3
- # Number of days of inactivity before a stale issue is closed
4
- daysUntilClose: 7
5
- # Issues with these labels will never be considered stale
6
- exemptLabels:
7
- - "discussion"
8
- - "feature request"
9
- - "bug"
10
- - "help wanted"
11
- - "plugin suggestion"
12
- - "good first issue"
13
- # Label to use when marking an issue as stale
14
- staleLabel: stale
15
- # Comment to post when marking an issue as stale. Set to `false` to disable
16
- markComment: >
17
- This issue has been automatically marked as stale because it has not had
18
- recent activity. It will be closed if no further activity occurs. Thank you
19
- for your contributions.
20
- # Comment to post when closing a stale issue. Set to `false` to disable
21
- closeComment: false