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
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.noWebsocketEval = void 0;
|
|
4
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
+
// Dangerous eval-like functions
|
|
6
|
+
const EVAL_FUNCTIONS = ['eval', 'Function'];
|
|
7
|
+
exports.noWebsocketEval = (0, eslint_devkit_1.createRule)({
|
|
8
|
+
name: 'no-websocket-eval',
|
|
9
|
+
meta: {
|
|
10
|
+
type: 'problem',
|
|
11
|
+
docs: {
|
|
12
|
+
description: 'Disallow using eval() or Function() with WebSocket message data',
|
|
13
|
+
},
|
|
14
|
+
hasSuggestions: true,
|
|
15
|
+
messages: {
|
|
16
|
+
evalWithWsData: (0, eslint_devkit_1.formatLLMMessage)({
|
|
17
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
18
|
+
issueName: 'Remote Code Execution via WebSocket',
|
|
19
|
+
cwe: 'CWE-95',
|
|
20
|
+
owasp: 'A03:2021',
|
|
21
|
+
cvss: 9.8,
|
|
22
|
+
description: 'Using {{method}} with WebSocket data enables remote code execution. A compromised server or MITM attacker can execute arbitrary JavaScript.',
|
|
23
|
+
severity: 'CRITICAL',
|
|
24
|
+
fix: 'Parse WebSocket data as JSON and validate the structure instead of executing it.',
|
|
25
|
+
documentationLink: 'https://cwe.mitre.org/data/definitions/95.html',
|
|
26
|
+
}),
|
|
27
|
+
parseDataSafely: (0, eslint_devkit_1.formatLLMMessage)({
|
|
28
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
29
|
+
issueName: 'Parse Data Safely',
|
|
30
|
+
description: 'Use JSON.parse() and validate the data structure',
|
|
31
|
+
severity: 'LOW',
|
|
32
|
+
fix: 'const data = JSON.parse(event.data); if (data.action === "update") { handleUpdate(data); }',
|
|
33
|
+
documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse',
|
|
34
|
+
}),
|
|
35
|
+
},
|
|
36
|
+
schema: [
|
|
37
|
+
{
|
|
38
|
+
type: 'object',
|
|
39
|
+
properties: {
|
|
40
|
+
allowInTests: {
|
|
41
|
+
type: 'boolean',
|
|
42
|
+
default: true,
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
additionalProperties: false,
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
},
|
|
49
|
+
defaultOptions: [{ allowInTests: true }],
|
|
50
|
+
create(context, [options = {}]) {
|
|
51
|
+
const { allowInTests = true } = options;
|
|
52
|
+
const filename = context.filename || context.getFilename();
|
|
53
|
+
const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
54
|
+
if (allowInTests && isTestFile) {
|
|
55
|
+
return {};
|
|
56
|
+
}
|
|
57
|
+
// Track WebSocket handlers
|
|
58
|
+
let inWsHandler = false;
|
|
59
|
+
let eventParamName = null;
|
|
60
|
+
/**
|
|
61
|
+
* Check if we're in a WebSocket onmessage assignment
|
|
62
|
+
*/
|
|
63
|
+
function isWsOnmessageAssignment(node) {
|
|
64
|
+
if (node.left.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
65
|
+
node.left.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
66
|
+
node.left.property.name === 'onmessage') {
|
|
67
|
+
const handler = node.right;
|
|
68
|
+
if (handler.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression ||
|
|
69
|
+
handler.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression) {
|
|
70
|
+
const firstParam = handler.params[0];
|
|
71
|
+
if (firstParam && firstParam.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
72
|
+
return { isHandler: true, eventParam: firstParam.name };
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return { isHandler: false, eventParam: null };
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Check if we're in a WebSocket addEventListener('message')
|
|
80
|
+
*/
|
|
81
|
+
function isWsAddEventListener(node) {
|
|
82
|
+
if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
83
|
+
node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
84
|
+
node.callee.property.name === 'addEventListener' &&
|
|
85
|
+
node.arguments.length >= 2) {
|
|
86
|
+
const eventType = node.arguments[0];
|
|
87
|
+
if (eventType.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
88
|
+
eventType.value === 'message') {
|
|
89
|
+
const callback = node.arguments[1];
|
|
90
|
+
if (callback.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression ||
|
|
91
|
+
callback.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression) {
|
|
92
|
+
const firstParam = callback.params[0];
|
|
93
|
+
if (firstParam && firstParam.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
94
|
+
return { isHandler: true, eventParam: firstParam.name };
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return { isHandler: false, eventParam: null };
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Check if expression references event.data
|
|
103
|
+
*/
|
|
104
|
+
function referencesEventData(node, eventName) {
|
|
105
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
|
|
106
|
+
if (node.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
107
|
+
node.object.name === eventName &&
|
|
108
|
+
node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
109
|
+
node.property.name === 'data') {
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
/* c8 ignore next - Recursive call for nested event.data.property */
|
|
113
|
+
return referencesEventData(node.object, eventName);
|
|
114
|
+
}
|
|
115
|
+
/* c8 ignore next 3 - Direct event identifier reference edge case */
|
|
116
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && node.name === eventName) {
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Check if this is an eval-like call
|
|
123
|
+
*/
|
|
124
|
+
function isEvalCall(node) {
|
|
125
|
+
// eval(...)
|
|
126
|
+
if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
127
|
+
EVAL_FUNCTIONS.includes(node.callee.name)) {
|
|
128
|
+
return node.callee.name;
|
|
129
|
+
}
|
|
130
|
+
/* c8 ignore start - Duplicate Function check for call expression pattern */
|
|
131
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.CallExpression &&
|
|
132
|
+
node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
133
|
+
node.callee.name === 'Function') {
|
|
134
|
+
return 'Function';
|
|
135
|
+
}
|
|
136
|
+
/* c8 ignore stop */
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
AssignmentExpression(node) {
|
|
141
|
+
const { isHandler, eventParam } = isWsOnmessageAssignment(node);
|
|
142
|
+
if (isHandler) {
|
|
143
|
+
inWsHandler = true;
|
|
144
|
+
eventParamName = eventParam;
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
'AssignmentExpression:exit'(node) {
|
|
148
|
+
const { isHandler } = isWsOnmessageAssignment(node);
|
|
149
|
+
if (isHandler) {
|
|
150
|
+
inWsHandler = false;
|
|
151
|
+
eventParamName = null;
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
CallExpression(node) {
|
|
155
|
+
// Check if entering addEventListener handler
|
|
156
|
+
const { isHandler, eventParam } = isWsAddEventListener(node);
|
|
157
|
+
if (isHandler) {
|
|
158
|
+
inWsHandler = true;
|
|
159
|
+
eventParamName = eventParam;
|
|
160
|
+
}
|
|
161
|
+
// Check for eval-like calls within handler
|
|
162
|
+
if (!inWsHandler || !eventParamName)
|
|
163
|
+
return;
|
|
164
|
+
const evalFn = isEvalCall(node);
|
|
165
|
+
if (evalFn) {
|
|
166
|
+
// Check if any argument references event.data
|
|
167
|
+
for (const arg of node.arguments) {
|
|
168
|
+
if (referencesEventData(arg, eventParamName)) {
|
|
169
|
+
context.report({
|
|
170
|
+
node,
|
|
171
|
+
messageId: 'evalWithWsData',
|
|
172
|
+
data: {
|
|
173
|
+
method: evalFn,
|
|
174
|
+
},
|
|
175
|
+
suggest: [
|
|
176
|
+
{
|
|
177
|
+
messageId: 'parseDataSafely',
|
|
178
|
+
fix: () => null,
|
|
179
|
+
},
|
|
180
|
+
],
|
|
181
|
+
});
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
'CallExpression:exit'(node) {
|
|
188
|
+
const { isHandler } = isWsAddEventListener(node);
|
|
189
|
+
if (isHandler) {
|
|
190
|
+
inWsHandler = false;
|
|
191
|
+
eventParamName = null;
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
// Also check new Function() constructor
|
|
195
|
+
NewExpression(node) {
|
|
196
|
+
if (!inWsHandler || !eventParamName)
|
|
197
|
+
return;
|
|
198
|
+
if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
199
|
+
node.callee.name === 'Function') {
|
|
200
|
+
for (const arg of node.arguments) {
|
|
201
|
+
if (referencesEventData(arg, eventParamName)) {
|
|
202
|
+
context.report({
|
|
203
|
+
node,
|
|
204
|
+
messageId: 'evalWithWsData',
|
|
205
|
+
data: {
|
|
206
|
+
method: 'new Function',
|
|
207
|
+
},
|
|
208
|
+
suggest: [
|
|
209
|
+
{
|
|
210
|
+
messageId: 'parseDataSafely',
|
|
211
|
+
fix: () => null,
|
|
212
|
+
},
|
|
213
|
+
],
|
|
214
|
+
});
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/eslint-plugin-browser-security/src/rules/no-websocket-eval/index.ts"],"names":[],"mappings":";;;AASA,4DAKkC;AAWlC,gCAAgC;AAChC,MAAM,cAAc,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;AAE/B,QAAA,eAAe,GAAG,IAAA,0BAAU,EAA0B;IACjE,IAAI,EAAE,mBAAmB;IACzB,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,IAAI,EAAE;YACJ,WAAW,EACT,iEAAiE;SACpE;QACD,cAAc,EAAE,IAAI;QACpB,QAAQ,EAAE;YACR,cAAc,EAAE,IAAA,gCAAgB,EAAC;gBAC/B,IAAI,EAAE,4BAAY,CAAC,QAAQ;gBAC3B,SAAS,EAAE,qCAAqC;gBAChD,GAAG,EAAE,QAAQ;gBACb,KAAK,EAAE,UAAU;gBACjB,IAAI,EAAE,GAAG;gBACT,WAAW,EACT,6IAA6I;gBAC/I,QAAQ,EAAE,UAAU;gBACpB,GAAG,EAAE,kFAAkF;gBACvF,iBAAiB,EACf,gDAAgD;aACnD,CAAC;YACF,eAAe,EAAE,IAAA,gCAAgB,EAAC;gBAChC,IAAI,EAAE,4BAAY,CAAC,IAAI;gBACvB,SAAS,EAAE,mBAAmB;gBAC9B,WAAW,EAAE,kDAAkD;gBAC/D,QAAQ,EAAE,KAAK;gBACf,GAAG,EAAE,4FAA4F;gBACjG,iBAAiB,EACf,6FAA6F;aAChG,CAAC;SACH;QACD,MAAM,EAAE;YACN;gBACE,IAAI,EAAE,QAAQ;gBACd,UAAU,EAAE;oBACV,YAAY,EAAE;wBACZ,IAAI,EAAE,SAAS;wB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AC;gBACH,CAAC;YACH,CAAC;YAED,qBAAqB,CAAC,IAA6B;gBACjD,MAAM,EAAE,SAAS,EAAE,GAAG,oBAAoB,CAAC,IAAI,CAAC,CAAC;gBACjD,IAAI,SAAS,EAAE,CAAC;oBACd,WAAW,GAAG,KAAK,CAAC;oBACpB,cAAc,GAAG,IAAI,CAAC;gBACxB,CAAC;YACH,CAAC;YAED,wCAAwC;YACxC,aAAa,CAAC,IAA4B;gBACxC,IAAI,CAAC,WAAW,IAAI,CAAC,cAAc;oBAAE,OAAO;gBAE5C,IACE,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,8BAAc,CAAC,UAAU;oBAC9C,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,UAAU,EAC/B,CAAC;oBACD,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;wBACjC,IAAI,mBAAmB,CAAC,GAAG,EAAE,cAAc,CAAC,EAAE,CAAC;4BAC7C,OAAO,CAAC,MAAM,CAAC;gCACb,IAAI;gCACJ,SAAS,EAAE,gBAAgB;gCAC3B,IAAI,EAAE;oCACJ,MAAM,EAAE,cAAc;iCACvB;gCACD,OAAO,EAAE;oCACP;wCACE,SAAS,EAAE,iBAAiB;wCAC5B,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI;qCAChB;iCACF;6BACF,CAAC,CAAC;4BACH,MAAM;wBACR,CAAC;oBACH,CAAC;gBACH,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.noWebsocketInnerhtml = void 0;
|
|
4
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
+
// Dangerous DOM properties and methods
|
|
6
|
+
const DANGEROUS_PROPERTIES = ['innerHTML', 'outerHTML'];
|
|
7
|
+
const DANGEROUS_METHODS = ['insertAdjacentHTML', 'write', 'writeln'];
|
|
8
|
+
exports.noWebsocketInnerhtml = (0, eslint_devkit_1.createRule)({
|
|
9
|
+
name: 'no-websocket-innerhtml',
|
|
10
|
+
meta: {
|
|
11
|
+
type: 'problem',
|
|
12
|
+
docs: {
|
|
13
|
+
description: 'Disallow using innerHTML or similar methods with WebSocket message data',
|
|
14
|
+
},
|
|
15
|
+
hasSuggestions: true,
|
|
16
|
+
messages: {
|
|
17
|
+
unsafeInnerhtml: (0, eslint_devkit_1.formatLLMMessage)({
|
|
18
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
19
|
+
issueName: 'XSS via WebSocket',
|
|
20
|
+
cwe: 'CWE-79',
|
|
21
|
+
owasp: 'A03:2021',
|
|
22
|
+
cvss: 8.1,
|
|
23
|
+
description: 'Using {{method}} with WebSocket data enables XSS attacks. Malicious server or MITM can send crafted HTML to execute scripts.',
|
|
24
|
+
severity: 'HIGH',
|
|
25
|
+
fix: 'Use textContent for plain text, or sanitize with DOMPurify before using innerHTML.',
|
|
26
|
+
documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/API/WebSocket',
|
|
27
|
+
}),
|
|
28
|
+
useTextContent: (0, eslint_devkit_1.formatLLMMessage)({
|
|
29
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
30
|
+
issueName: 'Use Safe DOM Methods',
|
|
31
|
+
description: 'Use textContent or sanitize input with DOMPurify',
|
|
32
|
+
severity: 'LOW',
|
|
33
|
+
fix: 'element.textContent = event.data; // For plain text\nelement.innerHTML = DOMPurify.sanitize(event.data); // For HTML',
|
|
34
|
+
documentationLink: 'https://github.com/cure53/DOMPurify',
|
|
35
|
+
}),
|
|
36
|
+
},
|
|
37
|
+
schema: [
|
|
38
|
+
{
|
|
39
|
+
type: 'object',
|
|
40
|
+
properties: {
|
|
41
|
+
allowInTests: {
|
|
42
|
+
type: 'boolean',
|
|
43
|
+
default: true,
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
additionalProperties: false,
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
defaultOptions: [{ allowInTests: true }],
|
|
51
|
+
create(context, [options = {}]) {
|
|
52
|
+
const { allowInTests = true } = options;
|
|
53
|
+
const filename = context.filename || context.getFilename();
|
|
54
|
+
const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
55
|
+
if (allowInTests && isTestFile) {
|
|
56
|
+
return {};
|
|
57
|
+
}
|
|
58
|
+
// Track WebSocket onmessage handlers
|
|
59
|
+
let inWsHandler = false;
|
|
60
|
+
let eventParamName = null;
|
|
61
|
+
/**
|
|
62
|
+
* Check if we're in a WebSocket onmessage assignment
|
|
63
|
+
*/
|
|
64
|
+
function isWsOnmessageAssignment(node) {
|
|
65
|
+
if (node.left.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
66
|
+
node.left.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
67
|
+
node.left.property.name === 'onmessage') {
|
|
68
|
+
const handler = node.right;
|
|
69
|
+
if (handler.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression ||
|
|
70
|
+
handler.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression) {
|
|
71
|
+
const firstParam = handler.params[0];
|
|
72
|
+
if (firstParam && firstParam.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
73
|
+
return { isHandler: true, eventParam: firstParam.name };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return { isHandler: false, eventParam: null };
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Check if we're in a WebSocket addEventListener('message')
|
|
81
|
+
*/
|
|
82
|
+
function isWsAddEventListener(node) {
|
|
83
|
+
if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
84
|
+
node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
85
|
+
node.callee.property.name === 'addEventListener' &&
|
|
86
|
+
node.arguments.length >= 2) {
|
|
87
|
+
const eventType = node.arguments[0];
|
|
88
|
+
if (eventType.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
89
|
+
eventType.value === 'message') {
|
|
90
|
+
const callback = node.arguments[1];
|
|
91
|
+
if (callback.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression ||
|
|
92
|
+
callback.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression) {
|
|
93
|
+
const firstParam = callback.params[0];
|
|
94
|
+
if (firstParam && firstParam.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
95
|
+
return { isHandler: true, eventParam: firstParam.name };
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return { isHandler: false, eventParam: null };
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Check if expression references event.data
|
|
104
|
+
*/
|
|
105
|
+
function referencesEventData(node, eventName) {
|
|
106
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
|
|
107
|
+
if (node.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
108
|
+
node.object.name === eventName &&
|
|
109
|
+
node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
110
|
+
node.property.name === 'data') {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
return referencesEventData(node.object, eventName);
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
AssignmentExpression(node) {
|
|
119
|
+
// Check if this is ws.onmessage = handler
|
|
120
|
+
const { isHandler, eventParam } = isWsOnmessageAssignment(node);
|
|
121
|
+
if (isHandler) {
|
|
122
|
+
inWsHandler = true;
|
|
123
|
+
eventParamName = eventParam;
|
|
124
|
+
}
|
|
125
|
+
// Check for innerHTML/outerHTML assignment within handler
|
|
126
|
+
if (!inWsHandler || !eventParamName)
|
|
127
|
+
return;
|
|
128
|
+
if (node.left.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
129
|
+
node.left.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
130
|
+
DANGEROUS_PROPERTIES.includes(node.left.property.name)) {
|
|
131
|
+
if (referencesEventData(node.right, eventParamName)) {
|
|
132
|
+
context.report({
|
|
133
|
+
node,
|
|
134
|
+
messageId: 'unsafeInnerhtml',
|
|
135
|
+
data: {
|
|
136
|
+
method: node.left.property.name,
|
|
137
|
+
},
|
|
138
|
+
suggest: [
|
|
139
|
+
{
|
|
140
|
+
messageId: 'useTextContent',
|
|
141
|
+
fix: () => null,
|
|
142
|
+
},
|
|
143
|
+
],
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
'AssignmentExpression:exit'(node) {
|
|
149
|
+
const { isHandler } = isWsOnmessageAssignment(node);
|
|
150
|
+
if (isHandler) {
|
|
151
|
+
inWsHandler = false;
|
|
152
|
+
eventParamName = null;
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
CallExpression(node) {
|
|
156
|
+
// Check if this is ws.addEventListener('message', handler)
|
|
157
|
+
const { isHandler, eventParam } = isWsAddEventListener(node);
|
|
158
|
+
if (isHandler) {
|
|
159
|
+
inWsHandler = true;
|
|
160
|
+
eventParamName = eventParam;
|
|
161
|
+
}
|
|
162
|
+
// Check for dangerous method calls within handler
|
|
163
|
+
if (!inWsHandler || !eventParamName)
|
|
164
|
+
return;
|
|
165
|
+
if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
166
|
+
node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
167
|
+
DANGEROUS_METHODS.includes(node.callee.property.name)) {
|
|
168
|
+
for (const arg of node.arguments) {
|
|
169
|
+
if (referencesEventData(arg, eventParamName)) {
|
|
170
|
+
context.report({
|
|
171
|
+
node,
|
|
172
|
+
messageId: 'unsafeInnerhtml',
|
|
173
|
+
data: {
|
|
174
|
+
method: node.callee.property.name,
|
|
175
|
+
},
|
|
176
|
+
suggest: [
|
|
177
|
+
{
|
|
178
|
+
messageId: 'useTextContent',
|
|
179
|
+
fix: () => null,
|
|
180
|
+
},
|
|
181
|
+
],
|
|
182
|
+
});
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
'CallExpression:exit'(node) {
|
|
189
|
+
const { isHandler } = isWsAddEventListener(node);
|
|
190
|
+
if (isHandler) {
|
|
191
|
+
inWsHandler = false;
|
|
192
|
+
eventParamName = null;
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/eslint-plugin-browser-security/src/rules/no-websocket-innerhtml/index.ts"],"names":[],"mappings":";;;AASA,4DAKkC;AAWlC,uCAAuC;AACvC,MAAM,oBAAoB,GAAG,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;AACxD,MAAM,iBAAiB,GAAG,CAAC,oBAAoB,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;AAExD,QAAA,oBAAoB,GAAG,IAAA,0BAAU,EAA0B;IACtE,IAAI,EAAE,wBAAwB;IAC9B,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,IAAI,EAAE;YACJ,WAAW,EACT,yEAAyE;SAC5E;QACD,cAAc,EAAE,IAAI;QACpB,QAAQ,EAAE;YACR,eAAe,EAAE,IAAA,gCAAgB,EAAC;gBAChC,IAAI,EAAE,4BAAY,CAAC,QAAQ;gBAC3B,SAAS,EAAE,mBAAmB;gBAC9B,GAAG,EAAE,QAAQ;gBACb,KAAK,EAAE,UAAU;gBACjB,IAAI,EAAE,GAAG;g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KAAK,8BAAc,CAAC,gBAAgB;oBACpD,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,KAAK,8BAAc,CAAC,UAAU;oBACvD,iBAAiB,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,EACrD,CAAC;oBACD,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;wBACjC,IAAI,mBAAmB,CAAC,GAAG,EAAE,cAAc,CAAC,EAAE,CAAC;4BAC7C,OAAO,CAAC,MAAM,CAAC;gCACb,IAAI;gCACJ,SAAS,EAAE,iBAAiB;gCAC5B,IAAI,EAAE;oCACJ,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI;iCAClC;gCACD,OAAO,EAAE;oCACP;wCACE,SAAS,EAAE,gBAAgB;wCAC3B,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI;qCAChB;iCACF;6BACF,CAAC,CAAC;4BACH,MAAM;wBACR,CAAC;oBACH,CAAC;gBACH,CAAC;YACH,CAAC;YAED,qBAAqB,CAAC,IAA6B;gBACjD,MAAM,EAAE,SAAS,EAAE,GAAG,oBAAoB,CAAC,IAAI,CAAC,CAAC;gBACjD,IAAI,SAAS,EAAE,CAAC;oBACd,WAAW,GAAG,KAAK,CAAC;oBACpB,cAAc,GAAG,IAAI,CAAC;gBACxB,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.noWorkerMessageInnerhtml = void 0;
|
|
4
|
+
const eslint_devkit_1 = require("@interlace/eslint-devkit");
|
|
5
|
+
// Dangerous DOM properties and methods
|
|
6
|
+
const DANGEROUS_PROPERTIES = ['innerHTML', 'outerHTML'];
|
|
7
|
+
const DANGEROUS_METHODS = ['insertAdjacentHTML', 'write', 'writeln'];
|
|
8
|
+
exports.noWorkerMessageInnerhtml = (0, eslint_devkit_1.createRule)({
|
|
9
|
+
name: 'no-worker-message-innerhtml',
|
|
10
|
+
meta: {
|
|
11
|
+
type: 'problem',
|
|
12
|
+
docs: {
|
|
13
|
+
description: 'Disallow using innerHTML with Web Worker message data',
|
|
14
|
+
},
|
|
15
|
+
hasSuggestions: true,
|
|
16
|
+
messages: {
|
|
17
|
+
workerInnerhtml: (0, eslint_devkit_1.formatLLMMessage)({
|
|
18
|
+
icon: eslint_devkit_1.MessageIcons.SECURITY,
|
|
19
|
+
issueName: 'XSS via Worker Message',
|
|
20
|
+
cwe: 'CWE-79',
|
|
21
|
+
owasp: 'A03:2021',
|
|
22
|
+
cvss: 7.5,
|
|
23
|
+
description: 'Using {{method}} with Worker message data can enable XSS if the worker processes untrusted input.',
|
|
24
|
+
severity: 'HIGH',
|
|
25
|
+
fix: 'Sanitize worker data with DOMPurify before rendering, or use textContent.',
|
|
26
|
+
documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/API/Worker/message_event',
|
|
27
|
+
}),
|
|
28
|
+
sanitizeWorkerData: (0, eslint_devkit_1.formatLLMMessage)({
|
|
29
|
+
icon: eslint_devkit_1.MessageIcons.INFO,
|
|
30
|
+
issueName: 'Sanitize Worker Data',
|
|
31
|
+
description: 'Use textContent or sanitize with DOMPurify',
|
|
32
|
+
severity: 'LOW',
|
|
33
|
+
fix: 'const sanitized = DOMPurify.sanitize(event.data);\nelement.innerHTML = sanitized;',
|
|
34
|
+
documentationLink: 'https://github.com/cure53/DOMPurify',
|
|
35
|
+
}),
|
|
36
|
+
},
|
|
37
|
+
schema: [
|
|
38
|
+
{
|
|
39
|
+
type: 'object',
|
|
40
|
+
properties: { allowInTests: { type: 'boolean', default: true } },
|
|
41
|
+
additionalProperties: false,
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
},
|
|
45
|
+
defaultOptions: [{ allowInTests: true }],
|
|
46
|
+
create(context, [options = {}]) {
|
|
47
|
+
const { allowInTests = true } = options;
|
|
48
|
+
const filename = context.filename || context.getFilename();
|
|
49
|
+
const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
|
|
50
|
+
if (allowInTests && isTestFile) {
|
|
51
|
+
return {};
|
|
52
|
+
}
|
|
53
|
+
// Track Worker message handlers
|
|
54
|
+
let inWorkerHandler = false;
|
|
55
|
+
let eventParamName = null;
|
|
56
|
+
/**
|
|
57
|
+
* Check if we're in a Worker onmessage assignment
|
|
58
|
+
*/
|
|
59
|
+
function isWorkerOnmessageAssignment(node) {
|
|
60
|
+
if (node.left.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
61
|
+
node.left.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
62
|
+
node.left.property.name === 'onmessage') {
|
|
63
|
+
// Check if object looks like a worker
|
|
64
|
+
const objName = node.left.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier
|
|
65
|
+
? node.left.object.name.toLowerCase()
|
|
66
|
+
: '';
|
|
67
|
+
if (objName.includes('worker') ||
|
|
68
|
+
objName === 'w' ||
|
|
69
|
+
objName === 'wk') {
|
|
70
|
+
const handler = node.right;
|
|
71
|
+
if (handler.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression ||
|
|
72
|
+
handler.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression) {
|
|
73
|
+
const firstParam = handler.params[0];
|
|
74
|
+
if (firstParam && firstParam.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
75
|
+
return { isHandler: true, eventParam: firstParam.name };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return { isHandler: false, eventParam: null };
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Check if we're in a Worker addEventListener('message')
|
|
84
|
+
*/
|
|
85
|
+
function isWorkerAddEventListener(node) {
|
|
86
|
+
if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
87
|
+
node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
88
|
+
node.callee.property.name === 'addEventListener' &&
|
|
89
|
+
node.arguments.length >= 2) {
|
|
90
|
+
// Check if it's a message event
|
|
91
|
+
const eventType = node.arguments[0];
|
|
92
|
+
if (eventType.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
|
|
93
|
+
eventType.value === 'message') {
|
|
94
|
+
// Check if object looks like a worker
|
|
95
|
+
const obj = node.callee.object;
|
|
96
|
+
if (obj.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
97
|
+
const objName = obj.name.toLowerCase();
|
|
98
|
+
if (objName.includes('worker') ||
|
|
99
|
+
objName === 'w' ||
|
|
100
|
+
objName === 'wk') {
|
|
101
|
+
const callback = node.arguments[1];
|
|
102
|
+
if (callback.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression ||
|
|
103
|
+
callback.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression) {
|
|
104
|
+
const firstParam = callback.params[0];
|
|
105
|
+
if (firstParam && firstParam.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
|
|
106
|
+
return { isHandler: true, eventParam: firstParam.name };
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return { isHandler: false, eventParam: null };
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Check if expression references event.data
|
|
117
|
+
*/
|
|
118
|
+
function referencesEventData(node, eventName) {
|
|
119
|
+
if (node.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
|
|
120
|
+
if (node.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
121
|
+
node.object.name === eventName &&
|
|
122
|
+
node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
123
|
+
node.property.name === 'data') {
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
/* c8 ignore next - Recursive call for deeply nested expressions */
|
|
127
|
+
return referencesEventData(node.object, eventName);
|
|
128
|
+
}
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
AssignmentExpression(node) {
|
|
133
|
+
// Check if entering Worker handler
|
|
134
|
+
const { isHandler, eventParam } = isWorkerOnmessageAssignment(node);
|
|
135
|
+
if (isHandler) {
|
|
136
|
+
inWorkerHandler = true;
|
|
137
|
+
eventParamName = eventParam;
|
|
138
|
+
}
|
|
139
|
+
// Check for innerHTML/outerHTML assignment within handler
|
|
140
|
+
if (!inWorkerHandler || !eventParamName)
|
|
141
|
+
return;
|
|
142
|
+
if (node.left.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
143
|
+
node.left.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
144
|
+
DANGEROUS_PROPERTIES.includes(node.left.property.name)) {
|
|
145
|
+
if (referencesEventData(node.right, eventParamName)) {
|
|
146
|
+
context.report({
|
|
147
|
+
node,
|
|
148
|
+
messageId: 'workerInnerhtml',
|
|
149
|
+
data: { method: node.left.property.name },
|
|
150
|
+
suggest: [{ messageId: 'sanitizeWorkerData', fix: () => null }],
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
'AssignmentExpression:exit'(node) {
|
|
156
|
+
const { isHandler } = isWorkerOnmessageAssignment(node);
|
|
157
|
+
if (isHandler) {
|
|
158
|
+
inWorkerHandler = false;
|
|
159
|
+
eventParamName = null;
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
CallExpression(node) {
|
|
163
|
+
// Check if entering addEventListener handler
|
|
164
|
+
const { isHandler, eventParam } = isWorkerAddEventListener(node);
|
|
165
|
+
if (isHandler) {
|
|
166
|
+
inWorkerHandler = true;
|
|
167
|
+
eventParamName = eventParam;
|
|
168
|
+
}
|
|
169
|
+
// Check for dangerous method calls within handler
|
|
170
|
+
if (!inWorkerHandler || !eventParamName)
|
|
171
|
+
return;
|
|
172
|
+
if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
|
|
173
|
+
node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
|
|
174
|
+
DANGEROUS_METHODS.includes(node.callee.property.name)) {
|
|
175
|
+
for (const arg of node.arguments) {
|
|
176
|
+
if (referencesEventData(arg, eventParamName)) {
|
|
177
|
+
context.report({
|
|
178
|
+
node,
|
|
179
|
+
messageId: 'workerInnerhtml',
|
|
180
|
+
data: { method: node.callee.property.name },
|
|
181
|
+
suggest: [{ messageId: 'sanitizeWorkerData', fix: () => null }],
|
|
182
|
+
});
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
'CallExpression:exit'(node) {
|
|
189
|
+
const { isHandler } = isWorkerAddEventListener(node);
|
|
190
|
+
if (isHandler) {
|
|
191
|
+
inWorkerHandler = false;
|
|
192
|
+
eventParamName = null;
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
//# sourceMappingURL=index.js.map
|