@fastify/express 4.0.8 → 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/index.js +55 -4
- package/package.json +1 -1
- package/test/absolute-form-url.test.js +189 -0
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
|
|
@@ -138,12 +144,57 @@ function normalizeUrl (url, options) {
|
|
|
138
144
|
useSemicolonDelimiter
|
|
139
145
|
} = options
|
|
140
146
|
|
|
141
|
-
|
|
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)
|
|
142
161
|
const normalizedPath = ignoreDuplicateSlashes ? removeDuplicateSlashes(path) : path
|
|
143
162
|
|
|
144
163
|
return normalizedPath + query
|
|
145
164
|
}
|
|
146
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
|
+
|
|
147
198
|
function splitPathAndQuery (url, useSemicolonDelimiter) {
|
|
148
199
|
const queryIndex = url.indexOf('?')
|
|
149
200
|
|
package/package.json
CHANGED
|
@@ -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
|
+
})
|