Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(javascript): honor computed keys when naming callables, exports, requests and options (#536)

morluto4c8dc39f439b
diff --git a/src/domain/javascriptSemanticAnalysis.runtime_boundaries.test.ts b/src/domain/javascriptSemanticAnalysis.runtime_boundaries.test.ts
index efb0555f2..03e25032a 100644
--- a/src/domain/javascriptSemanticAnalysis.runtime_boundaries.test.ts
+++ b/src/domain/javascriptSemanticAnalysis.runtime_boundaries.test.ts
@@ -84,6 +84,24 @@ describe("JavaScript semantic analysis: runtime boundaries 1", () => {
expect(ir.timerOperations).toEqual([]);
});
+ it("does not invent request fields or option keys from dynamic properties", () => {
+ const ir = analyzeJavaScriptSemantics(`
+ const key = resolveKey();
+ fetch("https://example.test", { [key]: 1, body: "x" });
+ spawn("/bin/tool", { [key]: vars });
+ `);
+ expect(
+ ir.requestOperations.flatMap(({ fields }) =>
+ fields.map(({ name }) => name),
+ ),
+ ).toEqual(["body"]);
+ expect(
+ ir.childProcessSpawns.some(
+ ({ environmentSupplied }) => environmentSupplied,
+ ),
+ ).toBe(false);
+ });
+
it("retains every added semantic effect family", () => {
const ir = analyzeJavaScriptSemantics(
`
diff --git a/src/domain/javascriptSemanticAnalysis.structure.test.ts b/src/domain/javascriptSemanticAnalysis.structure.test.ts
index d442e798d..98bad7ca4 100644
--- a/src/domain/javascriptSemanticAnalysis.structure.test.ts
+++ b/src/domain/javascriptSemanticAnalysis.structure.test.ts
@@ -319,6 +319,28 @@ describe("JavaScript semantic analysis: structure 2", () => {
);
});
+ it("does not invent export or method names from dynamic keys", () => {
+ const ir = analyzeJavaScriptSemantics(`
+ const key = resolveKey();
+ exports[key] = addon;
+ module.exports[key] = addon;
+ const target = { [key]() { return 1; } };
+ class Holder { [key]() { return 2; } }
+ `);
+ // The assignment is real, but its name is not knowable, so the only
+ // honest exported_name is the wildcard.
+ expect(
+ ir.moduleLinks
+ .filter(({ kind }) => kind === "commonjs-export")
+ .map(({ exportedName }) => exportedName),
+ ).toEqual(["*", "*"]);
+ const names = ir.callables.map(({ name }) => name);
+ expect(names).not.toContain("key");
+ expect(names.filter((name) => name?.startsWith("[computed@")).length).toBe(
+ 2,
+ );
+ });
+
it("keeps function, class, and method identities separate from bindings", () => {
const ir = analyzeJavaScriptSemantics(`
class Service {
diff --git a/src/domain/javascriptSemanticChildProcesses.ts b/src/domain/javascriptSemanticChildProcesses.ts
index 05bb0528d..4565c04ff 100644
--- a/src/domain/javascriptSemanticChildProcesses.ts
+++ b/src/domain/javascriptSemanticChildProcesses.ts
@@ -317,7 +317,9 @@ const objectProperty = (
object?.properties.find(
(property): property is t.ObjectMethod | t.ObjectProperty =>
(t.isObjectMethod(property) || t.isObjectProperty(property)) &&
- propertyName(property.key) === name,
+ // `{[env]: vars}` has a dynamic key; treating it as `env` would claim
+ // the caller supplied an environment when the real key is unknown.
+ semanticStaticPropertyName(property.key, property.computed) === name,
) ?? null;
const objectBinding = (
diff --git a/src/domain/javascriptSemanticDataEffects.ts b/src/domain/javascriptSemanticDataEffects.ts
index 782b77dde..ed33c33ec 100644
--- a/src/domain/javascriptSemanticDataEffects.ts
+++ b/src/domain/javascriptSemanticDataEffects.ts
@@ -370,7 +370,9 @@ const requestFields = (
if (!t.isObjectExpression(candidate)) return [];
return candidate.properties.flatMap((property) => {
if (!t.isObjectProperty(property)) return [];
- const name = propertyName(property.key);
+ // `{[key]: 1}` sends no knowable field name; reporting `key` would invent
+ // a request field that may not exist.
+ const name = semanticStaticPropertyName(property.key, property.computed);
if (name.length === 0) return [];
return [
{
diff --git a/src/domain/javascriptSemanticProjection.ts b/src/domain/javascriptSemanticProjection.ts
index 8856b11dc..377c249cf 100644
--- a/src/domain/javascriptSemanticProjection.ts
+++ b/src/domain/javascriptSemanticProjection.ts
@@ -226,7 +226,12 @@ const callableName = (node: t.Node, parent: t.Node | null): string | null => {
t.isObjectMethod(node)
) {
if (t.isPrivateName(node.key)) return `#${node.key.id.name}`;
- return propertyName(node.key) || `[computed@${String(node.start ?? -1)}]`;
+ // `{[key]() {}}` names nothing statically; reading `key` off the key node
+ // would report the variable name as the method name.
+ return (
+ semanticStaticPropertyName(node.key, node.computed) ||
+ `[computed@${String(node.start ?? -1)}]`
+ );
}
if (
parent !== null &&
@@ -490,18 +495,32 @@ const commonJsExportName = (node: t.Node): string | undefined => {
if (t.isIdentifier(node, { name: "exports" })) return "default";
if (!t.isMemberExpression(node) && !t.isOptionalMemberExpression(node))
return undefined;
- const name = propertyName(node.property);
- if (t.isIdentifier(node.object, { name: "exports" })) return name || "*";
+ const key = memberKey(node);
+ // `exports[key]`/`module.exports[key]` assign an export whose name is not
+ // knowable. Report the wildcard rather than the variable name, and never
+ // collapse it into `default`, which would claim a real default export.
+ if (key === null) return "*";
+ if (t.isIdentifier(node.object, { name: "exports" })) return key || "*";
if (
t.isMemberExpression(node.object) &&
t.isIdentifier(node.object.object, { name: "module" }) &&
- propertyName(node.object.property) === "exports"
+ memberKey(node.object) === "exports"
)
- return name || "default";
+ return key || "default";
if (
t.isIdentifier(node.object, { name: "module" }) &&
- propertyName(node.property) === "exports"
+ memberKey(node) === "exports"
)
return "default";
return undefined;
};
+
+/** Exact member key, or null when a computed key commits no name. */
+const memberKey = (
+ node: t.MemberExpression | t.OptionalMemberExpression,
+): string | null =>
+ node.computed &&
+ !t.isStringLiteral(node.property) &&
+ !t.isNumericLiteral(node.property)
+ ? null
+ : propertyName(node.property);
diff --git a/src/domain/javascriptStaticAnalysisHelpers.ts b/src/domain/javascriptStaticAnalysisHelpers.ts
index 560243211..6409148aa 100644
--- a/src/domain/javascriptStaticAnalysisHelpers.ts
+++ b/src/domain/javascriptStaticAnalysisHelpers.ts
@@ -296,12 +296,24 @@ const calleeNameAt = (node: t.Node): string => {
if (t.isImport(node)) return "import";
if (t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) {
const object = t.isNode(node.object) ? calleeNameAt(node.object) : "";
- const property = propertyName(node.property);
+ // A dynamic key commits no name: `target[method]()` must not be reported as
+ // `target.method`, which would fabricate a runtime call shape.
+ const property = memberPropertyName(node);
return object === "" ? property : `${object}.${property}`;
}
return "";
};
+/** Read a member property only when its syntax commits an exact name. */
+const memberPropertyName = (
+ node: t.MemberExpression | t.OptionalMemberExpression,
+): string =>
+ node.computed &&
+ !t.isStringLiteral(node.property) &&
+ !t.isNumericLiteral(node.property)
+ ? `[computed@${String(node.property.start ?? -1)}]`
+ : propertyName(node.property);
+
/** Return a string literal value without evaluating an expression. */
export const stringValue = (
node: t.Node | null | undefined,