@fastify/express 4.0.7 → 4.0.9
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 +1 -1
- package/.github/workflows/release.yml +24 -0
- package/README.md +1 -1
- package/index.js +55 -5
- package/package.json +3 -3
- package/test/absolute-form-url.test.js +189 -0
- package/test/basic.test.js +22 -0
package/.github/workflows/ci.yml
CHANGED
|
@@ -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@
|
|
30
|
+
uses: fastify/workflows/.github/workflows/plugins-ci.yml@ef591e2186785d5ab36b9fe6a79c7ce2f1d94e57 # v7.0.0
|
|
31
31
|
with:
|
|
32
32
|
license-check: true
|
|
33
33
|
lint: true
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
name: Release the package
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
workflow_dispatch:
|
|
5
|
+
inputs:
|
|
6
|
+
semver:
|
|
7
|
+
description: 'Release bump type'
|
|
8
|
+
required: true
|
|
9
|
+
type: choice
|
|
10
|
+
options:
|
|
11
|
+
- patch
|
|
12
|
+
- minor
|
|
13
|
+
- major
|
|
14
|
+
|
|
15
|
+
permissions: {}
|
|
16
|
+
|
|
17
|
+
jobs:
|
|
18
|
+
release:
|
|
19
|
+
permissions:
|
|
20
|
+
id-token: write # required for npm provenance via OIDC
|
|
21
|
+
contents: write # required to push the release commit and the tag
|
|
22
|
+
uses: fastify/workflows/.github/workflows/reusable-release.yml@b1a11bce713bc857e52b2231fec3c89071ad3135
|
|
23
|
+
with:
|
|
24
|
+
semver: ${{ inputs.semver }}
|
package/README.md
CHANGED
|
@@ -156,7 +156,7 @@ async function subsystem (fastify, opts) {
|
|
|
156
156
|
### Hooks and middleware
|
|
157
157
|
|
|
158
158
|
Every registered middleware will be run during the `onRequest` hook phase, so the registration order is important.
|
|
159
|
-
Take a look at the [Lifecycle](https://fastify.dev/docs/latest/Reference/Lifecycle) documentation page to understand better how every request is executed.
|
|
159
|
+
Take a look at the [Lifecycle](https://fastify.dev/docs/latest/Reference/Lifecycle/) documentation page to understand better how every request is executed.
|
|
160
160
|
|
|
161
161
|
```js
|
|
162
162
|
const fastify = require('fastify')()
|
package/index.js
CHANGED
|
@@ -49,15 +49,21 @@ function fastifyExpress (fastify, options, next) {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
const { url } = req.raw
|
|
52
|
+
req.raw.originalUrl = url
|
|
52
53
|
|
|
53
|
-
const
|
|
54
|
-
const normalizedUrl = normalizeUrl(decodedUrl, {
|
|
54
|
+
const normalizedUrl = normalizeUrl(url, {
|
|
55
55
|
ignoreDuplicateSlashes,
|
|
56
56
|
useSemicolonDelimiter
|
|
57
57
|
})
|
|
58
58
|
|
|
59
|
+
if (normalizedUrl === null) {
|
|
60
|
+
const error = new Error('Invalid URL')
|
|
61
|
+
error.statusCode = 400
|
|
62
|
+
next(error)
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
|
+
|
|
59
66
|
req.raw.url = normalizedUrl
|
|
60
|
-
req.raw.originalUrl = url
|
|
61
67
|
req.raw.id = req.id
|
|
62
68
|
req.raw.hostname = req.hostname
|
|
63
69
|
req.raw.ip = req.ip
|
|
@@ -82,7 +88,6 @@ function fastifyExpress (fastify, options, next) {
|
|
|
82
88
|
req.raw.body = req.body
|
|
83
89
|
}
|
|
84
90
|
// backward compatibility for cookie-parser
|
|
85
|
-
/* c8 ignore next 3 */
|
|
86
91
|
if (req.cookies) {
|
|
87
92
|
req.raw.cookies = req.cookies
|
|
88
93
|
}
|
|
@@ -139,12 +144,57 @@ function normalizeUrl (url, options) {
|
|
|
139
144
|
useSemicolonDelimiter
|
|
140
145
|
} = options
|
|
141
146
|
|
|
142
|
-
|
|
147
|
+
// Match find-my-way's absolute-form handling before normalizing the path.
|
|
148
|
+
const originFormUrl = getPathFromAbsoluteUrl(url)
|
|
149
|
+
if (originFormUrl === null) {
|
|
150
|
+
return null
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
let decodedUrl
|
|
154
|
+
try {
|
|
155
|
+
decodedUrl = decodeURI(originFormUrl)
|
|
156
|
+
} catch {
|
|
157
|
+
return null
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const { path, query } = splitPathAndQuery(decodedUrl, useSemicolonDelimiter)
|
|
143
161
|
const normalizedPath = ignoreDuplicateSlashes ? removeDuplicateSlashes(path) : path
|
|
144
162
|
|
|
145
163
|
return normalizedPath + query
|
|
146
164
|
}
|
|
147
165
|
|
|
166
|
+
function getPathFromAbsoluteUrl (url) {
|
|
167
|
+
const scheme = /^https?:\/\//i.exec(url)
|
|
168
|
+
if (scheme === null) {
|
|
169
|
+
return url
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const authorityStart = scheme[0].length
|
|
173
|
+
const pathStart = url.indexOf('/', authorityStart)
|
|
174
|
+
const queryStart = url.indexOf('?', authorityStart)
|
|
175
|
+
const fragmentStart = url.indexOf('#', authorityStart)
|
|
176
|
+
|
|
177
|
+
let authorityEnd = url.length
|
|
178
|
+
if (pathStart !== -1) {
|
|
179
|
+
authorityEnd = pathStart
|
|
180
|
+
}
|
|
181
|
+
if (queryStart !== -1 && queryStart < authorityEnd) {
|
|
182
|
+
authorityEnd = queryStart
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (fragmentStart !== -1 || authorityEnd === authorityStart || !URL.canParse(url)) {
|
|
186
|
+
return null
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (authorityEnd === url.length) {
|
|
190
|
+
return '/'
|
|
191
|
+
}
|
|
192
|
+
if (authorityEnd === queryStart) {
|
|
193
|
+
return '/' + url.slice(queryStart)
|
|
194
|
+
}
|
|
195
|
+
return url.slice(pathStart)
|
|
196
|
+
}
|
|
197
|
+
|
|
148
198
|
function splitPathAndQuery (url, useSemicolonDelimiter) {
|
|
149
199
|
const queryIndex = url.indexOf('?')
|
|
150
200
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/express",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.9",
|
|
4
4
|
"description": "Express compatibility layer for Fastify",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -59,11 +59,11 @@
|
|
|
59
59
|
}
|
|
60
60
|
],
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"@fastify/formbody": "^
|
|
62
|
+
"@fastify/formbody": "^9.0.0",
|
|
63
63
|
"@types/express": "^5.0.0",
|
|
64
64
|
"@types/node": "*",
|
|
65
65
|
"body-parser": "^1.20.2",
|
|
66
|
-
"c8": "^
|
|
66
|
+
"c8": "^12.0.0",
|
|
67
67
|
"cors": "^2.8.5",
|
|
68
68
|
"eslint": "^9.17.0",
|
|
69
69
|
"fastify": "^5.0.0",
|
|
@@ -0,0 +1,189 @@
|
|
|
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
|
+
socket.end(`${method} ${target} HTTP/1.1\r\nHost: ${hostname}:${port}\r\nConnection: close\r\n\r\n`)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
let response = ''
|
|
18
|
+
socket.setEncoding('utf8')
|
|
19
|
+
socket.on('data', chunk => { response += chunk })
|
|
20
|
+
socket.on('end', () => {
|
|
21
|
+
const status = /^HTTP\/1\.1 (\d{3})/.exec(response)
|
|
22
|
+
resolve({
|
|
23
|
+
statusCode: Number(status[1]),
|
|
24
|
+
payload: response.slice(response.indexOf('\r\n\r\n') + 4)
|
|
25
|
+
})
|
|
26
|
+
})
|
|
27
|
+
socket.on('error', reject)
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
test('absolute-form targets do not bypass path-scoped middleware', async t => {
|
|
32
|
+
const instance = Fastify({
|
|
33
|
+
routerOptions: {
|
|
34
|
+
ignoreDuplicateSlashes: true,
|
|
35
|
+
useSemicolonDelimiter: true
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
t.after(() => instance.close())
|
|
39
|
+
|
|
40
|
+
await instance.register(expressPlugin)
|
|
41
|
+
|
|
42
|
+
const seen = []
|
|
43
|
+
let middlewareCalls = 0
|
|
44
|
+
|
|
45
|
+
instance.use((req, res, next) => {
|
|
46
|
+
seen.push({ url: req.url, originalUrl: req.originalUrl })
|
|
47
|
+
next()
|
|
48
|
+
})
|
|
49
|
+
instance.use('/admin', (req, res) => {
|
|
50
|
+
middlewareCalls++
|
|
51
|
+
res.statusCode = 403
|
|
52
|
+
res.end('blocked')
|
|
53
|
+
})
|
|
54
|
+
instance.get('/admin/:section', async () => ({ secret: 'classified' }))
|
|
55
|
+
|
|
56
|
+
const address = await instance.listen({ port: 0, host: '127.0.0.1' })
|
|
57
|
+
const targets = [
|
|
58
|
+
'http://localhost/admin/dashboard',
|
|
59
|
+
'HTTP://localhost/admin/dashboard',
|
|
60
|
+
'https://user:pass@example.test:443/admin/dashboard?test=%76alue',
|
|
61
|
+
'http://example.test//admin//dashboard',
|
|
62
|
+
'http://localhost/admin/dashboard;bypass'
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
for (const target of targets) {
|
|
66
|
+
const response = await requestTarget(address, target)
|
|
67
|
+
t.assert.strictEqual(response.statusCode, 403, target)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
t.assert.strictEqual(middlewareCalls, targets.length)
|
|
71
|
+
t.assert.deepStrictEqual(seen, [
|
|
72
|
+
{ url: '/admin/dashboard', originalUrl: targets[0] },
|
|
73
|
+
{ url: '/admin/dashboard', originalUrl: targets[1] },
|
|
74
|
+
{ url: '/admin/dashboard?test=value', originalUrl: targets[2] },
|
|
75
|
+
{ url: '/admin/dashboard', originalUrl: targets[3] },
|
|
76
|
+
{ url: '/admin/dashboard?bypass', originalUrl: targets[4] }
|
|
77
|
+
])
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('absolute-form targets match middleware in prefixed plugins', async t => {
|
|
81
|
+
const instance = Fastify({
|
|
82
|
+
routerOptions: {
|
|
83
|
+
ignoreDuplicateSlashes: true
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
t.after(() => instance.close())
|
|
87
|
+
|
|
88
|
+
await instance.register(expressPlugin)
|
|
89
|
+
await instance.register(async child => {
|
|
90
|
+
child.use('/admin', (req, res) => {
|
|
91
|
+
res.statusCode = 403
|
|
92
|
+
res.end('blocked')
|
|
93
|
+
})
|
|
94
|
+
child.get('/admin/:id', async () => ({ secret: 'classified' }))
|
|
95
|
+
}, { prefix: '/api' })
|
|
96
|
+
|
|
97
|
+
const address = await instance.listen({ port: 0, host: '127.0.0.1' })
|
|
98
|
+
const response = await requestTarget(address, 'http://localhost/api//admin/42')
|
|
99
|
+
|
|
100
|
+
t.assert.strictEqual(response.statusCode, 403)
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
test('absolute-form targets are resolved when duplicate slash normalization is disabled', async t => {
|
|
104
|
+
const instance = Fastify()
|
|
105
|
+
t.after(() => instance.close())
|
|
106
|
+
|
|
107
|
+
await instance.register(expressPlugin)
|
|
108
|
+
|
|
109
|
+
instance.use('/admin', (req, res) => {
|
|
110
|
+
res.statusCode = 403
|
|
111
|
+
res.end('blocked')
|
|
112
|
+
})
|
|
113
|
+
instance.get('/admin/dashboard', async () => ({ secret: 'classified' }))
|
|
114
|
+
|
|
115
|
+
const address = await instance.listen({ port: 0, host: '127.0.0.1' })
|
|
116
|
+
const response = await requestTarget(address, 'http://localhost/admin/dashboard')
|
|
117
|
+
|
|
118
|
+
t.assert.strictEqual(response.statusCode, 403)
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
test('absolute-form targets without a path resolve to root and preserve the query', async t => {
|
|
122
|
+
const instance = Fastify()
|
|
123
|
+
t.after(() => instance.close())
|
|
124
|
+
|
|
125
|
+
await instance.register(expressPlugin)
|
|
126
|
+
|
|
127
|
+
const seen = []
|
|
128
|
+
instance.use((req, res, next) => {
|
|
129
|
+
seen.push(req.url)
|
|
130
|
+
next()
|
|
131
|
+
})
|
|
132
|
+
instance.get('/', async () => ({ ok: true }))
|
|
133
|
+
instance.setNotFoundHandler(async () => ({ ok: true }))
|
|
134
|
+
|
|
135
|
+
const address = await instance.listen({ port: 0, host: '127.0.0.1' })
|
|
136
|
+
|
|
137
|
+
t.assert.strictEqual((await requestTarget(address, 'http://example.test')).statusCode, 200)
|
|
138
|
+
t.assert.strictEqual((await requestTarget(address, 'http://example.test?next=/admin&x=%76alue')).statusCode, 200)
|
|
139
|
+
t.assert.strictEqual((await requestTarget(address, '*', 'OPTIONS')).statusCode, 200)
|
|
140
|
+
t.assert.deepStrictEqual(seen, ['/', '/?next=/admin&x=value', '*'])
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
test('malformed request targets are rejected without affecting later requests', async t => {
|
|
144
|
+
const instance = Fastify({
|
|
145
|
+
routerOptions: {
|
|
146
|
+
ignoreDuplicateSlashes: true
|
|
147
|
+
}
|
|
148
|
+
})
|
|
149
|
+
t.after(() => instance.close())
|
|
150
|
+
|
|
151
|
+
await instance.register(expressPlugin, {
|
|
152
|
+
createProxyHandler: request => ({
|
|
153
|
+
get (target, property, receiver) {
|
|
154
|
+
if (property === 'url' && request.headers['x-test-url']) {
|
|
155
|
+
return request.headers['x-test-url']
|
|
156
|
+
}
|
|
157
|
+
return Reflect.get(target, property, receiver)
|
|
158
|
+
}
|
|
159
|
+
})
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
instance.use((req, res, next) => next())
|
|
163
|
+
instance.get('/admin', async () => ({ ok: true }))
|
|
164
|
+
|
|
165
|
+
const address = await instance.listen({ port: 0, host: '127.0.0.1' })
|
|
166
|
+
const malformedTargets = [
|
|
167
|
+
'http:///admin',
|
|
168
|
+
'http://example.test:invalid/admin',
|
|
169
|
+
'http://example.test#fragment/admin',
|
|
170
|
+
'/admin?value=%zz'
|
|
171
|
+
]
|
|
172
|
+
|
|
173
|
+
for (const target of malformedTargets) {
|
|
174
|
+
const response = await requestTarget(address, target)
|
|
175
|
+
t.assert.strictEqual(response.statusCode, 400, target)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
for (const target of ['http:///admin', '/admin?value=%zz']) {
|
|
179
|
+
const response = await instance.inject({
|
|
180
|
+
method: 'GET',
|
|
181
|
+
url: '/admin',
|
|
182
|
+
headers: { 'x-test-url': target }
|
|
183
|
+
})
|
|
184
|
+
t.assert.strictEqual(response.statusCode, 400, target)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const validResponse = await requestTarget(address, '/admin')
|
|
188
|
+
t.assert.strictEqual(validResponse.statusCode, 200)
|
|
189
|
+
})
|
package/test/basic.test.js
CHANGED
|
@@ -66,6 +66,28 @@ test('Should support connect style middlewares (async await after)', async t =>
|
|
|
66
66
|
t.assert.deepStrictEqual(await result.json(), { hello: 'world' })
|
|
67
67
|
})
|
|
68
68
|
|
|
69
|
+
test('Should expose Fastify cookies to Express middleware', async t => {
|
|
70
|
+
t.plan(2)
|
|
71
|
+
const fastify = Fastify()
|
|
72
|
+
t.after(() => fastify.close())
|
|
73
|
+
|
|
74
|
+
fastify.addHook('onRequest', (request, _reply, done) => {
|
|
75
|
+
request.cookies = { session: 'cookie-value' }
|
|
76
|
+
done()
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
await fastify.register(expressPlugin)
|
|
80
|
+
fastify.use((request, _response, next) => {
|
|
81
|
+
t.assert.deepStrictEqual(request.cookies, { session: 'cookie-value' })
|
|
82
|
+
next()
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
fastify.get('/', async () => ({ hello: 'world' }))
|
|
86
|
+
|
|
87
|
+
const response = await fastify.inject('/')
|
|
88
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
89
|
+
})
|
|
90
|
+
|
|
69
91
|
test('Should support per path middlewares', async t => {
|
|
70
92
|
t.plan(2)
|
|
71
93
|
const fastify = Fastify()
|