eslint-plugin-browser-security 1.2.2 → 1.2.4
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/AGENTS.md +97 -0
- package/CHANGELOG.md +125 -0
- package/README.md +110 -88
- package/package.json +21 -5
- package/src/index.js +15 -1
- package/src/oxlint.d.ts +20 -0
- package/src/oxlint.js +21 -0
- package/src/rules/detect-mixed-content/index.js +3 -0
- package/src/rules/no-allow-arbitrary-loads/index.js +3 -0
- package/src/rules/no-clickjacking/index.js +10 -5
- package/src/rules/no-client-side-auth-logic/index.js +4 -0
- package/src/rules/no-cookie-auth-tokens/index.js +4 -1
- package/src/rules/no-credentials-in-query-params/index.js +3 -0
- package/src/rules/no-disabled-certificate-validation/index.js +5 -2
- package/src/rules/no-dynamic-service-worker-url/index.js +4 -1
- package/src/rules/no-eval/index.js +34 -15
- package/src/rules/no-filereader-innerhtml/index.js +8 -5
- package/src/rules/no-http-urls/index.js +3 -0
- package/src/rules/no-innerhtml/index.js +58 -34
- package/src/rules/no-insecure-redirects/index.js +61 -6
- package/src/rules/no-insecure-websocket/index.js +3 -0
- package/src/rules/no-jwt-in-storage/index.js +7 -4
- package/src/rules/no-missing-cors-check/index.js +5 -2
- package/src/rules/no-missing-csrf-protection/index.js +3 -0
- package/src/rules/no-missing-security-headers/index.js +20 -17
- package/src/rules/no-password-in-url/index.js +3 -0
- package/src/rules/no-permissive-cors/index.js +4 -0
- package/src/rules/no-postmessage-innerhtml/index.js +8 -5
- package/src/rules/no-postmessage-wildcard-origin/index.js +4 -1
- package/src/rules/no-sensitive-cookie-js/index.js +4 -1
- package/src/rules/no-sensitive-data-in-analytics/index.js +3 -0
- package/src/rules/no-sensitive-data-in-cache/index.js +5 -1
- package/src/rules/no-sensitive-indexeddb/index.js +4 -1
- package/src/rules/no-sensitive-localstorage/index.js +3 -0
- package/src/rules/no-sensitive-sessionstorage/index.js +4 -1
- package/src/rules/no-tracking-without-consent/index.js +3 -0
- package/src/rules/no-unencrypted-transmission/index.js +5 -2
- package/src/rules/no-unescaped-url-parameter/index.js +6 -2
- package/src/rules/no-unsafe-eval-csp/index.js +4 -1
- package/src/rules/no-unsafe-inline-csp/index.js +4 -1
- package/src/rules/no-unvalidated-deeplinks/index.js +3 -0
- package/src/rules/no-websocket-eval/index.js +6 -3
- package/src/rules/no-websocket-innerhtml/index.js +8 -5
- package/src/rules/no-worker-message-innerhtml/index.js +8 -5
- package/src/rules/require-blob-url-revocation/index.js +4 -1
- package/src/rules/require-cookie-secure-attrs/index.js +4 -1
- package/src/rules/require-csp-headers/index.js +3 -0
- package/src/rules/require-https-only/index.js +3 -0
- package/src/rules/require-mime-type-validation/index.js +3 -0
- package/src/rules/require-postmessage-origin-check/index.js +3 -0
- package/src/rules/require-url-validation/index.js +3 -0
- package/src/rules/require-websocket-wss/index.js +6 -2
package/src/index.js
CHANGED
|
@@ -127,7 +127,7 @@ exports.rules = {
|
|
|
127
127
|
exports.plugin = {
|
|
128
128
|
meta: {
|
|
129
129
|
name: 'eslint-plugin-browser-security',
|
|
130
|
-
version: '
|
|
130
|
+
version: '1.2.4',
|
|
131
131
|
},
|
|
132
132
|
rules: exports.rules,
|
|
133
133
|
};
|
|
@@ -170,6 +170,7 @@ const recommendedRules = {
|
|
|
170
170
|
'browser-security/no-clickjacking': 'error',
|
|
171
171
|
'browser-security/no-credentials-in-query-params': 'error',
|
|
172
172
|
'browser-security/no-http-urls': 'error',
|
|
173
|
+
'browser-security/no-insecure-redirects': 'error',
|
|
173
174
|
'browser-security/require-https-only': 'error',
|
|
174
175
|
'browser-security/no-insecure-websocket': 'error',
|
|
175
176
|
'browser-security/no-unvalidated-deeplinks': 'error',
|
|
@@ -179,6 +180,19 @@ const recommendedRules = {
|
|
|
179
180
|
* Preset configurations for browser security rules
|
|
180
181
|
*/
|
|
181
182
|
exports.configs = {
|
|
183
|
+
/**
|
|
184
|
+
* Flagship preset — the rule from this plugin in the ecosystem-wide
|
|
185
|
+
* flagship list (`.agent/flagship-rules.md`). Narrow signature, no
|
|
186
|
+
* legitimate use case for `postMessage(data, '*')` — high-signal CI gate.
|
|
187
|
+
*/
|
|
188
|
+
flagship: {
|
|
189
|
+
plugins: {
|
|
190
|
+
'browser-security': exports.plugin,
|
|
191
|
+
},
|
|
192
|
+
rules: {
|
|
193
|
+
'browser-security/no-postmessage-wildcard-origin': 'error',
|
|
194
|
+
},
|
|
195
|
+
},
|
|
182
196
|
/**
|
|
183
197
|
* Recommended security configuration
|
|
184
198
|
*/
|
package/src/oxlint.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
* oxlint sub-export.
|
|
8
|
+
*
|
|
9
|
+
* Consumers wire oxlint to this entry via:
|
|
10
|
+
* { "jsPlugins": ["eslint-plugin-browser-security/oxlint"] }
|
|
11
|
+
*
|
|
12
|
+
* oxlint's JS plugin loader does `require('eslint-plugin-browser-security/oxlint')` and reads
|
|
13
|
+
* `.rules` off the result. The compiled output of `export = plugin` is
|
|
14
|
+
* `module.exports = plugin`, which gives oxlint the plugin object directly
|
|
15
|
+
* (`{ meta, rules }`) — no wrapper, no `.default` indirection.
|
|
16
|
+
*
|
|
17
|
+
* Generated by scripts/generate-oxlint-shims.mjs. Do not edit by hand.
|
|
18
|
+
*/
|
|
19
|
+
import { plugin } from './index';
|
|
20
|
+
export = plugin;
|
package/src/oxlint.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) 2025 Ofri Peretz
|
|
4
|
+
* Licensed under the MIT License. Use of this source code is governed by the
|
|
5
|
+
* MIT license that can be found in the LICENSE file.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* oxlint sub-export.
|
|
9
|
+
*
|
|
10
|
+
* Consumers wire oxlint to this entry via:
|
|
11
|
+
* { "jsPlugins": ["eslint-plugin-browser-security/oxlint"] }
|
|
12
|
+
*
|
|
13
|
+
* oxlint's JS plugin loader does `require('eslint-plugin-browser-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
|
+
const index_1 = require("./index");
|
|
21
|
+
module.exports = index_1.plugin;
|
|
@@ -17,7 +17,10 @@ exports.detectMixedContent = (0, eslint_devkit_1.createRule)({
|
|
|
17
17
|
meta: {
|
|
18
18
|
type: 'problem',
|
|
19
19
|
docs: {
|
|
20
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/detect-mixed-content.md',
|
|
20
21
|
description: 'Detect HTTP resources in HTTPS pages',
|
|
22
|
+
cwe: 'CWE-311',
|
|
23
|
+
cvss: 5,
|
|
21
24
|
},
|
|
22
25
|
messages: {
|
|
23
26
|
violationDetected: (0, eslint_devkit_1.formatLLMMessage)({
|
|
@@ -17,7 +17,10 @@ exports.noAllowArbitraryLoads = (0, eslint_devkit_1.createRule)({
|
|
|
17
17
|
meta: {
|
|
18
18
|
type: 'problem',
|
|
19
19
|
docs: {
|
|
20
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-allow-arbitrary-loads.md',
|
|
20
21
|
description: 'Prevent configuration allowing insecure loads',
|
|
22
|
+
cwe: 'CWE-295',
|
|
23
|
+
cvss: 7.5,
|
|
21
24
|
},
|
|
22
25
|
messages: {
|
|
23
26
|
violationDetected: (0, eslint_devkit_1.formatLLMMessage)({
|
|
@@ -16,9 +16,10 @@ exports.noClickjacking = (0, eslint_devkit_1.createRule)({
|
|
|
16
16
|
deprecated: true,
|
|
17
17
|
replacedBy: ['@see eslint-plugin-express-security/require-helmet'],
|
|
18
18
|
docs: {
|
|
19
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-clickjacking.md',
|
|
19
20
|
description: 'Detects clickjacking vulnerabilities and missing frame protections',
|
|
21
|
+
cwe: 'CWE-1021',
|
|
20
22
|
},
|
|
21
|
-
fixable: 'code',
|
|
22
23
|
hasSuggestions: true,
|
|
23
24
|
messages: {
|
|
24
25
|
clickjackingVulnerability: (0, eslint_devkit_2.formatLLMMessage)({
|
|
@@ -185,8 +186,8 @@ exports.noClickjacking = (0, eslint_devkit_1.createRule)({
|
|
|
185
186
|
create(context) {
|
|
186
187
|
const options = context.options[0] || {};
|
|
187
188
|
const { trustedSources = ['self', 'same-origin'], requireFrameBusting = true, detectTransparentOverlays = true, trustedSanitizers = [], trustedAnnotations = [], strictMode = false, } = options;
|
|
188
|
-
const sourceCode = context.sourceCode
|
|
189
|
-
const filename = context.filename
|
|
189
|
+
const sourceCode = context.sourceCode;
|
|
190
|
+
const filename = context.filename;
|
|
190
191
|
// Create safety checker for false positive detection
|
|
191
192
|
const safetyChecker = (0, eslint_devkit_3.createSafetyChecker)({
|
|
192
193
|
trustedSanitizers,
|
|
@@ -221,6 +222,7 @@ exports.noClickjacking = (0, eslint_devkit_1.createRule)({
|
|
|
221
222
|
/**
|
|
222
223
|
* Check for transparent/invisible elements that could hide clickjacking
|
|
223
224
|
*/
|
|
225
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
224
226
|
const hasTransparentStyles = (styleText) => {
|
|
225
227
|
const styles = styleText.toLowerCase();
|
|
226
228
|
return styles.includes('opacity: 0') ||
|
|
@@ -283,8 +285,11 @@ exports.noClickjacking = (0, eslint_devkit_1.createRule)({
|
|
|
283
285
|
// Check if this is being assigned or compared
|
|
284
286
|
let current = node;
|
|
285
287
|
let isFrameManipulation = false;
|
|
286
|
-
// Walk up to see if this is an assignment or comparison
|
|
287
|
-
|
|
288
|
+
// Walk up to see if this is an assignment or comparison. Every
|
|
289
|
+
// path that sets `isFrameManipulation = true` is followed by
|
|
290
|
+
// `break`, so the negation in the loop condition is redundant
|
|
291
|
+
// (CodeQL: `js/useless-conditional`).
|
|
292
|
+
while (current) {
|
|
288
293
|
if (current.type === 'AssignmentExpression' &&
|
|
289
294
|
current.left === node) {
|
|
290
295
|
isFrameManipulation = true;
|
|
@@ -15,7 +15,10 @@ exports.noClientSideAuthLogic = (0, eslint_devkit_1.createRule)({
|
|
|
15
15
|
meta: {
|
|
16
16
|
type: 'problem',
|
|
17
17
|
docs: {
|
|
18
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-client-side-auth-logic.md',
|
|
18
19
|
description: 'Prevent authentication logic in client code',
|
|
20
|
+
cwe: 'CWE-602',
|
|
21
|
+
cvss: 9.5,
|
|
19
22
|
},
|
|
20
23
|
messages: {
|
|
21
24
|
violationDetected: (0, eslint_devkit_1.formatLLMMessage)({
|
|
@@ -55,6 +58,7 @@ exports.noClientSideAuthLogic = (0, eslint_devkit_1.createRule)({
|
|
|
55
58
|
}
|
|
56
59
|
// Detect password comparison
|
|
57
60
|
if (node.test.type === 'BinaryExpression') {
|
|
61
|
+
// oxlint-disable-next-line consistent-function-scoping
|
|
58
62
|
const checkMember = (expr) => {
|
|
59
63
|
if (expr.type === 'MemberExpression' &&
|
|
60
64
|
expr.property.type === 'Identifier' &&
|
|
@@ -44,7 +44,10 @@ exports.noCookieAuthTokens = (0, eslint_devkit_1.createRule)({
|
|
|
44
44
|
meta: {
|
|
45
45
|
type: 'problem',
|
|
46
46
|
docs: {
|
|
47
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-cookie-auth-tokens.md',
|
|
47
48
|
description: 'Disallow storing auth tokens in cookies via JavaScript',
|
|
49
|
+
cwe: 'CWE-1004',
|
|
50
|
+
cvss: 8.5,
|
|
48
51
|
},
|
|
49
52
|
hasSuggestions: true,
|
|
50
53
|
messages: {
|
|
@@ -79,7 +82,7 @@ exports.noCookieAuthTokens = (0, eslint_devkit_1.createRule)({
|
|
|
79
82
|
defaultOptions: [{ allowInTests: true }],
|
|
80
83
|
create(context, [options = {}]) {
|
|
81
84
|
const { allowInTests = true } = options;
|
|
82
|
-
const filename = context.filename
|
|
85
|
+
const filename = context.filename;
|
|
83
86
|
const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
84
87
|
if (allowInTests && isTestFile) {
|
|
85
88
|
return {};
|
|
@@ -17,7 +17,10 @@ exports.noCredentialsInQueryParams = (0, eslint_devkit_1.createRule)({
|
|
|
17
17
|
meta: {
|
|
18
18
|
type: 'problem',
|
|
19
19
|
docs: {
|
|
20
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-credentials-in-query-params.md',
|
|
20
21
|
description: 'Disallow credentials in URL query parameters',
|
|
22
|
+
cwe: 'CWE-798',
|
|
23
|
+
cvss: 9.5,
|
|
21
24
|
},
|
|
22
25
|
messages: {
|
|
23
26
|
violationDetected: (0, eslint_devkit_1.formatLLMMessage)({
|
|
@@ -15,7 +15,10 @@ exports.noDisabledCertificateValidation = (0, eslint_devkit_1.createRule)({
|
|
|
15
15
|
meta: {
|
|
16
16
|
type: 'problem',
|
|
17
17
|
docs: {
|
|
18
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-disabled-certificate-validation.md',
|
|
18
19
|
description: 'Prevent disabled SSL/TLS certificate validation',
|
|
20
|
+
cwe: 'CWE-295',
|
|
21
|
+
cvss: 9.5,
|
|
19
22
|
},
|
|
20
23
|
messages: {
|
|
21
24
|
violationDetected: (0, eslint_devkit_1.formatLLMMessage)({
|
|
@@ -35,12 +38,12 @@ exports.noDisabledCertificateValidation = (0, eslint_devkit_1.createRule)({
|
|
|
35
38
|
function report(node) {
|
|
36
39
|
context.report({ node, messageId: 'violationDetected' });
|
|
37
40
|
}
|
|
38
|
-
const dangerousProperties = ['rejectUnauthorized', 'strictSSL', 'verify'];
|
|
41
|
+
const dangerousProperties = new Set(['rejectUnauthorized', 'strictSSL', 'verify']);
|
|
39
42
|
return {
|
|
40
43
|
Property(node) {
|
|
41
44
|
// Check for dangerous SSL options set to false
|
|
42
45
|
if (node.key.type === 'Identifier' &&
|
|
43
|
-
dangerousProperties.
|
|
46
|
+
dangerousProperties.has(node.key.name) &&
|
|
44
47
|
node.value.type === 'Literal' &&
|
|
45
48
|
node.value.value === false) {
|
|
46
49
|
report(node);
|
|
@@ -12,7 +12,10 @@ exports.noDynamicServiceWorkerUrl = (0, eslint_devkit_1.createRule)({
|
|
|
12
12
|
meta: {
|
|
13
13
|
type: 'problem',
|
|
14
14
|
docs: {
|
|
15
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-dynamic-service-worker-url.md',
|
|
15
16
|
description: 'Disallow dynamic URLs in service worker registration',
|
|
17
|
+
cwe: 'CWE-829',
|
|
18
|
+
cvss: 8.1,
|
|
16
19
|
},
|
|
17
20
|
hasSuggestions: true,
|
|
18
21
|
messages: {
|
|
@@ -47,7 +50,7 @@ exports.noDynamicServiceWorkerUrl = (0, eslint_devkit_1.createRule)({
|
|
|
47
50
|
defaultOptions: [{ allowInTests: true }],
|
|
48
51
|
create(context, [options = {}]) {
|
|
49
52
|
const { allowInTests = true } = options;
|
|
50
|
-
const filename = context.filename
|
|
53
|
+
const filename = context.filename;
|
|
51
54
|
const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
52
55
|
if (allowInTests && isTestFile) {
|
|
53
56
|
return {};
|
|
@@ -21,7 +21,10 @@ exports.noEval = (0, eslint_devkit_1.createRule)({
|
|
|
21
21
|
meta: {
|
|
22
22
|
type: 'problem',
|
|
23
23
|
docs: {
|
|
24
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-eval.md',
|
|
24
25
|
description: 'Disallow eval(), Function(), and other code execution patterns',
|
|
26
|
+
cwe: 'CWE-95',
|
|
27
|
+
cvss: 9.5,
|
|
25
28
|
},
|
|
26
29
|
hasSuggestions: true,
|
|
27
30
|
messages: {
|
|
@@ -112,21 +115,37 @@ exports.noEval = (0, eslint_devkit_1.createRule)({
|
|
|
112
115
|
}
|
|
113
116
|
}
|
|
114
117
|
}
|
|
115
|
-
// Check for window.eval
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
118
|
+
// Check for `window.eval`, `global.eval`, `globalThis.eval`, `self.eval`
|
|
119
|
+
// (member access — non-computed) AND `window['eval']`, `globalThis['Function']`
|
|
120
|
+
// (computed — Literal property). The second form was an audit FN
|
|
121
|
+
// surfaced by `npm run ilb:stress-test` (see benchmarks/AUDIT_PATTERNS.md
|
|
122
|
+
// §3.3 — "indirect access via bracket notation").
|
|
123
|
+
if (callee.type === 'MemberExpression') {
|
|
124
|
+
let propertyName = null;
|
|
125
|
+
if (!callee.computed &&
|
|
126
|
+
callee.property.type === 'Identifier' &&
|
|
127
|
+
DANGEROUS_FUNCTIONS.has(callee.property.name)) {
|
|
128
|
+
propertyName = callee.property.name;
|
|
129
|
+
}
|
|
130
|
+
else if (callee.computed &&
|
|
131
|
+
callee.property.type === 'Literal' &&
|
|
132
|
+
typeof callee.property.value === 'string' &&
|
|
133
|
+
DANGEROUS_FUNCTIONS.has(callee.property.value)) {
|
|
134
|
+
propertyName = callee.property.value;
|
|
135
|
+
}
|
|
136
|
+
if (propertyName) {
|
|
137
|
+
context.report({
|
|
138
|
+
node,
|
|
139
|
+
messageId: 'dangerousEval',
|
|
140
|
+
data: { function: propertyName },
|
|
141
|
+
suggest: [
|
|
142
|
+
{
|
|
143
|
+
messageId: 'useSafeAlternative',
|
|
144
|
+
fix: () => null,
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
});
|
|
148
|
+
}
|
|
130
149
|
}
|
|
131
150
|
},
|
|
132
151
|
// Check for new Function()
|
|
@@ -8,14 +8,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
8
8
|
exports.noFilereaderInnerhtml = void 0;
|
|
9
9
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
10
|
// Dangerous DOM properties and methods
|
|
11
|
-
const DANGEROUS_PROPERTIES = ['innerHTML', 'outerHTML'];
|
|
12
|
-
const DANGEROUS_METHODS = ['insertAdjacentHTML', 'write', 'writeln'];
|
|
11
|
+
const DANGEROUS_PROPERTIES = new Set(['innerHTML', 'outerHTML']);
|
|
12
|
+
const DANGEROUS_METHODS = new Set(['insertAdjacentHTML', 'write', 'writeln']);
|
|
13
13
|
exports.noFilereaderInnerhtml = (0, eslint_devkit_1.createRule)({
|
|
14
14
|
name: 'no-filereader-innerhtml',
|
|
15
15
|
meta: {
|
|
16
16
|
type: 'problem',
|
|
17
17
|
docs: {
|
|
18
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-filereader-innerhtml.md',
|
|
18
19
|
description: 'Disallow using innerHTML or similar methods with FileReader data',
|
|
20
|
+
cwe: 'CWE-79',
|
|
21
|
+
cvss: 8.1,
|
|
19
22
|
},
|
|
20
23
|
hasSuggestions: true,
|
|
21
24
|
messages: {
|
|
@@ -55,7 +58,7 @@ exports.noFilereaderInnerhtml = (0, eslint_devkit_1.createRule)({
|
|
|
55
58
|
defaultOptions: [{ allowInTests: true }],
|
|
56
59
|
create(context, [options = {}]) {
|
|
57
60
|
const { allowInTests = true } = options;
|
|
58
|
-
const filename = context.filename
|
|
61
|
+
const filename = context.filename;
|
|
59
62
|
const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
60
63
|
if (allowInTests && isTestFile) {
|
|
61
64
|
return {};
|
|
@@ -147,7 +150,7 @@ exports.noFilereaderInnerhtml = (0, eslint_devkit_1.createRule)({
|
|
|
147
150
|
return;
|
|
148
151
|
if (node.left.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
149
152
|
node.left.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
150
|
-
DANGEROUS_PROPERTIES.
|
|
153
|
+
DANGEROUS_PROPERTIES.has(node.left.property.name)) {
|
|
151
154
|
if (referencesFileReaderResult(node.right, eventParamName)) {
|
|
152
155
|
context.report({
|
|
153
156
|
node,
|
|
@@ -186,7 +189,7 @@ exports.noFilereaderInnerhtml = (0, eslint_devkit_1.createRule)({
|
|
|
186
189
|
return;
|
|
187
190
|
if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
188
191
|
node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
189
|
-
DANGEROUS_METHODS.
|
|
192
|
+
DANGEROUS_METHODS.has(node.callee.property.name)) {
|
|
190
193
|
for (const arg of node.arguments) {
|
|
191
194
|
if (referencesFileReaderResult(arg, eventParamName)) {
|
|
192
195
|
context.report({
|
|
@@ -17,7 +17,10 @@ exports.noHttpUrls = (0, eslint_devkit_1.createRule)({
|
|
|
17
17
|
meta: {
|
|
18
18
|
type: 'problem',
|
|
19
19
|
docs: {
|
|
20
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-http-urls.md',
|
|
20
21
|
description: 'Disallow hardcoded HTTP URLs (require HTTPS)',
|
|
22
|
+
cwe: 'CWE-319',
|
|
23
|
+
cvss: 7.5,
|
|
21
24
|
},
|
|
22
25
|
messages: {
|
|
23
26
|
insecureHttp: (0, eslint_devkit_1.formatLLMMessage)({
|
|
@@ -38,7 +38,11 @@ exports.noInnerhtml = (0, eslint_devkit_1.createRule)({
|
|
|
38
38
|
meta: {
|
|
39
39
|
type: 'problem',
|
|
40
40
|
docs: {
|
|
41
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-innerhtml.md',
|
|
41
42
|
description: 'Disallow dangerous innerHTML/outerHTML assignments that can lead to XSS',
|
|
43
|
+
cwe: 'CWE-79',
|
|
44
|
+
cvss: 9.5,
|
|
45
|
+
confidence: 'medium',
|
|
42
46
|
},
|
|
43
47
|
hasSuggestions: true,
|
|
44
48
|
messages: {
|
|
@@ -97,7 +101,41 @@ exports.noInnerhtml = (0, eslint_devkit_1.createRule)({
|
|
|
97
101
|
return {};
|
|
98
102
|
}
|
|
99
103
|
const sourceCode = context.sourceCode;
|
|
100
|
-
const dangerousProperties = ['innerHTML', 'outerHTML'];
|
|
104
|
+
const dangerousProperties = new Set(['innerHTML', 'outerHTML']);
|
|
105
|
+
// Sibling DOM sinks that share the same XSS class — surfaced as
|
|
106
|
+
// FN by the hand-curated stress test. See benchmarks/AUDIT_PATTERNS.md
|
|
107
|
+
// §3.1 ("DOM XSS sink list"). Each takes user-controlled HTML and
|
|
108
|
+
// injects it into the live DOM exactly the same way innerHTML does.
|
|
109
|
+
const dangerousSinkMethods = new Set([
|
|
110
|
+
'insertAdjacentHTML',
|
|
111
|
+
'write',
|
|
112
|
+
'writeln',
|
|
113
|
+
]);
|
|
114
|
+
function reportSink(reportNode, sinkName, taintedNode) {
|
|
115
|
+
// Allow literal strings if configured.
|
|
116
|
+
if (allowLiteralStrings && isLiteralString(taintedNode))
|
|
117
|
+
return;
|
|
118
|
+
// Allow if sanitized via a trusted sanitiser call.
|
|
119
|
+
if (isSanitized(taintedNode, sourceCode, trustedSanitizers))
|
|
120
|
+
return;
|
|
121
|
+
// Determine source type for the diagnostic message.
|
|
122
|
+
let source = 'dynamic content';
|
|
123
|
+
if (taintedNode.type === 'Identifier') {
|
|
124
|
+
source = `variable "${taintedNode.name}"`;
|
|
125
|
+
}
|
|
126
|
+
else if (taintedNode.type === 'TemplateLiteral') {
|
|
127
|
+
source = 'template literal with expressions';
|
|
128
|
+
}
|
|
129
|
+
else if (taintedNode.type === 'CallExpression') {
|
|
130
|
+
source = 'function call result';
|
|
131
|
+
}
|
|
132
|
+
context.report({
|
|
133
|
+
node: reportNode,
|
|
134
|
+
messageId: 'dangerousInnerHTML',
|
|
135
|
+
data: { property: sinkName, source },
|
|
136
|
+
suggest: [{ messageId: 'useSanitizer', fix: () => null }],
|
|
137
|
+
});
|
|
138
|
+
}
|
|
101
139
|
return {
|
|
102
140
|
AssignmentExpression(node) {
|
|
103
141
|
// Check for element.innerHTML = ... or element.outerHTML = ...
|
|
@@ -109,43 +147,29 @@ exports.noInnerhtml = (0, eslint_devkit_1.createRule)({
|
|
|
109
147
|
if (property.type !== 'Identifier') {
|
|
110
148
|
return;
|
|
111
149
|
}
|
|
112
|
-
if (!dangerousProperties.
|
|
150
|
+
if (!dangerousProperties.has(property.name)) {
|
|
113
151
|
return;
|
|
114
152
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
153
|
+
reportSink(node, property.name, node.right);
|
|
154
|
+
},
|
|
155
|
+
// element.insertAdjacentHTML(position, htmlString) — same XSS class
|
|
156
|
+
// as innerHTML but a different sink shape. Also covers
|
|
157
|
+
// document.write(...) and document.writeln(...) which are the
|
|
158
|
+
// historic parent injection sinks.
|
|
159
|
+
CallExpression(node) {
|
|
160
|
+
if (node.callee.type !== 'MemberExpression')
|
|
118
161
|
return;
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (isSanitized(rightSide, sourceCode, trustedSanitizers)) {
|
|
162
|
+
const property = node.callee.property;
|
|
163
|
+
if (property.type !== 'Identifier')
|
|
122
164
|
return;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
else if (rightSide.type === 'CallExpression') {
|
|
133
|
-
source = 'function call result';
|
|
134
|
-
}
|
|
135
|
-
context.report({
|
|
136
|
-
node,
|
|
137
|
-
messageId: 'dangerousInnerHTML',
|
|
138
|
-
data: {
|
|
139
|
-
property: property.name,
|
|
140
|
-
source,
|
|
141
|
-
},
|
|
142
|
-
suggest: [
|
|
143
|
-
{
|
|
144
|
-
messageId: 'useSanitizer',
|
|
145
|
-
fix: () => null,
|
|
146
|
-
},
|
|
147
|
-
],
|
|
148
|
-
});
|
|
165
|
+
if (!dangerousSinkMethods.has(property.name))
|
|
166
|
+
return;
|
|
167
|
+
// The HTML payload is the LAST argument for insertAdjacentHTML
|
|
168
|
+
// (`(position, html)`) and the only argument for write/writeln.
|
|
169
|
+
const tainted = node.arguments[node.arguments.length - 1];
|
|
170
|
+
if (!tainted)
|
|
171
|
+
return;
|
|
172
|
+
reportSink(node, property.name, tainted);
|
|
149
173
|
},
|
|
150
174
|
};
|
|
151
175
|
},
|
|
@@ -8,6 +8,37 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
8
8
|
exports.noInsecureRedirects = void 0;
|
|
9
9
|
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
10
10
|
const eslint_devkit_2 = require("@interlace/eslint-devkit");
|
|
11
|
+
/**
|
|
12
|
+
* Resolve an identifier to the text of its initialization expression.
|
|
13
|
+
* Scans backward through sibling statements in the same block scope.
|
|
14
|
+
*/
|
|
15
|
+
function resolveVariableInit(ident, sourceCode) {
|
|
16
|
+
const name = ident.name;
|
|
17
|
+
let current = ident.parent;
|
|
18
|
+
while (current) {
|
|
19
|
+
const parent = current.parent;
|
|
20
|
+
if (!parent)
|
|
21
|
+
break;
|
|
22
|
+
if (parent.type === 'BlockStatement' || parent.type === 'Program') {
|
|
23
|
+
const body = parent.type === 'BlockStatement' ? parent.body : parent.body;
|
|
24
|
+
const idx = body.indexOf(current);
|
|
25
|
+
for (let i = idx - 1; i >= 0 && i >= idx - 10; i--) {
|
|
26
|
+
const stmt = body[i];
|
|
27
|
+
if (stmt.type === 'VariableDeclaration') {
|
|
28
|
+
for (const decl of stmt.declarations) {
|
|
29
|
+
if (decl.id.type === 'Identifier' &&
|
|
30
|
+
decl.id.name === name &&
|
|
31
|
+
decl.init) {
|
|
32
|
+
return sourceCode.getText(decl.init);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
current = parent;
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
11
42
|
/**
|
|
12
43
|
* Check if redirect URL is validated
|
|
13
44
|
*/
|
|
@@ -15,7 +46,20 @@ function isRedirectValidated(node, sourceCode) {
|
|
|
15
46
|
const callText = sourceCode.getText(node);
|
|
16
47
|
// Check if URL is from user input (req.query, req.body, req.params)
|
|
17
48
|
const userInputPattern = /\b(req\.(query|body|params)|window\.location|document\.location)\b/;
|
|
18
|
-
|
|
49
|
+
// Direct check: res.redirect(req.query.returnTo)
|
|
50
|
+
let hasUserInput = userInputPattern.test(callText);
|
|
51
|
+
// Indirect check: resolve variable references to their init expressions
|
|
52
|
+
// e.g., const returnUrl = req.query.returnTo; res.redirect(returnUrl);
|
|
53
|
+
if (!hasUserInput && node.arguments.length > 0) {
|
|
54
|
+
const arg = node.arguments[0];
|
|
55
|
+
if (arg.type === 'Identifier') {
|
|
56
|
+
const resolved = resolveVariableInit(arg, sourceCode);
|
|
57
|
+
if (resolved && userInputPattern.test(resolved)) {
|
|
58
|
+
hasUserInput = true;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (!hasUserInput) {
|
|
19
63
|
// Not from user input, assume safe
|
|
20
64
|
return true;
|
|
21
65
|
}
|
|
@@ -32,9 +76,9 @@ function isRedirectValidated(node, sourceCode) {
|
|
|
32
76
|
// Check for validation function calls before this redirect
|
|
33
77
|
const validationPatterns = [
|
|
34
78
|
/\b(validateUrl|validateRedirect|isValidUrl|isSafeUrl)\s*\(/,
|
|
35
|
-
/\b(whitelist|allowedDomains|permittedUrls)\s*\./,
|
|
79
|
+
/\b(whitelist|allowedDomains|permittedUrls|ALLOWED_\w+)\s*\./,
|
|
36
80
|
/\b(url\.hostname|url\.host)\s*===/,
|
|
37
|
-
/\
|
|
81
|
+
/\b\w+\.(includes|has)\s*\(/, // Generic allowlist check: arr.includes(x) or set.has(x)
|
|
38
82
|
/\bstartsWith\s*\(\s*['"]/,
|
|
39
83
|
/\bendsWith\s*\(\s*['"]/,
|
|
40
84
|
/\bindexOf\s*\(\s*['"]/,
|
|
@@ -49,7 +93,7 @@ function isRedirectValidated(node, sourceCode) {
|
|
|
49
93
|
const parent = current.parent;
|
|
50
94
|
if (!parent)
|
|
51
95
|
break;
|
|
52
|
-
if (parent.type === 'BlockStatement') {
|
|
96
|
+
if (parent.type === 'BlockStatement' || parent.type === 'Program') {
|
|
53
97
|
const body = parent.body;
|
|
54
98
|
const currentIndex = body.indexOf(current);
|
|
55
99
|
// Check previous statements in the same block
|
|
@@ -61,6 +105,14 @@ function isRedirectValidated(node, sourceCode) {
|
|
|
61
105
|
}
|
|
62
106
|
}
|
|
63
107
|
}
|
|
108
|
+
// Check if inside an if-block with validation in the condition
|
|
109
|
+
// e.g., if (isValidUrl(redirect)) { res.redirect(redirect); }
|
|
110
|
+
if (parent.type === 'IfStatement' && parent.test) {
|
|
111
|
+
const testText = sourceCode.getText(parent.test);
|
|
112
|
+
if (validationPatterns.some(pattern => pattern.test(testText))) {
|
|
113
|
+
return true; // Validation is the if-condition
|
|
114
|
+
}
|
|
115
|
+
}
|
|
64
116
|
current = parent;
|
|
65
117
|
depth++;
|
|
66
118
|
}
|
|
@@ -72,7 +124,10 @@ exports.noInsecureRedirects = (0, eslint_devkit_2.createRule)({
|
|
|
72
124
|
meta: {
|
|
73
125
|
type: 'problem',
|
|
74
126
|
docs: {
|
|
127
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-insecure-redirects.md',
|
|
75
128
|
description: 'Detects open redirect vulnerabilities',
|
|
129
|
+
cwe: 'CWE-601',
|
|
130
|
+
cvss: 7.5,
|
|
76
131
|
},
|
|
77
132
|
hasSuggestions: true,
|
|
78
133
|
messages: {
|
|
@@ -136,12 +191,12 @@ exports.noInsecureRedirects = (0, eslint_devkit_2.createRule)({
|
|
|
136
191
|
],
|
|
137
192
|
create(context, [options = {}]) {
|
|
138
193
|
const { ignoreInTests = true } = options || {};
|
|
139
|
-
const filename = context.
|
|
194
|
+
const filename = context.filename;
|
|
140
195
|
const isTestFile = ignoreInTests && /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
141
196
|
if (isTestFile) {
|
|
142
197
|
return {};
|
|
143
198
|
}
|
|
144
|
-
const sourceCode = context.sourceCode
|
|
199
|
+
const sourceCode = context.sourceCode;
|
|
145
200
|
/**
|
|
146
201
|
* Check redirect calls and assignments
|
|
147
202
|
*/
|
|
@@ -15,7 +15,10 @@ exports.noInsecureWebsocket = (0, eslint_devkit_1.createRule)({
|
|
|
15
15
|
meta: {
|
|
16
16
|
type: 'problem',
|
|
17
17
|
docs: {
|
|
18
|
+
url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-insecure-websocket.md',
|
|
18
19
|
description: 'Require secure WebSocket connections (wss://)',
|
|
20
|
+
cwe: 'CWE-319',
|
|
21
|
+
cvss: 7.5,
|
|
19
22
|
},
|
|
20
23
|
messages: {
|
|
21
24
|
violationDetected: (0, eslint_devkit_1.formatLLMMessage)({
|