Skip to content

Commit b58adaf

Browse files
committed
fix(javascript): align module lookup and source evidence
Preserve CommonJS literal paths and confined ESM URL semantics, including package main fallback, directory intent, Unicode identity, and present opaque modules. Parse extraction inputs at their AST/source owner, retain skipped HTML and source-map coverage, and distinguish endpoint candidates from proven receiver facts. Version structural fingerprints with computed literal contents and propagate them through worker contracts. Reviewed fixes include #1810, #1840, #2642, #2700, #2702, #2714, #2803, #2960, #2968, #2986, #3035, and #3036. Validation: 158 resolver tests passed; public CLI/MCP module lookup controls, package-main/Unicode workflows, and six builtin workflows passed in the current compiled lane. Worker schema/fingerprint regressions passed. Large public pdf.js completion and real Electron analysis remain unverified.
1 parent 04672f2 commit b58adaf

63 files changed

Lines changed: 4065 additions & 325 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/javascript-artifact-reconstruction.md‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,14 @@ suffix. Utility calls such as `useUtils().invoice.list.fetch()`, `useQueries`,
282282
and links whose callee was renamed by a minifier are not recognized.
283283

284284
Each recovered bundle module retains the exact factory-source digest. A complete
285-
bounded AST also receives a `babel-ast-v1` structural fingerprint that ignores
285+
bounded AST also receives a `babel-ast-v2` structural fingerprint that ignores
286286
ordinary identifier names while retaining syntax, literals, operators, object
287-
keys, and member properties. If fingerprint construction reaches its structural
287+
keys, and member properties. Computed member identifiers retain their access
288+
syntax without treating a local variable's spelling as a fixed property; template
289+
literal contents remain part of the fingerprint. Version comparison keeps the
290+
algorithm in its matching key, so retained `babel-ast-v1` fingerprints cannot match
291+
these new fingerprints. Re-analyze both snapshots to compare their new structure.
292+
If fingerprint construction reaches its structural
288293
bound, the fingerprint is unavailable and its status is `truncated`; a bounded
289294
prefix is never presented as a complete stable fingerprint.
290295

@@ -619,3 +624,11 @@ Unresolved HTML references retain their declaration, source range, and resolutio
619624
reason in the renderer observations.
620625
HTML script source ranges follow the HTML parser across LF, CRLF, and bare CR
621626
line endings, preserving UTF-16 columns.
627+
Inline JavaScript in local HTML, meaning classic or module `<script>` bodies
628+
without a `src` attribute, is not analyzed. Each HTML file that has one gets an
629+
unknown `parse-html-inline-scripts` scope with the exact body count and the
630+
script type and source range of the first 100 bodies; beyond that, coverage is
631+
truncated under the `html_inline_script_locations_per_file` limit, and the
632+
scope's evidence counts the omitted locations. The graph names the
633+
file and up to 10 starting lines in its limitations, and coverage is partial. Import maps, speculation rules, data blocks, and blank
634+
bodies are not JavaScript and are not reported.

‎src/application/javascript/ElectronBoundaryAnalysis.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export const unambiguousElectronIpcPairings = (
8383
/** Summarize static Electron findings without treating gaps as absence. */
8484
export const summarizeElectronBoundaries = (
8585
analysis: JavaScriptModuleArtifactAnalysis,
86+
directoryPaths?: ReadonlySet<string>,
8687
): ElectronBoundarySummary => {
8788
const files = new Map(analysis.files.map(({ file }) => [file.path, file]));
8889
const javascript = analysis.files.flatMap(({ javascript: value }) =>
@@ -163,6 +164,7 @@ export const summarizeElectronBoundaries = (
163164
specifier: finding.module_path,
164165
context: finding.module_resolution_context,
165166
files,
167+
directoryPaths,
166168
runtimePathKind: "node-entrypoint",
167169
}),
168170
).length,
@@ -173,6 +175,7 @@ export const summarizeElectronBoundaries = (
173175
specifier: finding.specifier,
174176
context: "module-specifier",
175177
files,
178+
directoryPaths,
176179
kind: "native-addon",
177180
moduleKind: finding.module_kind,
178181
}),
@@ -206,6 +209,7 @@ interface ResolveToFileInput {
206209
readonly specifier: string;
207210
readonly context: "module-specifier" | "filesystem-expression";
208211
readonly files: ReadonlyMap<string, JavaScriptArtifactFile>;
212+
readonly directoryPaths?: ReadonlySet<string> | undefined;
209213
readonly kind?: JavaScriptArtifactFile["kind"];
210214
readonly moduleKind?: "import" | "require";
211215
readonly runtimePathKind?: ResolveArtifactPathInput["runtimePathKind"];
@@ -217,6 +221,9 @@ const resolvesToFile = (input: ResolveToFileInput): boolean => {
217221
sourcePath: input.file.path,
218222
context: input.context,
219223
files: input.files,
224+
...(input.directoryPaths === undefined
225+
? {}
226+
: { directoryPaths: input.directoryPaths }),
220227
...(input.runtimePathKind === undefined
221228
? {}
222229
: { runtimePathKind: input.runtimePathKind }),

‎src/application/javascript/ElectronBoundaryGraphNative.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const addNativeBinding = (input: NativeBindingInput): void => {
5050
file.path,
5151
context.filesByPath,
5252
value.module_kind,
53+
context.directoryPaths,
5354
);
5455
const addon = resolved === null ? undefined : context.fileNodes.get(resolved);
5556
const binding = context.accumulator.addNode({

‎src/application/javascript/ElectronBoundaryGraphWindows.ts‎

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ const addWindowPreload = (
140140
sourcePath: input.file.path,
141141
context: resolutionContext,
142142
files: input.context.filesByPath,
143+
directoryPaths: input.context.directoryPaths,
144+
unanalyzedFiles: input.context.unanalyzedFiles,
143145
runtimePathKind: "exact-file",
144146
});
145147
const role = createElectronRoleNode(input.context, {
@@ -232,6 +234,23 @@ const addUtilityProcess = (
232234
input: FindingInput<ElectronUtilityProcessFinding>,
233235
): void => {
234236
const { context, file, value, coverage } = input;
237+
const resolution =
238+
value.module_path === null
239+
? undefined
240+
: resolveArtifactPathByContext({
241+
declaredPath: value.module_path,
242+
sourcePath: file.path,
243+
context: value.module_resolution_context,
244+
files: context.filesByPath,
245+
directoryPaths: context.directoryPaths,
246+
unanalyzedFiles: context.unanalyzedFiles,
247+
runtimePathKind: "node-entrypoint",
248+
});
249+
const resolutionProperties = {
250+
resolved_path: resolution?.resolved_path ?? null,
251+
resolution_status: resolution?.resolution_status ?? null,
252+
...artifactPathResolutionProperties(resolution),
253+
};
235254
const utility = context.accumulator.addNode({
236255
kind: "electron-utility",
237256
identity: artifactLocalIdentity(
@@ -256,18 +275,24 @@ const addUtilityProcess = (
256275
coverage,
257276
}),
258277
},
278+
...(resolution === undefined
279+
? []
280+
: [
281+
{
282+
label: "utility process entrypoint resolution",
283+
properties: resolutionProperties,
284+
evidence: staticInferenceEvidence({
285+
sha256: file.sha256,
286+
path: file.path,
287+
range: value.location,
288+
operation: "resolve-utility-process-entrypoint",
289+
coverage,
290+
limitations: resolution.limitations,
291+
}),
292+
},
293+
]),
259294
],
260295
});
261-
const resolution =
262-
value.module_path === null
263-
? undefined
264-
: resolveArtifactPathByContext({
265-
declaredPath: value.module_path,
266-
sourcePath: file.path,
267-
context: value.module_resolution_context,
268-
files: context.filesByPath,
269-
runtimePathKind: "node-entrypoint",
270-
});
271296
addElectronInferenceEdge(context, {
272297
source: input.source,
273298
target: utility,
@@ -276,18 +301,11 @@ const addUtilityProcess = (
276301
coverage,
277302
relation: "loads",
278303
operation: "map-utility-process-launch",
304+
limitations: resolution?.limitations ?? [],
279305
properties: {
280306
declared_path: value.module_path,
281307
expression: value.module_expression,
282-
...(resolution === undefined
283-
? {}
284-
: {
285-
resolution_context: resolution.resolution_context,
286-
resolved_path: resolution.resolved_path,
287-
resolution_status: resolution.resolution_status,
288-
limitations: resolution.limitations,
289-
...artifactPathResolutionProperties(resolution),
290-
}),
308+
...resolutionProperties,
291309
},
292310
});
293311
if (value.module_path === null) return;
@@ -311,6 +329,7 @@ const addUtilityProcess = (
311329
resolution_context: resolution.resolution_context,
312330
resolved_path: resolved,
313331
resolution_status: resolution.resolution_status,
332+
...artifactPathResolutionProperties(resolution),
314333
},
315334
});
316335
};

‎src/application/javascript/JavaScriptArtifactAnalysis.ts‎

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ import {
1717
flattenSourceMapLeaves,
1818
sourceMapJsonText,
1919
} from "../../domain/sourceMapEnvelope.js";
20-
import type {
21-
JavaScriptSourceRange,
22-
JavaScriptSourcePoint,
23-
JavaScriptStaticAnalysis,
24-
} from "../../domain/javascript/javascriptStaticAnalysisTypes.js";
20+
import type { JavaScriptStaticAnalysis } from "../../domain/javascript/javascriptStaticAnalysisTypes.js";
2521
import { failedJavaScriptStaticAnalysis } from "../../domain/javascript/javascriptStaticAnalysisHelpers.js";
2622
import type {
2723
JavaScriptArtifactFile,
@@ -32,6 +28,7 @@ import type {
3228
JavaScriptArtifactAnalysis,
3329
JavaScriptModuleArtifactAnalysis,
3430
JavaScriptModuleSemanticIr,
31+
JavaScriptHtmlInlineScriptsObservation,
3532
JavaScriptHtmlScriptObservation,
3633
JavaScriptJsonModuleObservation,
3734
JavaScriptPackageObservation,
@@ -58,6 +55,7 @@ interface MutableArtifactAnalysis<
5855
readonly packages: JavaScriptPackageObservation[];
5956
readonly jsonModules: JavaScriptJsonModuleObservation[];
6057
readonly htmlScripts: JavaScriptHtmlScriptObservation[];
58+
readonly htmlInlineScripts: JavaScriptHtmlInlineScriptsObservation[];
6159
readonly sourceMaps: JavaScriptSourceMapObservation[];
6260
visitedNodes: number;
6361
findings: number;
@@ -329,14 +327,22 @@ const finalizeArtifactAnalysis = <
329327
packages: state.packages,
330328
json_modules: state.jsonModules,
331329
html_scripts: state.htmlScripts,
330+
html_inline_scripts: state.htmlInlineScripts,
332331
source_maps: state.sourceMaps,
333332
visited_ast_nodes: state.visitedNodes,
334-
findings: state.findings + state.htmlScripts.length,
333+
findings:
334+
state.findings +
335+
state.htmlScripts.length +
336+
state.htmlInlineScripts.reduce(
337+
(count, { script_count }) => count + script_count,
338+
0,
339+
),
335340
modules: state.modules,
336341
parse_failures: state.parseFailures,
337342
limitations: [
338343
"JavaScript and HTML were parsed as inert text; bundle bootstrap code was never executed.",
339344
"Static paths and relationships may remain unresolved when expressions are dynamic or obfuscated.",
345+
...state.htmlInlineScripts.map(htmlInlineScriptLimitation),
340346
...state.files.flatMap(
341347
({ analysis_failure, application_projection_failure }) =>
342348
[analysis_failure, application_projection_failure].flatMap(
@@ -356,8 +362,13 @@ function* analyzeArtifactFileSteps<
356362
const { state } = context;
357363
if (classifyOutsideAnalysisScope(file, state)) return;
358364
addStructuredObservations(file, state);
359-
if (file.kind === "html" && file.text.included)
360-
state.htmlScripts.push(...parseHtmlScripts(file.path, file.text.value));
365+
if (file.kind === "html" && file.text.included) {
366+
const html = parseHtmlScripts(file.path, file.text.value);
367+
// Append without spreading: argument count is bounded by the engine.
368+
for (const script of html.scripts) state.htmlScripts.push(script);
369+
if (html.inlineScripts !== null)
370+
state.htmlInlineScripts.push(html.inlineScripts);
371+
}
361372
if (file.kind === "source-map") addSourceMap(file, context);
362373
if (file.kind !== "javascript" || !file.text.included) {
363374
state.files.push({ file, javascript: null, semantic: null });
@@ -443,6 +454,7 @@ const emptyArtifactAnalysis = <
443454
packages: [],
444455
jsonModules: [],
445456
htmlScripts: [],
457+
htmlInlineScripts: [],
446458
sourceMaps: [],
447459
visitedNodes: 0,
448460
findings: 0,
@@ -515,14 +527,31 @@ const unavailablePackage = (
515527
const parseHtmlScripts = (
516528
path: string,
517529
text: string,
518-
): JavaScriptHtmlScriptObservation[] => {
519-
const { scripts, baseHref } = htmlArtifactReferences(text);
520-
return scripts.map(({ scriptPath, startOffset, endOffset }) => ({
521-
html_path: path,
522-
script_path: scriptPath,
523-
base_href: baseHref,
524-
location: rangeForOffsets(text, startOffset, endOffset),
525-
}));
530+
): {
531+
readonly scripts: JavaScriptHtmlScriptObservation[];
532+
readonly inlineScripts: JavaScriptHtmlInlineScriptsObservation | null;
533+
} => {
534+
const { scripts, inlineScripts, inlineScriptCount, baseHref } =
535+
htmlArtifactReferences(text);
536+
return {
537+
scripts: scripts.map(({ scriptPath, location }) => ({
538+
html_path: path,
539+
script_path: scriptPath,
540+
base_href: baseHref,
541+
location,
542+
})),
543+
inlineScripts:
544+
inlineScriptCount === 0
545+
? null
546+
: {
547+
path,
548+
script_count: inlineScriptCount,
549+
scripts: inlineScripts.map(({ scriptType, location }) => ({
550+
script_type: scriptType,
551+
location,
552+
})),
553+
},
554+
};
526555
};
527556

528557
const parseSourceMap = (
@@ -599,6 +628,23 @@ const collectSourceMapOriginals = (
599628
};
600629
};
601630

631+
/** Name the HTML file and lines whose inline JavaScript was not analyzed. */
632+
export const htmlInlineScriptLimitation = ({
633+
path,
634+
script_count: count,
635+
scripts,
636+
}: JavaScriptHtmlInlineScriptsObservation): string => {
637+
const lines = scripts
638+
.slice(0, HTML_INLINE_SCRIPT_LIMITATION_LINES)
639+
.map(({ location }) => String(location.start.line));
640+
const unnamed = count - lines.length;
641+
const one = count === 1;
642+
return `${path} has ${String(count)} inline JavaScript ${one ? "body" : "bodies"} starting at line${one ? "" : "s"} ${lines.join(", ")}${unnamed > 0 ? ` and ${String(unnamed)} more` : ""} that ${one ? "was" : "were"} not analyzed; endpoints, storage, imports and other facts in ${one ? "it" : "them"} are absent from the evidence.`;
643+
};
644+
645+
// Keep the agent-facing limitation short; the unknown scope keeps the ranges.
646+
const HTML_INLINE_SCRIPT_LIMITATION_LINES = 10;
647+
602648
const unavailableSourceMap = (
603649
file: JavaScriptArtifactFile,
604650
): JavaScriptSourceMapObservation => {
@@ -638,23 +684,6 @@ const findingCount = (analysis: JavaScriptStaticAnalysis): number =>
638684
analysis.electron.utility_processes.length +
639685
analysis.electron.native_addon_bindings.length;
640686

641-
const rangeForOffsets = (
642-
text: string,
643-
start: number,
644-
end: number,
645-
): JavaScriptSourceRange => ({
646-
start: pointForOffset(text, start),
647-
end: pointForOffset(text, end),
648-
});
649-
650-
const pointForOffset = (
651-
text: string,
652-
offset: number,
653-
): JavaScriptSourcePoint => {
654-
const lines = text.slice(0, offset).split(/\r\n|\r|\n/u);
655-
return { line: lines.length, column: lines.at(-1)?.length ?? 0 };
656-
};
657-
658687
const optionalString = (value: unknown): string | null =>
659688
typeof value === "string" && value.length > 0 ? value : null;
660689

‎src/application/javascript/JavaScriptArtifactAnalysisTypes.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ export interface JavaScriptHtmlScriptObservation {
4545
readonly location: JavaScriptSourceRange;
4646
}
4747

48+
/** Bounded source locations for inline JavaScript bodies skipped by analysis. */
49+
export interface JavaScriptHtmlInlineScriptsObservation {
50+
readonly path: string;
51+
readonly script_count: number;
52+
readonly scripts: readonly {
53+
readonly script_type: "classic" | "module";
54+
readonly location: JavaScriptSourceRange;
55+
}[];
56+
}
57+
4858
/** One original source declared by a local source map. */
4959
export interface JavaScriptSourceMapOriginal {
5060
/** Index within the leaf map's sources and sourcesContent arrays. */
@@ -128,6 +138,7 @@ export interface JavaScriptArtifactAnalysis<
128138
readonly packages: readonly JavaScriptPackageObservation[];
129139
readonly json_modules: readonly JavaScriptJsonModuleObservation[];
130140
readonly html_scripts: readonly JavaScriptHtmlScriptObservation[];
141+
readonly html_inline_scripts: readonly JavaScriptHtmlInlineScriptsObservation[];
131142
readonly source_maps: readonly JavaScriptSourceMapObservation[];
132143
readonly visited_ast_nodes: number;
133144
readonly findings: number;

0 commit comments

Comments
 (0)