codex-browser-use 4.8.2 → 4.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/bin/browser-use.js +67 -11
- package/extension/background.js +425 -139
- package/extension/manifest.json +2 -10
- package/package.json +2 -2
- package/rules/browser_use.md +6 -20
- package/skill/SKILL.md +9 -8
- package/skill/references/cookbook.md +3 -3
- package/src/ax-tree.js +63 -35
- package/src/backend-discovery.js +15 -8
- package/src/backend-native-driver.js +38 -12
- package/src/backend-tabs.js +93 -7
- package/src/backend-transport.js +3 -3
- package/src/bridge-browser.js +420 -123
- package/src/config.js +44 -11
- package/src/content-extractor.js +53 -19
- package/src/extension.js +6 -1
- package/src/install.js +243 -19
- package/src/locators.js +259 -36
- package/src/read-only.js +254 -52
- package/src/script-runner.js +243 -63
- package/src/server.js +84 -30
package/src/read-only.js
CHANGED
|
@@ -33,6 +33,11 @@ const mutatingNamespaces = new Map([
|
|
|
33
33
|
const forbiddenCalls = new Set(["eval", "fetch", "import", "require"]);
|
|
34
34
|
|
|
35
35
|
const FORBIDDEN_ASSIGN_PROPS = new Set(["__proto__", "prototype", "constructor"]);
|
|
36
|
+
const LOCAL_COLLECTION_NAMES = new Set(["Set", "Map", "URLSearchParams"]);
|
|
37
|
+
const LOCAL_COLLECTION_METHODS = new Set(["clear", "add", "set", "delete", "append"]);
|
|
38
|
+
const READ_ONLY_CACHE_MAX = 256;
|
|
39
|
+
/** @type {Map<string, string|null>} */
|
|
40
|
+
const readOnlyCache = new Map();
|
|
36
41
|
|
|
37
42
|
/**
|
|
38
43
|
* Reject an expression that can mutate the page or reach the network.
|
|
@@ -44,19 +49,36 @@ export function assertReadOnly(source) {
|
|
|
44
49
|
if (typeof source !== "string" || source.trim() === "") {
|
|
45
50
|
throw new Error("Browser evaluate requires a JavaScript expression");
|
|
46
51
|
}
|
|
52
|
+
const cached = readOnlyCache.get(source);
|
|
53
|
+
if (cached !== undefined) {
|
|
54
|
+
readOnlyCache.delete(source);
|
|
55
|
+
readOnlyCache.set(source, cached);
|
|
56
|
+
if (cached !== null) throw new Error(cached);
|
|
57
|
+
return source;
|
|
58
|
+
}
|
|
47
59
|
let ast;
|
|
48
60
|
try {
|
|
49
61
|
ast = parse(`(${source})`, { ecmaVersion: "latest" });
|
|
50
62
|
} catch {
|
|
51
63
|
try {
|
|
52
|
-
ast = parse(source, { ecmaVersion: "latest" });
|
|
64
|
+
ast = parse(source, { ecmaVersion: "latest", allowReturnOutsideFunction: true });
|
|
53
65
|
} catch (error) {
|
|
54
|
-
|
|
66
|
+
const msg = `Browser evaluate could not parse the expression: ${error.message}`;
|
|
67
|
+
if (readOnlyCache.size >= READ_ONLY_CACHE_MAX) {
|
|
68
|
+
readOnlyCache.delete(readOnlyCache.keys().next().value);
|
|
69
|
+
}
|
|
70
|
+
readOnlyCache.set(source, msg);
|
|
71
|
+
throw new Error(msg);
|
|
55
72
|
}
|
|
56
73
|
}
|
|
57
|
-
const nonPlainReassigned =
|
|
58
|
-
const violation = findViolation(ast, [], nonPlainReassigned);
|
|
59
|
-
|
|
74
|
+
const { nonPlainReassigned, reducePlainParams, safeNestedPaths } = analyzeAstState(ast);
|
|
75
|
+
const violation = findViolation(ast, [], nonPlainReassigned, reducePlainParams, safeNestedPaths);
|
|
76
|
+
const errMsg = violation ? `Browser evaluate is read-only: ${violation}` : null;
|
|
77
|
+
if (readOnlyCache.size >= READ_ONLY_CACHE_MAX) {
|
|
78
|
+
readOnlyCache.delete(readOnlyCache.keys().next().value);
|
|
79
|
+
}
|
|
80
|
+
readOnlyCache.set(source, errMsg);
|
|
81
|
+
if (errMsg) throw new Error(errMsg);
|
|
60
82
|
return source;
|
|
61
83
|
}
|
|
62
84
|
|
|
@@ -89,30 +111,123 @@ function isPlainContainerInit(init) {
|
|
|
89
111
|
return false;
|
|
90
112
|
}
|
|
91
113
|
|
|
92
|
-
/** @param {any}
|
|
93
|
-
function
|
|
94
|
-
|
|
114
|
+
/** @param {any} init @returns {boolean} */
|
|
115
|
+
function isLocalCollectionInit(init) {
|
|
116
|
+
return Boolean(
|
|
117
|
+
init &&
|
|
118
|
+
init.type === "NewExpression" &&
|
|
119
|
+
init.callee?.type === "Identifier" &&
|
|
120
|
+
LOCAL_COLLECTION_NAMES.has(init.callee.name),
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** @param {any} node @returns {string|null} */
|
|
125
|
+
function plainPathOf(node) {
|
|
126
|
+
const parts = [];
|
|
127
|
+
let cur = node;
|
|
128
|
+
while (cur?.type === "MemberExpression") {
|
|
129
|
+
if (!cur.computed && cur.property?.type === "Identifier") {
|
|
130
|
+
parts.push(cur.property.name);
|
|
131
|
+
} else if (cur.computed && cur.property?.type === "Literal" && (typeof cur.property.value === "string" || typeof cur.property.value === "number")) {
|
|
132
|
+
parts.push(String(cur.property.value));
|
|
133
|
+
} else {
|
|
134
|
+
parts.push("*");
|
|
135
|
+
}
|
|
136
|
+
cur = cur.object;
|
|
137
|
+
}
|
|
138
|
+
if (parts.length === 0 || cur?.type !== "Identifier") return null;
|
|
139
|
+
parts.push(cur.name);
|
|
140
|
+
return parts.reverse().join(".");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** @param {string} prefix @param {any} expr @param {Set<string>} safeNestedPaths */
|
|
144
|
+
function recordPlainSubpaths(prefix, expr, safeNestedPaths) {
|
|
145
|
+
if (!expr || typeof expr !== "object") return;
|
|
146
|
+
if (expr.type === "ObjectExpression") {
|
|
147
|
+
safeNestedPaths.add(prefix);
|
|
148
|
+
for (const prop of expr.properties ?? []) {
|
|
149
|
+
if (prop?.type !== "Property") continue;
|
|
150
|
+
let key = null;
|
|
151
|
+
if (!prop.computed && prop.key?.type === "Identifier") key = prop.key.name;
|
|
152
|
+
else if (prop.key?.type === "Literal") key = String(prop.key.value);
|
|
153
|
+
if (key && isPlainContainerInit(prop.value)) {
|
|
154
|
+
recordPlainSubpaths(`${prefix}.${key}`, prop.value, safeNestedPaths);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
} else if (expr.type === "ArrayExpression") {
|
|
158
|
+
safeNestedPaths.add(prefix);
|
|
159
|
+
if ((expr.elements ?? []).length > 0 && (expr.elements ?? []).every((el) => el && isPlainContainerInit(el))) {
|
|
160
|
+
safeNestedPaths.add(`${prefix}.*`);
|
|
161
|
+
for (const el of expr.elements) recordPlainSubpaths(`${prefix}.*`, el, safeNestedPaths);
|
|
162
|
+
}
|
|
163
|
+
} else if (isPlainContainerInit(expr)) {
|
|
164
|
+
safeNestedPaths.add(prefix);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** @param {any} root @returns {{nonPlainReassigned:Set<string>, reducePlainParams:WeakMap<object, string>, safeNestedPaths:Set<string>}} */
|
|
169
|
+
function analyzeAstState(root) {
|
|
170
|
+
const nonPlainReassigned = new Set();
|
|
171
|
+
const reducePlainParams = new WeakMap();
|
|
172
|
+
const safeNestedPaths = new Set();
|
|
173
|
+
const taintedSubPaths = new Set();
|
|
95
174
|
const visit = (node) => {
|
|
96
175
|
if (!node || typeof node !== "object") return;
|
|
97
|
-
if (node.type === "
|
|
176
|
+
if (node.type === "VariableDeclarator" && node.id?.type === "Identifier" && isPlainContainerInit(node.init)) {
|
|
177
|
+
recordPlainSubpaths(node.id.name, node.init, safeNestedPaths);
|
|
178
|
+
} else if (node.type === "AssignmentExpression") {
|
|
98
179
|
if (node.left?.type === "Identifier") {
|
|
99
|
-
if (!(node.operator === "=" && isPlainContainerInit(node.right))) {
|
|
100
|
-
|
|
180
|
+
if (!(node.operator === "=" && (isPlainContainerInit(node.right) || isLocalCollectionInit(node.right)))) {
|
|
181
|
+
nonPlainReassigned.add(node.left.name);
|
|
182
|
+
} else if (isPlainContainerInit(node.right)) {
|
|
183
|
+
recordPlainSubpaths(node.left.name, node.right, safeNestedPaths);
|
|
101
184
|
}
|
|
102
185
|
} else if (node.left?.type === "ObjectPattern" || node.left?.type === "ArrayPattern") {
|
|
103
|
-
collectPatternIdentifiers(node.left,
|
|
186
|
+
collectPatternIdentifiers(node.left, nonPlainReassigned);
|
|
187
|
+
} else if (node.left?.type === "MemberExpression") {
|
|
188
|
+
const path = plainPathOf(node.left);
|
|
189
|
+
if (path) {
|
|
190
|
+
if (node.operator === "=" && isPlainContainerInit(node.right)) {
|
|
191
|
+
recordPlainSubpaths(path, node.right, safeNestedPaths);
|
|
192
|
+
} else {
|
|
193
|
+
taintedSubPaths.add(path);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
104
196
|
}
|
|
197
|
+
} else if (
|
|
198
|
+
node.type === "CallExpression" &&
|
|
199
|
+
node.callee?.type === "MemberExpression" &&
|
|
200
|
+
!node.callee.computed &&
|
|
201
|
+
(node.callee.property?.name === "reduce" || node.callee.property?.name === "reduceRight") &&
|
|
202
|
+
Array.isArray(node.arguments) &&
|
|
203
|
+
node.arguments.length >= 2 &&
|
|
204
|
+
isPlainContainerInit(node.arguments[1]) &&
|
|
205
|
+
(node.arguments[0]?.type === "FunctionExpression" || node.arguments[0]?.type === "ArrowFunctionExpression") &&
|
|
206
|
+
node.arguments[0].params?.[0]?.type === "Identifier"
|
|
207
|
+
) {
|
|
208
|
+
const paramName = node.arguments[0].params[0].name;
|
|
209
|
+
reducePlainParams.set(node.arguments[0], paramName);
|
|
210
|
+
recordPlainSubpaths(paramName, node.arguments[1], safeNestedPaths);
|
|
105
211
|
}
|
|
106
|
-
for (const
|
|
212
|
+
for (const key in node) {
|
|
213
|
+
if (key === "type" || key === "start" || key === "end" || key === "loc") continue;
|
|
214
|
+
const value = node[key];
|
|
107
215
|
if (Array.isArray(value)) {
|
|
108
|
-
for (
|
|
216
|
+
for (let i = 0; i < value.length; i++) visit(value[i]);
|
|
109
217
|
} else if (value && typeof value === "object") {
|
|
110
218
|
visit(value);
|
|
111
219
|
}
|
|
112
220
|
}
|
|
113
221
|
};
|
|
114
222
|
visit(root);
|
|
115
|
-
|
|
223
|
+
for (const path of taintedSubPaths) {
|
|
224
|
+
safeNestedPaths.delete(path);
|
|
225
|
+
const dotPrefix = `${path}.`;
|
|
226
|
+
for (const candidate of safeNestedPaths) {
|
|
227
|
+
if (candidate.startsWith(dotPrefix)) safeNestedPaths.delete(candidate);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return { nonPlainReassigned, reducePlainParams, safeNestedPaths };
|
|
116
231
|
}
|
|
117
232
|
|
|
118
233
|
/** @param {any} pattern @param {Set<string>} out */
|
|
@@ -151,24 +266,30 @@ function collectHoistedVars(node, out) {
|
|
|
151
266
|
for (const decl of node.declarations ?? []) collectPatternIdentifiers(decl.id, out);
|
|
152
267
|
return;
|
|
153
268
|
}
|
|
154
|
-
for (const
|
|
269
|
+
for (const key in node) {
|
|
270
|
+
if (key === "type" || key === "start" || key === "end" || key === "loc") continue;
|
|
271
|
+
const value = node[key];
|
|
155
272
|
if (Array.isArray(value)) {
|
|
156
|
-
for (
|
|
273
|
+
for (let i = 0; i < value.length; i++) collectHoistedVars(value[i], out);
|
|
157
274
|
} else if (value && typeof value === "object") {
|
|
158
275
|
collectHoistedVars(value, out);
|
|
159
276
|
}
|
|
160
277
|
}
|
|
161
278
|
}
|
|
162
279
|
|
|
163
|
-
/** @param {any} statements @param {{locals:Set<string>, plainContainers:Set<string>}} scope @param {Set<string>} nonPlainReassigned */
|
|
280
|
+
/** @param {any} statements @param {{locals:Set<string>, plainContainers:Set<string>, collections:Set<string>}} scope @param {Set<string>} nonPlainReassigned */
|
|
164
281
|
function collectDirectDeclarations(statements, scope, nonPlainReassigned) {
|
|
165
282
|
for (const stmt of Array.isArray(statements) ? statements : []) {
|
|
166
283
|
if (!stmt || typeof stmt !== "object") continue;
|
|
167
284
|
if (stmt.type === "VariableDeclaration") {
|
|
168
285
|
for (const decl of stmt.declarations ?? []) {
|
|
169
286
|
collectPatternIdentifiers(decl.id, scope.locals);
|
|
170
|
-
if (decl.id?.type === "Identifier" &&
|
|
171
|
-
|
|
287
|
+
if (decl.id?.type === "Identifier" && !nonPlainReassigned.has(decl.id.name)) {
|
|
288
|
+
if (isPlainContainerInit(decl.init)) {
|
|
289
|
+
scope.plainContainers.add(decl.id.name);
|
|
290
|
+
} else if (isLocalCollectionInit(decl.init)) {
|
|
291
|
+
scope.collections.add(decl.id.name);
|
|
292
|
+
}
|
|
172
293
|
}
|
|
173
294
|
}
|
|
174
295
|
} else if ((stmt.type === "FunctionDeclaration" || stmt.type === "ClassDeclaration") && stmt.id?.name) {
|
|
@@ -177,9 +298,9 @@ function collectDirectDeclarations(statements, scope, nonPlainReassigned) {
|
|
|
177
298
|
}
|
|
178
299
|
}
|
|
179
300
|
|
|
180
|
-
/** @param {any} node @param {Set<string>} nonPlainReassigned @returns {{locals:Set<string>, plainContainers:Set<string>}} */
|
|
181
|
-
function createScopeForNode(node, nonPlainReassigned) {
|
|
182
|
-
const scope = { locals: new Set(), plainContainers: new Set() };
|
|
301
|
+
/** @param {any} node @param {Set<string>} nonPlainReassigned @param {WeakMap<object, string>} reducePlainParams @returns {{locals:Set<string>, plainContainers:Set<string>, collections:Set<string>}} */
|
|
302
|
+
function createScopeForNode(node, nonPlainReassigned, reducePlainParams) {
|
|
303
|
+
const scope = { locals: new Set(), plainContainers: new Set(), collections: new Set() };
|
|
183
304
|
const type = node.type;
|
|
184
305
|
if (type === "Program" || type === "BlockStatement") {
|
|
185
306
|
collectDirectDeclarations(node.body, scope, nonPlainReassigned);
|
|
@@ -189,6 +310,10 @@ function createScopeForNode(node, nonPlainReassigned) {
|
|
|
189
310
|
} else if (type === "FunctionDeclaration" || type === "FunctionExpression" || type === "ArrowFunctionExpression") {
|
|
190
311
|
if (node.id?.name) scope.locals.add(node.id.name);
|
|
191
312
|
for (const param of node.params ?? []) collectPatternIdentifiers(param, scope.locals);
|
|
313
|
+
const accName = reducePlainParams?.get(node);
|
|
314
|
+
if (accName && !nonPlainReassigned.has(accName)) {
|
|
315
|
+
scope.plainContainers.add(accName);
|
|
316
|
+
}
|
|
192
317
|
if (node.body?.type === "BlockStatement") {
|
|
193
318
|
collectDirectDeclarations(node.body.body, scope, nonPlainReassigned);
|
|
194
319
|
for (const stmt of node.body.body ?? []) collectHoistedVars(stmt, scope.locals);
|
|
@@ -209,7 +334,7 @@ function createScopeForNode(node, nonPlainReassigned) {
|
|
|
209
334
|
return scope;
|
|
210
335
|
}
|
|
211
336
|
|
|
212
|
-
/** @param {string} name @param {Array<{locals:Set<string>, plainContainers:Set<string>}>} scopes @returns {boolean} */
|
|
337
|
+
/** @param {string} name @param {Array<{locals:Set<string>, plainContainers:Set<string>, collections:Set<string>}>} scopes @returns {boolean} */
|
|
213
338
|
function isLocalIdentifier(name, scopes) {
|
|
214
339
|
for (let i = scopes.length - 1; i >= 0; i--) {
|
|
215
340
|
if (scopes[i].locals.has(name)) return true;
|
|
@@ -217,7 +342,7 @@ function isLocalIdentifier(name, scopes) {
|
|
|
217
342
|
return false;
|
|
218
343
|
}
|
|
219
344
|
|
|
220
|
-
/** @param {string} name @param {Array<{locals:Set<string>, plainContainers:Set<string>}>} scopes @returns {boolean} */
|
|
345
|
+
/** @param {string} name @param {Array<{locals:Set<string>, plainContainers:Set<string>, collections:Set<string>}>} scopes @returns {boolean} */
|
|
221
346
|
function isLocalPlainContainer(name, scopes) {
|
|
222
347
|
for (let i = scopes.length - 1; i >= 0; i--) {
|
|
223
348
|
if (scopes[i].plainContainers.has(name)) return true;
|
|
@@ -226,10 +351,17 @@ function isLocalPlainContainer(name, scopes) {
|
|
|
226
351
|
return false;
|
|
227
352
|
}
|
|
228
353
|
|
|
229
|
-
/** @param {
|
|
230
|
-
function
|
|
231
|
-
|
|
232
|
-
|
|
354
|
+
/** @param {string} name @param {Array<{locals:Set<string>, plainContainers:Set<string>, collections:Set<string>}>} scopes @returns {boolean} */
|
|
355
|
+
function isLocalCollection(name, scopes) {
|
|
356
|
+
for (let i = scopes.length - 1; i >= 0; i--) {
|
|
357
|
+
if (scopes[i].collections.has(name)) return true;
|
|
358
|
+
if (scopes[i].locals.has(name)) return false;
|
|
359
|
+
}
|
|
360
|
+
return false;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** @param {any} node @returns {boolean} */
|
|
364
|
+
function isSafePlainProperty(node) {
|
|
233
365
|
if (!node.computed) return !FORBIDDEN_ASSIGN_PROPS.has(node.property?.name);
|
|
234
366
|
if (node.property?.type === "Literal") {
|
|
235
367
|
return !FORBIDDEN_ASSIGN_PROPS.has(String(node.property.value));
|
|
@@ -240,32 +372,78 @@ function isSafePlainMemberTarget(node, scopes) {
|
|
|
240
372
|
return true;
|
|
241
373
|
}
|
|
242
374
|
|
|
375
|
+
/** @param {any} node @param {Array<{locals:Set<string>, plainContainers:Set<string>, collections:Set<string>}>} scopes @param {Set<string>} safeNestedPaths @returns {boolean} */
|
|
376
|
+
function isSafePlainMemberTarget(node, scopes, safeNestedPaths) {
|
|
377
|
+
const parents = [];
|
|
378
|
+
let cur = node;
|
|
379
|
+
while (cur?.type === "MemberExpression") {
|
|
380
|
+
if (!isSafePlainProperty(cur)) return false;
|
|
381
|
+
parents.push(cur);
|
|
382
|
+
cur = cur.object;
|
|
383
|
+
}
|
|
384
|
+
if (parents.length === 0 || cur?.type !== "Identifier" || !isLocalPlainContainer(cur.name, scopes)) {
|
|
385
|
+
return false;
|
|
386
|
+
}
|
|
387
|
+
if (parents.length === 1) return true;
|
|
388
|
+
// For depth >= 2 (e.g., out.meta.total = 5 or rows[0].rank = 1), require each intermediate
|
|
389
|
+
// container step to originate from a statically known plain literal/array initializer,
|
|
390
|
+
// blocking `const r = {}; r.el = el; r.el.value = '1'`.
|
|
391
|
+
let prefix = cur.name;
|
|
392
|
+
for (let i = parents.length - 1; i >= 1; i--) {
|
|
393
|
+
const segNode = parents[i];
|
|
394
|
+
let seg = null;
|
|
395
|
+
if (!segNode.computed && segNode.property?.type === "Identifier") seg = segNode.property.name;
|
|
396
|
+
else if (segNode.computed && segNode.property?.type === "Literal") seg = String(segNode.property.value);
|
|
397
|
+
const exactCandidate = seg !== null ? `${prefix}.${seg}` : null;
|
|
398
|
+
const wildcardCandidate = `${prefix}.*`;
|
|
399
|
+
if (exactCandidate && safeNestedPaths.has(exactCandidate)) {
|
|
400
|
+
prefix = exactCandidate;
|
|
401
|
+
} else if (safeNestedPaths.has(wildcardCandidate)) {
|
|
402
|
+
prefix = wildcardCandidate;
|
|
403
|
+
} else {
|
|
404
|
+
return false;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** @param {any} node @param {Array<{locals:Set<string>, plainContainers:Set<string>, collections:Set<string>}>} scopes @param {Set<string>} nonPlainReassigned @param {WeakMap<object, string>} reducePlainParams @param {Set<string>} safeNestedPaths @returns {string|null} */
|
|
411
|
+
function validateComputedKeys(node, scopes, nonPlainReassigned, reducePlainParams, safeNestedPaths) {
|
|
412
|
+
for (let cur = node; cur?.type === "MemberExpression"; cur = cur.object) {
|
|
413
|
+
if (cur.computed) {
|
|
414
|
+
const violation = findViolation(cur.property, scopes, nonPlainReassigned, reducePlainParams, safeNestedPaths);
|
|
415
|
+
if (violation) return violation;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return null;
|
|
419
|
+
}
|
|
420
|
+
|
|
243
421
|
/**
|
|
244
422
|
* @param {unknown} node
|
|
245
|
-
* @param {Array<{locals:Set<string>, plainContainers:Set<string>}>} scopes
|
|
423
|
+
* @param {Array<{locals:Set<string>, plainContainers:Set<string>, collections:Set<string>}>} scopes
|
|
246
424
|
* @param {Set<string>} nonPlainReassigned
|
|
425
|
+
* @param {WeakMap<object, string>} reducePlainParams
|
|
426
|
+
* @param {Set<string>} safeNestedPaths
|
|
247
427
|
* @returns {string|null}
|
|
248
428
|
*/
|
|
249
|
-
function findViolation(node, scopes = [], nonPlainReassigned = new Set()) {
|
|
429
|
+
function findViolation(node, scopes = [], nonPlainReassigned = new Set(), reducePlainParams = new WeakMap(), safeNestedPaths = new Set()) {
|
|
250
430
|
if (!node || typeof node !== "object") return null;
|
|
251
431
|
const record = /** @type {Record<string, unknown>} */ (node);
|
|
252
432
|
const type = record.type;
|
|
253
433
|
|
|
254
434
|
const activeScopes = SCOPE_CREATORS.has(String(type))
|
|
255
|
-
? [...scopes, createScopeForNode(record, nonPlainReassigned)]
|
|
435
|
+
? [...scopes, createScopeForNode(record, nonPlainReassigned, reducePlainParams)]
|
|
256
436
|
: scopes;
|
|
257
437
|
|
|
258
438
|
if (type === "AssignmentExpression") {
|
|
259
439
|
const left = /** @type {any} */ (record.left);
|
|
260
440
|
if (left?.type === "Identifier" && isLocalIdentifier(left.name, activeScopes)) {
|
|
261
|
-
return findViolation(record.right, activeScopes, nonPlainReassigned);
|
|
441
|
+
return findViolation(record.right, activeScopes, nonPlainReassigned, reducePlainParams, safeNestedPaths);
|
|
262
442
|
}
|
|
263
|
-
if (isSafePlainMemberTarget(left, activeScopes)) {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
}
|
|
268
|
-
return findViolation(record.right, activeScopes, nonPlainReassigned);
|
|
443
|
+
if (isSafePlainMemberTarget(left, activeScopes, safeNestedPaths)) {
|
|
444
|
+
const keyViolation = validateComputedKeys(left, activeScopes, nonPlainReassigned, reducePlainParams, safeNestedPaths);
|
|
445
|
+
if (keyViolation) return keyViolation;
|
|
446
|
+
return findViolation(record.right, activeScopes, nonPlainReassigned, reducePlainParams, safeNestedPaths);
|
|
269
447
|
}
|
|
270
448
|
return "assignment is not allowed (only local variables and direct properties/indices of locally created {} or [] objects may be assigned; do not mutate DOM nodes, parameters, or globals)";
|
|
271
449
|
}
|
|
@@ -274,12 +452,18 @@ function findViolation(node, scopes = [], nonPlainReassigned = new Set()) {
|
|
|
274
452
|
if (arg?.type === "Identifier" && isLocalIdentifier(arg.name, activeScopes)) {
|
|
275
453
|
return null;
|
|
276
454
|
}
|
|
277
|
-
if (isSafePlainMemberTarget(arg, activeScopes)) {
|
|
278
|
-
return
|
|
455
|
+
if (isSafePlainMemberTarget(arg, activeScopes, safeNestedPaths)) {
|
|
456
|
+
return validateComputedKeys(arg, activeScopes, nonPlainReassigned, reducePlainParams, safeNestedPaths);
|
|
279
457
|
}
|
|
280
458
|
return "increment and decrement are not allowed";
|
|
281
459
|
}
|
|
282
|
-
if (type === "UnaryExpression" && record.operator === "delete")
|
|
460
|
+
if (type === "UnaryExpression" && record.operator === "delete") {
|
|
461
|
+
const arg = /** @type {any} */ (record.argument);
|
|
462
|
+
if (isSafePlainMemberTarget(arg, activeScopes, safeNestedPaths)) {
|
|
463
|
+
return validateComputedKeys(arg, activeScopes, nonPlainReassigned, reducePlainParams, safeNestedPaths);
|
|
464
|
+
}
|
|
465
|
+
return "delete is not allowed";
|
|
466
|
+
}
|
|
283
467
|
if (type === "ImportExpression") return "dynamic import is not allowed";
|
|
284
468
|
if (type === "NewExpression" && record.callee && /** @type {any} */ (record.callee).name === "Function") {
|
|
285
469
|
return "dynamic code construction is not allowed";
|
|
@@ -293,24 +477,42 @@ function findViolation(node, scopes = [], nonPlainReassigned = new Set()) {
|
|
|
293
477
|
if (callee?.type === "MemberExpression" && !callee.computed) {
|
|
294
478
|
const object = callee.object;
|
|
295
479
|
const property = callee.property?.name;
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
480
|
+
const isLocalCollCall =
|
|
481
|
+
typeof property === "string" &&
|
|
482
|
+
LOCAL_COLLECTION_METHODS.has(property) &&
|
|
483
|
+
object?.type === "Identifier" &&
|
|
484
|
+
isLocalCollection(object.name, activeScopes);
|
|
485
|
+
const firstArg = Array.isArray(record.arguments) ? record.arguments[0] : null;
|
|
486
|
+
const isSafeObjectAssign =
|
|
487
|
+
namespaceOf(object) === "Object" &&
|
|
488
|
+
property === "assign" &&
|
|
489
|
+
Boolean(
|
|
490
|
+
firstArg?.type === "ObjectExpression" ||
|
|
491
|
+
(firstArg?.type === "Identifier" && isLocalPlainContainer(firstArg.name, activeScopes)) ||
|
|
492
|
+
isSafePlainMemberTarget(firstArg, activeScopes, safeNestedPaths),
|
|
493
|
+
);
|
|
494
|
+
if (!isLocalCollCall && !isSafeObjectAssign) {
|
|
495
|
+
if (typeof property === "string" && mutatingMethods.has(property)) {
|
|
496
|
+
return `${property}() is not allowed`;
|
|
497
|
+
}
|
|
498
|
+
const namespace = namespaceOf(object);
|
|
499
|
+
if (namespace && typeof property === "string" && mutatingNamespaces.get(namespace)?.has(property)) {
|
|
500
|
+
return `${namespace}.${property}() is not allowed`;
|
|
501
|
+
}
|
|
302
502
|
}
|
|
303
503
|
}
|
|
304
504
|
}
|
|
305
505
|
|
|
306
|
-
for (const
|
|
506
|
+
for (const key in record) {
|
|
507
|
+
if (key === "type" || key === "start" || key === "end" || key === "loc") continue;
|
|
508
|
+
const value = record[key];
|
|
307
509
|
if (Array.isArray(value)) {
|
|
308
|
-
for (
|
|
309
|
-
const violation = findViolation(
|
|
510
|
+
for (let i = 0; i < value.length; i++) {
|
|
511
|
+
const violation = findViolation(value[i], activeScopes, nonPlainReassigned, reducePlainParams, safeNestedPaths);
|
|
310
512
|
if (violation) return violation;
|
|
311
513
|
}
|
|
312
514
|
} else if (value && typeof value === "object") {
|
|
313
|
-
const violation = findViolation(value, activeScopes, nonPlainReassigned);
|
|
515
|
+
const violation = findViolation(value, activeScopes, nonPlainReassigned, reducePlainParams, safeNestedPaths);
|
|
314
516
|
if (violation) return violation;
|
|
315
517
|
}
|
|
316
518
|
}
|