diff --git a/src/application/environment/environmentStateBulkAnswer.ts b/src/application/environment/environmentStateBulkAnswer.ts index b0d0894a6b8..2865a9f7c11 100644 --- a/src/application/environment/environmentStateBulkAnswer.ts +++ b/src/application/environment/environmentStateBulkAnswer.ts @@ -87,6 +87,7 @@ const namedTargets = ( supplied: false, })); const targets: BulkTarget[] = []; + const seen = new Set(); for (const { id, body: members } of named) { // A body's id is the entity's as written, never a percent-encoded spelling of another. const entity = namedEntity(serving.store, type, { @@ -101,13 +102,14 @@ const namedTargets = ( `The state holds no ${type} entity ${id}; the state is unchanged.`, ), ); - if (targets.some((target) => target.entity === entity)) + if (seen.has(entity)) return err( refused( tool, `The body names ${type} entity ${entity.id} more than once; the state is unchanged.`, ), ); + seen.add(entity); targets.push({ entity, body: members }); } return ok(targets); diff --git a/src/application/environment/environmentStateDocument.ts b/src/application/environment/environmentStateDocument.ts index 6877f97b8af..ac4579a63ec 100644 --- a/src/application/environment/environmentStateDocument.ts +++ b/src/application/environment/environmentStateDocument.ts @@ -290,11 +290,12 @@ const documentsProblem = ( state: EnvironmentState, documents: ParsedDocument["documents"], ): AnalysisInputIssue | undefined => { + const initialByTool = new Map( + state.documents.map((candidate) => [candidate.tool, candidate]), + ); const tools = new Set(); for (const [index, document] of documents.entries()) { - const initial = state.documents.find( - (candidate) => candidate.tool === document.tool, - ); + const initial = initialByTool.get(document.tool); if ( state.views.get(document.tool)?.shape !== "document" || initial === undefined || @@ -379,19 +380,22 @@ const heldContent = ( record_ordinals: unchanged ? recorded.record_ordinals : [], }; }), - documents: document.documents.map(({ tool, path, value }) => { - const recorded = state.documents.find( - (candidate) => candidate.tool === tool, + documents: (() => { + const recordedByTool = new Map( + state.documents.map((candidate) => [candidate.tool, candidate]), ); - return { - tool, - path, - value, - ...(recorded !== undefined && same(value, recorded.value) - ? { record_ordinal: recorded.record_ordinal } - : {}), - }; - }), + return document.documents.map(({ tool, path, value }) => { + const recorded = recordedByTool.get(tool); + return { + tool, + path, + value, + ...(recorded !== undefined && same(value, recorded.value) + ? { record_ordinal: recorded.record_ordinal } + : {}), + }; + }); + })(), }); /** diff --git a/src/application/javascript/ElectronBoundaryAnalysis.ts b/src/application/javascript/ElectronBoundaryAnalysis.ts index ecb6ea8bc00..8bf11056536 100644 --- a/src/application/javascript/ElectronBoundaryAnalysis.ts +++ b/src/application/javascript/ElectronBoundaryAnalysis.ts @@ -73,19 +73,24 @@ export const createElectronIpcContext = ( .map((transmission) => { const owner = owners.get(transmission.key); const matching = matchingMainHandlers(handlerIndex, transmission.finding); - const candidates = matching.filter( - (handler) => owners.get(handler.key) === owner, - ); + const candidates: ElectronIpcRecord[] = []; + let hasCandidateAuthority = false; + let hasUnknownOwner = false; + for (const handler of matching) { + const handlerOwner = owners.get(handler.key); + if (handlerOwner === owner) candidates.push(handler); + if (handler.finding.loader_authority === "candidate") + hasCandidateAuthority = true; + if (handlerOwner == null) hasUnknownOwner = true; + } // Unknown ownership can conceal competing dispatch contexts. Different owners // do not prove runtime isolation, so retain ambiguity rather than absence. const uncertain = !coverageComplete || owner == null || transmission.finding.loader_authority === "candidate" || - matching.some( - ({ finding }) => finding.loader_authority === "candidate", - ) || - matching.some((handler) => owners.get(handler.key) == null); + hasCandidateAuthority || + hasUnknownOwner; return { transmission, candidates, @@ -96,7 +101,7 @@ export const createElectronIpcContext = ( candidates.length === 1 && candidates[0]?.finding.loader_authority !== "candidate" && isExactIpcHandler(candidates[0]) && - !matching.some((handler) => owners.get(handler.key) == null) + !hasUnknownOwner ? (candidates[0] ?? null) : null, status: @@ -215,6 +220,15 @@ const electronModuleReferenced = ( analysis: JavaScriptArtifactAnalysis, ): boolean => analysis.files.some(electronModuleReferencedBy); +const countWhere = ( + items: readonly T[], + predicate: (item: T) => boolean, +): number => { + let count = 0; + for (const item of items) if (predicate(item)) count++; + return count; +}; + /** * Every Electron boundary fact family the graph reports. A rejected * application projection keeps its static facts on the file node only, so @@ -228,22 +242,32 @@ export const electronBoundaryFactCount = ( count + (javascript === null || application_projection_failure !== undefined ? 0 - : javascript.electron.browser_windows.filter(provenElectronFinding) - .length + - javascript.electron.context_bridge_apis.filter(provenElectronFinding) - .length + - javascript.electron.ipc.filter(provenElectronFinding).length + - javascript.electron.sender_validations.filter(provenElectronFinding) - .length + - javascript.electron.utility_processes.filter(provenElectronFinding) - .length + - javascript.electron.native_addon_bindings.filter( + : countWhere( + javascript.electron.browser_windows, provenElectronFinding, - ).length + - javascript.electron.protocols.filter(provenElectronFinding).length + - (javascript.electron.dynamic_code ?? []).filter( + ) + + countWhere( + javascript.electron.context_bridge_apis, + provenElectronFinding, + ) + + countWhere(javascript.electron.ipc, provenElectronFinding) + + countWhere( + javascript.electron.sender_validations, + provenElectronFinding, + ) + + countWhere( + javascript.electron.utility_processes, + provenElectronFinding, + ) + + countWhere( + javascript.electron.native_addon_bindings, + provenElectronFinding, + ) + + countWhere(javascript.electron.protocols, provenElectronFinding) + + countWhere( + javascript.electron.dynamic_code ?? [], isPossibleCachedDataCompilation, - ).length), + )), 0, ); diff --git a/src/application/javascript/JavaScriptArtifactGraphFindings.ts b/src/application/javascript/JavaScriptArtifactGraphFindings.ts index 02ad688057e..3fb1e0d9d64 100644 --- a/src/application/javascript/JavaScriptArtifactGraphFindings.ts +++ b/src/application/javascript/JavaScriptArtifactGraphFindings.ts @@ -963,16 +963,19 @@ const resolveBundlerChunkReference = ( if (key === null) return null; const file = context.filesByPath.get(sourcePath); if (file === undefined) return null; - const registrations = context.analysis.files - .find(({ file: analyzedFile }) => analyzedFile.path === sourcePath) - ?.javascript?.bundler_registrations.filter(({ runtime, modules }) => - reference.module_runtime === undefined - ? reference.module_key === null - : runtime === reference.module_runtime && - modules.some( - ({ module_key: moduleKey }) => moduleKey === reference.module_key, - ), - ); + const registrations = ( + context.analyzedFilesByPath?.get(sourcePath) ?? + context.analysis.files.find( + ({ file: analyzedFile }) => analyzedFile.path === sourcePath, + ) + )?.javascript?.bundler_registrations.filter(({ runtime, modules }) => + reference.module_runtime === undefined + ? reference.module_key === null + : runtime === reference.module_runtime && + modules.some( + ({ module_key: moduleKey }) => moduleKey === reference.module_key, + ), + ); if (registrations === undefined) return null; const candidates = registrations.flatMap(({ runtime }) => { const node = context.chunkNodes.get(