@fastify/express 4.0.4 → 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.
- package/.github/workflows/ci.yml +1 -1
- package/.github/workflows/lock-threads.yml +19 -0
- package/LICENSE +1 -3
- package/index.js +67 -5
- package/package.json +4 -4
- package/test/middleware.test.js +46 -0
- package/test/path-normalization.test.js +98 -0
- package/.github/stale.yml +0 -21
package/.github/workflows/ci.yml
CHANGED
|
@@ -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
|
@@ -10,6 +10,10 @@ function fastifyExpress (fastify, options, next) {
|
|
|
10
10
|
createProxyHandler
|
|
11
11
|
} = options
|
|
12
12
|
|
|
13
|
+
const routerOptions = fastify.initialConfig.routerOptions || {}
|
|
14
|
+
const ignoreDuplicateSlashes = routerOptions.ignoreDuplicateSlashes ?? fastify.initialConfig.ignoreDuplicateSlashes
|
|
15
|
+
const useSemicolonDelimiter = routerOptions.useSemicolonDelimiter ?? fastify.initialConfig.useSemicolonDelimiter
|
|
16
|
+
|
|
13
17
|
fastify.decorate('use', use)
|
|
14
18
|
fastify[kMiddlewares] = []
|
|
15
19
|
fastify.decorate('express', Express())
|
|
@@ -43,7 +47,12 @@ function fastifyExpress (fastify, options, next) {
|
|
|
43
47
|
const { url } = req.raw
|
|
44
48
|
|
|
45
49
|
const decodedUrl = decodeURI(url)
|
|
46
|
-
|
|
50
|
+
const normalizedUrl = normalizeUrl(decodedUrl, {
|
|
51
|
+
ignoreDuplicateSlashes,
|
|
52
|
+
useSemicolonDelimiter
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
req.raw.url = normalizedUrl
|
|
47
56
|
req.raw.originalUrl = url
|
|
48
57
|
req.raw.id = req.id
|
|
49
58
|
req.raw.hostname = req.hostname
|
|
@@ -53,7 +62,7 @@ function fastifyExpress (fastify, options, next) {
|
|
|
53
62
|
reply.raw.log = req.log
|
|
54
63
|
reply.raw.send = function send (...args) {
|
|
55
64
|
// Restore req.raw.url to its original value https://github.com/fastify/fastify-express/issues/11
|
|
56
|
-
req.raw.url =
|
|
65
|
+
req.raw.url = normalizedUrl
|
|
57
66
|
return reply.send.apply(reply, args)
|
|
58
67
|
}
|
|
59
68
|
|
|
@@ -91,18 +100,71 @@ function fastifyExpress (fastify, options, next) {
|
|
|
91
100
|
|
|
92
101
|
function onRegister (instance) {
|
|
93
102
|
const middlewares = instance[kMiddlewares].slice()
|
|
94
|
-
instance[kMiddlewares] =
|
|
103
|
+
instance[kMiddlewares] = middlewares.slice()
|
|
95
104
|
instance.decorate('express', Express())
|
|
96
105
|
instance.express.disable('x-powered-by')
|
|
97
106
|
instance.decorate('use', use)
|
|
98
|
-
for (const
|
|
99
|
-
|
|
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
|
+
}
|
|
100
113
|
}
|
|
101
114
|
}
|
|
102
115
|
|
|
103
116
|
next()
|
|
104
117
|
}
|
|
105
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
|
+
|
|
106
168
|
module.exports = fp(fastifyExpress, {
|
|
107
169
|
fastify: '5.x',
|
|
108
170
|
name: '@fastify/express'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/express",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.5",
|
|
4
4
|
"description": "Express compatibility layer for Fastify",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -63,19 +63,19 @@
|
|
|
63
63
|
"@types/express": "^5.0.0",
|
|
64
64
|
"@types/node": "*",
|
|
65
65
|
"body-parser": "^1.20.2",
|
|
66
|
-
"c8": "^
|
|
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.
|
|
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": "^
|
|
78
|
+
"express": "^5.2.1",
|
|
79
79
|
"fastify-plugin": "^5.0.0"
|
|
80
80
|
},
|
|
81
81
|
"tsd": {
|
package/test/middleware.test.js
CHANGED
|
@@ -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
|