@fastify/express 4.0.8 → 4.0.10
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/README.md +15 -3
- package/index.js +417 -43
- package/package.json +4 -2
- package/test/absolute-form-url.test.js +237 -0
- package/test/enhance-request.test.js +57 -6
- package/test/form-data.test.js +3 -3
- package/test/middleware.test.js +1099 -35
- package/test/path-normalization.test.js +158 -0
- package/test/url-encoding.test.js +144 -0
package/test/middleware.test.js
CHANGED
|
@@ -526,37 +526,6 @@ test('mixed array of strings and regexes is rewritten with prefix', async t => {
|
|
|
526
526
|
t.assert.deepStrictEqual(await privateWithAuth.json(), { secret: 'admin-data' })
|
|
527
527
|
})
|
|
528
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
|
-
|
|
560
529
|
test('res.end should block middleware execution', async t => {
|
|
561
530
|
t.plan(4)
|
|
562
531
|
|
|
@@ -709,7 +678,8 @@ test('middlewares for encoded paths', async t => {
|
|
|
709
678
|
})
|
|
710
679
|
|
|
711
680
|
await t.test('does not double decode the url', async (t) => {
|
|
712
|
-
|
|
681
|
+
// A literal `%` in the path stays encoded, as find-my-way keeps it.
|
|
682
|
+
await checkEncodedPath('/%65ncoded', '/%2565ncoded', t, '/%2565ncoded')
|
|
713
683
|
})
|
|
714
684
|
|
|
715
685
|
await t.test('handle the decoding for express handlers', async (t) => {
|
|
@@ -756,20 +726,297 @@ test('middlewares for encoded paths', async t => {
|
|
|
756
726
|
})
|
|
757
727
|
})
|
|
758
728
|
|
|
759
|
-
|
|
729
|
+
test('reject ambiguous blank parameters for dynamic middleware mounts', async t => {
|
|
730
|
+
const instance = fastify()
|
|
731
|
+
t.after(() => instance.close())
|
|
732
|
+
|
|
733
|
+
await instance.register(expressPlugin)
|
|
734
|
+
|
|
735
|
+
const middlewareCalls = []
|
|
736
|
+
function block (name) {
|
|
737
|
+
return function (_req, res) {
|
|
738
|
+
middlewareCalls.push(name)
|
|
739
|
+
res.statusCode = 403
|
|
740
|
+
res.end('blocked')
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
instance.use('/tenant/:tenantId', block('tenant'))
|
|
745
|
+
instance.use('/nested/:one/:two', block('nested'))
|
|
746
|
+
instance.use('/files/*splat', block('files'))
|
|
747
|
+
instance.use(['/array/:id', /^\/regex/], block('array'))
|
|
748
|
+
instance.use('/group{/:id}/private', block('group'))
|
|
749
|
+
instance.use('/static-parent', block('static-parent'))
|
|
750
|
+
|
|
751
|
+
instance.get('/tenant/:tenantId', async request => request.params)
|
|
752
|
+
instance.get('/nested/:one/:two', async request => request.params)
|
|
753
|
+
instance.get('/files/*', async request => request.params)
|
|
754
|
+
instance.get('/array/:id', async request => request.params)
|
|
755
|
+
instance.get('/group/:id/private', async request => request.params)
|
|
756
|
+
instance.get('/static-parent/:id', async request => request.params)
|
|
757
|
+
instance.get('/unrelated/:id', async request => request.params)
|
|
758
|
+
|
|
759
|
+
await instance.register(async child => {
|
|
760
|
+
child.use('/child/:id', block('child'))
|
|
761
|
+
child.get('/child/:id', async request => request.params)
|
|
762
|
+
}, { prefix: '/api' })
|
|
763
|
+
|
|
764
|
+
const address = await instance.listen({ port: 0 })
|
|
765
|
+
|
|
766
|
+
const nonEmpty = await fetch(address + '/tenant/acme')
|
|
767
|
+
t.assert.deepStrictEqual(nonEmpty.status, 403)
|
|
768
|
+
t.assert.deepStrictEqual(middlewareCalls, ['tenant'])
|
|
769
|
+
|
|
770
|
+
middlewareCalls.length = 0
|
|
771
|
+
const interiorWhitespace = await fetch(address + '/nested/%20/value')
|
|
772
|
+
t.assert.deepStrictEqual(interiorWhitespace.status, 403)
|
|
773
|
+
t.assert.deepStrictEqual(middlewareCalls, ['nested'])
|
|
774
|
+
|
|
775
|
+
// Whitespace stays percent-encoded in the URL Express matches, so a
|
|
776
|
+
// whitespace-only value is not blank to it and the middleware runs.
|
|
777
|
+
for (const path of ['/tenant/%20', '/tenant/%09']) {
|
|
778
|
+
middlewareCalls.length = 0
|
|
779
|
+
const response = await fetch(address + path)
|
|
780
|
+
t.assert.deepStrictEqual(response.status, 403, path)
|
|
781
|
+
t.assert.deepStrictEqual(middlewareCalls, ['tenant'], path)
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
for (const path of [
|
|
785
|
+
'/tenant/',
|
|
786
|
+
'/tenant/?query=value',
|
|
787
|
+
'/nested//value',
|
|
788
|
+
'/nested/value/',
|
|
789
|
+
'/files/',
|
|
790
|
+
'/array/',
|
|
791
|
+
'/group//private',
|
|
792
|
+
'/api/child/'
|
|
793
|
+
]) {
|
|
794
|
+
middlewareCalls.length = 0
|
|
795
|
+
const response = await fetch(address + path)
|
|
796
|
+
t.assert.deepStrictEqual(response.status, 400, path)
|
|
797
|
+
t.assert.deepStrictEqual(middlewareCalls, [], path)
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
const staticParent = await fetch(address + '/static-parent/')
|
|
801
|
+
t.assert.deepStrictEqual(staticParent.status, 403)
|
|
802
|
+
t.assert.deepStrictEqual(middlewareCalls, ['static-parent'])
|
|
803
|
+
|
|
804
|
+
middlewareCalls.length = 0
|
|
805
|
+
const unrelated = await fetch(address + '/unrelated/')
|
|
806
|
+
t.assert.deepStrictEqual(unrelated.status, 200)
|
|
807
|
+
t.assert.deepStrictEqual(await unrelated.json(), { id: '' })
|
|
808
|
+
t.assert.deepStrictEqual(middlewareCalls, [])
|
|
809
|
+
})
|
|
810
|
+
|
|
811
|
+
test('allow blank parameters when no dynamic middleware mount applies', async t => {
|
|
812
|
+
const instance = fastify()
|
|
813
|
+
t.after(() => instance.close())
|
|
814
|
+
|
|
815
|
+
await instance.register(expressPlugin)
|
|
816
|
+
|
|
817
|
+
let middlewareCalls = 0
|
|
818
|
+
instance.use(function (_req, _res, next) {
|
|
819
|
+
middlewareCalls++
|
|
820
|
+
next()
|
|
821
|
+
})
|
|
822
|
+
instance.get('/open/:id', async request => request.params)
|
|
823
|
+
|
|
824
|
+
const address = await instance.listen({ port: 0 })
|
|
825
|
+
const response = await fetch(address + '/open/')
|
|
826
|
+
|
|
827
|
+
t.assert.deepStrictEqual(response.status, 200)
|
|
828
|
+
t.assert.deepStrictEqual(await response.json(), { id: '' })
|
|
829
|
+
t.assert.deepStrictEqual(middlewareCalls, 1)
|
|
830
|
+
})
|
|
831
|
+
|
|
832
|
+
test('reject blank segments when the route shape differs from the dynamic mount', async t => {
|
|
833
|
+
const instance = fastify()
|
|
834
|
+
t.after(() => instance.close())
|
|
835
|
+
|
|
836
|
+
await instance.register(expressPlugin)
|
|
837
|
+
|
|
838
|
+
const middlewareCalls = []
|
|
839
|
+
function block (name) {
|
|
840
|
+
return function (_req, res) {
|
|
841
|
+
middlewareCalls.push(name)
|
|
842
|
+
res.statusCode = 403
|
|
843
|
+
res.end('blocked')
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
// Auth is mounted on a dynamic parameter, but the serving route is a wildcard
|
|
848
|
+
// or catch-all, so the blank segment is captured as a non-blank route value.
|
|
849
|
+
instance.use('/tenant/:tenantId', block('tenant'))
|
|
850
|
+
instance.use('/files/:id', block('files'))
|
|
851
|
+
|
|
852
|
+
instance.get('/tenant/*', async request => request.params)
|
|
853
|
+
instance.get('/*', async request => request.params)
|
|
854
|
+
|
|
855
|
+
const address = await instance.listen({ port: 0 })
|
|
856
|
+
|
|
857
|
+
for (const path of [
|
|
858
|
+
'/tenant//private',
|
|
859
|
+
'/tenant//',
|
|
860
|
+
'/files/'
|
|
861
|
+
]) {
|
|
862
|
+
middlewareCalls.length = 0
|
|
863
|
+
const response = await fetch(address + path)
|
|
864
|
+
t.assert.deepStrictEqual(response.status, 400, path)
|
|
865
|
+
t.assert.deepStrictEqual(middlewareCalls, [], path)
|
|
866
|
+
}
|
|
867
|
+
})
|
|
868
|
+
|
|
869
|
+
test('reject blank segments for dynamic middleware mounts with a trailing slash', async t => {
|
|
870
|
+
const instance = fastify()
|
|
871
|
+
t.after(() => instance.close())
|
|
872
|
+
|
|
873
|
+
await instance.register(expressPlugin)
|
|
874
|
+
|
|
875
|
+
const middlewareCalls = []
|
|
876
|
+
// Express strips the trailing slash from the mount before matching, so a
|
|
877
|
+
// blank segment must still be rejected.
|
|
878
|
+
instance.use('/tenant/:tenantId/', function (_req, res) {
|
|
879
|
+
middlewareCalls.push('tenant')
|
|
880
|
+
res.statusCode = 403
|
|
881
|
+
res.end('blocked')
|
|
882
|
+
})
|
|
883
|
+
instance.get('/tenant/:tenantId', async request => request.params)
|
|
884
|
+
|
|
885
|
+
const address = await instance.listen({ port: 0 })
|
|
886
|
+
const response = await fetch(address + '/tenant/')
|
|
887
|
+
|
|
888
|
+
t.assert.deepStrictEqual(response.status, 400)
|
|
889
|
+
t.assert.deepStrictEqual(middlewareCalls, [])
|
|
890
|
+
})
|
|
891
|
+
|
|
892
|
+
test('reject blank segments for RegExp dynamic middleware mounts', async t => {
|
|
893
|
+
const instance = fastify()
|
|
894
|
+
t.after(() => instance.close())
|
|
895
|
+
|
|
896
|
+
await instance.register(expressPlugin)
|
|
897
|
+
|
|
898
|
+
const middlewareCalls = []
|
|
899
|
+
// A RegExp mount that requires a non-empty segment is skipped by Express for
|
|
900
|
+
// a blank segment, so the plugin must still reject it.
|
|
901
|
+
instance.use(/^\/tenant\/[^/]+/, function (_req, res) {
|
|
902
|
+
middlewareCalls.push('tenant')
|
|
903
|
+
res.statusCode = 403
|
|
904
|
+
res.end('blocked')
|
|
905
|
+
})
|
|
906
|
+
instance.get('/tenant/:tenantId', async request => request.params)
|
|
907
|
+
|
|
908
|
+
const address = await instance.listen({ port: 0 })
|
|
909
|
+
const response = await fetch(address + '/tenant/')
|
|
910
|
+
|
|
911
|
+
t.assert.deepStrictEqual(response.status, 400)
|
|
912
|
+
t.assert.deepStrictEqual(middlewareCalls, [])
|
|
913
|
+
})
|
|
914
|
+
|
|
915
|
+
test('does not backtrack on dynamic mounts with multiple parameters in one segment', async t => {
|
|
916
|
+
const instance = fastify()
|
|
917
|
+
t.after(() => instance.close())
|
|
918
|
+
|
|
919
|
+
await instance.register(expressPlugin)
|
|
920
|
+
|
|
921
|
+
instance.use('/report/:a-:b-:c-:d-:e-:f-:g-:h/export', function (_req, res) {
|
|
922
|
+
res.statusCode = 403
|
|
923
|
+
res.end('blocked')
|
|
924
|
+
})
|
|
925
|
+
instance.get('/report/:a-:b-:c-:d-:e-:f-:g-:h/export', async request => request.params)
|
|
926
|
+
instance.get('/report/:x/:y', async request => request.params)
|
|
927
|
+
|
|
928
|
+
const address = await instance.listen({ port: 0 })
|
|
929
|
+
|
|
930
|
+
// A blank trailing parameter forces the ambiguity check to evaluate the
|
|
931
|
+
// permissive matcher against a long single segment.
|
|
932
|
+
const start = process.hrtime.bigint()
|
|
933
|
+
const response = await fetch(address + '/report/' + '-'.repeat(64) + '/')
|
|
934
|
+
const elapsedMs = Number(process.hrtime.bigint() - start) / 1e6
|
|
935
|
+
|
|
936
|
+
t.assert.ok(response.status === 200 || response.status === 400)
|
|
937
|
+
t.assert.ok(elapsedMs < 1000, `matcher took ${elapsedMs.toFixed(0)}ms`)
|
|
938
|
+
})
|
|
939
|
+
|
|
940
|
+
test('reject blank wildcards followed by a literal suffix in dynamic middleware mounts', async t => {
|
|
941
|
+
const instance = fastify()
|
|
942
|
+
t.after(() => instance.close())
|
|
943
|
+
|
|
944
|
+
await instance.register(expressPlugin)
|
|
945
|
+
|
|
946
|
+
const middlewareCalls = []
|
|
947
|
+
// The wildcard is followed by a non-slash literal, so the permissive matcher
|
|
948
|
+
// excludes that delimiter instead of using `.*`.
|
|
949
|
+
instance.use('/files/*splat.txt', function (_req, res) {
|
|
950
|
+
middlewareCalls.push('files')
|
|
951
|
+
res.statusCode = 403
|
|
952
|
+
res.end('blocked')
|
|
953
|
+
})
|
|
954
|
+
instance.get('/files/*', async request => request.params)
|
|
955
|
+
|
|
956
|
+
const address = await instance.listen({ port: 0 })
|
|
957
|
+
|
|
958
|
+
const nonEmpty = await fetch(address + '/files/report.txt')
|
|
959
|
+
t.assert.deepStrictEqual(nonEmpty.status, 403)
|
|
960
|
+
t.assert.deepStrictEqual(middlewareCalls, ['files'])
|
|
961
|
+
|
|
962
|
+
middlewareCalls.length = 0
|
|
963
|
+
const blank = await fetch(address + '/files/.txt')
|
|
964
|
+
t.assert.deepStrictEqual(blank.status, 400)
|
|
965
|
+
t.assert.deepStrictEqual(middlewareCalls, [])
|
|
966
|
+
})
|
|
967
|
+
|
|
968
|
+
test('static routes next to dynamic middleware mounts are not rejected', async t => {
|
|
969
|
+
const instance = fastify()
|
|
970
|
+
t.after(() => instance.close())
|
|
971
|
+
|
|
972
|
+
await instance.register(expressPlugin)
|
|
973
|
+
|
|
974
|
+
let middlewareCalls = 0
|
|
975
|
+
function block (_req, res) {
|
|
976
|
+
middlewareCalls++
|
|
977
|
+
res.statusCode = 403
|
|
978
|
+
res.end('blocked')
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
// None of these mounts covers the static route next to it, in Express or here.
|
|
982
|
+
instance.use('/users/:id', block)
|
|
983
|
+
instance.use('/:lang/docs', block)
|
|
984
|
+
instance.use(/^\/admin\/.+/, block)
|
|
985
|
+
instance.use('/files/*path', block)
|
|
986
|
+
instance.get('/users/', async () => ({ list: true }))
|
|
987
|
+
instance.get('/', async () => ({ home: true }))
|
|
988
|
+
instance.get('/admin/', async () => ({ index: true }))
|
|
989
|
+
instance.get('/files/', async () => ({ listing: true }))
|
|
990
|
+
|
|
991
|
+
await instance.register(async function (users) {
|
|
992
|
+
users.use('/:id', block)
|
|
993
|
+
users.get('/', async () => ({ list: true }))
|
|
994
|
+
}, { prefix: '/members' })
|
|
995
|
+
|
|
996
|
+
for (const path of ['/users/', '/', '/admin/', '/files/', '/members', '/members/']) {
|
|
997
|
+
const response = await instance.inject(path)
|
|
998
|
+
t.assert.deepStrictEqual(response.statusCode, 200, path)
|
|
999
|
+
}
|
|
1000
|
+
t.assert.deepStrictEqual(middlewareCalls, 0)
|
|
1001
|
+
|
|
1002
|
+
// Unmatched requests are plain 404s.
|
|
1003
|
+
t.assert.deepStrictEqual((await instance.inject('/missing/')).statusCode, 404)
|
|
1004
|
+
})
|
|
1005
|
+
|
|
1006
|
+
async function checkEncodedPath (routeUrl, requestUrl, t, expressUrl = routeUrl) {
|
|
760
1007
|
t.plan(6)
|
|
761
1008
|
|
|
762
1009
|
const instance = fastify()
|
|
763
1010
|
t.after(() => instance.close())
|
|
764
1011
|
|
|
765
1012
|
instance.addHook('onSend', async function hook (request, reply, payload) {
|
|
766
|
-
t.assert.deepStrictEqual(request.raw.url,
|
|
1013
|
+
t.assert.deepStrictEqual(request.raw.url, expressUrl)
|
|
767
1014
|
t.assert.deepStrictEqual(request.raw.originalUrl, requestUrl)
|
|
768
1015
|
return payload
|
|
769
1016
|
})
|
|
770
1017
|
|
|
771
1018
|
instance.addHook('onResponse', async function hook (request, reply) {
|
|
772
|
-
t.assert.deepStrictEqual(request.raw.url,
|
|
1019
|
+
t.assert.deepStrictEqual(request.raw.url, expressUrl)
|
|
773
1020
|
t.assert.deepStrictEqual(request.raw.originalUrl, requestUrl)
|
|
774
1021
|
})
|
|
775
1022
|
|
|
@@ -793,3 +1040,820 @@ async function checkEncodedPath (routeUrl, requestUrl, t) {
|
|
|
793
1040
|
t.assert.ok(response.ok)
|
|
794
1041
|
t.assert.deepStrictEqual(body, { slashed: true })
|
|
795
1042
|
}
|
|
1043
|
+
|
|
1044
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-84w6-xrf3-f8q8
|
|
1045
|
+
// A parameterized prefix must not be treated as literal text when rewriting a
|
|
1046
|
+
// RegExp mount path, otherwise the middleware is skipped and auth is bypassed.
|
|
1047
|
+
test('regex mount paths are rewritten with a parameterized prefix', async t => {
|
|
1048
|
+
t.plan(4)
|
|
1049
|
+
|
|
1050
|
+
const instance = fastify()
|
|
1051
|
+
t.after(() => instance.close())
|
|
1052
|
+
|
|
1053
|
+
await instance.register(expressPlugin)
|
|
1054
|
+
|
|
1055
|
+
await instance.register(async function (tenant) {
|
|
1056
|
+
tenant.use(/^\/private/, function (req, res, next) {
|
|
1057
|
+
if (req.headers.authorization !== 'Bearer secret') {
|
|
1058
|
+
res.statusCode = 403
|
|
1059
|
+
res.end('blocked by middleware')
|
|
1060
|
+
return
|
|
1061
|
+
}
|
|
1062
|
+
next()
|
|
1063
|
+
})
|
|
1064
|
+
|
|
1065
|
+
tenant.get('/private', async () => {
|
|
1066
|
+
return { secret: 'tenant-data' }
|
|
1067
|
+
})
|
|
1068
|
+
}, { prefix: '/tenant/:tenantId' })
|
|
1069
|
+
|
|
1070
|
+
const address = await instance.listen({ port: 0 })
|
|
1071
|
+
|
|
1072
|
+
// A non-empty parameter value must reach the middleware.
|
|
1073
|
+
const noAuth = await fetch(address + '/tenant/acme/private')
|
|
1074
|
+
t.assert.deepStrictEqual(noAuth.status, 403)
|
|
1075
|
+
|
|
1076
|
+
// An empty parameter is still routed by Fastify, so the middleware must run.
|
|
1077
|
+
const emptyParam = await fetch(address + '/tenant//private')
|
|
1078
|
+
t.assert.deepStrictEqual(emptyParam.status, 403)
|
|
1079
|
+
|
|
1080
|
+
const withAuth = await fetch(address + '/tenant/acme/private', {
|
|
1081
|
+
headers: { authorization: 'Bearer secret' }
|
|
1082
|
+
})
|
|
1083
|
+
t.assert.deepStrictEqual(withAuth.status, 200)
|
|
1084
|
+
t.assert.deepStrictEqual(await withAuth.json(), { secret: 'tenant-data' })
|
|
1085
|
+
})
|
|
1086
|
+
|
|
1087
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-84w6-xrf3-f8q8
|
|
1088
|
+
test('regex without anchor is rewritten with a parameterized prefix', async t => {
|
|
1089
|
+
t.plan(3)
|
|
1090
|
+
|
|
1091
|
+
const instance = fastify()
|
|
1092
|
+
t.after(() => instance.close())
|
|
1093
|
+
|
|
1094
|
+
await instance.register(expressPlugin)
|
|
1095
|
+
|
|
1096
|
+
await instance.register(async function (tenant) {
|
|
1097
|
+
tenant.use(/\/private/, function (req, res, next) {
|
|
1098
|
+
if (req.headers.authorization !== 'Bearer secret') {
|
|
1099
|
+
res.statusCode = 403
|
|
1100
|
+
res.end('blocked by middleware')
|
|
1101
|
+
return
|
|
1102
|
+
}
|
|
1103
|
+
next()
|
|
1104
|
+
})
|
|
1105
|
+
|
|
1106
|
+
tenant.get('/private', async () => {
|
|
1107
|
+
return { secret: 'tenant-data' }
|
|
1108
|
+
})
|
|
1109
|
+
}, { prefix: '/tenant/:tenantId' })
|
|
1110
|
+
|
|
1111
|
+
const address = await instance.listen({ port: 0 })
|
|
1112
|
+
|
|
1113
|
+
const noAuth = await fetch(address + '/tenant/acme/private')
|
|
1114
|
+
t.assert.deepStrictEqual(noAuth.status, 403)
|
|
1115
|
+
|
|
1116
|
+
const withAuth = await fetch(address + '/tenant/acme/private', {
|
|
1117
|
+
headers: { authorization: 'Bearer secret' }
|
|
1118
|
+
})
|
|
1119
|
+
t.assert.deepStrictEqual(withAuth.status, 200)
|
|
1120
|
+
t.assert.deepStrictEqual(await withAuth.json(), { secret: 'tenant-data' })
|
|
1121
|
+
})
|
|
1122
|
+
|
|
1123
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-84w6-xrf3-f8q8
|
|
1124
|
+
// An escaped colon (`::`) in a prefix maps to a single `:` in the request URL.
|
|
1125
|
+
test('regex mount paths with an escaped-colon prefix', async t => {
|
|
1126
|
+
t.plan(2)
|
|
1127
|
+
|
|
1128
|
+
const instance = fastify()
|
|
1129
|
+
t.after(() => instance.close())
|
|
1130
|
+
|
|
1131
|
+
await instance.register(expressPlugin)
|
|
1132
|
+
|
|
1133
|
+
await instance.register(async function (child) {
|
|
1134
|
+
child.use(/^\/x/, function (req, res, next) {
|
|
1135
|
+
if (req.headers.authorization !== 'Bearer secret') {
|
|
1136
|
+
res.statusCode = 403
|
|
1137
|
+
res.end('blocked by middleware')
|
|
1138
|
+
return
|
|
1139
|
+
}
|
|
1140
|
+
next()
|
|
1141
|
+
})
|
|
1142
|
+
|
|
1143
|
+
child.get('/x', async () => {
|
|
1144
|
+
return { ok: true }
|
|
1145
|
+
})
|
|
1146
|
+
}, { prefix: '/a::b' })
|
|
1147
|
+
|
|
1148
|
+
const address = await instance.listen({ port: 0 })
|
|
1149
|
+
|
|
1150
|
+
const noAuth = await fetch(address + '/a:b/x')
|
|
1151
|
+
t.assert.deepStrictEqual(noAuth.status, 403)
|
|
1152
|
+
|
|
1153
|
+
const withAuth = await fetch(address + '/a:b/x', {
|
|
1154
|
+
headers: { authorization: 'Bearer secret' }
|
|
1155
|
+
})
|
|
1156
|
+
t.assert.deepStrictEqual(withAuth.status, 200)
|
|
1157
|
+
})
|
|
1158
|
+
|
|
1159
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-84w6-xrf3-f8q8
|
|
1160
|
+
// A custom-regex parameter keeps its constraint when rewriting a RegExp mount.
|
|
1161
|
+
test('regex mount paths with a custom-regex parameter prefix', async t => {
|
|
1162
|
+
t.plan(3)
|
|
1163
|
+
|
|
1164
|
+
const instance = fastify()
|
|
1165
|
+
t.after(() => instance.close())
|
|
1166
|
+
|
|
1167
|
+
await instance.register(expressPlugin)
|
|
1168
|
+
|
|
1169
|
+
await instance.register(async function (user) {
|
|
1170
|
+
user.use(/^\/x/, function (req, res, next) {
|
|
1171
|
+
if (req.headers.authorization !== 'Bearer secret') {
|
|
1172
|
+
res.statusCode = 403
|
|
1173
|
+
res.end('blocked by middleware')
|
|
1174
|
+
return
|
|
1175
|
+
}
|
|
1176
|
+
next()
|
|
1177
|
+
})
|
|
1178
|
+
|
|
1179
|
+
user.get('/x', async () => {
|
|
1180
|
+
return { ok: true }
|
|
1181
|
+
})
|
|
1182
|
+
}, { prefix: '/user/:id(\\d+)' })
|
|
1183
|
+
|
|
1184
|
+
const address = await instance.listen({ port: 0 })
|
|
1185
|
+
|
|
1186
|
+
// The constraint is honoured: a non-digit param is not routed by Fastify.
|
|
1187
|
+
const nonDigit = await fetch(address + '/user/abc/x')
|
|
1188
|
+
t.assert.deepStrictEqual(nonDigit.status, 404)
|
|
1189
|
+
|
|
1190
|
+
const noAuth = await fetch(address + '/user/123/x')
|
|
1191
|
+
t.assert.deepStrictEqual(noAuth.status, 403)
|
|
1192
|
+
|
|
1193
|
+
const withAuth = await fetch(address + '/user/123/x', {
|
|
1194
|
+
headers: { authorization: 'Bearer secret' }
|
|
1195
|
+
})
|
|
1196
|
+
t.assert.deepStrictEqual(withAuth.status, 200)
|
|
1197
|
+
})
|
|
1198
|
+
|
|
1199
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-84w6-xrf3-f8q8
|
|
1200
|
+
// find-my-way ignores `^` and `$` around a parameter regex, so they must not
|
|
1201
|
+
// end up as anchors in the middle of the rewritten mount path.
|
|
1202
|
+
test('regex mount paths with an anchored custom-regex parameter in the prefix', async t => {
|
|
1203
|
+
const instance = fastify()
|
|
1204
|
+
t.after(() => instance.close())
|
|
1205
|
+
|
|
1206
|
+
await instance.register(expressPlugin)
|
|
1207
|
+
|
|
1208
|
+
let middlewareCalls = 0
|
|
1209
|
+
await instance.register(async function (user) {
|
|
1210
|
+
user.use(/^\/x/, function (_req, res) {
|
|
1211
|
+
middlewareCalls++
|
|
1212
|
+
res.statusCode = 403
|
|
1213
|
+
res.end('blocked by middleware')
|
|
1214
|
+
})
|
|
1215
|
+
|
|
1216
|
+
user.get('/x', async () => ({ ok: true }))
|
|
1217
|
+
}, { prefix: '/user/:id(^\\d+$)' })
|
|
1218
|
+
|
|
1219
|
+
t.assert.deepStrictEqual((await instance.inject('/user/abc/x')).statusCode, 404)
|
|
1220
|
+
t.assert.deepStrictEqual((await instance.inject('/user/123/x')).statusCode, 403)
|
|
1221
|
+
t.assert.deepStrictEqual(middlewareCalls, 1)
|
|
1222
|
+
})
|
|
1223
|
+
|
|
1224
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-84w6-xrf3-f8q8
|
|
1225
|
+
// A parameter immediately followed by a static fragment is still matched.
|
|
1226
|
+
test('regex mount paths with a parameter followed by a static fragment', async t => {
|
|
1227
|
+
t.plan(2)
|
|
1228
|
+
|
|
1229
|
+
const instance = fastify()
|
|
1230
|
+
t.after(() => instance.close())
|
|
1231
|
+
|
|
1232
|
+
await instance.register(expressPlugin)
|
|
1233
|
+
|
|
1234
|
+
await instance.register(async function (order) {
|
|
1235
|
+
order.use(/^\/x/, function (req, res, next) {
|
|
1236
|
+
if (req.headers.authorization !== 'Bearer secret') {
|
|
1237
|
+
res.statusCode = 403
|
|
1238
|
+
res.end('blocked by middleware')
|
|
1239
|
+
return
|
|
1240
|
+
}
|
|
1241
|
+
next()
|
|
1242
|
+
})
|
|
1243
|
+
|
|
1244
|
+
order.get('/x', async () => {
|
|
1245
|
+
return { ok: true }
|
|
1246
|
+
})
|
|
1247
|
+
}, { prefix: '/orders/:id-edit' })
|
|
1248
|
+
|
|
1249
|
+
const address = await instance.listen({ port: 0 })
|
|
1250
|
+
|
|
1251
|
+
const noAuth = await fetch(address + '/orders/123-edit/x')
|
|
1252
|
+
t.assert.deepStrictEqual(noAuth.status, 403)
|
|
1253
|
+
|
|
1254
|
+
const withAuth = await fetch(address + '/orders/123-edit/x', {
|
|
1255
|
+
headers: { authorization: 'Bearer secret' }
|
|
1256
|
+
})
|
|
1257
|
+
t.assert.deepStrictEqual(withAuth.status, 200)
|
|
1258
|
+
})
|
|
1259
|
+
|
|
1260
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-84w6-xrf3-f8q8
|
|
1261
|
+
// Array mount paths containing a RegExp are rewritten with a parameterized prefix.
|
|
1262
|
+
test('array mount paths are rewritten with a parameterized prefix', async t => {
|
|
1263
|
+
t.plan(4)
|
|
1264
|
+
|
|
1265
|
+
const instance = fastify()
|
|
1266
|
+
t.after(() => instance.close())
|
|
1267
|
+
|
|
1268
|
+
await instance.register(expressPlugin)
|
|
1269
|
+
|
|
1270
|
+
await instance.register(async function (tenant) {
|
|
1271
|
+
tenant.use(['/private', /^\/settings/], function (req, res, next) {
|
|
1272
|
+
if (req.headers.authorization !== 'Bearer secret') {
|
|
1273
|
+
res.statusCode = 403
|
|
1274
|
+
res.end('blocked by middleware')
|
|
1275
|
+
return
|
|
1276
|
+
}
|
|
1277
|
+
next()
|
|
1278
|
+
})
|
|
1279
|
+
|
|
1280
|
+
tenant.get('/private', async () => {
|
|
1281
|
+
return { secret: 'tenant-data' }
|
|
1282
|
+
})
|
|
1283
|
+
|
|
1284
|
+
tenant.get('/settings', async () => {
|
|
1285
|
+
return { settings: true }
|
|
1286
|
+
})
|
|
1287
|
+
}, { prefix: '/tenant/:tenantId' })
|
|
1288
|
+
|
|
1289
|
+
const address = await instance.listen({ port: 0 })
|
|
1290
|
+
|
|
1291
|
+
const privateNoAuth = await fetch(address + '/tenant/acme/private')
|
|
1292
|
+
t.assert.deepStrictEqual(privateNoAuth.status, 403)
|
|
1293
|
+
|
|
1294
|
+
const settingsNoAuth = await fetch(address + '/tenant/acme/settings')
|
|
1295
|
+
t.assert.deepStrictEqual(settingsNoAuth.status, 403)
|
|
1296
|
+
|
|
1297
|
+
const privateWithAuth = await fetch(address + '/tenant/acme/private', {
|
|
1298
|
+
headers: { authorization: 'Bearer secret' }
|
|
1299
|
+
})
|
|
1300
|
+
t.assert.deepStrictEqual(privateWithAuth.status, 200)
|
|
1301
|
+
t.assert.deepStrictEqual(await privateWithAuth.json(), { secret: 'tenant-data' })
|
|
1302
|
+
})
|
|
1303
|
+
|
|
1304
|
+
test('regex with grouped anchors is rewritten with prefix', async t => {
|
|
1305
|
+
const instance = fastify()
|
|
1306
|
+
t.after(() => instance.close())
|
|
1307
|
+
|
|
1308
|
+
await instance.register(expressPlugin)
|
|
1309
|
+
|
|
1310
|
+
const middlewareCalls = []
|
|
1311
|
+
|
|
1312
|
+
await instance.register(async function (child) {
|
|
1313
|
+
child.use(/(?:^\/private|^\/settings)/, function (req, res) {
|
|
1314
|
+
middlewareCalls.push(req.originalUrl)
|
|
1315
|
+
res.statusCode = 403
|
|
1316
|
+
res.end('blocked by middleware')
|
|
1317
|
+
})
|
|
1318
|
+
|
|
1319
|
+
child.get('/private', async () => ({ ok: true }))
|
|
1320
|
+
child.get('/settings', async () => ({ ok: true }))
|
|
1321
|
+
child.get('/public', async () => ({ ok: true }))
|
|
1322
|
+
child.get('/nested/private', async () => ({ ok: true }))
|
|
1323
|
+
}, { prefix: '/admin' })
|
|
1324
|
+
|
|
1325
|
+
t.assert.deepStrictEqual((await instance.inject('/admin/private')).statusCode, 403)
|
|
1326
|
+
t.assert.deepStrictEqual((await instance.inject('/admin/settings')).statusCode, 403)
|
|
1327
|
+
t.assert.deepStrictEqual(middlewareCalls, ['/admin/private', '/admin/settings'])
|
|
1328
|
+
|
|
1329
|
+
// The grouped anchors still pin the match to the start of the plugin path.
|
|
1330
|
+
middlewareCalls.length = 0
|
|
1331
|
+
t.assert.deepStrictEqual((await instance.inject('/admin/public')).statusCode, 200)
|
|
1332
|
+
t.assert.deepStrictEqual((await instance.inject('/admin/nested/private')).statusCode, 200)
|
|
1333
|
+
t.assert.deepStrictEqual(middlewareCalls, [])
|
|
1334
|
+
})
|
|
1335
|
+
|
|
1336
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-f44m-4qwg-5587
|
|
1337
|
+
test('path-scoped middleware is not skipped when the plugin prefix ends with a slash', async t => {
|
|
1338
|
+
t.plan(2)
|
|
1339
|
+
|
|
1340
|
+
const instance = fastify()
|
|
1341
|
+
t.after(() => instance.close())
|
|
1342
|
+
|
|
1343
|
+
await instance.register(expressPlugin)
|
|
1344
|
+
|
|
1345
|
+
let middlewareCalls = 0
|
|
1346
|
+
|
|
1347
|
+
await instance.register(async function (scope) {
|
|
1348
|
+
scope.use('/secret', function (_req, res) {
|
|
1349
|
+
middlewareCalls++
|
|
1350
|
+
res.statusCode = 401
|
|
1351
|
+
res.end('BLOCKED')
|
|
1352
|
+
})
|
|
1353
|
+
scope.get('/secret/data', async () => {
|
|
1354
|
+
return { leaked: 'CANARY' }
|
|
1355
|
+
})
|
|
1356
|
+
}, { prefix: '/api/' })
|
|
1357
|
+
|
|
1358
|
+
const address = await instance.listen({ port: 0 })
|
|
1359
|
+
|
|
1360
|
+
const result = await fetch(address + '/api/secret/data')
|
|
1361
|
+
t.assert.deepStrictEqual(result.status, 401)
|
|
1362
|
+
t.assert.equal(middlewareCalls, 1)
|
|
1363
|
+
})
|
|
1364
|
+
|
|
1365
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-f44m-4qwg-5587
|
|
1366
|
+
test('path-scoped middleware is not skipped when the mount path omits its leading slash', async t => {
|
|
1367
|
+
t.plan(2)
|
|
1368
|
+
|
|
1369
|
+
const instance = fastify()
|
|
1370
|
+
t.after(() => instance.close())
|
|
1371
|
+
|
|
1372
|
+
await instance.register(expressPlugin)
|
|
1373
|
+
|
|
1374
|
+
let middlewareCalls = 0
|
|
1375
|
+
|
|
1376
|
+
await instance.register(async function (scope) {
|
|
1377
|
+
scope.use('secret', function (_req, res) {
|
|
1378
|
+
middlewareCalls++
|
|
1379
|
+
res.statusCode = 401
|
|
1380
|
+
res.end('BLOCKED')
|
|
1381
|
+
})
|
|
1382
|
+
scope.get('/secret/data', async () => {
|
|
1383
|
+
return { leaked: 'CANARY' }
|
|
1384
|
+
})
|
|
1385
|
+
}, { prefix: '/api' })
|
|
1386
|
+
|
|
1387
|
+
const address = await instance.listen({ port: 0 })
|
|
1388
|
+
|
|
1389
|
+
const result = await fetch(address + '/api/secret/data')
|
|
1390
|
+
t.assert.deepStrictEqual(result.status, 401)
|
|
1391
|
+
t.assert.equal(middlewareCalls, 1)
|
|
1392
|
+
})
|
|
1393
|
+
|
|
1394
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-f44m-4qwg-5587
|
|
1395
|
+
test('array path-scoped middleware is not skipped when the prefix ends with a slash', async t => {
|
|
1396
|
+
t.plan(2)
|
|
1397
|
+
|
|
1398
|
+
const instance = fastify()
|
|
1399
|
+
t.after(() => instance.close())
|
|
1400
|
+
|
|
1401
|
+
await instance.register(expressPlugin)
|
|
1402
|
+
|
|
1403
|
+
let middlewareCalls = 0
|
|
1404
|
+
|
|
1405
|
+
await instance.register(async function (scope) {
|
|
1406
|
+
scope.use(['/secret', '/private'], function (_req, res) {
|
|
1407
|
+
middlewareCalls++
|
|
1408
|
+
res.statusCode = 401
|
|
1409
|
+
res.end('BLOCKED')
|
|
1410
|
+
})
|
|
1411
|
+
scope.get('/secret/data', async () => {
|
|
1412
|
+
return { leaked: 'CANARY' }
|
|
1413
|
+
})
|
|
1414
|
+
}, { prefix: '/api/' })
|
|
1415
|
+
|
|
1416
|
+
const address = await instance.listen({ port: 0 })
|
|
1417
|
+
|
|
1418
|
+
const result = await fetch(address + '/api/secret/data')
|
|
1419
|
+
t.assert.deepStrictEqual(result.status, 401)
|
|
1420
|
+
t.assert.equal(middlewareCalls, 1)
|
|
1421
|
+
})
|
|
1422
|
+
|
|
1423
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-f44m-4qwg-5587
|
|
1424
|
+
test('the two spellings cancel correctly and still block the request', async t => {
|
|
1425
|
+
t.plan(2)
|
|
1426
|
+
|
|
1427
|
+
const instance = fastify()
|
|
1428
|
+
t.after(() => instance.close())
|
|
1429
|
+
|
|
1430
|
+
await instance.register(expressPlugin)
|
|
1431
|
+
|
|
1432
|
+
let middlewareCalls = 0
|
|
1433
|
+
|
|
1434
|
+
await instance.register(async function (scope) {
|
|
1435
|
+
scope.use('secret', function (_req, res) {
|
|
1436
|
+
middlewareCalls++
|
|
1437
|
+
res.statusCode = 401
|
|
1438
|
+
res.end('BLOCKED')
|
|
1439
|
+
})
|
|
1440
|
+
scope.get('/secret/data', async () => {
|
|
1441
|
+
return { leaked: 'CANARY' }
|
|
1442
|
+
})
|
|
1443
|
+
}, { prefix: '/api/' })
|
|
1444
|
+
|
|
1445
|
+
const address = await instance.listen({ port: 0 })
|
|
1446
|
+
|
|
1447
|
+
const result = await fetch(address + '/api/secret/data')
|
|
1448
|
+
t.assert.deepStrictEqual(result.status, 401)
|
|
1449
|
+
t.assert.equal(middlewareCalls, 1)
|
|
1450
|
+
})
|
|
1451
|
+
|
|
1452
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-r342-v25c-43wv
|
|
1453
|
+
test('regex mount paths are not skipped when the prefix ends with a slash', async t => {
|
|
1454
|
+
t.plan(2)
|
|
1455
|
+
|
|
1456
|
+
const instance = fastify()
|
|
1457
|
+
t.after(() => instance.close())
|
|
1458
|
+
|
|
1459
|
+
await instance.register(expressPlugin)
|
|
1460
|
+
|
|
1461
|
+
let middlewareCalls = 0
|
|
1462
|
+
|
|
1463
|
+
await instance.register(async function (scope) {
|
|
1464
|
+
scope.use(/\/private/, function (_req, res) {
|
|
1465
|
+
middlewareCalls++
|
|
1466
|
+
res.statusCode = 401
|
|
1467
|
+
res.end('BLOCKED')
|
|
1468
|
+
})
|
|
1469
|
+
scope.get('/private/data', async () => {
|
|
1470
|
+
return { leaked: 'CANARY' }
|
|
1471
|
+
})
|
|
1472
|
+
}, { prefix: '/api/' })
|
|
1473
|
+
|
|
1474
|
+
const address = await instance.listen({ port: 0 })
|
|
1475
|
+
|
|
1476
|
+
const result = await fetch(address + '/api/private/data')
|
|
1477
|
+
t.assert.deepStrictEqual(result.status, 401)
|
|
1478
|
+
t.assert.equal(middlewareCalls, 1)
|
|
1479
|
+
})
|
|
1480
|
+
|
|
1481
|
+
test('regex mount paths match however the prefix boundary slash is spelled', async t => {
|
|
1482
|
+
const mounts = [
|
|
1483
|
+
[/^\/private/, '/private/data'],
|
|
1484
|
+
[/[/]private/, '/private/data'],
|
|
1485
|
+
[/\x2fprivate/, '/private/data'],
|
|
1486
|
+
[/(?:\/private)/, '/private/data'],
|
|
1487
|
+
[/^\/private|^settings/, '/private/data'],
|
|
1488
|
+
[/^\/private|^settings/, '/settings'],
|
|
1489
|
+
[/private/, '/private/data'],
|
|
1490
|
+
[/(?:^\/private)/, '/private/data'],
|
|
1491
|
+
[/^\/admin|private/, '/private/data']
|
|
1492
|
+
]
|
|
1493
|
+
|
|
1494
|
+
for (const prefix of ['/api', '/api/']) {
|
|
1495
|
+
for (const [mount, route] of mounts) {
|
|
1496
|
+
const label = `prefix=${prefix} use(${mount}) GET ${route}`
|
|
1497
|
+
const instance = fastify()
|
|
1498
|
+
t.after(() => instance.close())
|
|
1499
|
+
await instance.register(expressPlugin)
|
|
1500
|
+
|
|
1501
|
+
let middlewareCalls = 0
|
|
1502
|
+
await instance.register(async function (scope) {
|
|
1503
|
+
scope.use(mount, function (_req, res) {
|
|
1504
|
+
middlewareCalls++
|
|
1505
|
+
res.statusCode = 401
|
|
1506
|
+
res.end('blocked')
|
|
1507
|
+
})
|
|
1508
|
+
scope.get(route, async () => ({ leaked: true }))
|
|
1509
|
+
scope.get('/public', async () => ({ ok: true }))
|
|
1510
|
+
}, { prefix })
|
|
1511
|
+
|
|
1512
|
+
const blocked = await instance.inject('/api' + route)
|
|
1513
|
+
t.assert.deepStrictEqual(blocked.statusCode, 401, label)
|
|
1514
|
+
t.assert.deepStrictEqual(middlewareCalls, 1, label)
|
|
1515
|
+
|
|
1516
|
+
const open = await instance.inject('/api/public')
|
|
1517
|
+
t.assert.deepStrictEqual(open.statusCode, 200, label)
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
})
|
|
1521
|
+
|
|
1522
|
+
test('regex mount paths in prefixed plugins match from the prefix, as in a mounted Express router', async t => {
|
|
1523
|
+
// Express only runs a RegExp mount whose match starts the path, so a
|
|
1524
|
+
// pattern that only matches further along the path does not apply.
|
|
1525
|
+
const cases = [
|
|
1526
|
+
[/\/private/, '/private/data', 401],
|
|
1527
|
+
[/\/private/, '/nested/private', 200],
|
|
1528
|
+
[/private/, '/nested/private', 200],
|
|
1529
|
+
[/^\/admin|private/, '/nested/private', 200],
|
|
1530
|
+
[/.*private/, '/nested/private', 401]
|
|
1531
|
+
]
|
|
1532
|
+
|
|
1533
|
+
for (const [mount, route, statusCode] of cases) {
|
|
1534
|
+
const label = `use(${mount}) GET ${route}`
|
|
1535
|
+
const instance = fastify()
|
|
1536
|
+
t.after(() => instance.close())
|
|
1537
|
+
await instance.register(expressPlugin)
|
|
1538
|
+
|
|
1539
|
+
await instance.register(async function (scope) {
|
|
1540
|
+
scope.use(mount, function (_req, res) {
|
|
1541
|
+
res.statusCode = 401
|
|
1542
|
+
res.end('blocked')
|
|
1543
|
+
})
|
|
1544
|
+
scope.get(route, async () => ({ ok: true }))
|
|
1545
|
+
}, { prefix: '/api' })
|
|
1546
|
+
|
|
1547
|
+
const response = await instance.inject('/api' + route)
|
|
1548
|
+
t.assert.deepStrictEqual(response.statusCode, statusCode, label)
|
|
1549
|
+
}
|
|
1550
|
+
})
|
|
1551
|
+
|
|
1552
|
+
test('regex mount paths in prefixed plugins are matched in linear time', async t => {
|
|
1553
|
+
const long = 'a'.repeat(100_000)
|
|
1554
|
+
|
|
1555
|
+
for (const [prefix, url] of [
|
|
1556
|
+
['/api', '/api/' + long],
|
|
1557
|
+
['/tenant/:id', '/tenant/acme/' + long],
|
|
1558
|
+
// A long parameter must not be traded character by character with the
|
|
1559
|
+
// pattern that follows it.
|
|
1560
|
+
['/tenant/:id', '/tenant/' + long + '/x']
|
|
1561
|
+
]) {
|
|
1562
|
+
const instance = fastify({ routerOptions: { maxParamLength: 200_000 } })
|
|
1563
|
+
t.after(() => instance.close())
|
|
1564
|
+
await instance.register(expressPlugin)
|
|
1565
|
+
|
|
1566
|
+
await instance.register(async function (scope) {
|
|
1567
|
+
scope.use(/.*private/, function (_req, res) {
|
|
1568
|
+
res.statusCode = 401
|
|
1569
|
+
res.end('blocked')
|
|
1570
|
+
})
|
|
1571
|
+
scope.get('/*', async () => ({ ok: true }))
|
|
1572
|
+
}, { prefix })
|
|
1573
|
+
|
|
1574
|
+
// Searching for the pattern from every position after the prefix took
|
|
1575
|
+
// seconds on a path this long.
|
|
1576
|
+
const start = performance.now()
|
|
1577
|
+
const response = await instance.inject(url)
|
|
1578
|
+
const elapsed = performance.now() - start
|
|
1579
|
+
t.assert.deepStrictEqual(response.statusCode, 200, prefix)
|
|
1580
|
+
t.assert.ok(elapsed < 1000, `${prefix}: matching took ${Math.round(elapsed)} ms`)
|
|
1581
|
+
}
|
|
1582
|
+
})
|
|
1583
|
+
|
|
1584
|
+
test('escapes and character classes in regex mount paths are not rewritten', async t => {
|
|
1585
|
+
const instance = fastify()
|
|
1586
|
+
t.after(() => instance.close())
|
|
1587
|
+
await instance.register(expressPlugin)
|
|
1588
|
+
|
|
1589
|
+
const middlewareCalls = []
|
|
1590
|
+
function block (name) {
|
|
1591
|
+
return function (_req, res) {
|
|
1592
|
+
middlewareCalls.push(name)
|
|
1593
|
+
res.statusCode = 401
|
|
1594
|
+
res.end('blocked')
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
await instance.register(async function (scope) {
|
|
1599
|
+
// `\^` and `\|` are literals, `[^|]` is a class: none of them is an anchor
|
|
1600
|
+
// or an alternation, so none of them may be rewritten.
|
|
1601
|
+
scope.use(/\^caret/, block('escaped caret'))
|
|
1602
|
+
scope.use(/^\/a\|b/, block('escaped pipe'))
|
|
1603
|
+
scope.use(/^\/[^|]lass/, block('class'))
|
|
1604
|
+
// eslint-disable-next-line prefer-regex-literals -- the parser lacks the `v` flag
|
|
1605
|
+
scope.use(new RegExp('^\\/[[a-z]--[x]]ets', 'v'), block('nested class'))
|
|
1606
|
+
scope.get('/^caret', async () => ({ leaked: true }))
|
|
1607
|
+
scope.get('/a|b', async () => ({ leaked: true }))
|
|
1608
|
+
scope.get('/class', async () => ({ leaked: true }))
|
|
1609
|
+
scope.get('/sets', async () => ({ leaked: true }))
|
|
1610
|
+
scope.get('/a', async () => ({ ok: true }))
|
|
1611
|
+
}, { prefix: '/api' })
|
|
1612
|
+
|
|
1613
|
+
for (const [url, name] of [
|
|
1614
|
+
['/api/%5Ecaret', 'escaped caret'],
|
|
1615
|
+
['/api/a%7Cb', 'escaped pipe'],
|
|
1616
|
+
['/api/class', 'class'],
|
|
1617
|
+
['/api/sets', 'nested class']
|
|
1618
|
+
]) {
|
|
1619
|
+
middlewareCalls.length = 0
|
|
1620
|
+
const response = await instance.inject(url)
|
|
1621
|
+
t.assert.deepStrictEqual(response.statusCode, 401, url)
|
|
1622
|
+
t.assert.deepStrictEqual(middlewareCalls, [name], url)
|
|
1623
|
+
}
|
|
1624
|
+
|
|
1625
|
+
middlewareCalls.length = 0
|
|
1626
|
+
t.assert.deepStrictEqual((await instance.inject('/api/a')).statusCode, 200)
|
|
1627
|
+
t.assert.deepStrictEqual(middlewareCalls, [])
|
|
1628
|
+
})
|
|
1629
|
+
|
|
1630
|
+
test('named groups in prefixed regex mount paths keep their names', async t => {
|
|
1631
|
+
const instance = fastify()
|
|
1632
|
+
t.after(() => instance.close())
|
|
1633
|
+
await instance.register(expressPlugin)
|
|
1634
|
+
|
|
1635
|
+
let params
|
|
1636
|
+
await instance.register(async function (scope) {
|
|
1637
|
+
scope.use(/^\/user\/(?<id>\d+)/, function (req, res) {
|
|
1638
|
+
params = req.params
|
|
1639
|
+
res.statusCode = 401
|
|
1640
|
+
res.end('blocked')
|
|
1641
|
+
})
|
|
1642
|
+
scope.get('/user/:id', async () => ({ leaked: true }))
|
|
1643
|
+
}, { prefix: '/api' })
|
|
1644
|
+
|
|
1645
|
+
const response = await instance.inject('/api/user/42')
|
|
1646
|
+
t.assert.deepStrictEqual(response.statusCode, 401)
|
|
1647
|
+
t.assert.deepStrictEqual(params, { id: '42' })
|
|
1648
|
+
})
|
|
1649
|
+
|
|
1650
|
+
test('regex mount paths at the root are matched against the full path', async t => {
|
|
1651
|
+
const instance = fastify()
|
|
1652
|
+
t.after(() => instance.close())
|
|
1653
|
+
await instance.register(expressPlugin)
|
|
1654
|
+
|
|
1655
|
+
instance.use(/^\/private/, function (_req, res) {
|
|
1656
|
+
res.statusCode = 401
|
|
1657
|
+
res.end('blocked')
|
|
1658
|
+
})
|
|
1659
|
+
instance.get('/private', async () => ({ leaked: true }))
|
|
1660
|
+
instance.get('/public', async () => ({ ok: true }))
|
|
1661
|
+
|
|
1662
|
+
t.assert.deepStrictEqual((await instance.inject('/private')).statusCode, 401)
|
|
1663
|
+
t.assert.deepStrictEqual((await instance.inject('/public')).statusCode, 200)
|
|
1664
|
+
})
|
|
1665
|
+
|
|
1666
|
+
test('regex mount paths with the g or y flag do not skip every other request', async t => {
|
|
1667
|
+
for (const [prefix, mount] of [
|
|
1668
|
+
['', /^\/private/g],
|
|
1669
|
+
['', /^\/private/y],
|
|
1670
|
+
['/api', /^\/private/g],
|
|
1671
|
+
['/api', /^\/private/y]
|
|
1672
|
+
]) {
|
|
1673
|
+
const label = `prefix=${prefix} use(${mount})`
|
|
1674
|
+
const instance = fastify()
|
|
1675
|
+
t.after(() => instance.close())
|
|
1676
|
+
await instance.register(expressPlugin)
|
|
1677
|
+
|
|
1678
|
+
let middlewareCalls = 0
|
|
1679
|
+
await instance.register(async function (scope) {
|
|
1680
|
+
scope.use(mount, function (_req, res) {
|
|
1681
|
+
middlewareCalls++
|
|
1682
|
+
res.statusCode = 401
|
|
1683
|
+
res.end('blocked')
|
|
1684
|
+
})
|
|
1685
|
+
scope.get('/private', async () => ({ leaked: true }))
|
|
1686
|
+
}, { prefix })
|
|
1687
|
+
|
|
1688
|
+
for (let i = 0; i < 4; i++) {
|
|
1689
|
+
const response = await instance.inject(prefix + '/private')
|
|
1690
|
+
t.assert.deepStrictEqual(response.statusCode, 401, `${label} request ${i}`)
|
|
1691
|
+
}
|
|
1692
|
+
t.assert.deepStrictEqual(middlewareCalls, 4, label)
|
|
1693
|
+
}
|
|
1694
|
+
})
|
|
1695
|
+
|
|
1696
|
+
test('mount paths are collapsed like routes when ignoreDuplicateSlashes is enabled', async t => {
|
|
1697
|
+
for (const [prefix, mount] of [
|
|
1698
|
+
['/api', '//private'],
|
|
1699
|
+
['/api', ['//private']],
|
|
1700
|
+
['/api//v1', '/private'],
|
|
1701
|
+
['/api//v1', /^\/private/],
|
|
1702
|
+
['/api/', '//private'],
|
|
1703
|
+
['/api', 'private']
|
|
1704
|
+
]) {
|
|
1705
|
+
const label = `prefix=${prefix} use(${mount})`
|
|
1706
|
+
const instance = fastify({ routerOptions: { ignoreDuplicateSlashes: true } })
|
|
1707
|
+
t.after(() => instance.close())
|
|
1708
|
+
await instance.register(expressPlugin)
|
|
1709
|
+
|
|
1710
|
+
let middlewareCalls = 0
|
|
1711
|
+
await instance.register(async function (scope) {
|
|
1712
|
+
scope.use(mount, function (_req, res) {
|
|
1713
|
+
middlewareCalls++
|
|
1714
|
+
res.statusCode = 401
|
|
1715
|
+
res.end('blocked')
|
|
1716
|
+
})
|
|
1717
|
+
scope.get('/private/data', async () => ({ leaked: true }))
|
|
1718
|
+
}, { prefix })
|
|
1719
|
+
|
|
1720
|
+
const url = prefix.replace(/\/+$/, '').replace(/\/{2,}/g, '/') + '/private/data'
|
|
1721
|
+
for (const requestUrl of [url, url.replace(/\//g, '//')]) {
|
|
1722
|
+
const response = await instance.inject(requestUrl)
|
|
1723
|
+
t.assert.deepStrictEqual(response.statusCode, 401, `${label} GET ${requestUrl}`)
|
|
1724
|
+
}
|
|
1725
|
+
t.assert.deepStrictEqual(middlewareCalls, 2, label)
|
|
1726
|
+
}
|
|
1727
|
+
})
|
|
1728
|
+
|
|
1729
|
+
test('prefixed regex mount paths that can match the empty string still run', async t => {
|
|
1730
|
+
for (const mount of [/^(?!\/public)/, /^/, /(?:)/, /^(?:\/admin)?/]) {
|
|
1731
|
+
const label = `use(${mount})`
|
|
1732
|
+
const instance = fastify()
|
|
1733
|
+
t.after(() => instance.close())
|
|
1734
|
+
await instance.register(expressPlugin)
|
|
1735
|
+
|
|
1736
|
+
await instance.register(async function (scope) {
|
|
1737
|
+
scope.use(mount, function (_req, res) {
|
|
1738
|
+
res.statusCode = 401
|
|
1739
|
+
res.end('blocked')
|
|
1740
|
+
})
|
|
1741
|
+
scope.get('/secret', async () => ({ leaked: true }))
|
|
1742
|
+
}, { prefix: '/api' })
|
|
1743
|
+
|
|
1744
|
+
t.assert.deepStrictEqual((await instance.inject('/api/secret')).statusCode, 401, label)
|
|
1745
|
+
}
|
|
1746
|
+
})
|
|
1747
|
+
|
|
1748
|
+
test('prefixed regex mount paths that match the root also cover the bare prefix', async t => {
|
|
1749
|
+
for (const mount of [/^\/.*/, /^\/$/, /^\/(?!public)/]) {
|
|
1750
|
+
const label = `use(${mount})`
|
|
1751
|
+
const instance = fastify()
|
|
1752
|
+
t.after(() => instance.close())
|
|
1753
|
+
await instance.register(expressPlugin)
|
|
1754
|
+
|
|
1755
|
+
await instance.register(async function (scope) {
|
|
1756
|
+
scope.use(mount, function (_req, res) {
|
|
1757
|
+
res.statusCode = 401
|
|
1758
|
+
res.end('blocked')
|
|
1759
|
+
})
|
|
1760
|
+
scope.get('/', async () => ({ leaked: true }))
|
|
1761
|
+
scope.get('/public', async () => ({ ok: true }))
|
|
1762
|
+
}, { prefix: '/api' })
|
|
1763
|
+
|
|
1764
|
+
t.assert.deepStrictEqual((await instance.inject('/api')).statusCode, 401, `${label} GET /api`)
|
|
1765
|
+
t.assert.deepStrictEqual((await instance.inject('/api/')).statusCode, 401, `${label} GET /api/`)
|
|
1766
|
+
}
|
|
1767
|
+
|
|
1768
|
+
// A pattern that does not match the root leaves the bare prefix alone.
|
|
1769
|
+
const instance = fastify()
|
|
1770
|
+
t.after(() => instance.close())
|
|
1771
|
+
await instance.register(expressPlugin)
|
|
1772
|
+
await instance.register(async function (scope) {
|
|
1773
|
+
scope.use(/^\/private/, function (_req, res) {
|
|
1774
|
+
res.statusCode = 401
|
|
1775
|
+
res.end('blocked')
|
|
1776
|
+
})
|
|
1777
|
+
scope.get('/', async () => ({ ok: true }))
|
|
1778
|
+
}, { prefix: '/api' })
|
|
1779
|
+
t.assert.deepStrictEqual((await instance.inject('/api')).statusCode, 200)
|
|
1780
|
+
})
|
|
1781
|
+
|
|
1782
|
+
test('slashless mount paths cover both the glued and the separated route', async t => {
|
|
1783
|
+
const instance = fastify()
|
|
1784
|
+
t.after(() => instance.close())
|
|
1785
|
+
await instance.register(expressPlugin)
|
|
1786
|
+
|
|
1787
|
+
let middlewareCalls = 0
|
|
1788
|
+
await instance.register(async function (scope) {
|
|
1789
|
+
scope.use('secret', function (_req, res) {
|
|
1790
|
+
middlewareCalls++
|
|
1791
|
+
res.statusCode = 401
|
|
1792
|
+
res.end('blocked')
|
|
1793
|
+
})
|
|
1794
|
+
// Fastify serves a slashless route glued to the prefix, at /apisecret.
|
|
1795
|
+
scope.get('secret', async () => ({ leaked: true }))
|
|
1796
|
+
scope.get('/secret/data', async () => ({ leaked: true }))
|
|
1797
|
+
}, { prefix: '/api' })
|
|
1798
|
+
|
|
1799
|
+
t.assert.deepStrictEqual((await instance.inject('/apisecret')).statusCode, 401)
|
|
1800
|
+
t.assert.deepStrictEqual((await instance.inject('/api/secret/data')).statusCode, 401)
|
|
1801
|
+
t.assert.deepStrictEqual(middlewareCalls, 2)
|
|
1802
|
+
})
|
|
1803
|
+
|
|
1804
|
+
// @see https://github.com/fastify/fastify-express/security/advisories/GHSA-84w6-xrf3-f8q8
|
|
1805
|
+
// Prefix segments holding a parameter match one whole segment, whatever the
|
|
1806
|
+
// parameter regex says, so the pattern stays valid under the `u` and `v` flags
|
|
1807
|
+
// and anchors inside the parameter regex cannot leak into the pattern.
|
|
1808
|
+
test('regex mount paths under parameterized prefixes with flags and anchored parameter regexes', async t => {
|
|
1809
|
+
for (const [prefix, mount, url] of [
|
|
1810
|
+
// eslint-disable-next-line prefer-regex-literals -- the parser lacks the `v` flag
|
|
1811
|
+
['/tenant/:id', new RegExp('^\\/admin', 'v'), '/tenant/t1/admin'],
|
|
1812
|
+
['/d/:date(\\d{4}\\-\\d{2})', /^\/admin/u, '/d/2024-01/admin'],
|
|
1813
|
+
['/u/:id(^\\d+$|^me$)', /^\/admin/, '/u/me/admin'],
|
|
1814
|
+
['/r/:from-:to', /^\/admin/, '/r/a-b/admin'],
|
|
1815
|
+
['/a::b/:id', /^\/admin/, '/a:b/1/admin'],
|
|
1816
|
+
['/g/:id((?:x|y)\\d+)', /^\/admin/, '/g/x1/admin']
|
|
1817
|
+
]) {
|
|
1818
|
+
const label = `prefix=${prefix} use(${mount})`
|
|
1819
|
+
const instance = fastify()
|
|
1820
|
+
t.after(() => instance.close())
|
|
1821
|
+
await instance.register(expressPlugin)
|
|
1822
|
+
|
|
1823
|
+
let middlewareCalls = 0
|
|
1824
|
+
await instance.register(async function (scope) {
|
|
1825
|
+
scope.use(mount, function (_req, res) {
|
|
1826
|
+
middlewareCalls++
|
|
1827
|
+
res.statusCode = 401
|
|
1828
|
+
res.end('blocked')
|
|
1829
|
+
})
|
|
1830
|
+
scope.get('/admin', async () => ({ leaked: true }))
|
|
1831
|
+
scope.get('/public', async () => ({ ok: true }))
|
|
1832
|
+
}, { prefix })
|
|
1833
|
+
|
|
1834
|
+
t.assert.deepStrictEqual((await instance.inject(url)).statusCode, 401, label)
|
|
1835
|
+
t.assert.deepStrictEqual(middlewareCalls, 1, label)
|
|
1836
|
+
t.assert.deepStrictEqual((await instance.inject(url.replace(/admin$/, 'public'))).statusCode, 200, label)
|
|
1837
|
+
}
|
|
1838
|
+
})
|
|
1839
|
+
|
|
1840
|
+
test('regex mount paths under prefixes with many parameters in one segment do not backtrack', async t => {
|
|
1841
|
+
const instance = fastify({ routerOptions: { maxParamLength: 500 } })
|
|
1842
|
+
t.after(() => instance.close())
|
|
1843
|
+
await instance.register(expressPlugin)
|
|
1844
|
+
|
|
1845
|
+
await instance.register(async function (scope) {
|
|
1846
|
+
scope.use(/^\/admin/, function (_req, res) {
|
|
1847
|
+
res.statusCode = 401
|
|
1848
|
+
res.end('blocked')
|
|
1849
|
+
})
|
|
1850
|
+
scope.get('/x', async () => ({ ok: true }))
|
|
1851
|
+
}, { prefix: '/t/:a-:b-:c-:d' })
|
|
1852
|
+
|
|
1853
|
+
const start = process.hrtime.bigint()
|
|
1854
|
+
const response = await instance.inject('/t/' + '-'.repeat(500) + '-b-c-d/x')
|
|
1855
|
+
const elapsedMs = Number(process.hrtime.bigint() - start) / 1e6
|
|
1856
|
+
|
|
1857
|
+
t.assert.deepStrictEqual(response.statusCode, 200)
|
|
1858
|
+
t.assert.ok(elapsedMs < 1000, `matcher took ${elapsedMs.toFixed(0)}ms`)
|
|
1859
|
+
})
|