Commit 190f6e2
Merge commit from fork
* feat(utils/jwt): add JwtAlgorithmMismatch and JwtSymmetricAlgorithmNotAllowed error types
* fix(utils/jwt): prevent algorithm confusion attacks in verifyWithJwks
- Reject symmetric algorithms (HS256/HS384/HS512) in JWK verification
- Verify JWK alg matches JWT header alg when JWK has alg field
- Use header.alg for verification instead of JWK alg fallback
* test(utils/jwt): add security tests for verifyWithJwks
- Update header.alg fallback test to use asymmetric algorithm (RS256)
- Add tests for symmetric algorithm rejection (HS256/HS384/HS512)
- Add test for algorithm mismatch between JWK and JWT header
- Add test for algorithm confusion attack prevention
* feat(utils/jwt): add JwtAlgorithmNotAllowed error type
Add new error class for algorithm whitelist validation.
This error is thrown when JWT's algorithm is not in the allowed list.
* feat(utils/jwt): add algorithm whitelist support to verifyWithJwks
Add optional allowedAlgorithms parameter to verifyWithJwks function.
When specified, only tokens signed with algorithms in the whitelist
will be accepted. This provides an additional layer of security by
explicitly defining which algorithms are permitted.
Validation order:
1. Check algorithm against whitelist (if specified)
2. Reject symmetric algorithms (HS256/HS384/HS512)
3. Validate JWK alg matches header alg (if JWK has alg field)
* feat(middleware/jwk): add alg option for algorithm whitelist
Add alg option to JWK middleware to specify allowed algorithms.
This option is passed to verifyWithJwks as allowedAlgorithms.
Example usage:
jwk({ keys, alg: ['RS256', 'ES256'] })
* test(utils/jwt): add algorithm whitelist tests for verifyWithJwks
Add tests for:
- Reject algorithm not in whitelist
- Accept algorithm in whitelist
- Accept any asymmetric algorithm when whitelist not specified
- Accept any asymmetric algorithm when whitelist is empty
- Reject symmetric algorithm even if in whitelist
* test(middleware/jwk): add algorithm whitelist tests
Add tests for JWK middleware alg option:
- Authorize RS256 token when RS256 is in whitelist
- Reject token when algorithm is not in whitelist
- Authorize RS256 token when multiple algorithms are in whitelist
- Authorize RS256 token when no whitelist is specified
* feat(utils/jwt): add AsymmetricAlgorithm and SymmetricAlgorithm type definitions
- Added SymmetricAlgorithm type for HMAC algorithms: HS256, HS384, HS512
- Added AsymmetricAlgorithm type for RSA/ECDSA/EdDSA algorithms
- Enables compile-time prevention of algorithm confusion attacks
* fix(utils/jwt): make allowedAlgorithms required in verifyWithJwks
BREAKING CHANGE: allowedAlgorithms is now a required parameter with type AsymmetricAlgorithm[]
- Changed allowedAlgorithms from optional to required
- Changed type from SignatureAlgorithm[] to AsymmetricAlgorithm[]
- Reordered validation: symmetric algorithm rejection before whitelist check
- Prevents algorithm confusion attacks at both runtime and compile-time
* fix(middleware/jwk): make alg option required
BREAKING CHANGE: alg option is now required with type AsymmetricAlgorithm[]
- Changed alg from optional to required
- Changed type from SignatureAlgorithm[] to AsymmetricAlgorithm[]
- Updated JSDoc to reflect breaking change
- Ensures users must explicitly specify allowed algorithms
* test(utils/jwt): update tests for required allowedAlgorithms
- Added allowedAlgorithms: ['RS256'] to all verifyWithJwks calls
- Removed tests for 'whitelist not specified' and 'empty whitelist' scenarios
- Added comments explaining breaking changes
- Renamed test to 'Should reject symmetric algorithm (HS256) in JWT header'
* test(middleware/jwk): update tests for required alg option
- Added alg: ['RS256'] to all jwk() middleware calls
- Removed test for 'no whitelist' scenario (no longer applicable)
- Added comments explaining breaking changes
- Updated verifyWithJwks test to include allowedAlgorithms
* test(utils/jwt): add type tests for algorithm type definitions
- Added tests for SymmetricAlgorithm type (HS256, HS384, HS512)
- Added tests for AsymmetricAlgorithm type (RS*, PS*, ES*, EdDSA)
- Added tests for SignatureAlgorithm type (all 13 algorithms)
- Tests verify type constraints at runtime
* fix(test): resolve TypeScript errors in JWK middleware tests
- Removed unused @ts-expect-error directive on line 40
- Added @ts-expect-error for empty object test (line 210)
- Added required alg option to crypto.subtle test (line 220)
- All 115 tests still passing
* refactor: use SymmetricAlgorithm type for symmetricAlgorithms array
* fix(utils/jwt): cast header.alg to SymmetricAlgorithm to prevent type errors
* fix(utils/jwt): update comment for clarity on algorithm validation
---------
Co-authored-by: Yusuke Wada <[email protected]>1 parent a48ef18 commit 190f6e2
File tree
7 files changed
+673
-30
lines changed- src
- middleware/jwk
- utils/jwt
7 files changed
+673
-30
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | | - | |
| 38 | + | |
39 | 39 | | |
40 | | - | |
| 40 | + | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
55 | | - | |
| 55 | + | |
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
| |||
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
108 | | - | |
109 | | - | |
| 108 | + | |
| 109 | + | |
110 | 110 | | |
111 | | - | |
| 111 | + | |
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
| |||
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
| 122 | + | |
122 | 123 | | |
123 | 124 | | |
124 | 125 | | |
125 | 126 | | |
126 | 127 | | |
127 | 128 | | |
| 129 | + | |
128 | 130 | | |
129 | 131 | | |
130 | 132 | | |
131 | 133 | | |
132 | 134 | | |
133 | 135 | | |
134 | 136 | | |
| 137 | + | |
135 | 138 | | |
136 | 139 | | |
137 | 140 | | |
138 | 141 | | |
139 | 142 | | |
140 | 143 | | |
| 144 | + | |
141 | 145 | | |
142 | 146 | | |
143 | 147 | | |
144 | 148 | | |
145 | 149 | | |
146 | 150 | | |
| 151 | + | |
147 | 152 | | |
148 | 153 | | |
149 | 154 | | |
150 | 155 | | |
151 | 156 | | |
152 | 157 | | |
| 158 | + | |
153 | 159 | | |
154 | 160 | | |
155 | 161 | | |
| |||
200 | 206 | | |
201 | 207 | | |
202 | 208 | | |
| 209 | + | |
203 | 210 | | |
204 | 211 | | |
205 | 212 | | |
| |||
210 | 217 | | |
211 | 218 | | |
212 | 219 | | |
213 | | - | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
214 | 223 | | |
215 | 224 | | |
216 | 225 | | |
| |||
443 | 452 | | |
444 | 453 | | |
445 | 454 | | |
446 | | - | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
447 | 459 | | |
448 | 460 | | |
449 | 461 | | |
| |||
494 | 506 | | |
495 | 507 | | |
496 | 508 | | |
497 | | - | |
498 | | - | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
499 | 514 | | |
500 | 515 | | |
501 | | - | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
502 | 521 | | |
503 | 522 | | |
504 | 523 | | |
505 | | - | |
| 524 | + | |
506 | 525 | | |
507 | 526 | | |
508 | 527 | | |
| |||
637 | 656 | | |
638 | 657 | | |
639 | 658 | | |
640 | | - | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
641 | 664 | | |
642 | 665 | | |
643 | 666 | | |
644 | 667 | | |
645 | 668 | | |
646 | 669 | | |
| 670 | + | |
647 | 671 | | |
648 | 672 | | |
649 | 673 | | |
| |||
721 | 745 | | |
722 | 746 | | |
723 | 747 | | |
724 | | - | |
| 748 | + | |
725 | 749 | | |
726 | 750 | | |
727 | 751 | | |
| |||
761 | 785 | | |
762 | 786 | | |
763 | 787 | | |
764 | | - | |
| 788 | + | |
765 | 789 | | |
766 | 790 | | |
767 | | - | |
| 791 | + | |
768 | 792 | | |
769 | 793 | | |
770 | 794 | | |
| |||
874 | 898 | | |
875 | 899 | | |
876 | 900 | | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
877 | 997 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| 28 | + | |
27 | 29 | | |
28 | 30 | | |
29 | 31 | | |
| |||
54 | 56 | | |
55 | 57 | | |
56 | 58 | | |
| 59 | + | |
| 60 | + | |
57 | 61 | | |
58 | 62 | | |
59 | 63 | | |
| |||
132 | 136 | | |
133 | 137 | | |
134 | 138 | | |
135 | | - | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
136 | 144 | | |
137 | 145 | | |
138 | 146 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
24 | 86 | | |
0 commit comments