From 3e565f4bf638bee3eb19e822ed52758118a3dbf6 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Tue, 1 Dec 2026 22:17:07 +0800 Subject: [PATCH] refactor(javascript): share Electron graph edge projection (#15479) --- .../ElectronBoundaryGraphContext.ts | 88 ++++++++----------- 1 file changed, 38 insertions(+), 50 deletions(-) diff --git a/src/application/javascript/ElectronBoundaryGraphContext.ts b/src/application/javascript/ElectronBoundaryGraphContext.ts index c21748f61ed..b52f38d002d 100644 --- a/src/application/javascript/ElectronBoundaryGraphContext.ts +++ b/src/application/javascript/ElectronBoundaryGraphContext.ts @@ -40,21 +40,33 @@ export interface ElectronGraphEdgeInput { export const ELECTRON_CANDIDATE_LIMITATION = "The Electron module loader is unknown, so this relationship is only a candidate."; +type ElectronEvidenceInput = Parameters[0] & + Pick[0], "confidence">; + +const electronRelationshipEvidence = ( + input: ElectronEvidenceInput, + loaderAuthority: ElectronGraphEdgeInput["loaderAuthority"], + kind: "observation" | "inference", +) => { + if (loaderAuthority === "candidate") + return staticInferenceEvidence({ + ...input, + confidence: "low", + limitations: [ + ...(input.limitations ?? []), + ELECTRON_CANDIDATE_LIMITATION, + ], + }); + return kind === "observation" + ? astObservationEvidence(input) + : staticInferenceEvidence(input); +}; + /** Preserve syntax with uncertainty when its Electron receiver is unproven. */ export const electronFindingEvidence = ( input: Parameters[0], - loaderAuthority: "candidate" | undefined, -) => - loaderAuthority === "candidate" - ? staticInferenceEvidence({ - ...input, - confidence: "low", - limitations: [ - ...(input.limitations ?? []), - ELECTRON_CANDIDATE_LIMITATION, - ], - }) - : astObservationEvidence(input); + loaderAuthority: ElectronGraphEdgeInput["loaderAuthority"], +) => electronRelationshipEvidence(input, loaderAuthority, "observation"); /** Resolve the file or recovered bundle module that owns one finding. */ export const electronFindingSourceNode = ( @@ -81,10 +93,10 @@ export const electronObservationIdentity = ( scope, }); -/** Add a direct AST syntax relationship. */ -export const addElectronAstEdge = ( +const addElectronEdge = ( context: JavaScriptArtifactGraphContext, input: ElectronGraphEdgeInput, + kind: "observation" | "inference", ): void => { context.accumulator.addEdge({ source_node_id: input.source.node_id, @@ -96,58 +108,34 @@ export const addElectronAstEdge = ( ? { loader_authority: "candidate" } : {}), }, - evidence: electronFindingEvidence( + evidence: electronRelationshipEvidence( { sha256: input.file.sha256, path: input.file.path, range: input.range, operation: input.operation, coverage: input.coverage, + ...(kind === "inference" && input.confidence !== undefined + ? { confidence: input.confidence } + : {}), ...(input.limitations === undefined ? {} : { limitations: input.limitations }), }, input.loaderAuthority, + kind, ), }); }; +/** Add a direct AST syntax relationship. */ +export const addElectronAstEdge = ( + context: JavaScriptArtifactGraphContext, + input: ElectronGraphEdgeInput, +): void => addElectronEdge(context, input, "observation"); + /** Add an Electron relationship that static syntax suggests but cannot prove. */ export const addElectronInferenceEdge = ( context: JavaScriptArtifactGraphContext, input: ElectronGraphEdgeInput, -): void => { - context.accumulator.addEdge({ - source_node_id: input.source.node_id, - target_node_id: input.target.node_id, - relation: input.relation, - properties: { - ...input.properties, - ...(input.loaderAuthority === "candidate" - ? { loader_authority: "candidate" } - : {}), - }, - evidence: staticInferenceEvidence({ - sha256: input.file.sha256, - path: input.file.path, - range: input.range, - operation: input.operation, - coverage: input.coverage, - ...(input.loaderAuthority === "candidate" - ? { confidence: "low" as const } - : input.confidence === undefined - ? {} - : { confidence: input.confidence }), - ...(input.loaderAuthority === "candidate" - ? { - limitations: [ - ...(input.limitations ?? []), - ELECTRON_CANDIDATE_LIMITATION, - ], - } - : input.limitations === undefined - ? {} - : { limitations: input.limitations }), - }), - }); -}; +): void => addElectronEdge(context, input, "inference");