eslint-plugin-browser-security 1.2.13 → 1.3.0
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 +1 -1
- package/package.json +3 -3
- package/src/index.js +1 -159
- package/src/oxlint.js +1 -3
- package/src/rules/detect-mixed-content/index.js +1 -38
- package/src/rules/no-allow-arbitrary-loads/index.js +1 -41
- package/src/rules/no-clickjacking/index.js +1 -361
- package/src/rules/no-client-side-auth-logic/index.js +1 -67
- package/src/rules/no-cookie-auth-tokens/index.js +1 -109
- package/src/rules/no-credentials-in-query-params/index.js +1 -55
- package/src/rules/no-disabled-certificate-validation/index.js +1 -59
- package/src/rules/no-dynamic-service-worker-url/index.js +1 -78
- package/src/rules/no-eval/index.js +1 -160
- package/src/rules/no-filereader-innerhtml/index.js +1 -196
- package/src/rules/no-http-urls/index.js +1 -109
- package/src/rules/no-innerhtml/index.js +1 -150
- package/src/rules/no-insecure-redirects/index.js +1 -238
- package/src/rules/no-insecure-websocket/index.js +1 -57
- package/src/rules/no-jwt-in-storage/index.js +1 -178
- package/src/rules/no-missing-cors-check/index.js +1 -351
- package/src/rules/no-missing-csrf-protection/index.js +1 -165
- package/src/rules/no-missing-security-headers/index.js +1 -197
- package/src/rules/no-password-in-url/index.js +1 -47
- package/src/rules/no-permissive-cors/index.js +1 -65
- package/src/rules/no-postmessage-innerhtml/index.js +1 -161
- package/src/rules/no-postmessage-wildcard-origin/index.js +1 -112
- package/src/rules/no-sensitive-cookie-js/index.js +1 -151
- package/src/rules/no-sensitive-data-in-analytics/index.js +1 -58
- package/src/rules/no-sensitive-data-in-cache/index.js +1 -47
- package/src/rules/no-sensitive-indexeddb/index.js +1 -110
- package/src/rules/no-sensitive-localstorage/index.js +1 -173
- package/src/rules/no-sensitive-sessionstorage/index.js +1 -144
- package/src/rules/no-tracking-without-consent/index.js +1 -65
- package/src/rules/no-unencrypted-transmission/index.js +1 -208
- package/src/rules/no-unescaped-url-parameter/index.js +1 -319
- package/src/rules/no-unsafe-eval-csp/index.js +1 -75
- package/src/rules/no-unsafe-inline-csp/index.js +1 -75
- package/src/rules/no-unvalidated-deeplinks/index.js +1 -59
- package/src/rules/no-websocket-eval/index.js +1 -197
- package/src/rules/no-websocket-innerhtml/index.js +1 -185
- package/src/rules/no-worker-message-innerhtml/index.js +1 -181
- package/src/rules/require-blob-url-revocation/index.js +1 -93
- package/src/rules/require-cookie-secure-attrs/index.js +1 -101
- package/src/rules/require-csp-headers/index.js +1 -59
- package/src/rules/require-https-only/index.js +1 -57
- package/src/rules/require-mime-type-validation/index.js +1 -61
- package/src/rules/require-postmessage-origin-check/index.js +1 -132
- package/src/rules/require-url-validation/index.js +1 -68
- package/src/rules/require-websocket-wss/index.js +1 -125
|
@@ -1,150 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.noInnerhtml = void 0;
|
|
4
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
-
const DEFAULT_SANITIZERS = [
|
|
6
|
-
'DOMPurify.sanitize',
|
|
7
|
-
'sanitize',
|
|
8
|
-
'sanitizeHtml',
|
|
9
|
-
'xss',
|
|
10
|
-
'purify',
|
|
11
|
-
];
|
|
12
|
-
function isSanitized(node, sourceCode, sanitizers) {
|
|
13
|
-
const text = sourceCode.getText(node);
|
|
14
|
-
return sanitizers.some((s) => text.includes(s));
|
|
15
|
-
}
|
|
16
|
-
function isLiteralString(node) {
|
|
17
|
-
if (node.type === 'Literal' && typeof node.value === 'string') {
|
|
18
|
-
return true;
|
|
19
|
-
}
|
|
20
|
-
if (node.type === 'TemplateLiteral' && node.expressions.length === 0) {
|
|
21
|
-
return true;
|
|
22
|
-
}
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
exports.noInnerhtml = (0, eslint_devkit_1.createRule)({
|
|
26
|
-
name: 'no-innerhtml',
|
|
27
|
-
meta: {
|
|
28
|
-
type: 'problem',
|
|
29
|
-
docs: {
|
|
30
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-innerhtml.md',
|
|
31
|
-
description: 'Disallow dangerous innerHTML/outerHTML assignments that can lead to XSS',
|
|
32
|
-
cwe: 'CWE-79',
|
|
33
|
-
cvss: 6.1,
|
|
34
|
-
confidence: 'medium',
|
|
35
|
-
},
|
|
36
|
-
hasSuggestions: true,
|
|
37
|
-
messages: {
|
|
38
|
-
dangerousInnerHTML: (0, eslint_devkit_1.formatLLMMessage)({
|
|
39
|
-
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
40
|
-
issueName: 'Cross-Site Scripting (XSS) via innerHTML',
|
|
41
|
-
cwe: 'CWE-79',
|
|
42
|
-
description: 'Assigning to {{property}} with {{source}} can execute malicious scripts. This is a critical XSS vulnerability.',
|
|
43
|
-
severity: 'CRITICAL',
|
|
44
|
-
fix: 'Use textContent for text, or sanitize with DOMPurify.sanitize() before assignment.',
|
|
45
|
-
documentationLink: 'https://owasp.org/www-community/attacks/xss/',
|
|
46
|
-
}),
|
|
47
|
-
useSanitizer: (0, eslint_devkit_1.formatLLMMessage)({
|
|
48
|
-
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
49
|
-
issueName: 'Use HTML Sanitizer',
|
|
50
|
-
description: 'Sanitize HTML before assignment',
|
|
51
|
-
severity: 'LOW',
|
|
52
|
-
fix: 'element.innerHTML = DOMPurify.sanitize(userInput);',
|
|
53
|
-
documentationLink: 'https://www.npmjs.com/package/dompurify',
|
|
54
|
-
}),
|
|
55
|
-
},
|
|
56
|
-
schema: [
|
|
57
|
-
{
|
|
58
|
-
type: 'object',
|
|
59
|
-
properties: {
|
|
60
|
-
allowInTests: {
|
|
61
|
-
type: 'boolean',
|
|
62
|
-
default: false,
|
|
63
|
-
},
|
|
64
|
-
trustedSanitizers: {
|
|
65
|
-
type: 'array',
|
|
66
|
-
items: { type: 'string' },
|
|
67
|
-
default: DEFAULT_SANITIZERS,
|
|
68
|
-
},
|
|
69
|
-
allowLiteralStrings: {
|
|
70
|
-
type: 'boolean',
|
|
71
|
-
default: true,
|
|
72
|
-
},
|
|
73
|
-
},
|
|
74
|
-
additionalProperties: false,
|
|
75
|
-
},
|
|
76
|
-
],
|
|
77
|
-
},
|
|
78
|
-
defaultOptions: [
|
|
79
|
-
{
|
|
80
|
-
allowInTests: false,
|
|
81
|
-
trustedSanitizers: DEFAULT_SANITIZERS,
|
|
82
|
-
allowLiteralStrings: true,
|
|
83
|
-
},
|
|
84
|
-
],
|
|
85
|
-
create(context, [options = {}]) {
|
|
86
|
-
const { allowInTests = false, trustedSanitizers = DEFAULT_SANITIZERS, allowLiteralStrings = true, } = options;
|
|
87
|
-
const filename = context.filename;
|
|
88
|
-
const isTestFile = allowInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
89
|
-
if (isTestFile) {
|
|
90
|
-
return {};
|
|
91
|
-
}
|
|
92
|
-
const sourceCode = context.sourceCode;
|
|
93
|
-
const dangerousProperties = new Set(['innerHTML', 'outerHTML']);
|
|
94
|
-
const dangerousSinkMethods = new Set([
|
|
95
|
-
'insertAdjacentHTML',
|
|
96
|
-
'write',
|
|
97
|
-
'writeln',
|
|
98
|
-
]);
|
|
99
|
-
function reportSink(reportNode, sinkName, taintedNode) {
|
|
100
|
-
if (allowLiteralStrings && isLiteralString(taintedNode))
|
|
101
|
-
return;
|
|
102
|
-
if (isSanitized(taintedNode, sourceCode, trustedSanitizers))
|
|
103
|
-
return;
|
|
104
|
-
let source = 'dynamic content';
|
|
105
|
-
if (taintedNode.type === 'Identifier') {
|
|
106
|
-
source = `variable "${taintedNode.name}"`;
|
|
107
|
-
}
|
|
108
|
-
else if (taintedNode.type === 'TemplateLiteral') {
|
|
109
|
-
source = 'template literal with expressions';
|
|
110
|
-
}
|
|
111
|
-
else if (taintedNode.type === 'CallExpression') {
|
|
112
|
-
source = 'function call result';
|
|
113
|
-
}
|
|
114
|
-
context.report({
|
|
115
|
-
node: reportNode,
|
|
116
|
-
messageId: 'dangerousInnerHTML',
|
|
117
|
-
data: { property: sinkName, source },
|
|
118
|
-
suggest: [{ messageId: 'useSanitizer', fix: () => null }],
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
return {
|
|
122
|
-
AssignmentExpression(node) {
|
|
123
|
-
if (node.left.type !== 'MemberExpression') {
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
const property = node.left.property;
|
|
127
|
-
if (property.type !== 'Identifier') {
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
if (!dangerousProperties.has(property.name)) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
reportSink(node, property.name, node.right);
|
|
134
|
-
},
|
|
135
|
-
CallExpression(node) {
|
|
136
|
-
if (node.callee.type !== 'MemberExpression')
|
|
137
|
-
return;
|
|
138
|
-
const property = node.callee.property;
|
|
139
|
-
if (property.type !== 'Identifier')
|
|
140
|
-
return;
|
|
141
|
-
if (!dangerousSinkMethods.has(property.name))
|
|
142
|
-
return;
|
|
143
|
-
const tainted = node.arguments[node.arguments.length - 1];
|
|
144
|
-
if (!tainted)
|
|
145
|
-
return;
|
|
146
|
-
reportSink(node, property.name, tainted);
|
|
147
|
-
},
|
|
148
|
-
};
|
|
149
|
-
},
|
|
150
|
-
});
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noInnerhtml=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");const DEFAULT_SANITIZERS=["DOMPurify.sanitize","sanitize","sanitizeHtml","xss","purify"];function isSanitized(node,sourceCode,sanitizers){const text=sourceCode.getText(node);return sanitizers.some(s=>text.includes(s))}function isLiteralString(node){if(node.type==="Literal"&&typeof node.value==="string"){return true}if(node.type==="TemplateLiteral"&&node.expressions.length===0){return true}return false}exports.noInnerhtml=(0,eslint_devkit_2.createRule)({name:"no-innerhtml",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-innerhtml.md",description:"Disallow dangerous innerHTML/outerHTML assignments that can lead to XSS",cwe:"CWE-79",cvss:6.1,confidence:"medium"},hasSuggestions:true,messages:{dangerousInnerHTML:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.SECURITY,issueName:"Cross-Site Scripting (XSS) via innerHTML",cwe:"CWE-79",description:"Assigning to {{property}} with {{source}} can execute malicious scripts. This is a critical XSS vulnerability.",severity:"CRITICAL",fix:"Use textContent for text, or sanitize with DOMPurify.sanitize() before assignment.",documentationLink:"https://owasp.org/www-community/attacks/xss/"}),useSanitizer:(0,eslint_devkit_2.formatLLMMessage)({icon:eslint_devkit_2.MessageIcons.INFO,issueName:"Use HTML Sanitizer",description:"Sanitize HTML before assignment",severity:"LOW",fix:"element.innerHTML = DOMPurify.sanitize(userInput);",documentationLink:"https://www.npmjs.com/package/dompurify"})},schema:[{type:"object",properties:{allowInTests:{type:"boolean",default:false},trustedSanitizers:{type:"array",items:{type:"string"},default:DEFAULT_SANITIZERS},allowLiteralStrings:{type:"boolean",default:true}},additionalProperties:false}]},defaultOptions:[{allowInTests:false,trustedSanitizers:DEFAULT_SANITIZERS,allowLiteralStrings:true}],create(context,[options={}]){const{allowInTests=false,trustedSanitizers=DEFAULT_SANITIZERS,allowLiteralStrings=true}=options;const filename=context.filename;const isTestFile=allowInTests&&/\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);if(isTestFile){return{}}const sourceCode=context.sourceCode;const dangerousProperties=new Set(["innerHTML","outerHTML"]);const dangerousSinkMethods=new Set(["insertAdjacentHTML","write","writeln"]);function reportSink(reportNode,sinkName,taintedNode){if(payloadSource(taintedNode)!==void 0)return;if(allowLiteralStrings&&isLiteralString(taintedNode))return;if(isSanitized(taintedNode,sourceCode,trustedSanitizers))return;let source="dynamic content";if(taintedNode.type==="Identifier"){source=`variable "${taintedNode.name}"`}else if(taintedNode.type==="TemplateLiteral"){source="template literal with expressions"}else if(taintedNode.type==="CallExpression"){source="function call result"}context.report({node:reportNode,messageId:"dangerousInnerHTML",data:{property:sinkName,source},suggest:[{messageId:"useSanitizer",fix:()=>null}]})}const payloadSource=(0,eslint_devkit_1.createPayloadResolver)(context.sourceCode);return{AssignmentExpression(node){if(node.left.type!=="MemberExpression"){return}const property=node.left.property;if(property.type!=="Identifier"){return}if(!dangerousProperties.has(property.name)){return}reportSink(node,property.name,node.right)},CallExpression(node){if(node.callee.type!=="MemberExpression")return;const property=node.callee.property;if(property.type!=="Identifier")return;if(!dangerousSinkMethods.has(property.name))return;const tainted=node.arguments[node.arguments.length-1];if(!tainted)return;reportSink(node,property.name,tainted)}}}});
|
|
@@ -1,238 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.noInsecureRedirects = void 0;
|
|
4
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
-
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
6
|
-
function resolveVariableInit(ident, sourceCode) {
|
|
7
|
-
const name = ident.name;
|
|
8
|
-
let current = ident.parent;
|
|
9
|
-
while (current) {
|
|
10
|
-
const parent = current.parent;
|
|
11
|
-
if (!parent)
|
|
12
|
-
break;
|
|
13
|
-
if (parent.type === 'BlockStatement' || parent.type === 'Program') {
|
|
14
|
-
const body = parent.type === 'BlockStatement' ? parent.body : parent.body;
|
|
15
|
-
const idx = body.indexOf(current);
|
|
16
|
-
for (let i = idx - 1; i >= 0 && i >= idx - 10; i--) {
|
|
17
|
-
const stmt = body[i];
|
|
18
|
-
if (stmt.type === 'VariableDeclaration') {
|
|
19
|
-
for (const decl of stmt.declarations) {
|
|
20
|
-
if (decl.id.type === 'Identifier' &&
|
|
21
|
-
decl.id.name === name &&
|
|
22
|
-
decl.init) {
|
|
23
|
-
return sourceCode.getText(decl.init);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
current = parent;
|
|
30
|
-
}
|
|
31
|
-
return null;
|
|
32
|
-
}
|
|
33
|
-
function isRedirectValidated(node, sourceCode) {
|
|
34
|
-
const callText = sourceCode.getText(node);
|
|
35
|
-
const userInputPattern = /\b(req\.(query|body|params)|window\.location|document\.location)\b/;
|
|
36
|
-
let hasUserInput = userInputPattern.test(callText);
|
|
37
|
-
if (!hasUserInput && node.arguments.length > 0) {
|
|
38
|
-
const arg = node.arguments[0];
|
|
39
|
-
if (arg.type === 'Identifier') {
|
|
40
|
-
const resolved = resolveVariableInit(arg, sourceCode);
|
|
41
|
-
if (resolved && userInputPattern.test(resolved)) {
|
|
42
|
-
hasUserInput = true;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
if (!hasUserInput) {
|
|
47
|
-
return true;
|
|
48
|
-
}
|
|
49
|
-
const program = sourceCode.ast;
|
|
50
|
-
const nodeStart = node.loc?.start;
|
|
51
|
-
const nodeEnd = node.loc?.end;
|
|
52
|
-
if (!nodeStart || !nodeEnd || !program) {
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
const validationPatterns = [
|
|
56
|
-
/\b(validateUrl|validateRedirect|isValidUrl|isSafeUrl)\s*\(/,
|
|
57
|
-
/\b(whitelist|allowedDomains|permittedUrls|ALLOWED_\w+)\s*\./,
|
|
58
|
-
/\b(url\.hostname|url\.host)\s*===/,
|
|
59
|
-
/\b\w+\.(includes|has)\s*\(/,
|
|
60
|
-
/\bstartsWith\s*\(\s*['"]/,
|
|
61
|
-
/\bendsWith\s*\(\s*['"]/,
|
|
62
|
-
/\bindexOf\s*\(\s*['"]/,
|
|
63
|
-
/\bmatch\s*\(\s*\//,
|
|
64
|
-
];
|
|
65
|
-
let current = node;
|
|
66
|
-
let depth = 0;
|
|
67
|
-
const maxDepth = 20;
|
|
68
|
-
while (current && depth < maxDepth) {
|
|
69
|
-
const parent = current.parent;
|
|
70
|
-
if (!parent)
|
|
71
|
-
break;
|
|
72
|
-
if (parent.type === 'BlockStatement' || parent.type === 'Program') {
|
|
73
|
-
const body = parent.body;
|
|
74
|
-
const currentIndex = body.indexOf(current);
|
|
75
|
-
for (let i = currentIndex - 1; i >= 0 && i >= currentIndex - 5; i--) {
|
|
76
|
-
const stmt = body[i];
|
|
77
|
-
const stmtText = sourceCode.getText(stmt);
|
|
78
|
-
if (validationPatterns.some(pattern => pattern.test(stmtText))) {
|
|
79
|
-
return true;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
if (parent.type === 'IfStatement' && parent.test) {
|
|
84
|
-
const testText = sourceCode.getText(parent.test);
|
|
85
|
-
if (validationPatterns.some(pattern => pattern.test(testText))) {
|
|
86
|
-
return true;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
current = parent;
|
|
90
|
-
depth++;
|
|
91
|
-
}
|
|
92
|
-
return false;
|
|
93
|
-
}
|
|
94
|
-
exports.noInsecureRedirects = (0, eslint_devkit_2.createRule)({
|
|
95
|
-
name: 'no-insecure-redirects',
|
|
96
|
-
meta: {
|
|
97
|
-
type: 'problem',
|
|
98
|
-
docs: {
|
|
99
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-insecure-redirects.md',
|
|
100
|
-
description: 'Detects open redirect vulnerabilities',
|
|
101
|
-
cwe: 'CWE-601',
|
|
102
|
-
cvss: 7.5,
|
|
103
|
-
},
|
|
104
|
-
hasSuggestions: true,
|
|
105
|
-
messages: {
|
|
106
|
-
insecureRedirect: (0, eslint_devkit_1.formatLLMMessage)({
|
|
107
|
-
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
108
|
-
issueName: 'Open redirect',
|
|
109
|
-
cwe: 'CWE-601',
|
|
110
|
-
description: 'Unvalidated redirect detected - user-controlled URL',
|
|
111
|
-
severity: 'HIGH',
|
|
112
|
-
fix: 'Whitelist allowed domains or validate redirect target',
|
|
113
|
-
documentationLink: 'https://owasp.org/www-community/vulnerabilities/Unvalidated_Redirects_and_Forwards',
|
|
114
|
-
}),
|
|
115
|
-
whitelistDomains: (0, eslint_devkit_1.formatLLMMessage)({
|
|
116
|
-
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
117
|
-
issueName: 'Whitelist Domains',
|
|
118
|
-
description: 'Whitelist allowed redirect domains',
|
|
119
|
-
severity: 'LOW',
|
|
120
|
-
fix: 'if (allowedDomains.includes(url.hostname)) redirect(url)',
|
|
121
|
-
documentationLink: 'https://owasp.org/www-community/vulnerabilities/Unvalidated_Redirects_and_Forwards',
|
|
122
|
-
}),
|
|
123
|
-
validateRedirect: (0, eslint_devkit_1.formatLLMMessage)({
|
|
124
|
-
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
125
|
-
issueName: 'Validate Redirect',
|
|
126
|
-
description: 'Validate redirect URL before use',
|
|
127
|
-
severity: 'LOW',
|
|
128
|
-
fix: 'validateRedirectUrl(userInput) before redirect',
|
|
129
|
-
documentationLink: 'https://owasp.org/www-community/vulnerabilities/Unvalidated_Redirects_and_Forwards',
|
|
130
|
-
}),
|
|
131
|
-
useRelativeUrl: (0, eslint_devkit_1.formatLLMMessage)({
|
|
132
|
-
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
133
|
-
issueName: 'Use Relative URL',
|
|
134
|
-
description: 'Use relative URLs for internal redirects',
|
|
135
|
-
severity: 'LOW',
|
|
136
|
-
fix: 'redirect("/internal/path") instead of absolute URLs',
|
|
137
|
-
documentationLink: 'https://owasp.org/www-community/vulnerabilities/Unvalidated_Redirects_and_Forwards',
|
|
138
|
-
}),
|
|
139
|
-
},
|
|
140
|
-
schema: [
|
|
141
|
-
{
|
|
142
|
-
type: 'object',
|
|
143
|
-
properties: {
|
|
144
|
-
ignoreInTests: {
|
|
145
|
-
type: 'boolean',
|
|
146
|
-
default: true,
|
|
147
|
-
},
|
|
148
|
-
allowedDomains: {
|
|
149
|
-
type: 'array',
|
|
150
|
-
items: { type: 'string' },
|
|
151
|
-
default: [], description: 'Redirect target domains treated as safe'
|
|
152
|
-
},
|
|
153
|
-
},
|
|
154
|
-
additionalProperties: false,
|
|
155
|
-
},
|
|
156
|
-
],
|
|
157
|
-
},
|
|
158
|
-
defaultOptions: [
|
|
159
|
-
{
|
|
160
|
-
ignoreInTests: true,
|
|
161
|
-
allowedDomains: [],
|
|
162
|
-
},
|
|
163
|
-
],
|
|
164
|
-
create(context, [options = {}]) {
|
|
165
|
-
const { ignoreInTests = true } = options || {};
|
|
166
|
-
const filename = context.filename;
|
|
167
|
-
const isTestFile = ignoreInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
168
|
-
if (isTestFile) {
|
|
169
|
-
return {};
|
|
170
|
-
}
|
|
171
|
-
const sourceCode = context.sourceCode;
|
|
172
|
-
function checkCallExpression(node) {
|
|
173
|
-
if (node.callee.type === 'MemberExpression' &&
|
|
174
|
-
node.callee.property.type === 'Identifier') {
|
|
175
|
-
const methodName = node.callee.property.name;
|
|
176
|
-
if (['redirect', 'replace', 'assign'].includes(methodName)) {
|
|
177
|
-
if (!isRedirectValidated(node, sourceCode)) {
|
|
178
|
-
context.report({
|
|
179
|
-
node,
|
|
180
|
-
messageId: 'insecureRedirect',
|
|
181
|
-
suggest: [
|
|
182
|
-
{
|
|
183
|
-
messageId: 'whitelistDomains',
|
|
184
|
-
fix: () => null,
|
|
185
|
-
},
|
|
186
|
-
{
|
|
187
|
-
messageId: 'validateRedirect',
|
|
188
|
-
fix: () => null,
|
|
189
|
-
},
|
|
190
|
-
{
|
|
191
|
-
messageId: 'useRelativeUrl',
|
|
192
|
-
fix: () => null,
|
|
193
|
-
},
|
|
194
|
-
],
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
function checkAssignmentExpression(node) {
|
|
201
|
-
if (node.left.type === 'MemberExpression' &&
|
|
202
|
-
node.left.object.type === 'MemberExpression' &&
|
|
203
|
-
node.left.object.object.type === 'Identifier' &&
|
|
204
|
-
node.left.object.object.name === 'window' &&
|
|
205
|
-
node.left.object.property.type === 'Identifier' &&
|
|
206
|
-
node.left.object.property.name === 'location' &&
|
|
207
|
-
node.left.property.type === 'Identifier' &&
|
|
208
|
-
['href', 'replace', 'assign'].includes(node.left.property.name)) {
|
|
209
|
-
const rightText = sourceCode.getText(node.right);
|
|
210
|
-
const userInputPattern = /\b(req\.(query|body|params)|window\.location|document\.location)\b/;
|
|
211
|
-
if (userInputPattern.test(rightText)) {
|
|
212
|
-
context.report({
|
|
213
|
-
node,
|
|
214
|
-
messageId: 'insecureRedirect',
|
|
215
|
-
suggest: [
|
|
216
|
-
{
|
|
217
|
-
messageId: 'whitelistDomains',
|
|
218
|
-
fix: () => null,
|
|
219
|
-
},
|
|
220
|
-
{
|
|
221
|
-
messageId: 'validateRedirect',
|
|
222
|
-
fix: () => null,
|
|
223
|
-
},
|
|
224
|
-
{
|
|
225
|
-
messageId: 'useRelativeUrl',
|
|
226
|
-
fix: () => null,
|
|
227
|
-
},
|
|
228
|
-
],
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
return {
|
|
234
|
-
CallExpression: checkCallExpression,
|
|
235
|
-
AssignmentExpression: checkAssignmentExpression,
|
|
236
|
-
};
|
|
237
|
-
},
|
|
238
|
-
});
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noInsecureRedirects=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const eslint_devkit_2=require("@interlace/eslint-devkit");function resolveVariableInit(ident,sourceCode){const name=ident.name;let current=ident.parent;while(current){const parent=current.parent;if(!parent)break;if(parent.type==="BlockStatement"||parent.type==="Program"){const body=parent.type==="BlockStatement"?parent.body:parent.body;const idx=body.indexOf(current);for(let i=idx-1;i>=0&&i>=idx-10;i--){const stmt=body[i];if(stmt.type==="VariableDeclaration"){for(const decl of stmt.declarations){if(decl.id.type==="Identifier"&&decl.id.name===name&&decl.init){return sourceCode.getText(decl.init)}}}}}current=parent}return null}function isRedirectValidated(node,sourceCode){const callText=sourceCode.getText(node);const userInputPattern=/\b(req\.(query|body|params)|window\.location|document\.location)\b/;let hasUserInput=userInputPattern.test(callText);if(!hasUserInput&&node.arguments.length>0){const arg=node.arguments[0];if(arg.type==="Identifier"){const resolved=resolveVariableInit(arg,sourceCode);if(resolved&&userInputPattern.test(resolved)){hasUserInput=true}}}if(!hasUserInput){return true}const program=sourceCode.ast;const nodeStart=node.loc?.start;const nodeEnd=node.loc?.end;if(!nodeStart||!nodeEnd||!program){return false}const validationPatterns=[/\b(validateUrl|validateRedirect|isValidUrl|isSafeUrl)\s*\(/,/\b(whitelist|allowedDomains|permittedUrls|ALLOWED_\w+)\s*\./,/\b(url\.hostname|url\.host)\s*===/,/\b\w+\.(includes|has)\s*\(/,/\bstartsWith\s*\(\s*['"]/,/\bendsWith\s*\(\s*['"]/,/\bindexOf\s*\(\s*['"]/,/\bmatch\s*\(\s*\//];let current=node;let depth=0;const maxDepth=20;while(current&&depth<maxDepth){const parent=current.parent;if(!parent)break;if(parent.type==="BlockStatement"||parent.type==="Program"){const body=parent.body;const currentIndex=body.indexOf(current);for(let i=currentIndex-1;i>=0&&i>=currentIndex-5;i--){const stmt=body[i];const stmtText=sourceCode.getText(stmt);if(validationPatterns.some(pattern=>pattern.test(stmtText))){return true}}}if(parent.type==="IfStatement"&&parent.test){const testText=sourceCode.getText(parent.test);if(validationPatterns.some(pattern=>pattern.test(testText))){return true}}current=parent;depth++}return false}exports.noInsecureRedirects=(0,eslint_devkit_2.createRule)({name:"no-insecure-redirects",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-insecure-redirects.md",description:"Detects open redirect vulnerabilities",cwe:"CWE-601",cvss:7.5},hasSuggestions:true,messages:{insecureRedirect:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Open redirect",cwe:"CWE-601",description:"Unvalidated redirect detected - user-controlled URL",severity:"HIGH",fix:"Whitelist allowed domains or validate redirect target",documentationLink:"https://owasp.org/www-community/vulnerabilities/Unvalidated_Redirects_and_Forwards"}),whitelistDomains:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Whitelist Domains",description:"Whitelist allowed redirect domains",severity:"LOW",fix:"if (allowedDomains.includes(url.hostname)) redirect(url)",documentationLink:"https://owasp.org/www-community/vulnerabilities/Unvalidated_Redirects_and_Forwards"}),validateRedirect:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Validate Redirect",description:"Validate redirect URL before use",severity:"LOW",fix:"validateRedirectUrl(userInput) before redirect",documentationLink:"https://owasp.org/www-community/vulnerabilities/Unvalidated_Redirects_and_Forwards"}),useRelativeUrl:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Use Relative URL",description:"Use relative URLs for internal redirects",severity:"LOW",fix:'redirect("/internal/path") instead of absolute URLs',documentationLink:"https://owasp.org/www-community/vulnerabilities/Unvalidated_Redirects_and_Forwards"})},schema:[{type:"object",properties:{ignoreInTests:{type:"boolean",default:true},allowedDomains:{type:"array",items:{type:"string"},default:[],description:"Redirect target domains treated as safe"}},additionalProperties:false}]},defaultOptions:[{ignoreInTests:true,allowedDomains:[]}],create(context,[options={}]){const{ignoreInTests=true}=options||{};const filename=context.filename;const isTestFile=ignoreInTests&&/\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);if(isTestFile){return{}}const sourceCode=context.sourceCode;function checkCallExpression(node){if(node.callee.type==="MemberExpression"&&node.callee.property.type==="Identifier"){const methodName=node.callee.property.name;if(["redirect","replace","assign"].includes(methodName)){if(!isRedirectValidated(node,sourceCode)){context.report({node,messageId:"insecureRedirect",suggest:[{messageId:"whitelistDomains",fix:()=>null},{messageId:"validateRedirect",fix:()=>null},{messageId:"useRelativeUrl",fix:()=>null}]})}}}}function checkAssignmentExpression(node){if(node.left.type==="MemberExpression"&&node.left.object.type==="MemberExpression"&&node.left.object.object.type==="Identifier"&&node.left.object.object.name==="window"&&node.left.object.property.type==="Identifier"&&node.left.object.property.name==="location"&&node.left.property.type==="Identifier"&&["href","replace","assign"].includes(node.left.property.name)){const rightText=sourceCode.getText(node.right);const userInputPattern=/\b(req\.(query|body|params)|window\.location|document\.location)\b/;if(userInputPattern.test(rightText)){context.report({node,messageId:"insecureRedirect",suggest:[{messageId:"whitelistDomains",fix:()=>null},{messageId:"validateRedirect",fix:()=>null},{messageId:"useRelativeUrl",fix:()=>null}]})}}}return{CallExpression:checkCallExpression,AssignmentExpression:checkAssignmentExpression}}});
|
|
@@ -1,57 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.noInsecureWebsocket = void 0;
|
|
4
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
-
exports.noInsecureWebsocket = (0, eslint_devkit_1.createRule)({
|
|
6
|
-
name: 'no-insecure-websocket',
|
|
7
|
-
meta: {
|
|
8
|
-
type: 'problem',
|
|
9
|
-
docs: {
|
|
10
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-insecure-websocket.md',
|
|
11
|
-
description: 'Require secure WebSocket connections (wss://)',
|
|
12
|
-
cwe: 'CWE-319',
|
|
13
|
-
cvss: 7.5,
|
|
14
|
-
},
|
|
15
|
-
messages: {
|
|
16
|
-
violationDetected: (0, eslint_devkit_1.formatLLMMessage)({
|
|
17
|
-
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
18
|
-
issueName: 'Insecure WebSocket',
|
|
19
|
-
cwe: 'CWE-319',
|
|
20
|
-
description: 'Insecure WebSocket connection (ws://) - data transmitted in clear text',
|
|
21
|
-
severity: 'HIGH',
|
|
22
|
-
fix: 'Use wss:// instead of ws:// for secure WebSocket connections',
|
|
23
|
-
documentationLink: 'https://cwe.mitre.org/data/definitions/319.html',
|
|
24
|
-
})
|
|
25
|
-
},
|
|
26
|
-
schema: [],
|
|
27
|
-
},
|
|
28
|
-
defaultOptions: [],
|
|
29
|
-
create(context) {
|
|
30
|
-
function report(node) {
|
|
31
|
-
context.report({ node, messageId: 'violationDetected' });
|
|
32
|
-
}
|
|
33
|
-
return {
|
|
34
|
-
NewExpression(node) {
|
|
35
|
-
if (node.callee.type === 'Identifier' && node.callee.name === 'WebSocket') {
|
|
36
|
-
const urlArg = node.arguments[0];
|
|
37
|
-
if (urlArg && urlArg.type === 'Literal' &&
|
|
38
|
-
typeof urlArg.value === 'string' &&
|
|
39
|
-
urlArg.value.startsWith('ws://')) {
|
|
40
|
-
report(node);
|
|
41
|
-
}
|
|
42
|
-
if (urlArg && urlArg.type === 'TemplateLiteral') {
|
|
43
|
-
const text = context.sourceCode.getText(urlArg);
|
|
44
|
-
if (text.includes('ws://')) {
|
|
45
|
-
report(node);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
|
-
Literal(node) {
|
|
51
|
-
if (typeof node.value === 'string' && node.value.startsWith('ws://')) {
|
|
52
|
-
report(node);
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
};
|
|
56
|
-
},
|
|
57
|
-
});
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noInsecureWebsocket=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");exports.noInsecureWebsocket=(0,eslint_devkit_1.createRule)({name:"no-insecure-websocket",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-insecure-websocket.md",description:"Require secure WebSocket connections (wss://)",cwe:"CWE-319",cvss:7.5},messages:{violationDetected:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Insecure WebSocket",cwe:"CWE-319",description:"Insecure WebSocket connection (ws://) - data transmitted in clear text",severity:"HIGH",fix:"Use wss:// instead of ws:// for secure WebSocket connections",documentationLink:"https://cwe.mitre.org/data/definitions/319.html"})},schema:[]},defaultOptions:[],create(context){function report(node){context.report({node,messageId:"violationDetected"})}return{NewExpression(node){if(node.callee.type==="Identifier"&&node.callee.name==="WebSocket"){const urlArg=node.arguments[0];if(urlArg&&urlArg.type==="Literal"&&typeof urlArg.value==="string"&&urlArg.value.startsWith("ws://")){report(node)}if(urlArg&&urlArg.type==="TemplateLiteral"){const text=context.sourceCode.getText(urlArg);if(text.includes("ws://")){report(node)}}}},Literal(node){if(typeof node.value==="string"&&node.value.startsWith("ws://")){report(node)}}}}});
|
|
@@ -1,178 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.noJwtInStorage = void 0;
|
|
4
|
-
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
-
const JWT_KEY_PATTERNS = [
|
|
6
|
-
/^jwt$/i,
|
|
7
|
-
/^token$/i,
|
|
8
|
-
/^access[_-]?token$/i,
|
|
9
|
-
/^refresh[_-]?token$/i,
|
|
10
|
-
/^id[_-]?token$/i,
|
|
11
|
-
/^bearer$/i,
|
|
12
|
-
/^auth[_-]?token$/i,
|
|
13
|
-
/token$/i,
|
|
14
|
-
/^jwt[_-]?/i,
|
|
15
|
-
/[_-]jwt$/i,
|
|
16
|
-
];
|
|
17
|
-
const JWT_VALUE_REGEX = /^eyJ[A-Za-z0-9_-]*\.eyJ[A-Za-z0-9_-]*\.[A-Za-z0-9_-]+$/;
|
|
18
|
-
const EXCLUDE_PATTERNS = [
|
|
19
|
-
/count$/i,
|
|
20
|
-
/length$/i,
|
|
21
|
-
/size$/i,
|
|
22
|
-
/limit$/i,
|
|
23
|
-
/max$/i,
|
|
24
|
-
/min$/i,
|
|
25
|
-
/num$/i,
|
|
26
|
-
/index$/i,
|
|
27
|
-
/position$/i,
|
|
28
|
-
];
|
|
29
|
-
function isJwtKey(key) {
|
|
30
|
-
if (EXCLUDE_PATTERNS.some((pattern) => pattern.test(key))) {
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
return JWT_KEY_PATTERNS.some((pattern) => pattern.test(key));
|
|
34
|
-
}
|
|
35
|
-
function isJwtValue(value) {
|
|
36
|
-
return JWT_VALUE_REGEX.test(value);
|
|
37
|
-
}
|
|
38
|
-
exports.noJwtInStorage = (0, eslint_devkit_1.createRule)({
|
|
39
|
-
name: 'no-jwt-in-storage',
|
|
40
|
-
meta: {
|
|
41
|
-
type: 'problem',
|
|
42
|
-
docs: {
|
|
43
|
-
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-jwt-in-storage.md',
|
|
44
|
-
description: 'Disallow storing JWT tokens in localStorage or sessionStorage',
|
|
45
|
-
cwe: 'CWE-922',
|
|
46
|
-
cvss: 8.1,
|
|
47
|
-
},
|
|
48
|
-
hasSuggestions: true,
|
|
49
|
-
messages: {
|
|
50
|
-
jwtInStorage: (0, eslint_devkit_1.formatLLMMessage)({
|
|
51
|
-
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
52
|
-
issueName: 'JWT in Browser Storage',
|
|
53
|
-
cwe: 'CWE-922',
|
|
54
|
-
owasp: 'A02:2021',
|
|
55
|
-
cvss: 8.1,
|
|
56
|
-
description: 'Storing JWT "{{key}}" in {{storage}} exposes it to XSS attacks. Any malicious script can steal the token and impersonate the user.',
|
|
57
|
-
severity: 'HIGH',
|
|
58
|
-
fix: 'Store JWTs in HttpOnly cookies set by the server.',
|
|
59
|
-
documentationLink: 'https://auth0.com/docs/secure/security-guidance/data-security/token-storage',
|
|
60
|
-
}),
|
|
61
|
-
useHttpOnlyCookie: (0, eslint_devkit_1.formatLLMMessage)({
|
|
62
|
-
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
63
|
-
issueName: 'Use HttpOnly Cookie',
|
|
64
|
-
description: 'Store JWT via server-side Set-Cookie with HttpOnly, Secure, SameSite flags',
|
|
65
|
-
severity: 'LOW',
|
|
66
|
-
fix: 'Server: res.cookie("token", jwt, { httpOnly: true, secure: true, sameSite: "strict" })',
|
|
67
|
-
documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#security',
|
|
68
|
-
}),
|
|
69
|
-
},
|
|
70
|
-
schema: [
|
|
71
|
-
{
|
|
72
|
-
type: 'object',
|
|
73
|
-
properties: {
|
|
74
|
-
allowInTests: {
|
|
75
|
-
type: 'boolean',
|
|
76
|
-
default: true,
|
|
77
|
-
},
|
|
78
|
-
},
|
|
79
|
-
additionalProperties: false,
|
|
80
|
-
},
|
|
81
|
-
],
|
|
82
|
-
},
|
|
83
|
-
defaultOptions: [{ allowInTests: true }],
|
|
84
|
-
create(context, [options = {}]) {
|
|
85
|
-
const { allowInTests = true } = options;
|
|
86
|
-
const filename = context.filename;
|
|
87
|
-
const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
88
|
-
if (allowInTests && isTestFile) {
|
|
89
|
-
return {};
|
|
90
|
-
}
|
|
91
|
-
const storageObjects = new Set(['localStorage', 'sessionStorage']);
|
|
92
|
-
return {
|
|
93
|
-
CallExpression(node) {
|
|
94
|
-
const callee = node.callee;
|
|
95
|
-
if (callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
96
|
-
callee.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
97
|
-
storageObjects.has(callee.object.name) &&
|
|
98
|
-
callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
99
|
-
callee.property.name === 'setItem') {
|
|
100
|
-
const keyArg = node.arguments[0];
|
|
101
|
-
const valueArg = node.arguments[1];
|
|
102
|
-
if (!keyArg)
|
|
103
|
-
return;
|
|
104
|
-
let keyValue = null;
|
|
105
|
-
let hasJwtValue = false;
|
|
106
|
-
if (keyArg.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
107
|
-
typeof keyArg.value === 'string') {
|
|
108
|
-
keyValue = keyArg.value;
|
|
109
|
-
}
|
|
110
|
-
else if (keyArg.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
111
|
-
keyValue = keyArg.name;
|
|
112
|
-
}
|
|
113
|
-
if (valueArg &&
|
|
114
|
-
valueArg.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
115
|
-
typeof valueArg.value === 'string') {
|
|
116
|
-
hasJwtValue = isJwtValue(valueArg.value);
|
|
117
|
-
}
|
|
118
|
-
if ((keyValue && isJwtKey(keyValue)) ||
|
|
119
|
-
hasJwtValue) {
|
|
120
|
-
context.report({
|
|
121
|
-
node,
|
|
122
|
-
messageId: 'jwtInStorage',
|
|
123
|
-
data: {
|
|
124
|
-
key: keyValue || '<dynamic>',
|
|
125
|
-
storage: callee.object.name,
|
|
126
|
-
},
|
|
127
|
-
suggest: [
|
|
128
|
-
{
|
|
129
|
-
messageId: 'useHttpOnlyCookie',
|
|
130
|
-
fix: () => null,
|
|
131
|
-
},
|
|
132
|
-
],
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
},
|
|
137
|
-
AssignmentExpression(node) {
|
|
138
|
-
if (node.left.type !== eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
const obj = node.left.object;
|
|
142
|
-
if (obj.type !== eslint_devkit_1.AST_NODE_TYPES.Identifier ||
|
|
143
|
-
!storageObjects.has(obj.name)) {
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
let keyValue = null;
|
|
147
|
-
let hasJwtValue = false;
|
|
148
|
-
if (node.left.property.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
149
|
-
typeof node.left.property.value === 'string') {
|
|
150
|
-
keyValue = node.left.property.value;
|
|
151
|
-
}
|
|
152
|
-
else if (node.left.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
153
|
-
keyValue = node.left.property.name;
|
|
154
|
-
}
|
|
155
|
-
if (node.right.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
156
|
-
typeof node.right.value === 'string') {
|
|
157
|
-
hasJwtValue = isJwtValue(node.right.value);
|
|
158
|
-
}
|
|
159
|
-
if ((keyValue && isJwtKey(keyValue)) || hasJwtValue) {
|
|
160
|
-
context.report({
|
|
161
|
-
node,
|
|
162
|
-
messageId: 'jwtInStorage',
|
|
163
|
-
data: {
|
|
164
|
-
key: keyValue || '<dynamic>',
|
|
165
|
-
storage: obj.name,
|
|
166
|
-
},
|
|
167
|
-
suggest: [
|
|
168
|
-
{
|
|
169
|
-
messageId: 'useHttpOnlyCookie',
|
|
170
|
-
fix: () => null,
|
|
171
|
-
},
|
|
172
|
-
],
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
},
|
|
176
|
-
};
|
|
177
|
-
},
|
|
178
|
-
});
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noJwtInStorage=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const JWT_KEY_PATTERNS=[/^jwt$/i,/^token$/i,/^access[_-]?token$/i,/^refresh[_-]?token$/i,/^id[_-]?token$/i,/^bearer$/i,/^auth[_-]?token$/i,/token$/i,/^jwt[_-]?/i,/[_-]jwt$/i];const JWT_VALUE_REGEX=/^eyJ[A-Za-z0-9_-]*\.eyJ[A-Za-z0-9_-]*\.[A-Za-z0-9_-]+$/;const EXCLUDE_PATTERNS=[/count$/i,/length$/i,/size$/i,/limit$/i,/max$/i,/min$/i,/num$/i,/index$/i,/position$/i];function isJwtKey(key){if(EXCLUDE_PATTERNS.some(pattern=>pattern.test(key))){return false}return JWT_KEY_PATTERNS.some(pattern=>pattern.test(key))}function isJwtValue(value){return JWT_VALUE_REGEX.test(value)}exports.noJwtInStorage=(0,eslint_devkit_1.createRule)({name:"no-jwt-in-storage",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-jwt-in-storage.md",description:"Disallow storing JWT tokens in localStorage or sessionStorage",cwe:"CWE-922",cvss:8.1},hasSuggestions:true,messages:{jwtInStorage:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"JWT in Browser Storage",cwe:"CWE-922",owasp:"A02:2021",cvss:8.1,description:'Storing JWT "{{key}}" in {{storage}} exposes it to XSS attacks. Any malicious script can steal the token and impersonate the user.',severity:"HIGH",fix:"Store JWTs in HttpOnly cookies set by the server.",documentationLink:"https://auth0.com/docs/secure/security-guidance/data-security/token-storage"}),useHttpOnlyCookie:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Use HttpOnly Cookie",description:"Store JWT via server-side Set-Cookie with HttpOnly, Secure, SameSite flags",severity:"LOW",fix:'Server: res.cookie("token", jwt, { httpOnly: true, secure: true, sameSite: "strict" })',documentationLink:"https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#security"})},schema:[{type:"object",properties:{allowInTests:{type:"boolean",default:true}},additionalProperties:false}]},defaultOptions:[{allowInTests:true}],create(context,[options={}]){const{allowInTests=true}=options;const filename=context.filename;const isTestFile=/\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);if(allowInTests&&isTestFile){return{}}const storageObjects=new Set(["localStorage","sessionStorage"]);return{CallExpression(node){const callee=node.callee;if(callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&callee.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&storageObjects.has(callee.object.name)&&callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&callee.property.name==="setItem"){const keyArg=node.arguments[0];const valueArg=node.arguments[1];if(!keyArg)return;let keyValue=null;let hasJwtValue=false;if(keyArg.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&typeof keyArg.value==="string"){keyValue=keyArg.value}else if(keyArg.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){keyValue=keyArg.name}if(valueArg&&valueArg.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&typeof valueArg.value==="string"){hasJwtValue=isJwtValue(valueArg.value)}if(keyValue&&isJwtKey(keyValue)||hasJwtValue){context.report({node,messageId:"jwtInStorage",data:{key:keyValue||"<dynamic>",storage:callee.object.name},suggest:[{messageId:"useHttpOnlyCookie",fix:()=>null}]})}}},AssignmentExpression(node){if(node.left.type!==eslint_devkit_1.AST_NODE_TYPES.MemberExpression){return}const obj=node.left.object;if(obj.type!==eslint_devkit_1.AST_NODE_TYPES.Identifier||!storageObjects.has(obj.name)){return}let keyValue=null;let hasJwtValue=false;if(node.left.property.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&typeof node.left.property.value==="string"){keyValue=node.left.property.value}else if(node.left.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){keyValue=node.left.property.name}if(node.right.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&typeof node.right.value==="string"){hasJwtValue=isJwtValue(node.right.value)}if(keyValue&&isJwtKey(keyValue)||hasJwtValue){context.report({node,messageId:"jwtInStorage",data:{key:keyValue||"<dynamic>",storage:obj.name},suggest:[{messageId:"useHttpOnlyCookie",fix:()=>null}]})}}}}});
|