diff --git a/src/domain/electronStaticAnalysisBrowser.ts b/src/domain/electronStaticAnalysisBrowser.ts index 3287455cf..ff28c0ed2 100644 --- a/src/domain/electronStaticAnalysisBrowser.ts +++ b/src/domain/electronStaticAnalysisBrowser.ts @@ -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({ diff --git a/src/domain/electronStaticAnalysisValues.ts b/src/domain/electronStaticAnalysisValues.ts index be15bca35..2ed6551fd 100644 --- a/src/domain/electronStaticAnalysisValues.ts +++ b/src/domain/electronStaticAnalysisValues.ts @@ -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; diff --git a/src/domain/javascriptSemanticMemberMutations.ts b/src/domain/javascriptSemanticMemberMutations.ts index 495b779f9..4991f23e7 100644 --- a/src/domain/javascriptSemanticMemberMutations.ts +++ b/src/domain/javascriptSemanticMemberMutations.ts @@ -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; -}; diff --git a/src/domain/javascriptSemanticProjection.ts b/src/domain/javascriptSemanticProjection.ts index f856700a2..c44b95f6e 100644 --- a/src/domain/javascriptSemanticProjection.ts +++ b/src/domain/javascriptSemanticProjection.ts @@ -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); diff --git a/src/domain/javascriptSemanticValues.ts b/src/domain/javascriptSemanticValues.ts index 41085aab3..655c41ba7 100644 --- a/src/domain/javascriptSemanticValues.ts +++ b/src/domain/javascriptSemanticValues.ts @@ -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, diff --git a/src/domain/webBundleAnalyzer.test.ts b/src/domain/webBundleAnalyzer.test.ts index 16399bd8c..f91f06569 100644 --- a/src/domain/webBundleAnalyzer.test.ts +++ b/src/domain/webBundleAnalyzer.test.ts @@ -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 {};")); diff --git a/src/domain/webBundleAnalyzerAst.ts b/src/domain/webBundleAnalyzerAst.ts index a8f2ae25b..70b1b1ebf 100644 --- a/src/domain/webBundleAnalyzerAst.ts +++ b/src/domain/webBundleAnalyzerAst.ts @@ -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; -}; diff --git a/src/domain/webBundleAnalyzerInspection.ts b/src/domain/webBundleAnalyzerInspection.ts index 6328bc211..85eb2d011 100644 --- a/src/domain/webBundleAnalyzerInspection.ts +++ b/src/domain/webBundleAnalyzerInspection.ts @@ -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 }); }