Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

fix(admission): substitute actual callable command values [C7,O5]

Corey Olson4808ea2eb407
diff --git a/src/browser-call-values.js b/src/browser-call-values.js
new file mode 100644
index 00000000..c23c98ee
--- /dev/null
+++ b/src/browser-call-values.js
@@ -0,0 +1,238 @@
+/** Follow only local callable input-to-return command flows without evaluating JavaScript [C7,O5]. */
+const IDENTIFIER = /^[$A-Z_a-z][$\w]*$/u;
+const LIMIT = 256;
+
+/** Find a matching lexical delimiter in offset-preserving executable code. */
+function closeAt(text, start) {
+ const pairs = { '(': ')', '[': ']', '{': '}' };
+ const stack = [];
+ for (let at = start; at < text.length; at += 1) {
+ if (pairs[text[at]]) stack.push(pairs[text[at]]);
+ else if (')]}'.includes(text[at])) {
+ if (stack.pop() !== text[at]) return -1;
+ if (!stack.length) return at;
+ }
+ }
+ return -1;
+}
+
+/** Split lexical top-level fields, preserving nested expressions and blanked string contents. */
+function fields(text, separator = ',') {
+ const out = [];
+ let start = 0;
+ for (let at = 0; at < text.length; at += 1) {
+ if ('([{'.includes(text[at])) {
+ const end = closeAt(text, at);
+ if (end < 0) return [text];
+ at = end;
+ } else if (text[at] === separator) { out.push(text.slice(start, at)); start = at + 1; }
+ }
+ out.push(text.slice(start));
+ return out;
+}
+
+/** Strip expression-only wrappers; their predicates never become returned command values. */
+function clean(text) {
+ let value = text.trim().replace(/^await\s+/u, '').trim();
+ while (value[0] === '(' && closeAt(value, 0) === value.length - 1) value = value.slice(1, -1).trim();
+ return value;
+}
+
+/** Read one expression through its top-level statement or argument boundary. */
+function expression(text, start) {
+ let at = start;
+ for (; at < text.length; at += 1) {
+ if ('([{'.includes(text[at])) {
+ const end = closeAt(text, at);
+ if (end < 0) return text.slice(start);
+ at = end;
+ } else if (';,)}]'.includes(text[at]) || text[at] === '\n') break;
+ }
+ return text.slice(start, at);
+}
+
+/** Read the value branches of a top-level conditional without following its predicate. */
+function branches(text) {
+ let question = -1;
+ let nested = 0;
+ for (let at = 0; at < text.length; at += 1) {
+ if ('([{'.includes(text[at])) { const end = closeAt(text, at); if (end < 0) break; at = end; }
+ else if (text[at] === '?' && !['?', '.'].includes(text[at + 1]) && text[at - 1] !== '?') {
+ if (question < 0) question = at;
+ else nested += 1;
+ } else if (text[at] === ':' && question >= 0) {
+ if (!nested) return [text.slice(question + 1, at), text.slice(at + 1)];
+ nested -= 1;
+ }
+ }
+ return null;
+}
+
+/** Read a local function expression, retaining return spans but excluding nested callable bodies. */
+function callable(text, environment) {
+ const value = clean(text);
+ let params;
+ let body;
+ const declared = /^(?:async\s+)?function(?:\s+[$A-Z_a-z][$\w]*)?\s*\(/u.exec(value);
+ if (declared) {
+ const start = declared[0].length - 1;
+ const end = closeAt(value, start);
+ if (end < 0) return null;
+ params = value.slice(start + 1, end);
+ body = value.slice(end + 1).trim();
+ } else {
+ const arrow = /^(?:async\s+)?(\([^]*\)|[$A-Z_a-z][$\w]*)\s*=>\s*/u.exec(value);
+ if (!arrow) return null;
+ // Greedy parentheses may cross a nested arrow; accept only the balanced parameter span.
+ const prefix = value.replace(/^async\s+/u, '');
+ const end = prefix[0] === '(' ? closeAt(prefix, 0) + 1 : /^[$A-Z_a-z][$\w]*/u.exec(prefix)?.[0].length;
+ if (!end || !/^\s*=>/u.test(prefix.slice(end))) return null;
+ params = prefix[0] === '(' ? prefix.slice(1, end - 1) : prefix.slice(0, end);
+ body = prefix.slice(end).replace(/^\s*=>\s*/u, '');
+ }
+ if (body[0] !== '{') return { params: fields(params), returns: [body], environment };
+ const last = closeAt(body, 0);
+ if (last < 0) return null;
+ const content = body.slice(1, last);
+ const returns = [];
+ // Skip a nested function's complete body when collecting the enclosing function's returns.
+ for (let at = 0; at < content.length; at += 1) {
+ const rest = content.slice(at);
+ const nested = /^(?:async\s+)?function(?:\s+[$A-Z_a-z][$\w]*)?\s*\(/u.exec(rest);
+ if (nested) {
+ const end = closeAt(content, at + nested[0].length - 1);
+ const first = end + 1 + content.slice(end + 1).search(/\S/u);
+ if (end >= 0 && content[first] === '{') { at = closeAt(content, first); if (at < 0) break; continue; }
+ }
+ const arrowStart = /^(?:async\s+)?[$A-Z_a-z][$\w]*\s*=>\s*\{/u.exec(rest);
+ let arrowBody = arrowStart ? at + arrowStart[0].length - 1 : -1;
+ if (content[at] === '(') {
+ const parameters = closeAt(content, at);
+ const tail = parameters < 0 ? null : /^\s*=>\s*\{/u.exec(content.slice(parameters + 1));
+ if (tail) arrowBody = parameters + tail[0].length;
+ }
+ if (arrowBody >= 0) { at = closeAt(content, arrowBody); if (at < 0) break; continue; }
+ const found = /^return\b[ \t]*/u.exec(rest);
+ if (found && (at === 0 || !/[$\w]/u.test(content[at - 1]))) {
+ returns.push(expression(content, at + found[0].length));
+ }
+ }
+ return { params: fields(params), returns, environment };
+}
+
+/** Separate an executable callee and its actual argument expressions. */
+function invocation(text) {
+ for (let at = 0; at < text.length; at += 1) {
+ if (text[at] === '(') {
+ const end = closeAt(text, at);
+ if (end === text.length - 1) return { callee: text.slice(0, at).replace(/\?\.$/u, '').trim(), args: fields(text.slice(at + 1, end)) };
+ if (end < 0) return null;
+ at = end;
+ } else if ('[{'.includes(text[at])) { const end = closeAt(text, at); if (end < 0) return null; at = end; }
+ }
+ return null;
+}
+
+/** Bind a formal, including simple object/array patterns, to the caller's unevaluated actual value. */
+function bind(pattern, actual, environment) {
+ const parts = fields(pattern, '=');
+ const target = clean(parts[0]);
+ const values = [actual, ...parts.slice(1).map(expr => ({ expr, environment }))].filter(Boolean);
+ if (IDENTIFIER.test(target)) { environment.set(target, values); return; }
+ if (target[0] === '{' && closeAt(target, 0) === target.length - 1) {
+ for (const field of fields(target.slice(1, -1))) {
+ const pair = fields(field, ':');
+ const key = pair[0].trim();
+ if (!IDENTIFIER.test(key)) continue;
+ bind(pair.length > 1 ? pair.slice(1).join(':') : key,
+ { projection: key, values }, environment);
+ }
+ } else if (target[0] === '[' && closeAt(target, 0) === target.length - 1) {
+ fields(target.slice(1, -1)).forEach((field, index) => {
+ if (field.trim()) bind(field, { projection: String(index), values }, environment);
+ });
+ }
+}
+
+/**
+ * Add missing actual-argument flows to the existing command-only classifier. Existing binding
+ * entries may supply every assignment RHS as `expressions: string[]`; these must be lexical code,
+ * not raw string contents. The callbacks retain the caller's current literal/member policy.
+ * Unknown external calls do not inherit every input's taint: finder/runtime exceptions stay local.
+ *
+ * @param {string} code offset-preserving lexical executable code
+ * @param {Map<string, {expressions?: string[]}>} bindings existing local binding union
+ * @param {{unresolved:(expression:string)=>boolean,named:(name:string)=>boolean}} policy
+ * @returns {(expression:string)=>boolean}
+ */
+export function makeCallableCommandChecker(code, bindings, { unresolved, named }) {
+ const declarations = new Map();
+ for (const found of code.matchAll(/\bfunction\s+([$A-Z_a-z][$\w]*)\s*\(/gu)) {
+ const end = closeAt(code, found.index + found[0].length - 1);
+ const body = end + 1 + code.slice(end + 1).search(/\S/u);
+ if (end < 0 || code[body] !== '{') continue;
+ const last = closeAt(code, body);
+ if (last >= 0) declarations.set(found[1], [...declarations.get(found[1]) ?? [], code.slice(found.index, last + 1)]);
+ }
+ const environments = new WeakMap();
+ let environmentId = 0;
+ /** Evaluate only symbolic local return values, carrying closures rather than running source. */
+ function values(input, budget, trail = new Set()) {
+ if (budget.left-- <= 0) return [{ unknown: true }];
+ if (input.projection !== undefined) {
+ return input.values.flatMap(value => values(value, budget, trail)).flatMap(value => {
+ if (!value.expr) return value.callable ? [] : [value];
+ const text = clean(value.expr);
+ const key = input.projection;
+ if (text[0] === '{' && closeAt(text, 0) === text.length - 1) {
+ const row = fields(text.slice(1, -1)).find(field => fields(field, ':')[0].trim() === key);
+ if (row) { const pair = fields(row, ':'); return values({ expr: pair.length > 1 ? pair.slice(1).join(':') : key, environment: value.environment }, budget, trail); }
+ return [{ unknown: true }];
+ }
+ if (text[0] === '[' && closeAt(text, 0) === text.length - 1 && /^\d+$/u.test(key)) {
+ const entry = fields(text.slice(1, -1))[Number(key)];
+ return entry === undefined ? [{ unknown: true }] : values({ expr: entry, environment: value.environment }, budget, trail);
+ }
+ return [{ expr: /^\d+$/u.test(key) ? `${text}[${key}]` : `${text}.${key}`, environment: value.environment }];
+ });
+ }
+ const text = clean(input.expr ?? '');
+ const environment = input.environment ?? new Map();
+ if (!text) return [];
+ if (!environments.has(environment)) environments.set(environment, ++environmentId);
+ const signature = `${environments.get(environment)}:${text}`;
+ if (trail.has(signature)) return [{ unknown: true }];
+ const next = new Set(trail).add(signature);
+ const fn = callable(text, environment);
+ if (fn) return [{ callable: fn }];
+ const conditional = branches(text);
+ if (conditional) return conditional.flatMap(expr => values({ expr, environment }, budget, next));
+ if (IDENTIFIER.test(text)) {
+ const local = environment.get(text);
+ if (local) return local.flatMap(value => values(value, budget, next));
+ const assigned = bindings.get(text)?.expressions ?? [];
+ const expressions = [...assigned, ...declarations.get(text) ?? []];
+ if (expressions.length) return expressions.flatMap(expr => values({ expr, environment }, budget, next));
+ return [{ expr: text, environment, browser: named(text) }];
+ }
+ const returnedMember = /^(.*)\.\s*([$A-Z_a-z][$\w]*)$/u.exec(text);
+ if (returnedMember && returnedMember[1].trimEnd().endsWith(')')) {
+ return values({ projection: returnedMember[2], values: [{ expr: returnedMember[1], environment }] }, budget, next);
+ }
+ const call = invocation(text);
+ if (call) {
+ const callees = values({ expr: call.callee, environment }, budget, next);
+ return callees.flatMap(value => {
+ if (!value.callable) return value.unknown ? [value] : [];
+ const closure = new Map(value.callable.environment);
+ value.callable.params.forEach((formal, index) => bind(formal, { expr: call.args[index] ?? '', environment }, closure));
+ return value.callable.returns.flatMap(expr => values({ expr, environment: closure }, budget, next));
+ });
+ }
+ const member = /^([$A-Z_a-z][$\w]*)\s*\.\s*([$A-Z_a-z][$\w]*)$/u.exec(text);
+ if (member && environment.has(member[1])) return values({ projection: member[2], values: environment.get(member[1]) }, budget, next);
+ return [{ expr: text, environment }];
+ }
+ return expressionText => values({ expr: expressionText, environment: new Map() }, { left: LIMIT })
+ .some(value => value.unknown || value.browser || value.expr && unresolved(value.expr));
+}
diff --git a/src/browser-launch-source.js b/src/browser-launch-source.js
index 2a1b1c61..cbff8b80 100644
--- a/src/browser-launch-source.js
+++ b/src/browser-launch-source.js
@@ -1,5 +1,6 @@
/** Recognize Chrome-bearing process arguments in executable code, not generated fixture programs [C7,O5]. */
import { lex } from './js-lex.js';
+import { makeCallableCommandChecker } from './browser-call-values.js';
const PROCESS_CALLS = new Set(['spawn', 'spawnSync', 'exec', 'execSync', 'execFile', 'execFileSync', 'fork',
'runFixtureChild', 'runFixtureChildAsync', 'startFixtureChild', 'Worker', 'SharedWorker',
@@ -58,7 +59,7 @@ function processNamespaces(source) {
}
/** Unknown command members stay bound without tainting unrelated calls in the same source. */
-function unresolvedCommandValue(expression, links, parameters) {
+function unresolvedCommandValue(expression, links) {
const compact = expression.replace(/^\s*await\s+/u, '').replace(/\s/gu, '');
// A conditional's predicate is not one of the executable values it returns.
let nested = 0;
@@ -76,8 +77,8 @@ function unresolvedCommandValue(expression, links, parameters) {
at = end;
} else if (compact[at] === '?' && !['?', '.'].includes(compact[at + 1]) && compact[at - 1] !== '?') nested += 1;
else if (compact[at] === ':' && nested-- === 0) {
- return unresolvedCommandValue(compact.slice(start, at), links, parameters)
- || unresolvedCommandValue(compact.slice(at + 1), links, parameters);
+ return unresolvedCommandValue(compact.slice(start, at), links)
+ || unresolvedCommandValue(compact.slice(at + 1), links);
}
}
return true;
@@ -86,9 +87,6 @@ function unresolvedCommandValue(expression, links, parameters) {
// Only returned values and bare aliases feed the command graph, never a conditional's predicate.
const linked = /^([$A-Z_a-z][$\w]*)(?=$|(?:\?\.)?\()/u.exec(compact);
if (linked && links) links.add(linked[1]);
- // A returned input is not a proven non-browser executable. It affects only callers that
- // actually supply a process command; unused callable parameters never taint literal launches.
- if (parameters && [...compact.matchAll(WORD)].some(word => parameters.has(word[0]))) return true;
// Calls and object/template construction do not return the value of each member they read.
// In particular, a pid used to build a directory name is not an executable-valued alias.
let brackets = 0;
@@ -124,15 +122,9 @@ function expressionEnd(code, start, newlineEnds = true) {
return end;
}
-/** Keep parameter binding names while omitting defaults, which are executable expressions. */
-function parameterNames(source) {
- return new Set([...source.replace(/=[^,]*/gu, '').matchAll(WORD)].map(word => word[0]));
-}
-
/** Read callable result spans without evaluating either a function body or its generated source. */
function callableReturns(code, start, end) {
let at = start + code.slice(start, end).search(/\S/u);
- let names;
const declared = /^(?:async\s+)?function(?:\s+[$A-Z_a-z][$\w]*)?\s*\(/u.exec(code.slice(at, end));
if (declared) at += declared[0].length - 1;
else {
@@ -142,12 +134,10 @@ function callableReturns(code, start, end) {
if (code[at] === '(') {
const parameters = closing(code, at);
if (parameters === null) return [];
- names = parameterNames(code.slice(at + 1, parameters));
at = parameters + 1;
} else {
const parameter = /^[$A-Z_a-z][$\w]*/u.exec(code.slice(at, end));
if (!parameter || declared) return [];
- names = new Set([parameter[0]]);
at += parameter[0].length;
}
at += code.slice(at, end).search(/\S/u);
@@ -155,7 +145,7 @@ function callableReturns(code, start, end) {
if (code.slice(at, at + 2) !== '=>') return [];
at += 2;
at += code.slice(at, end).search(/\S/u);
- if (code[at] !== '{') return [[at, end, names]];
+ if (code[at] !== '{') return [[at, end]];
}
if (code[at] !== '{') return [];
const last = closing(code, at);
@@ -163,7 +153,7 @@ function callableReturns(code, start, end) {
return [...code.slice(at + 1, last).matchAll(/\breturn[ \t]+/gu)].flatMap(match => {
const first = at + 1 + match.index + match[0].length;
const final = expressionEnd(code, first);
- return final === null ? [] : [[first, final, names]];
+ return final === null ? [] : [[first, final]];
});
}
@@ -211,11 +201,12 @@ export function sourceLaunchesBrowser(source, reviewedExecutors = []) {
const start = match.index + match[0].length;
const end = expressionEnd(code, start);
if (end === null) return CHROME.test(source);
- const prior = bindings.get(match[1]) ?? { browser: false, unresolved: false, aliases: new Set(), dependencies: new Set() };
+ const prior = bindings.get(match[1]) ?? { browser: false, unresolved: false, aliases: new Set(), dependencies: new Set(), expressions: [] };
+ prior.expressions.push(code.slice(start, end));
prior.browser ||= browserIn(start, end);
prior.unresolved ||= unresolvedCommandValue(code.slice(start, end), prior.aliases);
- for (const [first, last, parameters] of callableReturns(code, start, end)) {
- prior.unresolved ||= unresolvedCommandValue(code.slice(first, last), prior.aliases, parameters);
+ for (const [first, last] of callableReturns(code, start, end)) {
+ prior.unresolved ||= unresolvedCommandValue(code.slice(first, last), prior.aliases);
}
for (const word of code.slice(start, end).matchAll(WORD)) prior.dependencies.add(word[0]);
bindings.set(match[1], prior);
@@ -229,14 +220,13 @@ export function sourceLaunchesBrowser(source, reviewedExecutors = []) {
if (code[body] !== '{') continue;
const last = closing(code, body);
if (last === null) continue;
- const value = bindings.get(match[1]) ?? { browser: false, unresolved: false, aliases: new Set(), dependencies: new Set() };
- const names = parameterNames(code.slice(match.index + match[0].length, parameters));
+ const value = bindings.get(match[1]) ?? { browser: false, unresolved: false, aliases: new Set(), dependencies: new Set(), expressions: [] };
for (const returned of code.slice(body + 1, last).matchAll(/\breturn\s+(?![\r\n])/gu)) {
const start = body + 1 + returned.index + returned[0].length;
const end = expressionEnd(code, start);
if (end === null) continue;
value.browser ||= browserIn(start, end);
- value.unresolved ||= unresolvedCommandValue(code.slice(start, end), value.aliases, names);
+ value.unresolved ||= unresolvedCommandValue(code.slice(start, end), value.aliases);
for (const word of code.slice(start, end).matchAll(WORD)) value.dependencies.add(word[0]);
}
bindings.set(match[1], value);
@@ -249,6 +239,8 @@ export function sourceLaunchesBrowser(source, reviewedExecutors = []) {
return Boolean(binding && (binding.browser || command && binding.unresolved
|| [...(command ? binding.aliases : binding.dependencies)].some(next => namedBrowser(next, seen, command))));
}
+ const callableCommand = makeCallableCommandChecker(code, bindings, { unresolved: unresolvedCommandValue,
+ named: name => namedBrowser(name, new Set(), true) });
const calls = [...code.matchAll(/(?<![\w$])([$A-Z_a-z][$\w]*)\s*(?:\?\.)?\s*\(/gu)]
.filter(match => names.has(match[1])).map(match => {
const before = code.slice(0, match.index).trimEnd();
@@ -277,7 +269,7 @@ export function sourceLaunchesBrowser(source, reviewedExecutors = []) {
const commandEnd = expressionEnd(code, start + 1, false);
if (commandEnd === null) return true;
const command = code.slice(start + 1, commandEnd);
- if (unresolvedCommandValue(command)
+ if (unresolvedCommandValue(command) || callableCommand(command)
|| [...command.matchAll(WORD)].some(word => namedBrowser(word[0], new Set(), true))) return true;
}
}