eslint-plugin-browser-security 0.0.1
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 +75 -0
- package/README.md +205 -0
- package/package.json +92 -0
- package/src/index.d.ts +59 -0
- package/src/index.js +216 -0
- package/src/index.js.map +1 -0
- package/src/rules/no-cookie-auth-tokens/index.d.ts +5 -0
- package/src/rules/no-cookie-auth-tokens/index.js +115 -0
- package/src/rules/no-cookie-auth-tokens/index.js.map +1 -0
- package/src/rules/no-dynamic-service-worker-url/index.d.ts +5 -0
- package/src/rules/no-dynamic-service-worker-url/index.js +80 -0
- package/src/rules/no-dynamic-service-worker-url/index.js.map +1 -0
- package/src/rules/no-eval/index.d.ts +7 -0
- package/src/rules/no-eval/index.js +150 -0
- package/src/rules/no-eval/index.js.map +1 -0
- package/src/rules/no-filereader-innerhtml/index.d.ts +5 -0
- package/src/rules/no-filereader-innerhtml/index.js +217 -0
- package/src/rules/no-filereader-innerhtml/index.js.map +1 -0
- package/src/rules/no-innerhtml/index.d.ts +9 -0
- package/src/rules/no-innerhtml/index.js +148 -0
- package/src/rules/no-innerhtml/index.js.map +1 -0
- package/src/rules/no-jwt-in-storage/index.d.ts +5 -0
- package/src/rules/no-jwt-in-storage/index.js +197 -0
- package/src/rules/no-jwt-in-storage/index.js.map +1 -0
- package/src/rules/no-postmessage-innerhtml/index.d.ts +5 -0
- package/src/rules/no-postmessage-innerhtml/index.js +174 -0
- package/src/rules/no-postmessage-innerhtml/index.js.map +1 -0
- package/src/rules/no-postmessage-wildcard-origin/index.d.ts +5 -0
- package/src/rules/no-postmessage-wildcard-origin/index.js +115 -0
- package/src/rules/no-postmessage-wildcard-origin/index.js.map +1 -0
- package/src/rules/no-sensitive-cookie-js/index.d.ts +7 -0
- package/src/rules/no-sensitive-cookie-js/index.js +163 -0
- package/src/rules/no-sensitive-cookie-js/index.js.map +1 -0
- package/src/rules/no-sensitive-indexeddb/index.d.ts +5 -0
- package/src/rules/no-sensitive-indexeddb/index.js +111 -0
- package/src/rules/no-sensitive-indexeddb/index.js.map +1 -0
- package/src/rules/no-sensitive-localstorage/index.d.ts +9 -0
- package/src/rules/no-sensitive-localstorage/index.js +179 -0
- package/src/rules/no-sensitive-localstorage/index.js.map +1 -0
- package/src/rules/no-sensitive-sessionstorage/index.d.ts +7 -0
- package/src/rules/no-sensitive-sessionstorage/index.js +148 -0
- package/src/rules/no-sensitive-sessionstorage/index.js.map +1 -0
- package/src/rules/no-unsafe-eval-csp/index.d.ts +5 -0
- package/src/rules/no-unsafe-eval-csp/index.js +78 -0
- package/src/rules/no-unsafe-eval-csp/index.js.map +1 -0
- package/src/rules/no-unsafe-inline-csp/index.d.ts +5 -0
- package/src/rules/no-unsafe-inline-csp/index.js +78 -0
- package/src/rules/no-unsafe-inline-csp/index.js.map +1 -0
- package/src/rules/no-websocket-eval/index.d.ts +5 -0
- package/src/rules/no-websocket-eval/index.js +223 -0
- package/src/rules/no-websocket-eval/index.js.map +1 -0
- package/src/rules/no-websocket-innerhtml/index.d.ts +5 -0
- package/src/rules/no-websocket-innerhtml/index.js +198 -0
- package/src/rules/no-websocket-innerhtml/index.js.map +1 -0
- package/src/rules/no-worker-message-innerhtml/index.d.ts +5 -0
- package/src/rules/no-worker-message-innerhtml/index.js +198 -0
- package/src/rules/no-worker-message-innerhtml/index.js.map +1 -0
- package/src/rules/require-blob-url-revocation/index.d.ts +5 -0
- package/src/rules/require-blob-url-revocation/index.js +96 -0
- package/src/rules/require-blob-url-revocation/index.js.map +1 -0
- package/src/rules/require-cookie-secure-attrs/index.d.ts +5 -0
- package/src/rules/require-cookie-secure-attrs/index.js +101 -0
- package/src/rules/require-cookie-secure-attrs/index.js.map +1 -0
- package/src/rules/require-postmessage-origin-check/index.d.ts +7 -0
- package/src/rules/require-postmessage-origin-check/index.js +142 -0
- package/src/rules/require-postmessage-origin-check/index.js.map +1 -0
- package/src/rules/require-websocket-wss/index.d.ts +7 -0
- package/src/rules/require-websocket-wss/index.js +134 -0
- package/src/rules/require-websocket-wss/index.js.map +1 -0
package/AGENTS.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# AGENTS.md
|
|
2
|
+
|
|
3
|
+
> Context for AI coding agents working on eslint-plugin-browser-security
|
|
4
|
+
|
|
5
|
+
## Setup Commands
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# Install dependencies (from monorepo root)
|
|
9
|
+
pnpm install
|
|
10
|
+
|
|
11
|
+
# Build this package
|
|
12
|
+
nx build eslint-plugin-browser-security
|
|
13
|
+
|
|
14
|
+
# Run tests
|
|
15
|
+
nx test eslint-plugin-browser-security
|
|
16
|
+
|
|
17
|
+
# Run tests with coverage
|
|
18
|
+
nx test eslint-plugin-browser-security --coverage
|
|
19
|
+
|
|
20
|
+
# Lint this package
|
|
21
|
+
nx lint eslint-plugin-browser-security
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Code Style
|
|
25
|
+
|
|
26
|
+
- TypeScript strict mode with `@interlace/eslint-devkit` types
|
|
27
|
+
- Use `AST_NODE_TYPES` constants, never string literals for node types
|
|
28
|
+
- Use `formatLLMMessage()` for all rule error messages
|
|
29
|
+
- Include CWE, CVSS, OWASP in every security message
|
|
30
|
+
- Use `c8 ignore` comments with documented reasons for untestable code
|
|
31
|
+
- Single-pass AST traversal patterns (O(n) complexity)
|
|
32
|
+
|
|
33
|
+
## Testing Instructions
|
|
34
|
+
|
|
35
|
+
- Tests use `@typescript-eslint/rule-tester` with Vitest
|
|
36
|
+
- Each rule has `index.ts` (implementation) and `*.test.ts` (tests) in same directory
|
|
37
|
+
- Run specific rule test: `nx test eslint-plugin-browser-security --testPathPattern="no-innerhtml"`
|
|
38
|
+
- Coverage target: ≥90% lines, ≥95% functions
|
|
39
|
+
- All tests must pass before committing
|
|
40
|
+
|
|
41
|
+
## Project Structure
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
src/
|
|
45
|
+
├── index.ts # Plugin entry, exports rules and 7 configs
|
|
46
|
+
└── rules/ # 21 rule directories
|
|
47
|
+
└── [rule-name]/
|
|
48
|
+
├── index.ts # Rule implementation
|
|
49
|
+
└── *.test.ts # Rule tests
|
|
50
|
+
docs/rules/ # Markdown documentation per rule
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Plugin Purpose
|
|
54
|
+
|
|
55
|
+
Security-focused ESLint plugin with **21 rules** for browser application security. Covers XSS prevention, postMessage abuse, storage security, cookie security, WebSocket vulnerabilities, File API, Workers, and CSP.
|
|
56
|
+
|
|
57
|
+
## Rule Categories
|
|
58
|
+
|
|
59
|
+
| Category | Rules | CWEs |
|
|
60
|
+
| ------------------ | ------------------------------------------------------------------------------------------------------------------------ | ------------ |
|
|
61
|
+
| XSS Prevention | `no-innerhtml`, `no-eval` | 79, 95 |
|
|
62
|
+
| postMessage | `require-postmessage-origin-check`, `no-postmessage-wildcard-origin`, `no-postmessage-innerhtml` | 79, 346 |
|
|
63
|
+
| Storage | `no-sensitive-localstorage`, `no-sensitive-sessionstorage`, `no-sensitive-indexeddb`, `no-jwt-in-storage` | 922 |
|
|
64
|
+
| Cookies | `no-sensitive-cookie-js`, `no-cookie-auth-tokens`, `require-cookie-secure-attrs` | 614, 1004 |
|
|
65
|
+
| WebSocket | `require-websocket-wss`, `no-websocket-innerhtml`, `no-websocket-eval` | 79, 95, 319 |
|
|
66
|
+
| File API & Workers | `no-filereader-innerhtml`, `require-blob-url-revocation`, `no-dynamic-service-worker-url`, `no-worker-message-innerhtml` | 79, 401, 829 |
|
|
67
|
+
| CSP | `no-unsafe-inline-csp`, `no-unsafe-eval-csp` | 79, 95 |
|
|
68
|
+
|
|
69
|
+
## Common Fix Patterns
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
// XSS - innerHTML
|
|
73
|
+
// BAD: element.innerHTML = userInput
|
|
74
|
+
// GOOD: element.textContent = userInput
|
|
75
|
+
// GOOD: element.innerHTML = DOMPurify.sanitize(userInput)
|
|
76
|
+
|
|
77
|
+
// postMessage origin check
|
|
78
|
+
// BAD: window.addEventListener('message', (e) => process(e.data))
|
|
79
|
+
// GOOD: window.addEventListener('message', (e) => {
|
|
80
|
+
// if (e.origin !== 'https://trusted.com') return;
|
|
81
|
+
// process(e.data);
|
|
82
|
+
// })
|
|
83
|
+
|
|
84
|
+
// Storage - JWT
|
|
85
|
+
// BAD: localStorage.setItem('token', jwt)
|
|
86
|
+
// GOOD: Use HttpOnly cookies set by server
|
|
87
|
+
|
|
88
|
+
// WebSocket
|
|
89
|
+
// BAD: new WebSocket('ws://example.com')
|
|
90
|
+
// GOOD: new WebSocket('wss://example.com')
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Security Considerations
|
|
94
|
+
|
|
95
|
+
- All rules map to OWASP Top 10 2021 categories
|
|
96
|
+
- CWE coverage: 79, 95, 319, 346, 401, 614, 829, 922, 1004
|
|
97
|
+
- Every rule message includes remediation guidance for both humans and LLMs
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `eslint-plugin-browser-security` will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [1.0.0] - 2025-12-29
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
#### XSS Prevention Rules (2)
|
|
13
|
+
|
|
14
|
+
- `no-innerhtml` - Detect dangerous innerHTML/outerHTML assignments (CWE-79)
|
|
15
|
+
- `no-eval` - Detect eval(), new Function(), and string-based callbacks (CWE-95)
|
|
16
|
+
|
|
17
|
+
#### postMessage Security Rules (3)
|
|
18
|
+
|
|
19
|
+
- `require-postmessage-origin-check` - Require origin validation in message handlers (CWE-346)
|
|
20
|
+
- `no-postmessage-wildcard-origin` - Prevent wildcard targetOrigin in postMessage (CWE-346)
|
|
21
|
+
- `no-postmessage-innerhtml` - Prevent XSS via innerHTML in postMessage handlers (CWE-79)
|
|
22
|
+
|
|
23
|
+
#### Storage Security Rules (4)
|
|
24
|
+
|
|
25
|
+
- `no-sensitive-localstorage` - Prevent storing sensitive data in localStorage (CWE-922)
|
|
26
|
+
- `no-sensitive-sessionstorage` - Prevent storing sensitive data in sessionStorage (CWE-922)
|
|
27
|
+
- `no-sensitive-indexeddb` - Prevent storing sensitive data in IndexedDB (CWE-922)
|
|
28
|
+
- `no-jwt-in-storage` - Prevent storing JWT tokens in browser storage (CWE-922)
|
|
29
|
+
|
|
30
|
+
#### Cookie Security Rules (3)
|
|
31
|
+
|
|
32
|
+
- `no-sensitive-cookie-js` - Prevent storing sensitive data in cookies via JavaScript (CWE-1004)
|
|
33
|
+
- `no-cookie-auth-tokens` - Prevent auth tokens in JS-accessible cookies (CWE-1004)
|
|
34
|
+
- `require-cookie-secure-attrs` - Require Secure and SameSite cookie attributes (CWE-614)
|
|
35
|
+
|
|
36
|
+
#### WebSocket Security Rules (3)
|
|
37
|
+
|
|
38
|
+
- `require-websocket-wss` - Require secure WebSocket connections (wss://) (CWE-319)
|
|
39
|
+
- `no-websocket-innerhtml` - Prevent XSS via innerHTML in WebSocket handlers (CWE-79)
|
|
40
|
+
- `no-websocket-eval` - Prevent RCE via eval() in WebSocket handlers (CWE-95)
|
|
41
|
+
|
|
42
|
+
#### File API & Workers Security Rules (4)
|
|
43
|
+
|
|
44
|
+
- `no-filereader-innerhtml` - Prevent XSS via innerHTML with FileReader data (CWE-79)
|
|
45
|
+
- `require-blob-url-revocation` - Require revoking Blob URLs to prevent memory leaks (CWE-401)
|
|
46
|
+
- `no-dynamic-service-worker-url` - Prevent dynamic URLs in service worker registration (CWE-829)
|
|
47
|
+
- `no-worker-message-innerhtml` - Prevent XSS via innerHTML in Worker message handlers (CWE-79)
|
|
48
|
+
|
|
49
|
+
#### CSP Security Rules (2)
|
|
50
|
+
|
|
51
|
+
- `no-unsafe-inline-csp` - Disallow 'unsafe-inline' in CSP (CWE-79)
|
|
52
|
+
- `no-unsafe-eval-csp` - Disallow 'unsafe-eval' in CSP (CWE-95)
|
|
53
|
+
|
|
54
|
+
#### Presets (7)
|
|
55
|
+
|
|
56
|
+
- `recommended` - Balanced security defaults
|
|
57
|
+
- `strict` - All 21 rules as errors
|
|
58
|
+
- `xss` - XSS-focused rules only
|
|
59
|
+
- `storage` - Storage security rules only
|
|
60
|
+
- `postmessage` - postMessage security rules only
|
|
61
|
+
- `websocket` - WebSocket security rules only
|
|
62
|
+
- `cookies` - Cookie security rules only
|
|
63
|
+
|
|
64
|
+
#### Features
|
|
65
|
+
|
|
66
|
+
- LLM-optimized error messages with CWE references
|
|
67
|
+
- OWASP Top 10 2021 alignment
|
|
68
|
+
- TypeScript support with exported option types
|
|
69
|
+
- Comprehensive test coverage (297 tests, 97.73% line coverage)
|
|
70
|
+
- Auto-fix suggestions where safe
|
|
71
|
+
|
|
72
|
+
### Security
|
|
73
|
+
|
|
74
|
+
- Covers 8 CWEs: 79, 95, 319, 346, 401, 614, 829, 922, 1004
|
|
75
|
+
- Maps to OWASP Top 10 2021: A01, A02, A03
|
package/README.md
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# eslint-plugin-browser-security
|
|
2
|
+
|
|
3
|
+
> 🔐 Security-focused ESLint plugin for browser applications. Detects XSS vulnerabilities, postMessage abuse, storage API token exposure, cookie security issues, WebSocket vulnerabilities, and more.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/eslint-plugin-browser-security)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
[](https://app.codecov.io/gh/ofri-peretz/eslint/components?components%5B0%5D=browser_security)
|
|
8
|
+
|
|
9
|
+
## 🎯 Why This Plugin?
|
|
10
|
+
|
|
11
|
+
Modern browser applications face unique security challenges across storage APIs, cross-origin communication, and dynamic content rendering. This plugin provides static analysis rules specifically designed for browser security patterns:
|
|
12
|
+
|
|
13
|
+
- **XSS Prevention**: Detects dangerous DOM manipulation patterns
|
|
14
|
+
- **Storage Security**: Prevents sensitive data exposure in localStorage/sessionStorage/IndexedDB
|
|
15
|
+
- **Cross-Origin Protection**: Validates postMessage origin checks
|
|
16
|
+
- **Cookie Security**: Identifies insecure cookie handling in JavaScript
|
|
17
|
+
- **LLM-Optimized**: All rules include AI-friendly remediation guidance
|
|
18
|
+
|
|
19
|
+
## 📦 Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install eslint-plugin-browser-security --save-dev
|
|
23
|
+
# or
|
|
24
|
+
pnpm add -D eslint-plugin-browser-security
|
|
25
|
+
# or
|
|
26
|
+
yarn add -D eslint-plugin-browser-security
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## 🚀 Quick Start
|
|
30
|
+
|
|
31
|
+
### Flat Config (ESLint 9+)
|
|
32
|
+
|
|
33
|
+
```javascript
|
|
34
|
+
// eslint.config.js
|
|
35
|
+
import browserSecurity from 'eslint-plugin-browser-security';
|
|
36
|
+
|
|
37
|
+
export default [
|
|
38
|
+
{
|
|
39
|
+
plugins: {
|
|
40
|
+
'browser-security': browserSecurity,
|
|
41
|
+
},
|
|
42
|
+
rules: {
|
|
43
|
+
'browser-security/no-eval': 'error',
|
|
44
|
+
'browser-security/no-innerhtml': 'error',
|
|
45
|
+
'browser-security/no-sensitive-localstorage': 'error',
|
|
46
|
+
'browser-security/require-postmessage-origin-check': 'error',
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
];
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Using Recommended Config
|
|
53
|
+
|
|
54
|
+
```javascript
|
|
55
|
+
// eslint.config.js
|
|
56
|
+
import browserSecurity from 'eslint-plugin-browser-security';
|
|
57
|
+
|
|
58
|
+
export default [browserSecurity.configs.recommended];
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## 📋 Rules (21 total)
|
|
62
|
+
|
|
63
|
+
### XSS Prevention
|
|
64
|
+
|
|
65
|
+
| Rule | Description | CVSS | CWE |
|
|
66
|
+
| ---------------------------------------------- | --------------------------------------------------------------------------------- | ---- | ------ |
|
|
67
|
+
| [`no-eval`](./docs/rules/no-eval.md) | Prevent code injection via `eval()`, `new Function()`, and string-based callbacks | 9.3 | CWE-95 |
|
|
68
|
+
| [`no-innerhtml`](./docs/rules/no-innerhtml.md) | Prevent XSS via `innerHTML`, `outerHTML`, and `insertAdjacentHTML()` | 8.1 | CWE-79 |
|
|
69
|
+
|
|
70
|
+
### postMessage Security
|
|
71
|
+
|
|
72
|
+
| Rule | Description | CVSS | CWE |
|
|
73
|
+
| -------------------------------------------------------------------------------------- | -------------------------------------------------- | ---- | ------- |
|
|
74
|
+
| [`require-postmessage-origin-check`](./docs/rules/require-postmessage-origin-check.md) | Require origin validation in postMessage handlers | 9.3 | CWE-346 |
|
|
75
|
+
| [`no-postmessage-wildcard-origin`](./docs/rules/no-postmessage-wildcard-origin.md) | Prevent wildcard targetOrigin in postMessage calls | 7.5 | CWE-346 |
|
|
76
|
+
| [`no-postmessage-innerhtml`](./docs/rules/no-postmessage-innerhtml.md) | Prevent XSS via innerHTML in postMessage handlers | 8.8 | CWE-79 |
|
|
77
|
+
|
|
78
|
+
### Storage Security
|
|
79
|
+
|
|
80
|
+
| Rule | Description | CVSS | CWE |
|
|
81
|
+
| ---------------------------------------------------------------------------- | ------------------------------------------------ | ---- | ------- |
|
|
82
|
+
| [`no-sensitive-localstorage`](./docs/rules/no-sensitive-localstorage.md) | Prevent storing sensitive data in localStorage | 7.5 | CWE-922 |
|
|
83
|
+
| [`no-sensitive-sessionstorage`](./docs/rules/no-sensitive-sessionstorage.md) | Prevent storing sensitive data in sessionStorage | 7.5 | CWE-922 |
|
|
84
|
+
| [`no-sensitive-indexeddb`](./docs/rules/no-sensitive-indexeddb.md) | Prevent storing sensitive data in IndexedDB | 7.5 | CWE-922 |
|
|
85
|
+
| [`no-jwt-in-storage`](./docs/rules/no-jwt-in-storage.md) | Prevent storing JWT tokens in browser storage | 8.1 | CWE-922 |
|
|
86
|
+
|
|
87
|
+
### Cookie Security
|
|
88
|
+
|
|
89
|
+
| Rule | Description | CVSS | CWE |
|
|
90
|
+
| ---------------------------------------------------------------------------- | -------------------------------------------------------- | ---- | -------- |
|
|
91
|
+
| [`no-sensitive-cookie-js`](./docs/rules/no-sensitive-cookie-js.md) | Prevent storing sensitive data in cookies via JavaScript | 8.1 | CWE-1004 |
|
|
92
|
+
| [`no-cookie-auth-tokens`](./docs/rules/no-cookie-auth-tokens.md) | Prevent auth tokens in JS-accessible cookies | 8.5 | CWE-1004 |
|
|
93
|
+
| [`require-cookie-secure-attrs`](./docs/rules/require-cookie-secure-attrs.md) | Require Secure and SameSite cookie attributes | 6.5 | CWE-614 |
|
|
94
|
+
|
|
95
|
+
### WebSocket Security
|
|
96
|
+
|
|
97
|
+
| Rule | Description | CVSS | CWE |
|
|
98
|
+
| ------------------------------------------------------------------ | ----------------------------------------------- | ---- | ------- |
|
|
99
|
+
| [`require-websocket-wss`](./docs/rules/require-websocket-wss.md) | Require secure WebSocket connections (wss://) | 7.5 | CWE-319 |
|
|
100
|
+
| [`no-websocket-innerhtml`](./docs/rules/no-websocket-innerhtml.md) | Prevent XSS via innerHTML in WebSocket handlers | 8.1 | CWE-79 |
|
|
101
|
+
| [`no-websocket-eval`](./docs/rules/no-websocket-eval.md) | Prevent RCE via eval() in WebSocket handlers | 9.8 | CWE-95 |
|
|
102
|
+
|
|
103
|
+
### File API & Workers Security
|
|
104
|
+
|
|
105
|
+
| Rule | Description | CVSS | CWE |
|
|
106
|
+
| -------------------------------------------------------------------------------- | ---------------------------------------------------- | ---- | ------- |
|
|
107
|
+
| [`no-filereader-innerhtml`](./docs/rules/no-filereader-innerhtml.md) | Prevent XSS via innerHTML with FileReader data | 8.1 | CWE-79 |
|
|
108
|
+
| [`require-blob-url-revocation`](./docs/rules/require-blob-url-revocation.md) | Require revoking Blob URLs to prevent memory leaks | 5.3 | CWE-401 |
|
|
109
|
+
| [`no-dynamic-service-worker-url`](./docs/rules/no-dynamic-service-worker-url.md) | Prevent dynamic URLs in service worker registration | 8.1 | CWE-829 |
|
|
110
|
+
| [`no-worker-message-innerhtml`](./docs/rules/no-worker-message-innerhtml.md) | Prevent XSS via innerHTML in Worker message handlers | 7.5 | CWE-79 |
|
|
111
|
+
|
|
112
|
+
### CSP Security
|
|
113
|
+
|
|
114
|
+
| Rule | Description | CVSS | CWE |
|
|
115
|
+
| -------------------------------------------------------------- | ------------------------------- | ---- | ------ |
|
|
116
|
+
| [`no-unsafe-inline-csp`](./docs/rules/no-unsafe-inline-csp.md) | Disallow 'unsafe-inline' in CSP | 7.5 | CWE-79 |
|
|
117
|
+
| [`no-unsafe-eval-csp`](./docs/rules/no-unsafe-eval-csp.md) | Disallow 'unsafe-eval' in CSP | 8.1 | CWE-95 |
|
|
118
|
+
|
|
119
|
+
## 🔍 Detection Examples
|
|
120
|
+
|
|
121
|
+
### ❌ Vulnerable Code
|
|
122
|
+
|
|
123
|
+
```javascript
|
|
124
|
+
// XSS via innerHTML
|
|
125
|
+
element.innerHTML = userInput;
|
|
126
|
+
|
|
127
|
+
// Code injection via eval
|
|
128
|
+
eval(dynamicCode);
|
|
129
|
+
|
|
130
|
+
// JWT in localStorage (XSS can steal it)
|
|
131
|
+
localStorage.setItem('token', jwt);
|
|
132
|
+
|
|
133
|
+
// postMessage without origin check
|
|
134
|
+
window.addEventListener('message', (event) => {
|
|
135
|
+
processData(event.data); // Anyone can send messages!
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### ✅ Secure Code
|
|
140
|
+
|
|
141
|
+
```javascript
|
|
142
|
+
// Safe text assignment
|
|
143
|
+
element.textContent = userInput;
|
|
144
|
+
|
|
145
|
+
// Or sanitize before HTML insertion
|
|
146
|
+
element.innerHTML = DOMPurify.sanitize(userInput);
|
|
147
|
+
|
|
148
|
+
// Use HttpOnly cookies for auth tokens (set by server)
|
|
149
|
+
// Server: Set-Cookie: token=xxx; HttpOnly; Secure; SameSite=Strict
|
|
150
|
+
|
|
151
|
+
// Origin validation
|
|
152
|
+
window.addEventListener('message', (event) => {
|
|
153
|
+
if (event.origin !== 'https://trusted-domain.com') return;
|
|
154
|
+
processData(event.data);
|
|
155
|
+
});
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## 🗂️ OWASP Coverage
|
|
159
|
+
|
|
160
|
+
| Rule | OWASP Top 10 2021 | Category |
|
|
161
|
+
| ---------------------------------- | --------------------------- | ----------------------- |
|
|
162
|
+
| `no-eval` | A03: Injection | Code Injection |
|
|
163
|
+
| `no-innerhtml` | A03: Injection | DOM XSS |
|
|
164
|
+
| `no-sensitive-localstorage` | A02: Cryptographic Failures | Sensitive Data Exposure |
|
|
165
|
+
| `require-postmessage-origin-check` | A01: Broken Access Control | Origin Validation |
|
|
166
|
+
|
|
167
|
+
## 🤖 LLM-Optimized Messages
|
|
168
|
+
|
|
169
|
+
All rules include structured remediation guidance designed for AI assistants:
|
|
170
|
+
|
|
171
|
+
```
|
|
172
|
+
[browser-security/no-innerhtml] XSS vulnerability: Direct HTML assignment detected.
|
|
173
|
+
|
|
174
|
+
📋 CONTEXT:
|
|
175
|
+
• Pattern: element.innerHTML = unsanitizedInput
|
|
176
|
+
• Risk: Any script in unsanitizedInput will execute
|
|
177
|
+
|
|
178
|
+
🛠️ REMEDIATION:
|
|
179
|
+
Option A (Preferred): Use textContent for plain text
|
|
180
|
+
element.textContent = userInput;
|
|
181
|
+
|
|
182
|
+
Option B: Sanitize before insertion
|
|
183
|
+
element.innerHTML = DOMPurify.sanitize(userInput);
|
|
184
|
+
|
|
185
|
+
📚 References:
|
|
186
|
+
• CWE-79: https://cwe.mitre.org/data/definitions/79.html
|
|
187
|
+
• OWASP XSS Prevention: https://owasp.org/...
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## 🔗 Related Packages
|
|
191
|
+
|
|
192
|
+
Part of the [Forge-JS ESLint Security Ecosystem](https://github.com/ofri-peretz/eslint):
|
|
193
|
+
|
|
194
|
+
| Package | Focus |
|
|
195
|
+
| --------------------------------------------------------------------- | --------------------------- |
|
|
196
|
+
| [`eslint-plugin-secure-coding`](../eslint-plugin-secure-coding) | Universal security patterns |
|
|
197
|
+
| [`eslint-plugin-crypto`](../eslint-plugin-crypto) | Cryptographic security |
|
|
198
|
+
| [`eslint-plugin-jwt`](../eslint-plugin-jwt) | JWT handling security |
|
|
199
|
+
| [`eslint-plugin-express-security`](../eslint-plugin-express-security) | Express.js security |
|
|
200
|
+
| [`eslint-plugin-nestjs-security`](../eslint-plugin-nestjs-security) | NestJS security |
|
|
201
|
+
| [`eslint-plugin-lambda-security`](../eslint-plugin-lambda-security) | AWS Lambda security |
|
|
202
|
+
|
|
203
|
+
## 📄 License
|
|
204
|
+
|
|
205
|
+
MIT © [Ofri Peretz](https://github.com/ofri-peretz)
|
package/package.json
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "eslint-plugin-browser-security",
|
|
3
|
+
"version": "0.0.1",
|
|
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
|
+
"type": "commonjs",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"types": "./src/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.d.ts",
|
|
11
|
+
"default": "./src/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./types": {
|
|
14
|
+
"types": "./src/types/index.d.ts",
|
|
15
|
+
"default": "./src/types/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"author": "Ofri Peretz <ofriperetzdev@gmail.com>",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"homepage": "https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/README.md",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/ofri-peretz/eslint.git",
|
|
24
|
+
"directory": "packages/eslint-plugin-browser-security"
|
|
25
|
+
},
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/ofri-peretz/eslint/issues"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"src/",
|
|
34
|
+
"dist/",
|
|
35
|
+
"README.md",
|
|
36
|
+
"LICENSE",
|
|
37
|
+
"CHANGELOG.md",
|
|
38
|
+
"AGENTS.md"
|
|
39
|
+
],
|
|
40
|
+
"keywords": [
|
|
41
|
+
"eslint",
|
|
42
|
+
"eslint-plugin",
|
|
43
|
+
"eslintplugin",
|
|
44
|
+
"browser",
|
|
45
|
+
"browser-security",
|
|
46
|
+
"web-security",
|
|
47
|
+
"security",
|
|
48
|
+
"xss",
|
|
49
|
+
"dom-xss",
|
|
50
|
+
"postmessage",
|
|
51
|
+
"localstorage",
|
|
52
|
+
"sessionstorage",
|
|
53
|
+
"indexeddb",
|
|
54
|
+
"cookies",
|
|
55
|
+
"websocket",
|
|
56
|
+
"service-worker",
|
|
57
|
+
"web-worker",
|
|
58
|
+
"file-api",
|
|
59
|
+
"blob-url",
|
|
60
|
+
"csp",
|
|
61
|
+
"content-security-policy",
|
|
62
|
+
"innerhtml",
|
|
63
|
+
"eval",
|
|
64
|
+
"owasp",
|
|
65
|
+
"cwe",
|
|
66
|
+
"llm-optimized",
|
|
67
|
+
"ai-assistant",
|
|
68
|
+
"typescript",
|
|
69
|
+
"static-analysis"
|
|
70
|
+
],
|
|
71
|
+
"engines": {
|
|
72
|
+
"node": ">=18.0.0"
|
|
73
|
+
},
|
|
74
|
+
"dependencies": {
|
|
75
|
+
"@interlace/eslint-devkit": "^1.2.1",
|
|
76
|
+
"vitest": "^4.0.6",
|
|
77
|
+
"@nx/vite": "^22.0.3",
|
|
78
|
+
"tslib": "^2.8.1"
|
|
79
|
+
},
|
|
80
|
+
"peerDependencies": {},
|
|
81
|
+
"scripts": {
|
|
82
|
+
"test": "vitest run",
|
|
83
|
+
"test:watch": "vitest watch",
|
|
84
|
+
"test:coverage": "vitest run --coverage"
|
|
85
|
+
},
|
|
86
|
+
"devDependencies": {
|
|
87
|
+
"@typescript-eslint/parser": "^8.46.2",
|
|
88
|
+
"@typescript-eslint/rule-tester": "^8.46.2",
|
|
89
|
+
"@vitest/coverage-v8": "^4.0.6",
|
|
90
|
+
"vitest": "^4.0.6"
|
|
91
|
+
}
|
|
92
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* eslint-plugin-browser-security
|
|
3
|
+
*
|
|
4
|
+
* Security-focused ESLint plugin for browser applications.
|
|
5
|
+
* Detects XSS vulnerabilities, postMessage abuse, localStorage/sessionStorage/IndexedDB
|
|
6
|
+
* token exposure, cookie security issues, WebSocket vulnerabilities, and more.
|
|
7
|
+
*
|
|
8
|
+
* Features:
|
|
9
|
+
* - LLM-optimized error messages with CWE references
|
|
10
|
+
* - XSS prevention rules (innerHTML, eval)
|
|
11
|
+
* - postMessage security
|
|
12
|
+
* - Storage security (localStorage, sessionStorage, IndexedDB)
|
|
13
|
+
* - Cookie security
|
|
14
|
+
* - WebSocket security
|
|
15
|
+
* - Browser API security
|
|
16
|
+
*
|
|
17
|
+
* @see https://github.com/ofri-peretz/eslint#readme
|
|
18
|
+
*/
|
|
19
|
+
import { TSESLint } from '@interlace/eslint-devkit';
|
|
20
|
+
/**
|
|
21
|
+
* Collection of all browser security ESLint rules
|
|
22
|
+
*/
|
|
23
|
+
export declare const rules: Record<string, TSESLint.RuleModule<string, readonly unknown[]>>;
|
|
24
|
+
/**
|
|
25
|
+
* ESLint Plugin object
|
|
26
|
+
*/
|
|
27
|
+
export declare const plugin: TSESLint.FlatConfig.Plugin;
|
|
28
|
+
/**
|
|
29
|
+
* Preset configurations for browser security rules
|
|
30
|
+
*/
|
|
31
|
+
export declare const configs: Record<string, TSESLint.FlatConfig.Config>;
|
|
32
|
+
/**
|
|
33
|
+
* Default export for ESLint plugin
|
|
34
|
+
*/
|
|
35
|
+
export default plugin;
|
|
36
|
+
/**
|
|
37
|
+
* Re-export types
|
|
38
|
+
*/
|
|
39
|
+
export type { Options as NoInnerhtmlOptions } from './rules/no-innerhtml';
|
|
40
|
+
export type { Options as NoEvalOptions } from './rules/no-eval';
|
|
41
|
+
export type { Options as RequirePostmessageOriginCheckOptions } from './rules/require-postmessage-origin-check';
|
|
42
|
+
export type { Options as NoPostmessageWildcardOriginOptions } from './rules/no-postmessage-wildcard-origin';
|
|
43
|
+
export type { Options as NoPostmessageInnerhtmlOptions } from './rules/no-postmessage-innerhtml';
|
|
44
|
+
export type { Options as NoSensitiveLocalstorageOptions } from './rules/no-sensitive-localstorage';
|
|
45
|
+
export type { Options as NoJwtInStorageOptions } from './rules/no-jwt-in-storage';
|
|
46
|
+
export type { Options as NoSensitiveSessionstorageOptions } from './rules/no-sensitive-sessionstorage';
|
|
47
|
+
export type { Options as NoSensitiveIndexeddbOptions } from './rules/no-sensitive-indexeddb';
|
|
48
|
+
export type { Options as NoSensitiveCookieJsOptions } from './rules/no-sensitive-cookie-js';
|
|
49
|
+
export type { Options as NoCookieAuthTokensOptions } from './rules/no-cookie-auth-tokens';
|
|
50
|
+
export type { Options as RequireCookieSecureAttrsOptions } from './rules/require-cookie-secure-attrs';
|
|
51
|
+
export type { Options as RequireWebsocketWssOptions } from './rules/require-websocket-wss';
|
|
52
|
+
export type { Options as NoWebsocketInnerhtmlOptions } from './rules/no-websocket-innerhtml';
|
|
53
|
+
export type { Options as NoWebsocketEvalOptions } from './rules/no-websocket-eval';
|
|
54
|
+
export type { Options as NoFilereaderInnerhtmlOptions } from './rules/no-filereader-innerhtml';
|
|
55
|
+
export type { Options as RequireBlobUrlRevocationOptions } from './rules/require-blob-url-revocation';
|
|
56
|
+
export type { Options as NoDynamicServiceWorkerUrlOptions } from './rules/no-dynamic-service-worker-url';
|
|
57
|
+
export type { Options as NoWorkerMessageInnerhtmlOptions } from './rules/no-worker-message-innerhtml';
|
|
58
|
+
export type { Options as NoUnsafeInlineCspOptions } from './rules/no-unsafe-inline-csp';
|
|
59
|
+
export type { Options as NoUnsafeEvalCspOptions } from './rules/no-unsafe-eval-csp';
|