Repository navigation
Expand file tree
/
Copy pathJavaScriptArtifactGraphContext.ts
More file actions
358 lines (336 loc) · 11.9 KB
/
Copy pathJavaScriptArtifactGraphContext.ts
File metadata and controls
358 lines (336 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import type { ArtifactInventorySnapshot } from "../../artifacts/inventory/ArtifactInventory.js";
import {
completeApplicationCoverage,
partialApplicationCoverage,
type ApplicationGraphEvidence,
} from "../../domain/javascript/javascriptApplicationEvidenceSchemas.js";
import type { ApplicationNode } from "../../domain/javascript/javascriptApplicationGraph.js";
import type { JavaScriptSourceRange } from "../../domain/javascript/javascriptStaticAnalysisTypes.js";
import type { JavaScriptModuleArtifactAnalysis } from "./JavaScriptArtifactAnalysisTypes.js";
import type {
JavaScriptArtifactFile,
JavaScriptArtifactFileSet,
JavaScriptArtifactFileKind,
} from "../../domain/javascript/javascriptArtifactFiles.js";
import { JavaScriptArtifactGraphAccumulator } from "./JavaScriptArtifactGraphAccumulator.js";
import {
artifactObservationEvidence,
astObservationEvidence,
staticInferenceEvidence,
unavailableAstEvidence,
} from "./JavaScriptArtifactGraphEvidence.js";
import {
resolveArtifactPathByContext,
type ArtifactPathResolution,
} from "./JavaScriptArtifactPathResolution.js";
/** Mutable graph-construction state scoped to one reconstruction. */
export interface JavaScriptArtifactGraphContext {
readonly accumulator: JavaScriptArtifactGraphAccumulator;
readonly snapshot: ArtifactInventorySnapshot;
readonly fileSet: JavaScriptArtifactFileSet;
readonly analysis: JavaScriptModuleArtifactAnalysis;
readonly root: ApplicationNode;
readonly filesByPath: ReadonlyMap<string, JavaScriptArtifactFile>;
readonly fileNodes: Map<string, ApplicationNode>;
readonly assetNodes: Map<string, ApplicationNode>;
readonly sourceModuleNodes: Map<string, ApplicationNode>;
readonly chunkNodes: Map<string, ApplicationNode>;
readonly moduleNodes: Map<string, ApplicationNode>;
readonly containerNodes: Map<string, ApplicationNode>;
}
/** Evidence coverage shape shared by reconstruction graph helpers. */
export type JavaScriptArtifactGraphCoverage =
ApplicationGraphEvidence["coverage"];
/** Direct artifact-containment edge values. */
export interface ContainsEdgeInput {
readonly source: ApplicationNode;
readonly target: ApplicationNode;
readonly file: JavaScriptArtifactFile;
readonly operation: string;
}
/** Exact AST containment edge values. */
export interface AstEdgeInput {
readonly source: ApplicationNode;
readonly target: ApplicationNode;
readonly file: JavaScriptArtifactFile;
readonly range: JavaScriptSourceRange;
readonly coverage: JavaScriptArtifactGraphCoverage;
readonly properties: Readonly<Record<string, unknown>>;
}
/** Inferred static relationship edge values. */
export interface InferenceEdgeInput extends AstEdgeInput {
readonly relation:
| "imports"
| "loads"
| "maps_to"
| "exposes"
| "calls"
| "persists_to";
}
/** Electron role identity and static discovery evidence. */
export interface RoleNodeInput {
readonly kind: "electron-main" | "electron-preload" | "electron-renderer";
readonly anchor: JavaScriptArtifactFile;
readonly resolution: ArtifactPathResolution;
readonly mechanism: string;
readonly range?: JavaScriptSourceRange;
readonly coverage?: JavaScriptArtifactGraphCoverage;
}
/** Resolution from an inferred Electron role to an artifact asset. */
export interface RoleAssetLinkInput {
readonly role: ApplicationNode;
readonly anchor: JavaScriptArtifactFile;
readonly resolution: ArtifactPathResolution;
readonly range?: JavaScriptSourceRange;
readonly coverage?: JavaScriptArtifactGraphCoverage;
}
/** Explicit package, JavaScript, or source-map parse gap. */
export interface UnavailableParseInput {
readonly file: JavaScriptArtifactFile;
readonly asset: ApplicationNode;
readonly operation: string;
readonly limitation: string;
}
/** Add a direct artifact-containment edge. */
export const addArtifactContainsEdge = (
context: JavaScriptArtifactGraphContext,
input: ContainsEdgeInput,
): void => {
context.accumulator.addEdge({
source_node_id: input.source.node_id,
target_node_id: input.target.node_id,
relation: "contains",
properties: { path: input.file.path },
evidence: artifactObservationEvidence({
sha256:
input.operation === "inventory-entry"
? input.file.container_sha256
: input.file.sha256,
path: input.file.path,
operation: input.operation,
coverage: completeApplicationCoverage(),
}),
});
};
/** Add an exact AST containment edge. */
export const addAstContainsEdge = (
context: JavaScriptArtifactGraphContext,
input: AstEdgeInput,
): void => {
context.accumulator.addEdge({
source_node_id: input.source.node_id,
target_node_id: input.target.node_id,
relation: "contains",
properties: input.properties,
evidence: astObservationEvidence({
sha256: input.file.sha256,
path: input.file.path,
range: input.range,
operation: "map-bundler-structure",
coverage: input.coverage,
}),
});
};
/** Add an explicitly inferred static relationship edge. */
export const addStaticInferenceEdge = (
context: JavaScriptArtifactGraphContext,
input: InferenceEdgeInput,
): void => {
context.accumulator.addEdge({
source_node_id: input.source.node_id,
target_node_id: input.target.node_id,
relation: input.relation,
properties: input.properties,
evidence: staticInferenceEvidence({
sha256: input.file.sha256,
path: input.file.path,
range: input.range,
operation: "map-static-relationship",
coverage: input.coverage,
}),
});
};
/** Preserve a static parse or approval gap as an explicit unknown scope. */
export const addUnavailableStaticParseScope = (
context: JavaScriptArtifactGraphContext,
input: UnavailableParseInput,
): void => {
const node = context.accumulator.addNode({
kind: "unknown",
identity: artifactLocalIdentity(
input.file.sha256,
"static-parse-scope",
`${input.operation}:${input.file.path}`,
),
observations: [
{
label: `${input.file.path} parse scope`,
properties: {
path: input.file.path,
operation: input.operation,
limitation: input.limitation,
},
evidence: unavailableAstEvidence({
sha256: input.file.sha256,
path: input.file.path,
operation: input.operation,
coverage: partialApplicationCoverage([], null),
limitation: input.limitation,
}),
},
],
});
context.accumulator.addEdge({
source_node_id: input.asset.node_id,
target_node_id: node.node_id,
relation: "contains",
properties: { scope: "unavailable-static-analysis" },
evidence: artifactObservationEvidence({
sha256: input.file.sha256,
path: input.file.path,
operation: "associate-unavailable-parse-scope",
coverage: completeApplicationCoverage(),
}),
});
};
/** Coverage for one bounded JavaScript AST analysis. */
export const javascriptAnalysisCoverage = (
analysis: NonNullable<
JavaScriptModuleArtifactAnalysis["files"][number]["javascript"]
>,
): JavaScriptArtifactGraphCoverage =>
analysis.parse_status === "complete"
? completeApplicationCoverage()
: partialApplicationCoverage([], null);
/** Stable identity for a fact scoped to one exact artifact. */
export const artifactLocalIdentity = (
sha256: string,
namespace: string,
key: string,
) => ({
strategy: "artifact-local-key" as const,
stability: "artifact-version" as const,
artifact_sha256: sha256,
namespace,
key,
});
/** Select the graph node kind for one inventoried relevant file. */
export const artifactFileNodeKind = (
kind: JavaScriptArtifactFileKind,
): ApplicationNode["kind"] =>
kind === "javascript"
? "javascript-asset"
: kind === "source-map"
? "source-map"
: kind === "native-addon"
? "native-addon"
: "artifact";
/** Create or merge an Electron role inferred from static syntax or metadata. */
export const createElectronRoleNode = (
context: JavaScriptArtifactGraphContext,
input: RoleNodeInput,
): ApplicationNode =>
context.accumulator.addNode({
kind: input.kind,
identity: artifactLocalIdentity(
input.anchor.sha256,
"electron-role",
`${input.kind}:${input.resolution.resolution_context}:${input.resolution.resolved_path ?? input.resolution.declared_path}`,
),
observations: [
{
label: input.resolution.resolved_path ?? input.resolution.declared_path,
properties: {
declared_path: input.resolution.declared_path,
resolution_context: input.resolution.resolution_context,
resolved_path: input.resolution.resolved_path,
resolution_status: input.resolution.resolution_status,
limitations: input.resolution.limitations,
mechanism: input.mechanism,
},
evidence: staticInferenceEvidence({
sha256: input.anchor.sha256,
path: input.anchor.path,
operation: "discover-electron-entrypoint",
coverage: input.coverage ?? completeApplicationCoverage(),
limitations: input.resolution.limitations,
...(input.range === undefined ? {} : { range: input.range }),
}),
},
],
});
/** Resolve an Electron role to an inventoried asset when available. */
export const linkElectronRoleToAsset = (
context: JavaScriptArtifactGraphContext,
input: RoleAssetLinkInput,
): void => {
const path = input.resolution.resolved_path;
if (path === null) return;
const asset = context.assetNodes.get(path) ?? context.fileNodes.get(path);
if (asset === undefined) return;
context.accumulator.addEdge({
source_node_id: input.role.node_id,
target_node_id: asset.node_id,
relation: "maps_to",
properties: {
declared_path: input.resolution.declared_path,
resolution_context: input.resolution.resolution_context,
resolved_path: path,
resolution_status: input.resolution.resolution_status,
},
evidence: staticInferenceEvidence({
sha256: input.anchor.sha256,
path: input.anchor.path,
operation: "resolve-electron-entrypoint",
coverage: input.coverage ?? completeApplicationCoverage(),
limitations: input.resolution.limitations,
...(input.range === undefined ? {} : { range: input.range }),
}),
});
};
/**
* Disclose relationships omitted because they resolved to their own source;
* the application graph forbids self-referential edges.
*/
export const selfReferenceOmissions = (
count: number,
noun: readonly [singular: string, plural: string],
owner: string,
): string[] =>
count === 0
? []
: [
`${String(count)} ${count === 1 ? noun[0] : noun[1]} resolved back to ${owner} itself and ${count === 1 ? "was" : "were"} omitted; application graph edges cannot be self-referential.`,
];
/** Deterministic key for one recovered module inside a bundle asset. */
export const moduleLookupKey = (path: string, moduleKey: string): string =>
`${path}\0${moduleKey}`;
/** Deterministic key for one recovered bundler chunk inside a bundle asset. */
export const chunkLookupKey = (
path: string,
runtime: string,
chunkKey: string,
): string => `${path}\0${runtime}\0${chunkKey}`;
/** Resolve a relative static specifier without escaping the artifact root. */
export const resolveArtifactPath = (
specifier: string,
sourcePath: string,
files: ReadonlyMap<string, JavaScriptArtifactFile>,
moduleKind?: "import" | "require",
): string | null =>
resolveArtifactPathByContext({
declaredPath: specifier,
sourcePath,
context: "module-specifier",
files,
...(moduleKind === undefined ? {} : { moduleKind }),
}).resolved_path;
/** Locate the recovered module that owns a static finding. */
export const sourceNodeFor = (
context: JavaScriptArtifactGraphContext,
path: string,
moduleKey: string | null,
): ApplicationNode | undefined =>
moduleKey === null
? context.sourceModuleNodes.get(path)
: context.moduleNodes.get(moduleLookupKey(path, moduleKey));