eslint-plugin-browser-security 1.2.2 → 1.2.3
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 +1 -1
- package/src/index.js +1 -0
- package/src/rules/no-insecure-redirects/index.js +56 -4
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
<a href="https://www.npmjs.com/package/eslint-plugin-browser-security" target="_blank"><img src="https://img.shields.io/npm/v/eslint-plugin-browser-security.svg" alt="NPM Version" /></a>
|
|
11
11
|
<a href="https://www.npmjs.com/package/eslint-plugin-browser-security" target="_blank"><img src="https://img.shields.io/npm/dm/eslint-plugin-browser-security.svg" alt="NPM Downloads" /></a>
|
|
12
12
|
<a href="https://opensource.org/licenses/MIT" target="_blank"><img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="Package License" /></a>
|
|
13
|
-
<a href="https://app.codecov.io/gh/ofri-peretz/eslint/components?components%5B0%5D=browser-security" target="_blank"><img src="https://codecov.io/gh/ofri-peretz/eslint/graph/badge.svg?component=browser-security" alt="Codecov" /></a>
|
|
13
|
+
<a href="https://app.codecov.io/gh/ofri-peretz/eslint/components?components%5B0%5D=browser-security" target="_blank"><img src="https://codecov.io/gh/ofri-peretz/eslint/graph/badge.svg?component=eslint-plugin-browser-security" alt="Codecov" /></a>
|
|
14
14
|
<a href="https://github.com/ofri-peretz/eslint" target="_blank"><img src="https://img.shields.io/badge/Since-Dec_2025-blue?logo=rocket&logoColor=white" alt="Since Dec 2025" /></a>
|
|
15
15
|
</p>
|
|
16
16
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-browser-security",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"description": "Security-focused ESLint plugin for browser applications. Detects XSS vulnerabilities, postMessage abuse, localStorage/sessionStorage/IndexedDB token exposure, cookie security issues, WebSocket security, Blob URL leaks, Service Worker risks, and CSP misconfigurations.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./src/index.js",
|
package/src/index.js
CHANGED
|
@@ -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',
|
|
@@ -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
|
}
|