refactor(domain): consolidate property-key extraction behind semanticStaticPropertyKey (#823)

Inline the private memberKey/propertyKey wrappers in semantic
MemberMutations/Projection/Values to direct canonical calls.
Migrate the electron Browser/Values member keys off legacy
propertyName (provably identical: explicit computed guard retained).
Delegate bundle calleeName/endpointArgument/stringArgument to the
static helpers, adding loadURL endpoint parity with static analysis.

Bundle regressions pin nested-dynamic-receiver and loadURL parity.
Remaining computed-identifier forks in electron Native/Ipc tracked
in #822.
This commit is contained in:
morluto
2026-10-07 11:13:59 +08:00
committed by GitHub
parent 75f50d86f6
commit 764ff05231
8 changed files with 54 additions and 84 deletions
+2 -2
View File
@@ -20,11 +20,11 @@ import { addLocatedFinding } from "./javascriptStaticAnalysisFindings.js";
import {
argumentNode,
calleeName,
propertyName,
range,
staticPath,
staticPathResolutionContext,
} from "./javascriptStaticAnalysisHelpers.js";
import { semanticStaticPropertyName } from "./javascriptAstValues.js";
import type { JavaScriptFindingContext } from "./javascriptStaticAnalysisState.js";
/** Inspect BrowserWindow, contextBridge, and utility-process syntax. */
@@ -115,7 +115,7 @@ const collectWebPreferences = (
});
continue;
}
const name = propertyName(property.key);
const name = semanticStaticPropertyName(property.key, property.computed);
if (name === "" || property.computed) {
unknown += 1;
preferences.push({
+2 -5
View File
@@ -3,10 +3,7 @@ import * as t from "@babel/types";
import { compareCodePoints } from "./canonicalOrdering.js";
import type { ElectronStaticValue } from "./electronStaticAnalysisTypes.js";
import { semanticStaticPropertyName } from "./javascriptAstValues.js";
import {
propertyName,
sourceSlice,
} from "./javascriptStaticAnalysisHelpers.js";
import { sourceSlice } from "./javascriptStaticAnalysisHelpers.js";
/** Preserve one literal value or the exact inert expression. */
export const electronStaticValue = (
@@ -128,7 +125,7 @@ const collectMembersAt = (
state.unknown += 1;
continue;
}
const name = propertyName(property.key);
const name = semanticStaticPropertyName(property.key, property.computed);
if (name === "" || property.computed) {
state.unknown += 1;
continue;
+14 -12
View File
@@ -6,7 +6,10 @@ import {
} from "./javascriptSemanticState.js";
import { evaluateSemanticBinding } from "./javascriptSemanticValues.js";
import { traverseJavaScriptAst } from "./javascriptSemanticTraversal.js";
import { semanticStaticPropertyName } from "./javascriptAstValues.js";
import {
semanticStaticPropertyKey,
semanticStaticPropertyName,
} from "./javascriptAstValues.js";
type PropertyPath = readonly (string | number | null)[];
@@ -36,7 +39,11 @@ export const collectSemanticMemberMutations = (
return;
}
if (t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) {
markValue(node.object, [propertyKey(node), ...path], bindings);
markValue(
node.object,
[semanticStaticPropertyKey(node.property, node.computed), ...path],
bindings,
);
return;
}
for (const value of referencedValues(node, path))
@@ -44,7 +51,11 @@ export const collectSemanticMemberMutations = (
};
const markTarget = (node: t.Node): void => {
if (t.isMemberExpression(node) || t.isOptionalMemberExpression(node))
markValue(node.object, [propertyKey(node)], new Set());
markValue(
node.object,
[semanticStaticPropertyKey(node.property, node.computed)],
new Set(),
);
else if (t.isRestElement(node)) markTarget(node.argument);
else if (t.isAssignmentPattern(node)) markTarget(node.left);
else if (t.isArrayPattern(node)) {
@@ -129,12 +140,3 @@ const referencedValues = (
];
return [];
};
const propertyKey = (
node: t.MemberExpression | t.OptionalMemberExpression,
): string | null => {
const name = semanticStaticPropertyName(node.property, node.computed);
return name !== "" || t.isStringLiteral(node.property, { value: "" })
? name
: null;
};
+4 -8
View File
@@ -618,7 +618,7 @@ const commonJsExportName = (
if (isUnshadowedGlobal(node, state, "exports")) return "default";
if (!t.isMemberExpression(node) && !t.isOptionalMemberExpression(node))
return undefined;
const key = memberKey(node);
const key = semanticStaticPropertyKey(node.property, node.computed);
// `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.
@@ -627,18 +627,14 @@ const commonJsExportName = (
if (
t.isMemberExpression(node.object) &&
isUnshadowedGlobal(node.object.object, state, "module") &&
memberKey(node.object) === "exports"
semanticStaticPropertyKey(node.object.property, node.object.computed) ===
"exports"
)
return key === null ? "*" : key || "default";
if (
isUnshadowedGlobal(node.object, state, "module") &&
memberKey(node) === "exports"
semanticStaticPropertyKey(node.property, node.computed) === "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 => semanticStaticPropertyKey(node.property, node.computed);
+6 -10
View File
@@ -255,7 +255,8 @@ const evaluateMember = (
): JavaScriptSemanticValue => {
if (!t.isNode(node.object))
return { status: "unknown", reason: "Unsupported member base." };
const key = memberKey(node);
const key =
semanticStaticPropertyKey(node.property, node.computed) ?? undefined;
if (key === undefined)
return { status: "unknown", reason: "Dynamic member key." };
return projectValue(evaluateExpression(node.object, nestedContext(context)), [
@@ -415,7 +416,8 @@ const provenanceForExpression = (
"unknown",
"Unsupported member base.",
);
const member = memberKey(node);
const member =
semanticStaticPropertyKey(node.property, node.computed) ?? undefined;
if (typeof member !== "string")
return semanticUnresolvedProvenance(
"unknown",
@@ -459,7 +461,8 @@ const requireOrigin = (
if (t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) {
if (!t.isNode(node.object)) return undefined;
const nested = requireOrigin(node.object, state);
const member = memberKey(node);
const member =
semanticStaticPropertyKey(node.property, node.computed) ?? undefined;
return nested === undefined || typeof member !== "string"
? undefined
: { ...nested, importedPath: [...nested.importedPath, member] };
@@ -491,13 +494,6 @@ const mergeValues = (
: { status: "ambiguous", reason: "Branches have incompatible values." };
};
const memberKey = (
node: t.MemberExpression | t.OptionalMemberExpression,
): string | number | undefined => {
const name = semanticStaticPropertyKey(node.property, node.computed);
return name ?? undefined;
};
const nestedContext = (
context: EvaluationContext,
bindingId?: string,
+20
View File
@@ -164,6 +164,26 @@ describe("web bundle analyzer", () => {
});
});
describe("web bundle static-analysis parity", () => {
it("recognizes endpoints through nested dynamic receivers like static analysis", () => {
const result = analyzeCapturedWebBundle(
inspection('window[recv].fetch("/nested-fetch");'),
);
expect(result.observations.endpoints).toContainEqual(
expect.objectContaining({ value: "/nested-fetch" }),
);
});
it("recognizes loadURL endpoints like static analysis", () => {
const result = analyzeCapturedWebBundle(
inspection('window.win.loadURL("https://embedded.test/app");'),
);
expect(result.observations.endpoints).toContainEqual(
expect.objectContaining({ value: "https://embedded.test/app" }),
);
});
});
describe("web bundle artifact metadata", () => {
it("preserves long provider media types in artifact summaries", () => {
const result = analyzeCapturedWebBundle(inspection("export {};"));
+1 -44
View File
@@ -3,27 +3,7 @@ import * as t from "@babel/types";
import { sanitizeBrowserUrl } from "./browserObservation.js";
import { semanticStaticPropertyName } from "./javascriptAstValues.js";
/** Name of a call/new callee from its AST node. */
export const calleeName = (callee: t.Node): string => {
if (t.isIdentifier(callee)) return callee.name;
if (t.isImport(callee)) return "import";
if (t.isMemberExpression(callee) || t.isOptionalMemberExpression(callee)) {
const object = t.isExpression(callee.object)
? calleeName(callee.object)
: "";
const property = semanticStaticPropertyName(
callee.property,
callee.computed,
);
if (property === "") return "";
return object === "" ? property : `${object}.${property}`;
}
return "";
};
export const stringArgument = (
value: t.Node | null | undefined,
): string | undefined => (t.isStringLiteral(value) ? value.value : undefined);
/** Pick the endpoint argument for common network-call patterns. */
export const objectValue = (
object: t.ObjectExpression,
@@ -83,26 +63,3 @@ export const locationFields = (node: t.Node) => ({
line: node.loc?.start.line ?? null,
column: node.loc?.start.column ?? null,
});
/** Pick the endpoint argument for common network-call patterns. */
export const endpointArgument = (
name: string,
args: readonly (
| t.Expression
| t.SpreadElement
| t.JSXNamespacedName
| t.ArgumentPlaceholder
)[],
): string | undefined => {
if (name === "fetch" || name.endsWith(".fetch") || name === "WebSocket")
return stringArgument(args[0]);
if (name.endsWith(".open") && stringArgument(args[1]) !== undefined)
return stringArgument(args[1]);
if (
["get", "post", "put", "patch", "delete", "request"].some(
(method) => name === method || name.endsWith(`.${method}`),
)
)
return stringArgument(args[0]);
return undefined;
};
+5 -3
View File
@@ -8,13 +8,15 @@ import { semanticStaticPropertyName } from "./javascriptAstValues.js";
import {
calleeName,
endpointArgument,
stringValue,
} from "./javascriptStaticAnalysisHelpers.js";
import {
location,
locationFields,
objectString,
objectValue,
resolveSpecifier,
isUrlLikeModuleSpecifier,
stringArgument,
} from "./webBundleAnalyzerAst.js";
type BundleObservations = WebBundleAnalysis["observations"];
@@ -109,7 +111,7 @@ const inspectCall = (
accumulator: AnalysisAccumulator,
): void => {
const name = calleeName(node.callee);
const first = stringArgument(node.arguments[0]);
const first = stringValue(node.arguments[0]);
if ((name === "require" || name.endsWith(".require")) && first !== undefined)
addEdge({
script,
@@ -120,7 +122,7 @@ const inspectCall = (
});
if (name === "importScripts" || name.endsWith(".importScripts"))
for (const argument of node.arguments) {
const specifier = stringArgument(argument);
const specifier = stringValue(argument);
if (specifier === undefined) continue;
addEdge({ script, specifier, kind: "worker_import", node, accumulator });
}