eslint-plugin-browser-security 1.2.13 → 1.2.14

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.
Files changed (48) hide show
  1. package/package.json +2 -2
  2. package/src/index.js +1 -159
  3. package/src/oxlint.js +1 -3
  4. package/src/rules/detect-mixed-content/index.js +1 -38
  5. package/src/rules/no-allow-arbitrary-loads/index.js +1 -41
  6. package/src/rules/no-clickjacking/index.js +1 -361
  7. package/src/rules/no-client-side-auth-logic/index.js +1 -67
  8. package/src/rules/no-cookie-auth-tokens/index.js +1 -109
  9. package/src/rules/no-credentials-in-query-params/index.js +1 -55
  10. package/src/rules/no-disabled-certificate-validation/index.js +1 -59
  11. package/src/rules/no-dynamic-service-worker-url/index.js +1 -78
  12. package/src/rules/no-eval/index.js +1 -160
  13. package/src/rules/no-filereader-innerhtml/index.js +1 -196
  14. package/src/rules/no-http-urls/index.js +1 -109
  15. package/src/rules/no-innerhtml/index.js +1 -150
  16. package/src/rules/no-insecure-redirects/index.js +1 -238
  17. package/src/rules/no-insecure-websocket/index.js +1 -57
  18. package/src/rules/no-jwt-in-storage/index.js +1 -178
  19. package/src/rules/no-missing-cors-check/index.js +1 -351
  20. package/src/rules/no-missing-csrf-protection/index.js +1 -165
  21. package/src/rules/no-missing-security-headers/index.js +1 -197
  22. package/src/rules/no-password-in-url/index.js +1 -47
  23. package/src/rules/no-permissive-cors/index.js +1 -65
  24. package/src/rules/no-postmessage-innerhtml/index.js +1 -161
  25. package/src/rules/no-postmessage-wildcard-origin/index.js +1 -112
  26. package/src/rules/no-sensitive-cookie-js/index.js +1 -151
  27. package/src/rules/no-sensitive-data-in-analytics/index.js +1 -58
  28. package/src/rules/no-sensitive-data-in-cache/index.js +1 -47
  29. package/src/rules/no-sensitive-indexeddb/index.js +1 -110
  30. package/src/rules/no-sensitive-localstorage/index.js +1 -173
  31. package/src/rules/no-sensitive-sessionstorage/index.js +1 -144
  32. package/src/rules/no-tracking-without-consent/index.js +1 -65
  33. package/src/rules/no-unencrypted-transmission/index.js +1 -208
  34. package/src/rules/no-unescaped-url-parameter/index.js +1 -319
  35. package/src/rules/no-unsafe-eval-csp/index.js +1 -75
  36. package/src/rules/no-unsafe-inline-csp/index.js +1 -75
  37. package/src/rules/no-unvalidated-deeplinks/index.js +1 -59
  38. package/src/rules/no-websocket-eval/index.js +1 -197
  39. package/src/rules/no-websocket-innerhtml/index.js +1 -185
  40. package/src/rules/no-worker-message-innerhtml/index.js +1 -181
  41. package/src/rules/require-blob-url-revocation/index.js +1 -93
  42. package/src/rules/require-cookie-secure-attrs/index.js +1 -101
  43. package/src/rules/require-csp-headers/index.js +1 -59
  44. package/src/rules/require-https-only/index.js +1 -57
  45. package/src/rules/require-mime-type-validation/index.js +1 -61
  46. package/src/rules/require-postmessage-origin-check/index.js +1 -132
  47. package/src/rules/require-url-validation/index.js +1 -68
  48. package/src/rules/require-websocket-wss/index.js +1 -125
@@ -1,197 +1 @@
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
- const EVAL_FUNCTIONS = new Set(['eval', 'Function']);
6
- exports.noWebsocketEval = (0, eslint_devkit_1.createRule)({
7
- name: 'no-websocket-eval',
8
- meta: {
9
- type: 'problem',
10
- docs: {
11
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-websocket-eval.md',
12
- description: 'Disallow using eval() or Function() with WebSocket message data',
13
- cwe: 'CWE-95',
14
- cvss: 9.8,
15
- },
16
- hasSuggestions: true,
17
- messages: {
18
- evalWithWsData: (0, eslint_devkit_1.formatLLMMessage)({
19
- icon: eslint_devkit_1.MessageIcons.SECURITY,
20
- issueName: 'Remote Code Execution via WebSocket',
21
- cwe: 'CWE-95',
22
- owasp: 'A03:2021',
23
- cvss: 9.8,
24
- description: 'Using {{method}} with WebSocket data enables remote code execution. A compromised server or MITM attacker can execute arbitrary JavaScript.',
25
- severity: 'CRITICAL',
26
- fix: 'Parse WebSocket data as JSON and validate the structure instead of executing it.',
27
- documentationLink: 'https://cwe.mitre.org/data/definitions/95.html',
28
- }),
29
- parseDataSafely: (0, eslint_devkit_1.formatLLMMessage)({
30
- icon: eslint_devkit_1.MessageIcons.INFO,
31
- issueName: 'Parse Data Safely',
32
- description: 'Use JSON.parse() and validate the data structure',
33
- severity: 'LOW',
34
- fix: 'const data = JSON.parse(event.data); if (data.action === "update") { handleUpdate(data); }',
35
- documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse',
36
- }),
37
- },
38
- schema: [
39
- {
40
- type: 'object',
41
- properties: {
42
- allowInTests: {
43
- type: 'boolean',
44
- default: true,
45
- },
46
- },
47
- additionalProperties: false,
48
- },
49
- ],
50
- },
51
- defaultOptions: [{ allowInTests: true }],
52
- create(context, [options = {}]) {
53
- const { allowInTests = true } = options;
54
- const filename = context.filename;
55
- const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
56
- if (allowInTests && isTestFile) {
57
- return {};
58
- }
59
- let inWsHandler = false;
60
- let eventParamName = null;
61
- function isWsOnmessageAssignment(node) {
62
- if (node.left.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
63
- node.left.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
64
- node.left.property.name === 'onmessage') {
65
- const handler = node.right;
66
- if (handler.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression ||
67
- handler.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression) {
68
- const firstParam = handler.params[0];
69
- if (firstParam && firstParam.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
70
- return { isHandler: true, eventParam: firstParam.name };
71
- }
72
- }
73
- }
74
- return { isHandler: false, eventParam: null };
75
- }
76
- function isWsAddEventListener(node) {
77
- if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
78
- node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
79
- node.callee.property.name === 'addEventListener' &&
80
- node.arguments.length >= 2) {
81
- const eventType = node.arguments[0];
82
- if (eventType.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
83
- eventType.value === 'message') {
84
- const callback = node.arguments[1];
85
- if (callback.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression ||
86
- callback.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression) {
87
- const firstParam = callback.params[0];
88
- if (firstParam && firstParam.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
89
- return { isHandler: true, eventParam: firstParam.name };
90
- }
91
- }
92
- }
93
- }
94
- return { isHandler: false, eventParam: null };
95
- }
96
- function referencesEventData(node, eventName) {
97
- if (node.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
98
- if (node.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
99
- node.object.name === eventName &&
100
- node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
101
- node.property.name === 'data') {
102
- return true;
103
- }
104
- return referencesEventData(node.object, eventName);
105
- }
106
- if (node.type === eslint_devkit_1.AST_NODE_TYPES.Identifier && node.name === eventName) {
107
- return true;
108
- }
109
- return false;
110
- }
111
- function isEvalCall(node) {
112
- if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
113
- EVAL_FUNCTIONS.has(node.callee.name)) {
114
- return node.callee.name;
115
- }
116
- return null;
117
- }
118
- return {
119
- AssignmentExpression(node) {
120
- const { isHandler, eventParam } = isWsOnmessageAssignment(node);
121
- if (isHandler) {
122
- inWsHandler = true;
123
- eventParamName = eventParam;
124
- }
125
- },
126
- 'AssignmentExpression:exit'(node) {
127
- const { isHandler } = isWsOnmessageAssignment(node);
128
- if (isHandler) {
129
- inWsHandler = false;
130
- eventParamName = null;
131
- }
132
- },
133
- CallExpression(node) {
134
- const { isHandler, eventParam } = isWsAddEventListener(node);
135
- if (isHandler) {
136
- inWsHandler = true;
137
- eventParamName = eventParam;
138
- }
139
- if (!inWsHandler || !eventParamName)
140
- return;
141
- const evalFn = isEvalCall(node);
142
- if (evalFn) {
143
- for (const arg of node.arguments) {
144
- if (referencesEventData(arg, eventParamName)) {
145
- context.report({
146
- node,
147
- messageId: 'evalWithWsData',
148
- data: {
149
- method: evalFn,
150
- },
151
- suggest: [
152
- {
153
- messageId: 'parseDataSafely',
154
- fix: () => null,
155
- },
156
- ],
157
- });
158
- break;
159
- }
160
- }
161
- }
162
- },
163
- 'CallExpression:exit'(node) {
164
- const { isHandler } = isWsAddEventListener(node);
165
- if (isHandler) {
166
- inWsHandler = false;
167
- eventParamName = null;
168
- }
169
- },
170
- NewExpression(node) {
171
- if (!inWsHandler || !eventParamName)
172
- return;
173
- if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
174
- node.callee.name === 'Function') {
175
- for (const arg of node.arguments) {
176
- if (referencesEventData(arg, eventParamName)) {
177
- context.report({
178
- node,
179
- messageId: 'evalWithWsData',
180
- data: {
181
- method: 'new Function',
182
- },
183
- suggest: [
184
- {
185
- messageId: 'parseDataSafely',
186
- fix: () => null,
187
- },
188
- ],
189
- });
190
- break;
191
- }
192
- }
193
- }
194
- },
195
- };
196
- },
197
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noWebsocketEval=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const EVAL_FUNCTIONS=new Set(["eval","Function"]);exports.noWebsocketEval=(0,eslint_devkit_1.createRule)({name:"no-websocket-eval",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-websocket-eval.md",description:"Disallow using eval() or Function() with WebSocket message data",cwe:"CWE-95",cvss:9.8},hasSuggestions:true,messages:{evalWithWsData:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"Remote Code Execution via WebSocket",cwe:"CWE-95",owasp:"A03:2021",cvss:9.8,description:"Using {{method}} with WebSocket data enables remote code execution. A compromised server or MITM attacker can execute arbitrary JavaScript.",severity:"CRITICAL",fix:"Parse WebSocket data as JSON and validate the structure instead of executing it.",documentationLink:"https://cwe.mitre.org/data/definitions/95.html"}),parseDataSafely:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Parse Data Safely",description:"Use JSON.parse() and validate the data structure",severity:"LOW",fix:'const data = JSON.parse(event.data); if (data.action === "update") { handleUpdate(data); }',documentationLink:"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse"})},schema:[{type:"object",properties:{allowInTests:{type:"boolean",default:true}},additionalProperties:false}]},defaultOptions:[{allowInTests:true}],create(context,[options={}]){const{allowInTests=true}=options;const filename=context.filename;const isTestFile=/\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);if(allowInTests&&isTestFile){return{}}let inWsHandler=false;let eventParamName=null;function isWsOnmessageAssignment(node){if(node.left.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&node.left.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.left.property.name==="onmessage"){const handler=node.right;if(handler.type===eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression||handler.type===eslint_devkit_1.AST_NODE_TYPES.FunctionExpression){const firstParam=handler.params[0];if(firstParam&&firstParam.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){return{isHandler:true,eventParam:firstParam.name}}}}return{isHandler:false,eventParam:null}}function isWsAddEventListener(node){if(node.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&node.callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.callee.property.name==="addEventListener"&&node.arguments.length>=2){const eventType=node.arguments[0];if(eventType.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&eventType.value==="message"){const callback=node.arguments[1];if(callback.type===eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression||callback.type===eslint_devkit_1.AST_NODE_TYPES.FunctionExpression){const firstParam=callback.params[0];if(firstParam&&firstParam.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){return{isHandler:true,eventParam:firstParam.name}}}}}return{isHandler:false,eventParam:null}}function referencesEventData(node,eventName){if(node.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression){if(node.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.object.name===eventName&&node.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.property.name==="data"){return true}return referencesEventData(node.object,eventName)}if(node.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.name===eventName){return true}return false}function isEvalCall(node){if(node.callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&EVAL_FUNCTIONS.has(node.callee.name)){return node.callee.name}return null}return{AssignmentExpression(node){const{isHandler,eventParam}=isWsOnmessageAssignment(node);if(isHandler){inWsHandler=true;eventParamName=eventParam}},"AssignmentExpression:exit"(node){const{isHandler}=isWsOnmessageAssignment(node);if(isHandler){inWsHandler=false;eventParamName=null}},CallExpression(node){const{isHandler,eventParam}=isWsAddEventListener(node);if(isHandler){inWsHandler=true;eventParamName=eventParam}if(!inWsHandler||!eventParamName)return;const evalFn=isEvalCall(node);if(evalFn){for(const arg of node.arguments){if(referencesEventData(arg,eventParamName)){context.report({node,messageId:"evalWithWsData",data:{method:evalFn},suggest:[{messageId:"parseDataSafely",fix:()=>null}]});break}}}},"CallExpression:exit"(node){const{isHandler}=isWsAddEventListener(node);if(isHandler){inWsHandler=false;eventParamName=null}},NewExpression(node){if(!inWsHandler||!eventParamName)return;if(node.callee.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.callee.name==="Function"){for(const arg of node.arguments){if(referencesEventData(arg,eventParamName)){context.report({node,messageId:"evalWithWsData",data:{method:"new Function"},suggest:[{messageId:"parseDataSafely",fix:()=>null}]});break}}}}}}});
@@ -1,185 +1 @@
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
- const DANGEROUS_PROPERTIES = new Set(['innerHTML', 'outerHTML']);
6
- const DANGEROUS_METHODS = new Set(['insertAdjacentHTML', 'write', 'writeln']);
7
- exports.noWebsocketInnerhtml = (0, eslint_devkit_1.createRule)({
8
- name: 'no-websocket-innerhtml',
9
- meta: {
10
- type: 'problem',
11
- docs: {
12
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-websocket-innerhtml.md',
13
- description: 'Disallow using innerHTML or similar methods with WebSocket message data',
14
- cwe: 'CWE-79',
15
- cvss: 8.1,
16
- },
17
- hasSuggestions: true,
18
- messages: {
19
- unsafeInnerhtml: (0, eslint_devkit_1.formatLLMMessage)({
20
- icon: eslint_devkit_1.MessageIcons.SECURITY,
21
- issueName: 'XSS via WebSocket',
22
- cwe: 'CWE-79',
23
- owasp: 'A03:2021',
24
- cvss: 8.1,
25
- description: 'Using {{method}} with WebSocket data enables XSS attacks. Malicious server or MITM can send crafted HTML to execute scripts.',
26
- severity: 'HIGH',
27
- fix: 'Use textContent for plain text, or sanitize with DOMPurify before using innerHTML.',
28
- documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/API/WebSocket',
29
- }),
30
- useTextContent: (0, eslint_devkit_1.formatLLMMessage)({
31
- icon: eslint_devkit_1.MessageIcons.INFO,
32
- issueName: 'Use Safe DOM Methods',
33
- description: 'Use textContent or sanitize input with DOMPurify',
34
- severity: 'LOW',
35
- fix: 'element.textContent = event.data; // For plain text\nelement.innerHTML = DOMPurify.sanitize(event.data); // For HTML',
36
- documentationLink: 'https://github.com/cure53/DOMPurify',
37
- }),
38
- },
39
- schema: [
40
- {
41
- type: 'object',
42
- properties: {
43
- allowInTests: {
44
- type: 'boolean',
45
- default: true,
46
- },
47
- },
48
- additionalProperties: false,
49
- },
50
- ],
51
- },
52
- defaultOptions: [{ allowInTests: true }],
53
- create(context, [options = {}]) {
54
- const { allowInTests = true } = options;
55
- const filename = context.filename;
56
- const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
57
- if (allowInTests && isTestFile) {
58
- return {};
59
- }
60
- let inWsHandler = false;
61
- let eventParamName = null;
62
- function isWsOnmessageAssignment(node) {
63
- if (node.left.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
64
- node.left.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
65
- node.left.property.name === 'onmessage') {
66
- const handler = node.right;
67
- if (handler.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression ||
68
- handler.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression) {
69
- const firstParam = handler.params[0];
70
- if (firstParam && firstParam.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
71
- return { isHandler: true, eventParam: firstParam.name };
72
- }
73
- }
74
- }
75
- return { isHandler: false, eventParam: null };
76
- }
77
- function isWsAddEventListener(node) {
78
- if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
79
- node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
80
- node.callee.property.name === 'addEventListener' &&
81
- node.arguments.length >= 2) {
82
- const eventType = node.arguments[0];
83
- if (eventType.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
84
- eventType.value === 'message') {
85
- const callback = node.arguments[1];
86
- if (callback.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression ||
87
- callback.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression) {
88
- const firstParam = callback.params[0];
89
- if (firstParam && firstParam.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
90
- return { isHandler: true, eventParam: firstParam.name };
91
- }
92
- }
93
- }
94
- }
95
- return { isHandler: false, eventParam: null };
96
- }
97
- function referencesEventData(node, eventName) {
98
- if (node.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
99
- if (node.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
100
- node.object.name === eventName &&
101
- node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
102
- node.property.name === 'data') {
103
- return true;
104
- }
105
- return referencesEventData(node.object, eventName);
106
- }
107
- return false;
108
- }
109
- return {
110
- AssignmentExpression(node) {
111
- const { isHandler, eventParam } = isWsOnmessageAssignment(node);
112
- if (isHandler) {
113
- inWsHandler = true;
114
- eventParamName = eventParam;
115
- }
116
- if (!inWsHandler || !eventParamName)
117
- return;
118
- if (node.left.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
119
- node.left.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
120
- DANGEROUS_PROPERTIES.has(node.left.property.name)) {
121
- if (referencesEventData(node.right, eventParamName)) {
122
- context.report({
123
- node,
124
- messageId: 'unsafeInnerhtml',
125
- data: {
126
- method: node.left.property.name,
127
- },
128
- suggest: [
129
- {
130
- messageId: 'useTextContent',
131
- fix: () => null,
132
- },
133
- ],
134
- });
135
- }
136
- }
137
- },
138
- 'AssignmentExpression:exit'(node) {
139
- const { isHandler } = isWsOnmessageAssignment(node);
140
- if (isHandler) {
141
- inWsHandler = false;
142
- eventParamName = null;
143
- }
144
- },
145
- CallExpression(node) {
146
- const { isHandler, eventParam } = isWsAddEventListener(node);
147
- if (isHandler) {
148
- inWsHandler = true;
149
- eventParamName = eventParam;
150
- }
151
- if (!inWsHandler || !eventParamName)
152
- return;
153
- if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
154
- node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
155
- DANGEROUS_METHODS.has(node.callee.property.name)) {
156
- for (const arg of node.arguments) {
157
- if (referencesEventData(arg, eventParamName)) {
158
- context.report({
159
- node,
160
- messageId: 'unsafeInnerhtml',
161
- data: {
162
- method: node.callee.property.name,
163
- },
164
- suggest: [
165
- {
166
- messageId: 'useTextContent',
167
- fix: () => null,
168
- },
169
- ],
170
- });
171
- break;
172
- }
173
- }
174
- }
175
- },
176
- 'CallExpression:exit'(node) {
177
- const { isHandler } = isWsAddEventListener(node);
178
- if (isHandler) {
179
- inWsHandler = false;
180
- eventParamName = null;
181
- }
182
- },
183
- };
184
- },
185
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noWebsocketInnerhtml=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const DANGEROUS_PROPERTIES=new Set(["innerHTML","outerHTML"]);const DANGEROUS_METHODS=new Set(["insertAdjacentHTML","write","writeln"]);exports.noWebsocketInnerhtml=(0,eslint_devkit_1.createRule)({name:"no-websocket-innerhtml",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-websocket-innerhtml.md",description:"Disallow using innerHTML or similar methods with WebSocket message data",cwe:"CWE-79",cvss:8.1},hasSuggestions:true,messages:{unsafeInnerhtml:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"XSS via WebSocket",cwe:"CWE-79",owasp:"A03:2021",cvss:8.1,description:"Using {{method}} with WebSocket data enables XSS attacks. Malicious server or MITM can send crafted HTML to execute scripts.",severity:"HIGH",fix:"Use textContent for plain text, or sanitize with DOMPurify before using innerHTML.",documentationLink:"https://developer.mozilla.org/en-US/docs/Web/API/WebSocket"}),useTextContent:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Use Safe DOM Methods",description:"Use textContent or sanitize input with DOMPurify",severity:"LOW",fix:"element.textContent = event.data; // For plain text\nelement.innerHTML = DOMPurify.sanitize(event.data); // For HTML",documentationLink:"https://github.com/cure53/DOMPurify"})},schema:[{type:"object",properties:{allowInTests:{type:"boolean",default:true}},additionalProperties:false}]},defaultOptions:[{allowInTests:true}],create(context,[options={}]){const{allowInTests=true}=options;const filename=context.filename;const isTestFile=/\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);if(allowInTests&&isTestFile){return{}}let inWsHandler=false;let eventParamName=null;function isWsOnmessageAssignment(node){if(node.left.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&node.left.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.left.property.name==="onmessage"){const handler=node.right;if(handler.type===eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression||handler.type===eslint_devkit_1.AST_NODE_TYPES.FunctionExpression){const firstParam=handler.params[0];if(firstParam&&firstParam.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){return{isHandler:true,eventParam:firstParam.name}}}}return{isHandler:false,eventParam:null}}function isWsAddEventListener(node){if(node.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&node.callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.callee.property.name==="addEventListener"&&node.arguments.length>=2){const eventType=node.arguments[0];if(eventType.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&eventType.value==="message"){const callback=node.arguments[1];if(callback.type===eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression||callback.type===eslint_devkit_1.AST_NODE_TYPES.FunctionExpression){const firstParam=callback.params[0];if(firstParam&&firstParam.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){return{isHandler:true,eventParam:firstParam.name}}}}}return{isHandler:false,eventParam:null}}function referencesEventData(node,eventName){if(node.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression){if(node.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.object.name===eventName&&node.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.property.name==="data"){return true}return referencesEventData(node.object,eventName)}return false}return{AssignmentExpression(node){const{isHandler,eventParam}=isWsOnmessageAssignment(node);if(isHandler){inWsHandler=true;eventParamName=eventParam}if(!inWsHandler||!eventParamName)return;if(node.left.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&node.left.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&DANGEROUS_PROPERTIES.has(node.left.property.name)){if(referencesEventData(node.right,eventParamName)){context.report({node,messageId:"unsafeInnerhtml",data:{method:node.left.property.name},suggest:[{messageId:"useTextContent",fix:()=>null}]})}}},"AssignmentExpression:exit"(node){const{isHandler}=isWsOnmessageAssignment(node);if(isHandler){inWsHandler=false;eventParamName=null}},CallExpression(node){const{isHandler,eventParam}=isWsAddEventListener(node);if(isHandler){inWsHandler=true;eventParamName=eventParam}if(!inWsHandler||!eventParamName)return;if(node.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&node.callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&DANGEROUS_METHODS.has(node.callee.property.name)){for(const arg of node.arguments){if(referencesEventData(arg,eventParamName)){context.report({node,messageId:"unsafeInnerhtml",data:{method:node.callee.property.name},suggest:[{messageId:"useTextContent",fix:()=>null}]});break}}}},"CallExpression:exit"(node){const{isHandler}=isWsAddEventListener(node);if(isHandler){inWsHandler=false;eventParamName=null}}}}});
@@ -1,181 +1 @@
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
- const DANGEROUS_PROPERTIES = new Set(['innerHTML', 'outerHTML']);
6
- const DANGEROUS_METHODS = new Set(['insertAdjacentHTML', 'write', 'writeln']);
7
- exports.noWorkerMessageInnerhtml = (0, eslint_devkit_1.createRule)({
8
- name: 'no-worker-message-innerhtml',
9
- meta: {
10
- type: 'problem',
11
- docs: {
12
- url: 'https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-worker-message-innerhtml.md',
13
- description: 'Disallow using innerHTML with Web Worker message data',
14
- cwe: 'CWE-79',
15
- cvss: 7.5,
16
- },
17
- hasSuggestions: true,
18
- messages: {
19
- workerInnerhtml: (0, eslint_devkit_1.formatLLMMessage)({
20
- icon: eslint_devkit_1.MessageIcons.SECURITY,
21
- issueName: 'XSS via Worker Message',
22
- cwe: 'CWE-79',
23
- owasp: 'A03:2021',
24
- cvss: 7.5,
25
- description: 'Using {{method}} with Worker message data can enable XSS if the worker processes untrusted input.',
26
- severity: 'HIGH',
27
- fix: 'Sanitize worker data with DOMPurify before rendering, or use textContent.',
28
- documentationLink: 'https://developer.mozilla.org/en-US/docs/Web/API/Worker/message_event',
29
- }),
30
- sanitizeWorkerData: (0, eslint_devkit_1.formatLLMMessage)({
31
- icon: eslint_devkit_1.MessageIcons.INFO,
32
- issueName: 'Sanitize Worker Data',
33
- description: 'Use textContent or sanitize with DOMPurify',
34
- severity: 'LOW',
35
- fix: 'const sanitized = DOMPurify.sanitize(event.data);\nelement.innerHTML = sanitized;',
36
- documentationLink: 'https://github.com/cure53/DOMPurify',
37
- }),
38
- },
39
- schema: [
40
- {
41
- type: 'object',
42
- properties: { allowInTests: { type: 'boolean', default: true } },
43
- additionalProperties: false,
44
- },
45
- ],
46
- },
47
- defaultOptions: [{ allowInTests: true }],
48
- create(context, [options = {}]) {
49
- const { allowInTests = true } = options;
50
- const filename = context.filename;
51
- const isTestFile = /\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);
52
- if (allowInTests && isTestFile) {
53
- return {};
54
- }
55
- let inWorkerHandler = false;
56
- let eventParamName = null;
57
- function isWorkerOnmessageAssignment(node) {
58
- if (node.left.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
59
- node.left.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
60
- node.left.property.name === 'onmessage') {
61
- const objName = node.left.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier
62
- ? node.left.object.name.toLowerCase()
63
- : '';
64
- if (objName.includes('worker') ||
65
- objName === 'w' ||
66
- objName === 'wk') {
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
- }
77
- return { isHandler: false, eventParam: null };
78
- }
79
- function isWorkerAddEventListener(node) {
80
- if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
81
- node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
82
- node.callee.property.name === 'addEventListener' &&
83
- node.arguments.length >= 2) {
84
- const eventType = node.arguments[0];
85
- if (eventType.type === eslint_devkit_1.AST_NODE_TYPES.Literal &&
86
- eventType.value === 'message') {
87
- const obj = node.callee.object;
88
- if (obj.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
89
- const objName = obj.name.toLowerCase();
90
- if (objName.includes('worker') ||
91
- objName === 'w' ||
92
- objName === 'wk') {
93
- const callback = node.arguments[1];
94
- if (callback.type === eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression ||
95
- callback.type === eslint_devkit_1.AST_NODE_TYPES.FunctionExpression) {
96
- const firstParam = callback.params[0];
97
- if (firstParam && firstParam.type === eslint_devkit_1.AST_NODE_TYPES.Identifier) {
98
- return { isHandler: true, eventParam: firstParam.name };
99
- }
100
- }
101
- }
102
- }
103
- }
104
- }
105
- return { isHandler: false, eventParam: null };
106
- }
107
- function referencesEventData(node, eventName) {
108
- if (node.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression) {
109
- if (node.object.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
110
- node.object.name === eventName &&
111
- node.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
112
- node.property.name === 'data') {
113
- return true;
114
- }
115
- return referencesEventData(node.object, eventName);
116
- }
117
- return false;
118
- }
119
- return {
120
- AssignmentExpression(node) {
121
- const { isHandler, eventParam } = isWorkerOnmessageAssignment(node);
122
- if (isHandler) {
123
- inWorkerHandler = true;
124
- eventParamName = eventParam;
125
- }
126
- if (!inWorkerHandler || !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.has(node.left.property.name)) {
131
- if (referencesEventData(node.right, eventParamName)) {
132
- context.report({
133
- node,
134
- messageId: 'workerInnerhtml',
135
- data: { method: node.left.property.name },
136
- suggest: [{ messageId: 'sanitizeWorkerData', fix: () => null }],
137
- });
138
- }
139
- }
140
- },
141
- 'AssignmentExpression:exit'(node) {
142
- const { isHandler } = isWorkerOnmessageAssignment(node);
143
- if (isHandler) {
144
- inWorkerHandler = false;
145
- eventParamName = null;
146
- }
147
- },
148
- CallExpression(node) {
149
- const { isHandler, eventParam } = isWorkerAddEventListener(node);
150
- if (isHandler) {
151
- inWorkerHandler = true;
152
- eventParamName = eventParam;
153
- }
154
- if (!inWorkerHandler || !eventParamName)
155
- return;
156
- if (node.callee.type === eslint_devkit_1.AST_NODE_TYPES.MemberExpression &&
157
- node.callee.property.type === eslint_devkit_1.AST_NODE_TYPES.Identifier &&
158
- DANGEROUS_METHODS.has(node.callee.property.name)) {
159
- for (const arg of node.arguments) {
160
- if (referencesEventData(arg, eventParamName)) {
161
- context.report({
162
- node,
163
- messageId: 'workerInnerhtml',
164
- data: { method: node.callee.property.name },
165
- suggest: [{ messageId: 'sanitizeWorkerData', fix: () => null }],
166
- });
167
- break;
168
- }
169
- }
170
- }
171
- },
172
- 'CallExpression:exit'(node) {
173
- const { isHandler } = isWorkerAddEventListener(node);
174
- if (isHandler) {
175
- inWorkerHandler = false;
176
- eventParamName = null;
177
- }
178
- },
179
- };
180
- },
181
- });
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.noWorkerMessageInnerhtml=void 0;const eslint_devkit_1=require("@interlace/eslint-devkit");const DANGEROUS_PROPERTIES=new Set(["innerHTML","outerHTML"]);const DANGEROUS_METHODS=new Set(["insertAdjacentHTML","write","writeln"]);exports.noWorkerMessageInnerhtml=(0,eslint_devkit_1.createRule)({name:"no-worker-message-innerhtml",meta:{type:"problem",docs:{url:"https://github.com/ofri-peretz/eslint/blob/main/packages/eslint-plugin-browser-security/docs/rules/no-worker-message-innerhtml.md",description:"Disallow using innerHTML with Web Worker message data",cwe:"CWE-79",cvss:7.5},hasSuggestions:true,messages:{workerInnerhtml:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.SECURITY,issueName:"XSS via Worker Message",cwe:"CWE-79",owasp:"A03:2021",cvss:7.5,description:"Using {{method}} with Worker message data can enable XSS if the worker processes untrusted input.",severity:"HIGH",fix:"Sanitize worker data with DOMPurify before rendering, or use textContent.",documentationLink:"https://developer.mozilla.org/en-US/docs/Web/API/Worker/message_event"}),sanitizeWorkerData:(0,eslint_devkit_1.formatLLMMessage)({icon:eslint_devkit_1.MessageIcons.INFO,issueName:"Sanitize Worker Data",description:"Use textContent or sanitize with DOMPurify",severity:"LOW",fix:"const sanitized = DOMPurify.sanitize(event.data);\nelement.innerHTML = sanitized;",documentationLink:"https://github.com/cure53/DOMPurify"})},schema:[{type:"object",properties:{allowInTests:{type:"boolean",default:true}},additionalProperties:false}]},defaultOptions:[{allowInTests:true}],create(context,[options={}]){const{allowInTests=true}=options;const filename=context.filename;const isTestFile=/\.(test|spec)\.(ts|tsx|js|jsx)$/.test(filename);if(allowInTests&&isTestFile){return{}}let inWorkerHandler=false;let eventParamName=null;function isWorkerOnmessageAssignment(node){if(node.left.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&node.left.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.left.property.name==="onmessage"){const objName=node.left.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier?node.left.object.name.toLowerCase():"";if(objName.includes("worker")||objName==="w"||objName==="wk"){const handler=node.right;if(handler.type===eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression||handler.type===eslint_devkit_1.AST_NODE_TYPES.FunctionExpression){const firstParam=handler.params[0];if(firstParam&&firstParam.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){return{isHandler:true,eventParam:firstParam.name}}}}}return{isHandler:false,eventParam:null}}function isWorkerAddEventListener(node){if(node.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&node.callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.callee.property.name==="addEventListener"&&node.arguments.length>=2){const eventType=node.arguments[0];if(eventType.type===eslint_devkit_1.AST_NODE_TYPES.Literal&&eventType.value==="message"){const obj=node.callee.object;if(obj.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){const objName=obj.name.toLowerCase();if(objName.includes("worker")||objName==="w"||objName==="wk"){const callback=node.arguments[1];if(callback.type===eslint_devkit_1.AST_NODE_TYPES.ArrowFunctionExpression||callback.type===eslint_devkit_1.AST_NODE_TYPES.FunctionExpression){const firstParam=callback.params[0];if(firstParam&&firstParam.type===eslint_devkit_1.AST_NODE_TYPES.Identifier){return{isHandler:true,eventParam:firstParam.name}}}}}}}return{isHandler:false,eventParam:null}}function referencesEventData(node,eventName){if(node.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression){if(node.object.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.object.name===eventName&&node.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&node.property.name==="data"){return true}return referencesEventData(node.object,eventName)}return false}return{AssignmentExpression(node){const{isHandler,eventParam}=isWorkerOnmessageAssignment(node);if(isHandler){inWorkerHandler=true;eventParamName=eventParam}if(!inWorkerHandler||!eventParamName)return;if(node.left.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&node.left.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&DANGEROUS_PROPERTIES.has(node.left.property.name)){if(referencesEventData(node.right,eventParamName)){context.report({node,messageId:"workerInnerhtml",data:{method:node.left.property.name},suggest:[{messageId:"sanitizeWorkerData",fix:()=>null}]})}}},"AssignmentExpression:exit"(node){const{isHandler}=isWorkerOnmessageAssignment(node);if(isHandler){inWorkerHandler=false;eventParamName=null}},CallExpression(node){const{isHandler,eventParam}=isWorkerAddEventListener(node);if(isHandler){inWorkerHandler=true;eventParamName=eventParam}if(!inWorkerHandler||!eventParamName)return;if(node.callee.type===eslint_devkit_1.AST_NODE_TYPES.MemberExpression&&node.callee.property.type===eslint_devkit_1.AST_NODE_TYPES.Identifier&&DANGEROUS_METHODS.has(node.callee.property.name)){for(const arg of node.arguments){if(referencesEventData(arg,eventParamName)){context.report({node,messageId:"workerInnerhtml",data:{method:node.callee.property.name},suggest:[{messageId:"sanitizeWorkerData",fix:()=>null}]});break}}}},"CallExpression:exit"(node){const{isHandler}=isWorkerAddEventListener(node);if(isHandler){inWorkerHandler=false;eventParamName=null}}}}});