eslint-plugin-jwt-security 2.3.0 → 2.3.2
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 +5 -5
- package/package.json +16 -24
- package/src/index.d.ts +17 -0
- package/src/index.js +1 -0
- package/src/oxlint.d.ts +17 -0
- package/src/oxlint.js +1 -0
- package/src/rules/no-algorithm-confusion/index.js +1 -0
- package/src/rules/no-algorithm-none/index.js +1 -0
- package/src/rules/no-decode-without-verify/index.js +1 -0
- package/src/rules/no-hardcoded-secret/index.js +1 -0
- package/src/rules/no-sensitive-payload/index.js +1 -0
- package/src/rules/no-timestamp-manipulation/index.js +1 -0
- package/src/rules/no-weak-secret/index.js +1 -0
- package/src/rules/require-algorithm-whitelist/index.js +1 -0
- package/src/rules/require-audience-validation/index.js +1 -0
- package/src/rules/require-expiration/index.js +1 -0
- package/src/rules/require-issued-at/index.js +1 -0
- package/src/rules/require-issuer-validation/index.js +1 -0
- package/src/rules/require-max-age/index.js +1 -0
- package/src/types/index.js +1 -0
- package/src/utils/index.js +1 -0
- package/AGENTS.md +0 -102
- package/CHANGELOG.md +0 -161
- package/dist/AGENTS.md +0 -102
- package/dist/CHANGELOG.md +0 -161
- package/dist/LICENSE +0 -23
- package/dist/README.md +0 -158
- package/dist/package.json +0 -125
- package/dist/src/index.d.ts +0 -101
- package/dist/src/index.js +0 -171
- package/dist/src/oxlint.d.ts +0 -20
- package/dist/src/oxlint.js +0 -21
- package/dist/src/rules/no-algorithm-confusion/index.d.ts +0 -24
- package/dist/src/rules/no-algorithm-confusion/index.js +0 -165
- package/dist/src/rules/no-algorithm-none/index.d.ts +0 -24
- package/dist/src/rules/no-algorithm-none/index.js +0 -222
- package/dist/src/rules/no-decode-without-verify/index.d.ts +0 -23
- package/dist/src/rules/no-decode-without-verify/index.js +0 -131
- package/dist/src/rules/no-hardcoded-secret/index.d.ts +0 -23
- package/dist/src/rules/no-hardcoded-secret/index.js +0 -189
- package/dist/src/rules/no-sensitive-payload/index.d.ts +0 -20
- package/dist/src/rules/no-sensitive-payload/index.js +0 -103
- package/dist/src/rules/no-timestamp-manipulation/index.d.ts +0 -23
- package/dist/src/rules/no-timestamp-manipulation/index.js +0 -97
- package/dist/src/rules/no-weak-secret/index.d.ts +0 -23
- package/dist/src/rules/no-weak-secret/index.js +0 -165
- package/dist/src/rules/require-algorithm-whitelist/index.d.ts +0 -24
- package/dist/src/rules/require-algorithm-whitelist/index.js +0 -113
- package/dist/src/rules/require-audience-validation/index.d.ts +0 -20
- package/dist/src/rules/require-audience-validation/index.js +0 -100
- package/dist/src/rules/require-expiration/index.d.ts +0 -23
- package/dist/src/rules/require-expiration/index.js +0 -136
- package/dist/src/rules/require-issued-at/index.d.ts +0 -23
- package/dist/src/rules/require-issued-at/index.js +0 -113
- package/dist/src/rules/require-issuer-validation/index.d.ts +0 -20
- package/dist/src/rules/require-issuer-validation/index.js +0 -100
- package/dist/src/rules/require-max-age/index.d.ts +0 -24
- package/dist/src/rules/require-max-age/index.js +0 -93
- package/dist/src/types/index.js +0 -7
- package/dist/src/utils/index.d.ts +0 -98
- package/dist/src/utils/index.js +0 -238
- package/src/__compatibility__/jwt-interface.spec.ts +0 -192
- package/src/index.test.ts +0 -47
- package/src/index.ts +0 -209
- package/src/oxlint.test.ts +0 -61
- package/src/oxlint.ts +0 -23
- package/src/rules/no-algorithm-confusion/index.ts +0 -208
- package/src/rules/no-algorithm-confusion/no-algorithm-confusion.test.ts +0 -106
- package/src/rules/no-algorithm-none/index.ts +0 -270
- package/src/rules/no-algorithm-none/no-algorithm-none.test.ts +0 -146
- package/src/rules/no-decode-without-verify/index.ts +0 -162
- package/src/rules/no-decode-without-verify/no-decode-without-verify.test.ts +0 -76
- package/src/rules/no-hardcoded-secret/index.ts +0 -230
- package/src/rules/no-hardcoded-secret/no-hardcoded-secret.test.ts +0 -97
- package/src/rules/no-sensitive-payload/index.ts +0 -123
- package/src/rules/no-sensitive-payload/no-sensitive-payload.test.ts +0 -157
- package/src/rules/no-timestamp-manipulation/index.ts +0 -118
- package/src/rules/no-timestamp-manipulation/no-timestamp-manipulation.test.ts +0 -54
- package/src/rules/no-weak-secret/index.ts +0 -204
- package/src/rules/no-weak-secret/no-weak-secret.test.ts +0 -114
- package/src/rules/require-algorithm-whitelist/index.ts +0 -145
- package/src/rules/require-algorithm-whitelist/require-algorithm-whitelist.test.ts +0 -72
- package/src/rules/require-audience-validation/index.ts +0 -120
- package/src/rules/require-audience-validation/require-audience-validation.test.ts +0 -68
- package/src/rules/require-expiration/index.ts +0 -170
- package/src/rules/require-expiration/require-expiration.test.ts +0 -135
- package/src/rules/require-issued-at/index.ts +0 -143
- package/src/rules/require-issued-at/require-issued-at.test.ts +0 -64
- package/src/rules/require-issuer-validation/index.ts +0 -120
- package/src/rules/require-issuer-validation/require-issuer-validation.test.ts +0 -68
- package/src/rules/require-max-age/index.ts +0 -117
- package/src/rules/require-max-age/require-max-age.test.ts +0 -69
- package/src/tests/coverage-dual-layer.test.ts +0 -323
- package/src/types/index.ts +0 -107
- package/src/utils/index.ts +0 -285
- package/src/utils/utils.test.ts +0 -324
- /package/{dist/src → src}/types/index.d.ts +0 -0
package/src/oxlint.test.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
3
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
4
|
-
* MIT license that can be found in the LICENSE file.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { describe, it, expect } from 'vitest';
|
|
8
|
-
import * as fs from 'node:fs';
|
|
9
|
-
import * as path from 'node:path';
|
|
10
|
-
import { plugin as mainPlugin } from './index';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Smoke test for the `./oxlint` sub-export.
|
|
14
|
-
*
|
|
15
|
-
* Runtime shape: `oxlint.ts` uses `export = plugin`, which compiles to
|
|
16
|
-
* `module.exports = plugin`. From an ESM dynamic `import('./oxlint')` view,
|
|
17
|
-
* Node's CJS interop surfaces that value at `.default`. From oxlint's
|
|
18
|
-
* `require('eslint-plugin-jwt-security/oxlint')` view, it sits at the top of
|
|
19
|
-
* `module.exports` — exactly the `{ meta, rules }` shape oxlint's JS plugin
|
|
20
|
-
* loader expects.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
type Plugin = typeof mainPlugin;
|
|
24
|
-
|
|
25
|
-
describe('eslint-plugin-jwt-security/oxlint sub-export', () => {
|
|
26
|
-
it('declares the ./oxlint sub-export in package.json', () => {
|
|
27
|
-
const pkgJson = JSON.parse(
|
|
28
|
-
fs.readFileSync(path.resolve(__dirname, '..', 'package.json'), 'utf8'),
|
|
29
|
-
);
|
|
30
|
-
expect(pkgJson.exports['./oxlint']).toEqual({
|
|
31
|
-
types: './src/oxlint.d.ts',
|
|
32
|
-
default: './src/oxlint.js',
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('re-exports the plugin object (meta + rules at top level)', async () => {
|
|
37
|
-
const oxlintModule = await import('./oxlint.js');
|
|
38
|
-
const oxlintPlugin = (oxlintModule as unknown as { default: Plugin }).default;
|
|
39
|
-
expect(oxlintPlugin).toBeDefined();
|
|
40
|
-
expect(oxlintPlugin.meta?.name).toBe('eslint-plugin-jwt-security');
|
|
41
|
-
expect(oxlintPlugin.rules).toBeDefined();
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('exposes the same rule names as the main entry (no rules dropped)', async () => {
|
|
45
|
-
const oxlintModule = await import('./oxlint.js');
|
|
46
|
-
const oxlintPlugin = (oxlintModule as unknown as { default: Plugin }).default;
|
|
47
|
-
expect(Object.keys(oxlintPlugin.rules || {}).toSorted()).toEqual(
|
|
48
|
-
Object.keys(mainPlugin.rules || {}).toSorted(),
|
|
49
|
-
);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it('exposes the same rule references (pass-through, not a copy)', async () => {
|
|
53
|
-
const oxlintModule = await import('./oxlint.js');
|
|
54
|
-
const oxlintPlugin = (oxlintModule as unknown as { default: Plugin }).default;
|
|
55
|
-
for (const ruleName of Object.keys(mainPlugin.rules || {})) {
|
|
56
|
-
expect(oxlintPlugin.rules?.[ruleName]).toBe(
|
|
57
|
-
(mainPlugin.rules as Record<string, unknown>)[ruleName],
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
});
|
package/src/oxlint.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
3
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
4
|
-
* MIT license that can be found in the LICENSE file.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* oxlint sub-export.
|
|
9
|
-
*
|
|
10
|
-
* Consumers wire oxlint to this entry via:
|
|
11
|
-
* { "jsPlugins": ["eslint-plugin-jwt-security/oxlint"] }
|
|
12
|
-
*
|
|
13
|
-
* oxlint's JS plugin loader does `require('eslint-plugin-jwt-security/oxlint')` and reads
|
|
14
|
-
* `.rules` off the result. The compiled output of `export = plugin` is
|
|
15
|
-
* `module.exports = plugin`, which gives oxlint the plugin object directly
|
|
16
|
-
* (`{ meta, rules }`) — no wrapper, no `.default` indirection.
|
|
17
|
-
*
|
|
18
|
-
* Generated by scripts/generate-oxlint-shims.mjs. Do not edit by hand.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
import { plugin } from './index';
|
|
22
|
-
|
|
23
|
-
export = plugin;
|
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
3
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
4
|
-
* MIT license that can be found in the LICENSE file.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* ESLint Rule: no-algorithm-confusion
|
|
9
|
-
*
|
|
10
|
-
* Detects algorithm confusion attacks where symmetric algorithms (HS256/384/512)
|
|
11
|
-
* are used with asymmetric keys (public keys). This allows attackers to sign
|
|
12
|
-
* tokens using the public key as an HMAC secret.
|
|
13
|
-
*
|
|
14
|
-
* CWE-347: Improper Verification of Cryptographic Signature
|
|
15
|
-
*
|
|
16
|
-
* @see https://portswigger.net/web-security/jwt/algorithm-confusion
|
|
17
|
-
*/
|
|
18
|
-
import type { TSESLint, TSESTree } from '@interlace/eslint-devkit';
|
|
19
|
-
import { createRule, formatLLMMessage, MessageIcons } from '@interlace/eslint-devkit';
|
|
20
|
-
import {
|
|
21
|
-
isVerifyOperation,
|
|
22
|
-
getOptionsArgument,
|
|
23
|
-
extractAlgorithms,
|
|
24
|
-
} from '../../utils';
|
|
25
|
-
import type { NoAlgorithmConfusionOptions } from '../../types';
|
|
26
|
-
|
|
27
|
-
type MessageIds =
|
|
28
|
-
| 'algorithmConfusion'
|
|
29
|
-
| 'symmetricWithPublicKey'
|
|
30
|
-
| 'useAsymmetricAlgorithm';
|
|
31
|
-
|
|
32
|
-
type RuleOptions = [NoAlgorithmConfusionOptions?];
|
|
33
|
-
|
|
34
|
-
// Patterns that indicate a public key
|
|
35
|
-
const PUBLIC_KEY_PATTERNS = [
|
|
36
|
-
/public/i,
|
|
37
|
-
/\.pub$/,
|
|
38
|
-
/\.pem$/,
|
|
39
|
-
/-----BEGIN PUBLIC KEY-----/,
|
|
40
|
-
/-----BEGIN RSA PUBLIC KEY-----/,
|
|
41
|
-
/-----BEGIN EC PUBLIC KEY-----/,
|
|
42
|
-
/getPublicKey/i,
|
|
43
|
-
/publicKey/i,
|
|
44
|
-
/jwks/i,
|
|
45
|
-
];
|
|
46
|
-
|
|
47
|
-
export const noAlgorithmConfusion = createRule<RuleOptions, MessageIds>({
|
|
48
|
-
name: 'no-algorithm-confusion',
|
|
49
|
-
meta: {
|
|
50
|
-
type: 'problem',
|
|
51
|
-
docs: {
|
|
52
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-jwt-security/docs/rules/no-algorithm-confusion.md',
|
|
53
|
-
description:
|
|
54
|
-
'Prevent algorithm confusion attacks using symmetric algorithms with asymmetric keys',
|
|
55
|
-
cwe: 'CWE-347',
|
|
56
|
-
cvss: 9.5,
|
|
57
|
-
},
|
|
58
|
-
fixable: undefined,
|
|
59
|
-
hasSuggestions: false,
|
|
60
|
-
messages: {
|
|
61
|
-
algorithmConfusion: formatLLMMessage({
|
|
62
|
-
icon: MessageIcons.SECURITY,
|
|
63
|
-
issueName: 'JWT Algorithm Confusion Attack',
|
|
64
|
-
cwe: 'CWE-347',
|
|
65
|
-
description:
|
|
66
|
-
'Symmetric algorithm (HS*) used with public key allows token forgery',
|
|
67
|
-
severity: 'CRITICAL',
|
|
68
|
-
fix: 'Use asymmetric algorithms (RS256, ES256) with public keys',
|
|
69
|
-
documentationLink:
|
|
70
|
-
'https://portswigger.net/web-security/jwt/algorithm-confusion',
|
|
71
|
-
}),
|
|
72
|
-
symmetricWithPublicKey: formatLLMMessage({
|
|
73
|
-
icon: MessageIcons.SECURITY,
|
|
74
|
-
issueName: 'Symmetric Algorithm with Public Key',
|
|
75
|
-
cwe: 'CWE-347',
|
|
76
|
-
description:
|
|
77
|
-
'HS256/384/512 should only be used with shared secrets, not public keys',
|
|
78
|
-
severity: 'CRITICAL',
|
|
79
|
-
fix: 'Switch to RS256 or ES256 for asymmetric key verification',
|
|
80
|
-
documentationLink:
|
|
81
|
-
'https://portswigger.net/web-security/jwt/algorithm-confusion',
|
|
82
|
-
}),
|
|
83
|
-
useAsymmetricAlgorithm: formatLLMMessage({
|
|
84
|
-
icon: MessageIcons.INFO,
|
|
85
|
-
issueName: 'Use Asymmetric Algorithm',
|
|
86
|
-
description: 'Replace HS* with RS* or ES* algorithm',
|
|
87
|
-
severity: 'LOW',
|
|
88
|
-
fix: 'Use RS256 or ES256 with public key verification',
|
|
89
|
-
documentationLink: 'https://tools.ietf.org/html/rfc8725',
|
|
90
|
-
}),
|
|
91
|
-
},
|
|
92
|
-
schema: [
|
|
93
|
-
{
|
|
94
|
-
type: 'object',
|
|
95
|
-
properties: {
|
|
96
|
-
symmetricAlgorithms: {
|
|
97
|
-
type: 'array',
|
|
98
|
-
items: { type: 'string' },
|
|
99
|
-
default: ['HS256', 'HS384', 'HS512'],
|
|
100
|
-
description: 'Algorithms to flag when used with public keys',
|
|
101
|
-
},
|
|
102
|
-
trustedSanitizers: {
|
|
103
|
-
type: 'array',
|
|
104
|
-
items: { type: 'string' },
|
|
105
|
-
default: [],
|
|
106
|
-
},
|
|
107
|
-
trustedAnnotations: {
|
|
108
|
-
type: 'array',
|
|
109
|
-
items: { type: 'string' },
|
|
110
|
-
default: [],
|
|
111
|
-
},
|
|
112
|
-
strictMode: {
|
|
113
|
-
type: 'boolean',
|
|
114
|
-
default: false,
|
|
115
|
-
},
|
|
116
|
-
},
|
|
117
|
-
additionalProperties: false,
|
|
118
|
-
},
|
|
119
|
-
],
|
|
120
|
-
},
|
|
121
|
-
defaultOptions: [
|
|
122
|
-
{
|
|
123
|
-
symmetricAlgorithms: ['HS256', 'HS384', 'HS512'],
|
|
124
|
-
trustedSanitizers: [],
|
|
125
|
-
trustedAnnotations: [],
|
|
126
|
-
strictMode: false,
|
|
127
|
-
},
|
|
128
|
-
],
|
|
129
|
-
create(context: TSESLint.RuleContext<MessageIds, RuleOptions>) {
|
|
130
|
-
const options = context.options[0] ?? {};
|
|
131
|
-
const symmetricAlgSet = new Set(
|
|
132
|
-
options.symmetricAlgorithms ?? ['HS256', 'HS384', 'HS512']
|
|
133
|
-
);
|
|
134
|
-
const sourceCode = context.sourceCode;
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Check if a node looks like a public key reference
|
|
138
|
-
*/
|
|
139
|
-
const looksLikePublicKey = (node: TSESTree.Node): boolean => {
|
|
140
|
-
const text = sourceCode.getText(node);
|
|
141
|
-
return PUBLIC_KEY_PATTERNS.some((pattern) => pattern.test(text));
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Check if algorithms include symmetric algorithms
|
|
146
|
-
*/
|
|
147
|
-
const hasSymmetricAlgorithm = (algorithms: string[]): string | null => {
|
|
148
|
-
for (const alg of algorithms) {
|
|
149
|
-
if (symmetricAlgSet.has(alg)) {
|
|
150
|
-
return alg;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
return null;
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
return {
|
|
157
|
-
CallExpression(node: TSESTree.CallExpression) {
|
|
158
|
-
// Only check verify operations
|
|
159
|
-
if (!isVerifyOperation(node)) {
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// Need at least 2 arguments: token, key
|
|
164
|
-
if (node.arguments.length < 2) {
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const keyArg = node.arguments[1];
|
|
169
|
-
const optionsArg = getOptionsArgument(node, 2);
|
|
170
|
-
|
|
171
|
-
// Check if key looks like a public key
|
|
172
|
-
if (!looksLikePublicKey(keyArg)) {
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// If there are options, check the algorithms
|
|
177
|
-
if (optionsArg) {
|
|
178
|
-
const algorithms = extractAlgorithms(optionsArg);
|
|
179
|
-
const symmetricAlg = hasSymmetricAlgorithm(algorithms);
|
|
180
|
-
|
|
181
|
-
if (symmetricAlg) {
|
|
182
|
-
// Find the algorithm node for precise error location
|
|
183
|
-
for (const prop of optionsArg.properties) {
|
|
184
|
-
if (
|
|
185
|
-
prop.type === 'Property' &&
|
|
186
|
-
prop.key.type === 'Identifier' &&
|
|
187
|
-
['algorithms', 'algorithm', 'alg'].includes(prop.key.name)
|
|
188
|
-
) {
|
|
189
|
-
context.report({
|
|
190
|
-
node: prop.value,
|
|
191
|
-
messageId: 'algorithmConfusion',
|
|
192
|
-
data: { algorithm: symmetricAlg },
|
|
193
|
-
});
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// If no options specified but key looks like public key,
|
|
201
|
-
// and we're in verify, flag as potential issue
|
|
202
|
-
// (verification without explicit algorithm with public key is risky)
|
|
203
|
-
},
|
|
204
|
-
};
|
|
205
|
-
},
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
export default noAlgorithmConfusion;
|
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for no-algorithm-confusion rule
|
|
3
|
-
* Security: CWE-347 (Algorithm Confusion Attack)
|
|
4
|
-
*/
|
|
5
|
-
import { RuleTester } from '@typescript-eslint/rule-tester';
|
|
6
|
-
import { describe, it, afterAll } from 'vitest';
|
|
7
|
-
import parser from '@typescript-eslint/parser';
|
|
8
|
-
import { noAlgorithmConfusion } from './index';
|
|
9
|
-
|
|
10
|
-
RuleTester.afterAll = afterAll;
|
|
11
|
-
RuleTester.it = it;
|
|
12
|
-
RuleTester.itOnly = it.only;
|
|
13
|
-
RuleTester.describe = describe;
|
|
14
|
-
|
|
15
|
-
const ruleTester = new RuleTester({
|
|
16
|
-
languageOptions: {
|
|
17
|
-
parser,
|
|
18
|
-
ecmaVersion: 2022,
|
|
19
|
-
sourceType: 'module',
|
|
20
|
-
},
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
describe('no-algorithm-confusion', () => {
|
|
24
|
-
describe('Valid Code - Safe Patterns', () => {
|
|
25
|
-
ruleTester.run('valid - asymmetric algorithms with public keys', noAlgorithmConfusion, {
|
|
26
|
-
valid: [
|
|
27
|
-
// RS256 with public key - SAFE
|
|
28
|
-
{ code: `jwt.verify(token, publicKey, { algorithms: ['RS256'] });` },
|
|
29
|
-
// ES256 with public key - SAFE
|
|
30
|
-
{ code: `jwt.verify(token, publicKey, { algorithms: ['ES256'] });` },
|
|
31
|
-
// Multiple asymmetric algorithms
|
|
32
|
-
{ code: `jwt.verify(token, publicKey, { algorithms: ['RS256', 'ES256'] });` },
|
|
33
|
-
// HS256 with secret (not public key) - SAFE
|
|
34
|
-
{ code: `jwt.verify(token, secret, { algorithms: ['HS256'] });` },
|
|
35
|
-
// HS256 with env var - SAFE
|
|
36
|
-
{ code: `jwt.verify(token, process.env.JWT_SECRET, { algorithms: ['HS256'] });` },
|
|
37
|
-
// No options specified
|
|
38
|
-
{ code: `jwt.verify(token, publicKey);` },
|
|
39
|
-
// Sign operation (not verify)
|
|
40
|
-
{ code: `jwt.sign(payload, privateKey, { algorithm: 'HS256' });` },
|
|
41
|
-
// Only 1 argument (edge case - line 157 coverage)
|
|
42
|
-
{ code: `jwt.verify(token);` },
|
|
43
|
-
// No key that looks like public key
|
|
44
|
-
{ code: `jwt.verify(token, sharedSecret, { algorithms: ['HS256'] });` },
|
|
45
|
-
// Empty options object with public key (no algorithms)
|
|
46
|
-
{ code: `jwt.verify(token, publicKey, {});` },
|
|
47
|
-
// Options without algorithms property
|
|
48
|
-
{ code: `jwt.verify(token, publicKey, { complete: true });` },
|
|
49
|
-
],
|
|
50
|
-
invalid: [],
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
describe('Invalid Code - Algorithm Confusion', () => {
|
|
55
|
-
ruleTester.run('invalid - symmetric with public key', noAlgorithmConfusion, {
|
|
56
|
-
valid: [],
|
|
57
|
-
invalid: [
|
|
58
|
-
// HS256 with publicKey variable
|
|
59
|
-
{
|
|
60
|
-
code: `jwt.verify(token, publicKey, { algorithms: ['HS256'] });`,
|
|
61
|
-
errors: [{ messageId: 'algorithmConfusion' }],
|
|
62
|
-
},
|
|
63
|
-
// HS384 with public key
|
|
64
|
-
{
|
|
65
|
-
code: `jwt.verify(token, publicKey, { algorithms: ['HS384'] });`,
|
|
66
|
-
errors: [{ messageId: 'algorithmConfusion' }],
|
|
67
|
-
},
|
|
68
|
-
// HS512 with public key
|
|
69
|
-
{
|
|
70
|
-
code: `jwt.verify(token, publicKey, { algorithms: ['HS512'] });`,
|
|
71
|
-
errors: [{ messageId: 'algorithmConfusion' }],
|
|
72
|
-
},
|
|
73
|
-
// Single algorithm option
|
|
74
|
-
{
|
|
75
|
-
code: `jwt.verify(token, publicKey, { algorithm: 'HS256' });`,
|
|
76
|
-
errors: [{ messageId: 'algorithmConfusion' }],
|
|
77
|
-
},
|
|
78
|
-
// Mixed algorithms with symmetric
|
|
79
|
-
{
|
|
80
|
-
code: `jwt.verify(token, publicKey, { algorithms: ['RS256', 'HS256'] });`,
|
|
81
|
-
errors: [{ messageId: 'algorithmConfusion' }],
|
|
82
|
-
},
|
|
83
|
-
// getPublicKey() function
|
|
84
|
-
{
|
|
85
|
-
code: `jwt.verify(token, getPublicKey(), { algorithms: ['HS256'] });`,
|
|
86
|
-
errors: [{ messageId: 'algorithmConfusion' }],
|
|
87
|
-
},
|
|
88
|
-
// JWKS pattern
|
|
89
|
-
{
|
|
90
|
-
code: `jwt.verify(token, jwksKey, { algorithms: ['HS256'] });`,
|
|
91
|
-
errors: [{ messageId: 'algorithmConfusion' }],
|
|
92
|
-
},
|
|
93
|
-
// alg shorthand with public key
|
|
94
|
-
{
|
|
95
|
-
code: `jwt.verify(token, publicKey, { alg: 'HS256' });`,
|
|
96
|
-
errors: [{ messageId: 'algorithmConfusion' }],
|
|
97
|
-
},
|
|
98
|
-
// jwtVerify with public key
|
|
99
|
-
{
|
|
100
|
-
code: `jwtVerify(token, publicKey, { algorithms: ['HS256'] });`,
|
|
101
|
-
errors: [{ messageId: 'algorithmConfusion' }],
|
|
102
|
-
},
|
|
103
|
-
],
|
|
104
|
-
});
|
|
105
|
-
});
|
|
106
|
-
});
|
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) 2025 Ofri Peretz
|
|
3
|
-
* Licensed under the MIT License. Use of this source code is governed by the
|
|
4
|
-
* MIT license that can be found in the LICENSE file.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* ESLint Rule: no-algorithm-none
|
|
9
|
-
*
|
|
10
|
-
* Detects attempts to use the 'none' algorithm which bypasses JWT signature verification.
|
|
11
|
-
* This is a critical vulnerability (CVE-2022-23540) that allows attackers to forge JWT tokens.
|
|
12
|
-
*
|
|
13
|
-
* CWE-347: Improper Verification of Cryptographic Signature
|
|
14
|
-
*
|
|
15
|
-
* @see https://nvd.nist.gov/vuln/detail/CVE-2022-23540
|
|
16
|
-
* @see https://tools.ietf.org/html/rfc8725
|
|
17
|
-
*/
|
|
18
|
-
import type { TSESLint, TSESTree } from '@interlace/eslint-devkit';
|
|
19
|
-
import { createRule, formatLLMMessage, MessageIcons } from '@interlace/eslint-devkit';
|
|
20
|
-
import {
|
|
21
|
-
isVerifyOperation,
|
|
22
|
-
isSignOperation,
|
|
23
|
-
getOptionsArgument,
|
|
24
|
-
isTestFile,
|
|
25
|
-
} from '../../utils';
|
|
26
|
-
import type { NoAlgorithmNoneOptions } from '../../types';
|
|
27
|
-
|
|
28
|
-
type MessageIds =
|
|
29
|
-
| 'algorithmNone'
|
|
30
|
-
| 'algorithmNoneInArray'
|
|
31
|
-
| 'emptyAlgorithms'
|
|
32
|
-
| 'useSecureAlgorithm'
|
|
33
|
-
| 'decodeWithoutVerify';
|
|
34
|
-
|
|
35
|
-
type RuleOptions = [NoAlgorithmNoneOptions?];
|
|
36
|
-
|
|
37
|
-
export const noAlgorithmNone = createRule<RuleOptions, MessageIds>({
|
|
38
|
-
name: 'no-algorithm-none',
|
|
39
|
-
meta: {
|
|
40
|
-
type: 'problem',
|
|
41
|
-
docs: {
|
|
42
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-jwt-security/docs/rules/no-algorithm-none.md',
|
|
43
|
-
description:
|
|
44
|
-
'Disallow JWT "none" algorithm which bypasses signature verification (CVE-2022-23540)',
|
|
45
|
-
cwe: 'CWE-347',
|
|
46
|
-
cvss: 9.5,
|
|
47
|
-
confidence: 'high',
|
|
48
|
-
},
|
|
49
|
-
fixable: undefined,
|
|
50
|
-
hasSuggestions: false,
|
|
51
|
-
messages: {
|
|
52
|
-
algorithmNone: formatLLMMessage({
|
|
53
|
-
icon: MessageIcons.SECURITY,
|
|
54
|
-
issueName: 'JWT Algorithm None Attack',
|
|
55
|
-
cwe: 'CWE-347',
|
|
56
|
-
description:
|
|
57
|
-
'Using alg:"none" bypasses signature verification, allowing token forgery',
|
|
58
|
-
severity: 'CRITICAL',
|
|
59
|
-
fix: 'Remove "none" and use RS256, ES256, or other secure algorithms',
|
|
60
|
-
documentationLink: 'https://nvd.nist.gov/vuln/detail/CVE-2022-23540',
|
|
61
|
-
}),
|
|
62
|
-
algorithmNoneInArray: formatLLMMessage({
|
|
63
|
-
icon: MessageIcons.SECURITY,
|
|
64
|
-
issueName: 'JWT Algorithm None in Whitelist',
|
|
65
|
-
cwe: 'CWE-347',
|
|
66
|
-
description:
|
|
67
|
-
'Including "none" in algorithms array allows unsigned tokens',
|
|
68
|
-
severity: 'CRITICAL',
|
|
69
|
-
fix: 'Remove "none" from the algorithms array',
|
|
70
|
-
documentationLink: 'https://nvd.nist.gov/vuln/detail/CVE-2022-23540',
|
|
71
|
-
}),
|
|
72
|
-
emptyAlgorithms: formatLLMMessage({
|
|
73
|
-
icon: MessageIcons.SECURITY,
|
|
74
|
-
issueName: 'Empty Algorithms Array',
|
|
75
|
-
cwe: 'CWE-347',
|
|
76
|
-
description:
|
|
77
|
-
'Empty algorithms array may default to accepting any algorithm including none',
|
|
78
|
-
severity: 'HIGH',
|
|
79
|
-
fix: 'Specify explicit algorithms: ["RS256"] or ["ES256"]',
|
|
80
|
-
documentationLink: 'https://tools.ietf.org/html/rfc8725',
|
|
81
|
-
}),
|
|
82
|
-
useSecureAlgorithm: formatLLMMessage({
|
|
83
|
-
icon: MessageIcons.INFO,
|
|
84
|
-
issueName: 'Use Secure Algorithm',
|
|
85
|
-
description: 'Replace with a secure algorithm',
|
|
86
|
-
severity: 'LOW',
|
|
87
|
-
fix: 'Use algorithms: ["RS256"] or algorithms: ["ES256"]',
|
|
88
|
-
documentationLink: 'https://tools.ietf.org/html/rfc8725',
|
|
89
|
-
}),
|
|
90
|
-
decodeWithoutVerify: formatLLMMessage({
|
|
91
|
-
icon: MessageIcons.SECURITY,
|
|
92
|
-
issueName: 'JWT Decode Without Verify',
|
|
93
|
-
cwe: 'CWE-347',
|
|
94
|
-
description:
|
|
95
|
-
'jwt.decode() does NOT verify the signature — using its result as if it were authenticated is functionally equivalent to algorithm:"none". Any token, including forged ones, will decode.',
|
|
96
|
-
severity: 'CRITICAL',
|
|
97
|
-
fix: 'Use jwt.verify(token, secret, { algorithms: ["RS256"] }) instead. Reserve jwt.decode() for non-security inspection only (e.g. logging the issuer).',
|
|
98
|
-
documentationLink: 'https://github.com/auth0/node-jsonwebtoken#jwtdecodetoken--options',
|
|
99
|
-
}),
|
|
100
|
-
},
|
|
101
|
-
schema: [
|
|
102
|
-
{
|
|
103
|
-
type: 'object',
|
|
104
|
-
properties: {
|
|
105
|
-
allowInTests: {
|
|
106
|
-
type: 'boolean',
|
|
107
|
-
default: false,
|
|
108
|
-
description: 'Allow "none" algorithm in test files',
|
|
109
|
-
},
|
|
110
|
-
trustedSanitizers: {
|
|
111
|
-
type: 'array',
|
|
112
|
-
items: { type: 'string' },
|
|
113
|
-
default: [],
|
|
114
|
-
},
|
|
115
|
-
trustedAnnotations: {
|
|
116
|
-
type: 'array',
|
|
117
|
-
items: { type: 'string' },
|
|
118
|
-
default: [],
|
|
119
|
-
},
|
|
120
|
-
strictMode: {
|
|
121
|
-
type: 'boolean',
|
|
122
|
-
default: false,
|
|
123
|
-
},
|
|
124
|
-
},
|
|
125
|
-
additionalProperties: false,
|
|
126
|
-
},
|
|
127
|
-
],
|
|
128
|
-
},
|
|
129
|
-
defaultOptions: [
|
|
130
|
-
{
|
|
131
|
-
allowInTests: false,
|
|
132
|
-
trustedSanitizers: [],
|
|
133
|
-
trustedAnnotations: [],
|
|
134
|
-
strictMode: false,
|
|
135
|
-
},
|
|
136
|
-
],
|
|
137
|
-
create(context: TSESLint.RuleContext<MessageIds, RuleOptions>) {
|
|
138
|
-
const options = context.options[0] ?? {};
|
|
139
|
-
const { allowInTests = false } = options;
|
|
140
|
-
const filename = context.filename;
|
|
141
|
-
|
|
142
|
-
// Skip test files if configured
|
|
143
|
-
if (allowInTests && isTestFile(filename)) {
|
|
144
|
-
return {};
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Check if an algorithm value is 'none' (case insensitive)
|
|
149
|
-
*/
|
|
150
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
151
|
-
const isNoneAlgorithm = (value: string): boolean => {
|
|
152
|
-
return value.toLowerCase() === 'none';
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Check options object for 'none' algorithm
|
|
157
|
-
*/
|
|
158
|
-
const checkOptionsForNone = (
|
|
159
|
-
optionsNode: TSESTree.ObjectExpression
|
|
160
|
-
): void => {
|
|
161
|
-
for (const prop of optionsNode.properties) {
|
|
162
|
-
if (prop.type !== 'Property' || prop.key.type !== 'Identifier') {
|
|
163
|
-
continue;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
const keyName = prop.key.name;
|
|
167
|
-
if (
|
|
168
|
-
keyName !== 'algorithms' &&
|
|
169
|
-
keyName !== 'algorithm' &&
|
|
170
|
-
keyName !== 'alg'
|
|
171
|
-
) {
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
// Single algorithm: { algorithm: 'none' }
|
|
176
|
-
if (
|
|
177
|
-
prop.value.type === 'Literal' &&
|
|
178
|
-
typeof prop.value.value === 'string'
|
|
179
|
-
) {
|
|
180
|
-
if (isNoneAlgorithm(prop.value.value)) {
|
|
181
|
-
context.report({
|
|
182
|
-
node: prop.value,
|
|
183
|
-
messageId: 'algorithmNone',
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// Array of algorithms: { algorithms: ['none'] } or { algorithms: [] }
|
|
189
|
-
if (prop.value.type === 'ArrayExpression') {
|
|
190
|
-
// Check for empty array
|
|
191
|
-
if (prop.value.elements.length === 0) {
|
|
192
|
-
context.report({
|
|
193
|
-
node: prop.value,
|
|
194
|
-
messageId: 'emptyAlgorithms',
|
|
195
|
-
});
|
|
196
|
-
continue;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// Check each element for 'none'
|
|
200
|
-
for (const elem of prop.value.elements) {
|
|
201
|
-
if (
|
|
202
|
-
elem &&
|
|
203
|
-
elem.type === 'Literal' &&
|
|
204
|
-
typeof elem.value === 'string' &&
|
|
205
|
-
isNoneAlgorithm(elem.value)
|
|
206
|
-
) {
|
|
207
|
-
context.report({
|
|
208
|
-
node: elem,
|
|
209
|
-
messageId: 'algorithmNoneInArray',
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
};
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Detect `jwt.decode()` calls — closes the audit FN where
|
|
219
|
-
* `jwt.decode(token)` followed by trusting the decoded payload was
|
|
220
|
-
* silent, despite being functionally equivalent to algorithm:"none".
|
|
221
|
-
* See benchmarks/AUDIT_PATTERNS.md §3.8 ("verify-then-trust mismatch").
|
|
222
|
-
*
|
|
223
|
-
* The check is conservative: we flag every `jwt.decode()` call. In
|
|
224
|
-
* the rare case where `decode` is used legitimately (e.g. logging
|
|
225
|
-
* the issuer of an inbound token without authentication semantics),
|
|
226
|
-
* the user can disable the rule on that line. The default-on
|
|
227
|
-
* behaviour is correct — most production uses of `jwt.decode` are
|
|
228
|
-
* authentication mistakes.
|
|
229
|
-
*/
|
|
230
|
-
// oxlint-disable-next-line consistent-function-scoping
|
|
231
|
-
function isDecodeCall(node: TSESTree.CallExpression): boolean {
|
|
232
|
-
const callee = node.callee;
|
|
233
|
-
if (callee.type !== 'MemberExpression') return false;
|
|
234
|
-
if (callee.property.type !== 'Identifier') return false;
|
|
235
|
-
if (callee.property.name !== 'decode') return false;
|
|
236
|
-
// Object should be `jwt` (or imported as such). Check both
|
|
237
|
-
// `jwt.decode` and `JWT.decode` patterns.
|
|
238
|
-
if (callee.object.type === 'Identifier') {
|
|
239
|
-
const name = callee.object.name.toLowerCase();
|
|
240
|
-
return name === 'jwt' || name === 'jsonwebtoken';
|
|
241
|
-
}
|
|
242
|
-
return false;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
return {
|
|
246
|
-
CallExpression(node: TSESTree.CallExpression) {
|
|
247
|
-
// Detect `jwt.decode(...)` — equivalent to algorithm:"none" if
|
|
248
|
-
// its result is trusted. We always flag (with critical severity)
|
|
249
|
-
// because the FN risk far outweighs the FP risk on real code.
|
|
250
|
-
if (isDecodeCall(node)) {
|
|
251
|
-
context.report({ node, messageId: 'decodeWithoutVerify' });
|
|
252
|
-
return;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// Check both verify and sign operations
|
|
256
|
-
if (!isVerifyOperation(node) && !isSignOperation(node)) {
|
|
257
|
-
return;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
// Get options argument (usually 3rd argument)
|
|
261
|
-
const optionsArg = getOptionsArgument(node, 2);
|
|
262
|
-
if (optionsArg) {
|
|
263
|
-
checkOptionsForNone(optionsArg);
|
|
264
|
-
}
|
|
265
|
-
},
|
|
266
|
-
};
|
|
267
|
-
},
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
export default noAlgorithmNone;
|