@fastify/express 4.0.6 → 4.0.7
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/dependabot.yml +42 -2
- package/README.md +6 -1
- package/index.js +26 -1
- package/package.json +3 -3
- package/test/middleware.test.js +290 -0
package/.github/dependabot.yml
CHANGED
|
@@ -2,12 +2,52 @@ version: 2
|
|
|
2
2
|
updates:
|
|
3
3
|
- package-ecosystem: "github-actions"
|
|
4
4
|
directory: "/"
|
|
5
|
+
commit-message:
|
|
6
|
+
# Prefix all commit messages with "chore: "
|
|
7
|
+
prefix: "chore"
|
|
5
8
|
schedule:
|
|
6
|
-
interval: "
|
|
9
|
+
interval: "weekly"
|
|
10
|
+
cooldown:
|
|
11
|
+
default-days: 7
|
|
12
|
+
allow:
|
|
13
|
+
- dependency-name: "*"
|
|
14
|
+
update-types:
|
|
15
|
+
- "version-update:semver-major"
|
|
7
16
|
open-pull-requests-limit: 10
|
|
8
17
|
|
|
9
18
|
- package-ecosystem: "npm"
|
|
10
19
|
directory: "/"
|
|
20
|
+
commit-message:
|
|
21
|
+
# Prefix all commit messages with "chore: "
|
|
22
|
+
prefix: "chore"
|
|
11
23
|
schedule:
|
|
12
|
-
interval: "
|
|
24
|
+
interval: "weekly"
|
|
25
|
+
cooldown:
|
|
26
|
+
default-days: 7
|
|
27
|
+
versioning-strategy: "increase-if-necessary"
|
|
28
|
+
allow:
|
|
29
|
+
- dependency-name: "*"
|
|
30
|
+
update-types:
|
|
31
|
+
- "version-update:semver-major"
|
|
32
|
+
ignore:
|
|
33
|
+
# TODO: remove ignore until neostandard support ESLint 10
|
|
34
|
+
- dependency-name: "eslint"
|
|
35
|
+
- dependency-name: "neostandard"
|
|
36
|
+
- dependency-name: "@stylistic/*"
|
|
13
37
|
open-pull-requests-limit: 10
|
|
38
|
+
groups:
|
|
39
|
+
# Production dependencies with breaking changes
|
|
40
|
+
dependencies:
|
|
41
|
+
dependency-type: "production"
|
|
42
|
+
# ESLint related dependencies
|
|
43
|
+
dev-dependencies-eslint:
|
|
44
|
+
patterns:
|
|
45
|
+
- "eslint"
|
|
46
|
+
- "neostandard"
|
|
47
|
+
- "@stylistic/*"
|
|
48
|
+
# TypeScript related dependencies
|
|
49
|
+
dev-dependencies-typescript:
|
|
50
|
+
patterns:
|
|
51
|
+
- "@types/*"
|
|
52
|
+
- "tstyche"
|
|
53
|
+
- "typescript"
|
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
[](https://www.npmjs.com/package/@fastify/express)
|
|
5
5
|
[](https://github.com/neostandard/neostandard)
|
|
6
6
|
|
|
7
|
-
This plugin adds full [Express](
|
|
7
|
+
This plugin adds full [Express](https://expressjs.com) compatibility to Fastify, it exposes the same `use` function of Express, and it allows you to use any Express middleware or application.<br/>
|
|
8
8
|
|
|
9
9
|
<table>
|
|
10
10
|
<tbody>
|
|
@@ -206,6 +206,11 @@ async function subsystem (fastify, opts) {
|
|
|
206
206
|
}
|
|
207
207
|
```
|
|
208
208
|
|
|
209
|
+
> **Note:** In prefixed plugins, `@fastify/express` rewrites string, array, and
|
|
210
|
+
> RegExp mount paths to include the prefix. RegExp paths with `^` anchors inside
|
|
211
|
+
> groups (e.g., `/(?:^\/a|^\/b)/`) are not rewritten correctly. Use array paths
|
|
212
|
+
> instead: `fastify.use(['/a', '/b'], middleware)`.
|
|
213
|
+
|
|
209
214
|
### Wrap Express req in Proxy
|
|
210
215
|
|
|
211
216
|
It is possible to wrap the Express request object in a Proxy by passing `createProxyHandler` function to generate the Proxy handler. The function will receive the Fastify request object as the first parameter.
|
package/index.js
CHANGED
|
@@ -25,9 +25,13 @@ function fastifyExpress (fastify, options, next) {
|
|
|
25
25
|
.addHook('onRegister', onRegister)
|
|
26
26
|
|
|
27
27
|
function use (path, fn) {
|
|
28
|
+
const prefix = this.prefix
|
|
28
29
|
if (typeof path === 'string') {
|
|
29
|
-
const prefix = this.prefix
|
|
30
30
|
path = prefix + (path === '/' && prefix.length > 0 ? '' : path)
|
|
31
|
+
} else if (Array.isArray(path) && path.length > 0 && typeof path[0] !== 'function') {
|
|
32
|
+
path = path.map(p => prefixMountPath(p, prefix))
|
|
33
|
+
} else if (path instanceof RegExp && prefix.length > 0) {
|
|
34
|
+
path = prefixRegExp(path, prefix)
|
|
31
35
|
}
|
|
32
36
|
this[kMiddlewares].push([path, fn])
|
|
33
37
|
if (fn == null) {
|
|
@@ -178,6 +182,27 @@ function removeDuplicateSlashes (path) {
|
|
|
178
182
|
return path.replace(/\/{2,}/g, '/')
|
|
179
183
|
}
|
|
180
184
|
|
|
185
|
+
function prefixMountPath (p, prefix) {
|
|
186
|
+
if (typeof p === 'string') {
|
|
187
|
+
return prefix + (p === '/' && prefix.length > 0 ? '' : p)
|
|
188
|
+
}
|
|
189
|
+
if (p instanceof RegExp && prefix.length > 0) {
|
|
190
|
+
return prefixRegExp(p, prefix)
|
|
191
|
+
}
|
|
192
|
+
/* c8 ignore next */
|
|
193
|
+
return p
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function prefixRegExp (re, prefix) {
|
|
197
|
+
const escapedPrefix = prefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
198
|
+
const inner = re.source.replace(/(?:^|(?<=\|))\^/g, '')
|
|
199
|
+
const hasAnchor = inner !== re.source
|
|
200
|
+
const source = hasAnchor
|
|
201
|
+
? '^' + escapedPrefix + '(?:' + inner + ')'
|
|
202
|
+
: escapedPrefix + '(?:' + re.source + ')'
|
|
203
|
+
return new RegExp(source, re.flags)
|
|
204
|
+
}
|
|
205
|
+
|
|
181
206
|
module.exports = fp(fastifyExpress, {
|
|
182
207
|
fastify: '5.x',
|
|
183
208
|
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.7",
|
|
4
4
|
"description": "Express compatibility layer for Fastify",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"use",
|
|
24
24
|
"compatibility"
|
|
25
25
|
],
|
|
26
|
-
"author": "Tomas Della Vedova - @delvedor (
|
|
26
|
+
"author": "Tomas Della Vedova - @delvedor (https://delvedor.dev)",
|
|
27
27
|
"contributors": [
|
|
28
28
|
{
|
|
29
29
|
"name": "Matteo Collina",
|
|
@@ -76,7 +76,7 @@
|
|
|
76
76
|
},
|
|
77
77
|
"dependencies": {
|
|
78
78
|
"express": "^5.2.1",
|
|
79
|
-
"fastify-plugin": "^
|
|
79
|
+
"fastify-plugin": "^6.0.0"
|
|
80
80
|
},
|
|
81
81
|
"publishConfig": {
|
|
82
82
|
"access": "public"
|
package/test/middleware.test.js
CHANGED
|
@@ -267,6 +267,296 @@ test('should not double-prefix inherited middleware paths in child scopes', asyn
|
|
|
267
267
|
t.assert.deepStrictEqual(await childWithAuth.json(), { data: 'child-secret' })
|
|
268
268
|
})
|
|
269
269
|
|
|
270
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-3wf5-7852-vcfq
|
|
271
|
+
test('array mount paths are rewritten with prefix', async t => {
|
|
272
|
+
t.plan(4)
|
|
273
|
+
|
|
274
|
+
const instance = fastify()
|
|
275
|
+
t.after(() => instance.close())
|
|
276
|
+
|
|
277
|
+
await instance.register(expressPlugin)
|
|
278
|
+
|
|
279
|
+
await instance.register(async function (admin) {
|
|
280
|
+
admin.use(['/private', '/settings'], function (req, res, next) {
|
|
281
|
+
if (req.headers.authorization !== 'Bearer secret') {
|
|
282
|
+
res.statusCode = 403
|
|
283
|
+
res.end('blocked by middleware')
|
|
284
|
+
return
|
|
285
|
+
}
|
|
286
|
+
next()
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
admin.get('/private', async () => {
|
|
290
|
+
return { secret: 'admin-data' }
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
admin.get('/settings', async () => {
|
|
294
|
+
return { settings: true }
|
|
295
|
+
})
|
|
296
|
+
}, { prefix: '/admin' })
|
|
297
|
+
|
|
298
|
+
const address = await instance.listen({ port: 0 })
|
|
299
|
+
|
|
300
|
+
const privateNoAuth = await fetch(address + '/admin/private')
|
|
301
|
+
t.assert.deepStrictEqual(privateNoAuth.status, 403)
|
|
302
|
+
|
|
303
|
+
const settingsNoAuth = await fetch(address + '/admin/settings')
|
|
304
|
+
t.assert.deepStrictEqual(settingsNoAuth.status, 403)
|
|
305
|
+
|
|
306
|
+
const privateWithAuth = await fetch(address + '/admin/private', {
|
|
307
|
+
headers: { authorization: 'Bearer secret' }
|
|
308
|
+
})
|
|
309
|
+
t.assert.deepStrictEqual(privateWithAuth.status, 200)
|
|
310
|
+
t.assert.deepStrictEqual(await privateWithAuth.json(), { secret: 'admin-data' })
|
|
311
|
+
})
|
|
312
|
+
|
|
313
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-3wf5-7852-vcfq
|
|
314
|
+
test('regex mount paths are rewritten with prefix', async t => {
|
|
315
|
+
t.plan(3)
|
|
316
|
+
|
|
317
|
+
const instance = fastify()
|
|
318
|
+
t.after(() => instance.close())
|
|
319
|
+
|
|
320
|
+
await instance.register(expressPlugin)
|
|
321
|
+
|
|
322
|
+
await instance.register(async function (admin) {
|
|
323
|
+
admin.use(/^\/private/, function (req, res, next) {
|
|
324
|
+
if (req.headers.authorization !== 'Bearer secret') {
|
|
325
|
+
res.statusCode = 403
|
|
326
|
+
res.end('blocked by middleware')
|
|
327
|
+
return
|
|
328
|
+
}
|
|
329
|
+
next()
|
|
330
|
+
})
|
|
331
|
+
|
|
332
|
+
admin.get('/private', async () => {
|
|
333
|
+
return { secret: 'admin-data' }
|
|
334
|
+
})
|
|
335
|
+
}, { prefix: '/admin' })
|
|
336
|
+
|
|
337
|
+
const address = await instance.listen({ port: 0 })
|
|
338
|
+
|
|
339
|
+
const noAuth = await fetch(address + '/admin/private')
|
|
340
|
+
t.assert.deepStrictEqual(noAuth.status, 403)
|
|
341
|
+
|
|
342
|
+
const withAuth = await fetch(address + '/admin/private', {
|
|
343
|
+
headers: { authorization: 'Bearer secret' }
|
|
344
|
+
})
|
|
345
|
+
t.assert.deepStrictEqual(withAuth.status, 200)
|
|
346
|
+
t.assert.deepStrictEqual(await withAuth.json(), { secret: 'admin-data' })
|
|
347
|
+
})
|
|
348
|
+
|
|
349
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-3wf5-7852-vcfq
|
|
350
|
+
test('regex without anchor is rewritten with prefix', async t => {
|
|
351
|
+
t.plan(3)
|
|
352
|
+
|
|
353
|
+
const instance = fastify()
|
|
354
|
+
t.after(() => instance.close())
|
|
355
|
+
|
|
356
|
+
await instance.register(expressPlugin)
|
|
357
|
+
|
|
358
|
+
await instance.register(async function (admin) {
|
|
359
|
+
admin.use(/\/private/, function (req, res, next) {
|
|
360
|
+
if (req.headers.authorization !== 'Bearer secret') {
|
|
361
|
+
res.statusCode = 403
|
|
362
|
+
res.end('blocked by middleware')
|
|
363
|
+
return
|
|
364
|
+
}
|
|
365
|
+
next()
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
admin.get('/private', async () => {
|
|
369
|
+
return { secret: 'admin-data' }
|
|
370
|
+
})
|
|
371
|
+
}, { prefix: '/admin' })
|
|
372
|
+
|
|
373
|
+
const address = await instance.listen({ port: 0 })
|
|
374
|
+
|
|
375
|
+
const noAuth = await fetch(address + '/admin/private')
|
|
376
|
+
t.assert.deepStrictEqual(noAuth.status, 403)
|
|
377
|
+
|
|
378
|
+
const withAuth = await fetch(address + '/admin/private', {
|
|
379
|
+
headers: { authorization: 'Bearer secret' }
|
|
380
|
+
})
|
|
381
|
+
t.assert.deepStrictEqual(withAuth.status, 200)
|
|
382
|
+
t.assert.deepStrictEqual(await withAuth.json(), { secret: 'admin-data' })
|
|
383
|
+
})
|
|
384
|
+
|
|
385
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-3wf5-7852-vcfq
|
|
386
|
+
test('array with root slash is handled correctly with prefix', async t => {
|
|
387
|
+
t.plan(2)
|
|
388
|
+
|
|
389
|
+
const instance = fastify()
|
|
390
|
+
t.after(() => instance.close())
|
|
391
|
+
|
|
392
|
+
await instance.register(expressPlugin)
|
|
393
|
+
|
|
394
|
+
let middlewareCalled = false
|
|
395
|
+
|
|
396
|
+
await instance.register(async function (child) {
|
|
397
|
+
child.use(['/', '/health'], function (_req, _res, next) {
|
|
398
|
+
middlewareCalled = true
|
|
399
|
+
next()
|
|
400
|
+
})
|
|
401
|
+
|
|
402
|
+
child.get('/health', async () => {
|
|
403
|
+
return { ok: true }
|
|
404
|
+
})
|
|
405
|
+
}, { prefix: '/api' })
|
|
406
|
+
|
|
407
|
+
const address = await instance.listen({ port: 0 })
|
|
408
|
+
|
|
409
|
+
const result = await fetch(address + '/api/health')
|
|
410
|
+
t.assert.deepStrictEqual(result.status, 200)
|
|
411
|
+
t.assert.ok(middlewareCalled)
|
|
412
|
+
})
|
|
413
|
+
|
|
414
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-3wf5-7852-vcfq
|
|
415
|
+
test('array of middleware functions (no path) works in prefixed plugin', async t => {
|
|
416
|
+
t.plan(2)
|
|
417
|
+
|
|
418
|
+
const instance = fastify()
|
|
419
|
+
t.after(() => instance.close())
|
|
420
|
+
|
|
421
|
+
await instance.register(expressPlugin)
|
|
422
|
+
|
|
423
|
+
let middlewareCalled = false
|
|
424
|
+
|
|
425
|
+
await instance.register(async function (child) {
|
|
426
|
+
child.use([function (_req, _res, next) {
|
|
427
|
+
middlewareCalled = true
|
|
428
|
+
next()
|
|
429
|
+
}])
|
|
430
|
+
|
|
431
|
+
child.get('/data', async () => {
|
|
432
|
+
return { ok: true }
|
|
433
|
+
})
|
|
434
|
+
}, { prefix: '/api' })
|
|
435
|
+
|
|
436
|
+
const address = await instance.listen({ port: 0 })
|
|
437
|
+
|
|
438
|
+
const result = await fetch(address + '/api/data')
|
|
439
|
+
t.assert.deepStrictEqual(result.status, 200)
|
|
440
|
+
t.assert.ok(middlewareCalled)
|
|
441
|
+
})
|
|
442
|
+
|
|
443
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-3wf5-7852-vcfq
|
|
444
|
+
test('regex with alternation is fully rewritten with prefix', async t => {
|
|
445
|
+
t.plan(4)
|
|
446
|
+
|
|
447
|
+
const instance = fastify()
|
|
448
|
+
t.after(() => instance.close())
|
|
449
|
+
|
|
450
|
+
await instance.register(expressPlugin)
|
|
451
|
+
|
|
452
|
+
await instance.register(async function (admin) {
|
|
453
|
+
admin.use(/^\/private|^\/settings/, function (req, res, next) {
|
|
454
|
+
if (req.headers.authorization !== 'Bearer secret') {
|
|
455
|
+
res.statusCode = 403
|
|
456
|
+
res.end('blocked by middleware')
|
|
457
|
+
return
|
|
458
|
+
}
|
|
459
|
+
next()
|
|
460
|
+
})
|
|
461
|
+
|
|
462
|
+
admin.get('/private', async () => {
|
|
463
|
+
return { secret: 'admin-data' }
|
|
464
|
+
})
|
|
465
|
+
|
|
466
|
+
admin.get('/settings', async () => {
|
|
467
|
+
return { settings: true }
|
|
468
|
+
})
|
|
469
|
+
}, { prefix: '/admin' })
|
|
470
|
+
|
|
471
|
+
const address = await instance.listen({ port: 0 })
|
|
472
|
+
|
|
473
|
+
const privateNoAuth = await fetch(address + '/admin/private')
|
|
474
|
+
t.assert.deepStrictEqual(privateNoAuth.status, 403)
|
|
475
|
+
|
|
476
|
+
const settingsNoAuth = await fetch(address + '/admin/settings')
|
|
477
|
+
t.assert.deepStrictEqual(settingsNoAuth.status, 403)
|
|
478
|
+
|
|
479
|
+
const privateWithAuth = await fetch(address + '/admin/private', {
|
|
480
|
+
headers: { authorization: 'Bearer secret' }
|
|
481
|
+
})
|
|
482
|
+
t.assert.deepStrictEqual(privateWithAuth.status, 200)
|
|
483
|
+
t.assert.deepStrictEqual(await privateWithAuth.json(), { secret: 'admin-data' })
|
|
484
|
+
})
|
|
485
|
+
|
|
486
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-3wf5-7852-vcfq
|
|
487
|
+
test('mixed array of strings and regexes is rewritten with prefix', async t => {
|
|
488
|
+
t.plan(4)
|
|
489
|
+
|
|
490
|
+
const instance = fastify()
|
|
491
|
+
t.after(() => instance.close())
|
|
492
|
+
|
|
493
|
+
await instance.register(expressPlugin)
|
|
494
|
+
|
|
495
|
+
await instance.register(async function (admin) {
|
|
496
|
+
admin.use(['/private', /^\/settings/], function (req, res, next) {
|
|
497
|
+
if (req.headers.authorization !== 'Bearer secret') {
|
|
498
|
+
res.statusCode = 403
|
|
499
|
+
res.end('blocked by middleware')
|
|
500
|
+
return
|
|
501
|
+
}
|
|
502
|
+
next()
|
|
503
|
+
})
|
|
504
|
+
|
|
505
|
+
admin.get('/private', async () => {
|
|
506
|
+
return { secret: 'admin-data' }
|
|
507
|
+
})
|
|
508
|
+
|
|
509
|
+
admin.get('/settings', async () => {
|
|
510
|
+
return { settings: true }
|
|
511
|
+
})
|
|
512
|
+
}, { prefix: '/admin' })
|
|
513
|
+
|
|
514
|
+
const address = await instance.listen({ port: 0 })
|
|
515
|
+
|
|
516
|
+
const privateNoAuth = await fetch(address + '/admin/private')
|
|
517
|
+
t.assert.deepStrictEqual(privateNoAuth.status, 403)
|
|
518
|
+
|
|
519
|
+
const settingsNoAuth = await fetch(address + '/admin/settings')
|
|
520
|
+
t.assert.deepStrictEqual(settingsNoAuth.status, 403)
|
|
521
|
+
|
|
522
|
+
const privateWithAuth = await fetch(address + '/admin/private', {
|
|
523
|
+
headers: { authorization: 'Bearer secret' }
|
|
524
|
+
})
|
|
525
|
+
t.assert.deepStrictEqual(privateWithAuth.status, 200)
|
|
526
|
+
t.assert.deepStrictEqual(await privateWithAuth.json(), { secret: 'admin-data' })
|
|
527
|
+
})
|
|
528
|
+
|
|
529
|
+
test('regex with grouped anchors does not match in prefixed plugins (known limitation)', async t => {
|
|
530
|
+
t.plan(1)
|
|
531
|
+
|
|
532
|
+
const instance = fastify()
|
|
533
|
+
t.after(() => instance.close())
|
|
534
|
+
|
|
535
|
+
await instance.register(expressPlugin)
|
|
536
|
+
|
|
537
|
+
let middlewareCalled = false
|
|
538
|
+
|
|
539
|
+
await instance.register(async function (child) {
|
|
540
|
+
// (?:^\/private|^\/settings) -- the ^ inside the group is not stripped
|
|
541
|
+
// by prefixRegExp, so the rewritten regex still anchors to position 0
|
|
542
|
+
// of the full URL and fails to match /admin/private
|
|
543
|
+
child.use(/(?:^\/private|^\/settings)/, function (_req, _res, next) {
|
|
544
|
+
middlewareCalled = true
|
|
545
|
+
next()
|
|
546
|
+
})
|
|
547
|
+
|
|
548
|
+
child.get('/private', async () => {
|
|
549
|
+
return { ok: true }
|
|
550
|
+
})
|
|
551
|
+
}, { prefix: '/admin' })
|
|
552
|
+
|
|
553
|
+
const address = await instance.listen({ port: 0 })
|
|
554
|
+
|
|
555
|
+
await fetch(address + '/admin/private')
|
|
556
|
+
// Middleware is NOT called -- this is expected behavior, not a bug
|
|
557
|
+
t.assert.ok(!middlewareCalled)
|
|
558
|
+
})
|
|
559
|
+
|
|
270
560
|
test('res.end should block middleware execution', async t => {
|
|
271
561
|
t.plan(4)
|
|
272
562
|
|