Repository navigation
Expand file tree
/
Copy pathjavascriptApplicationGraph.ts
More file actions
656 lines (614 loc) · 20.2 KB
/
Copy pathjavascriptApplicationGraph.ts
File metadata and controls
656 lines (614 loc) · 20.2 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
import { z } from "zod";
import {
arrayContainerSchema,
parseElementSteps,
} from "../zodCollectionSteps.js";
import { canonicalJsonDigestSteps } from "../canonicalJsonDigestSteps.js";
import { canonicalJson } from "../comparisonSemantics.js";
import { digestCanonicalValue } from "../canonicalDigest.js";
import { uniqueSorted } from "../canonicalOrdering.js";
import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js";
import {
freezeJsonSnapshot,
freezeOwnedJsonSnapshotSteps,
} from "../immutableJson.js";
import type {
ApplicationGraphEvidence,
ApplicationNodeIdentity,
} from "./javascriptApplicationEvidenceSchemas.js";
import {
applicationEdgeInputSchema,
applicationEdgeSchema,
applicationNodeInputSchema,
applicationNodeObservationSchema,
applicationNodeSchema,
javascriptApplicationGraphInputSchema,
javascriptApplicationGraphRecordSchema,
type ApplicationEdge,
type ApplicationNode,
type JavaScriptApplicationGraphInput,
} from "./javascriptApplicationGraphSchemas.js";
const OBSERVATION_IDENTIFIER_STRATEGY = {
strategy: "semantic-content-sha256" as const,
stability: "observation-exact" as const,
};
const EDGE_IDENTIFIER_STRATEGY = {
strategy: "semantic-content-sha256" as const,
stability: "relationship-exact" as const,
};
const limitKey = (
limit: ApplicationGraphEvidence["coverage"]["limits"][number],
) => canonicalJson(limit, "JavaScript Application Graph");
const normalizeLimits = (
limits: ApplicationGraphEvidence["coverage"]["limits"],
): ApplicationGraphEvidence["coverage"]["limits"] =>
[...new Map(limits.map((limit) => [limitKey(limit), limit])).values()].sort(
(left, right) => compareUnicodeCodePoints(limitKey(left), limitKey(right)),
);
const normalizeEvidence = (
evidence: ApplicationGraphEvidence,
): ApplicationGraphEvidence => ({
...evidence,
coverage: {
...evidence.coverage,
limits: normalizeLimits(evidence.coverage.limits),
},
limitations: uniqueSorted(evidence.limitations),
evidence_ids: uniqueSorted(evidence.evidence_ids),
});
const normalizeIdentity = (
identity: ApplicationNodeIdentity,
): ApplicationNodeIdentity =>
identity.strategy === "structural-fingerprint"
? { ...identity, basis: uniqueSorted(identity.basis) }
: identity;
const nodeSemantic = (node: Pick<ApplicationNode, "kind" | "identity">) => ({
kind: node.kind,
identity: node.identity,
});
const nodeId = (node: Pick<ApplicationNode, "kind" | "identity">): string =>
`jag_node_${digestCanonicalValue(nodeSemantic(node), "JavaScript Application Graph")}`;
const observationSemantic = (
nodeIdentifier: string,
observation: Omit<
z.infer<typeof applicationNodeObservationSchema>,
"observation_id"
>,
) => ({ node_id: nodeIdentifier, ...observation });
const observationId = (
nodeIdentifier: string,
observation: Omit<
z.infer<typeof applicationNodeObservationSchema>,
"observation_id"
>,
): string =>
`jag_observation_${digestCanonicalValue(
observationSemantic(nodeIdentifier, observation),
)}`;
type EdgeSemantic = Omit<ApplicationEdge, "edge_id">;
const edgeId = (edge: EdgeSemantic): string =>
`jag_edge_${digestCanonicalValue(edge, "JavaScript Application Graph")}`;
/** The issue sink shared by schema refinement and stepwise checking. */
type GraphIssueContext = Pick<z.RefinementCtx, "addIssue">;
const sortedUniqueIssue = (
values: readonly string[],
path: PropertyKey[],
label: string,
context: GraphIssueContext,
): void => {
for (let index = 1; index < values.length; index += 1) {
if (
compareUnicodeCodePoints(values[index - 1] ?? "", values[index] ?? "") < 0
)
continue;
context.addIssue({
code: "custom",
message: `${label} must be sorted and unique by Unicode code point`,
path: [...path, index],
});
return;
}
};
const checkEvidenceNormalization = (
evidence: ApplicationGraphEvidence,
path: PropertyKey[],
context: GraphIssueContext,
): void => {
sortedUniqueIssue(
evidence.limitations,
[...path, "limitations"],
"Evidence limitations",
context,
);
sortedUniqueIssue(
evidence.evidence_ids,
[...path, "evidence_ids"],
"Evidence identifiers",
context,
);
sortedUniqueIssue(
evidence.coverage.limits.map(limitKey),
[...path, "coverage", "limits"],
"Evidence limits",
context,
);
const runtimeLocation =
evidence.location.available && evidence.location.value.kind === "runtime";
const staticAuthority = [
"artifact-bytes",
"ast-static-analysis",
"static-relationship-inference",
"native-analysis-provider",
].includes(evidence.authority);
if (runtimeLocation && staticAuthority)
context.addIssue({
code: "custom",
message: "Static and native authorities cannot claim a runtime location",
path: [...path, "location"],
});
};
const sourceMapObservationMatchesIdentity = (
identity: Extract<
ApplicationNodeIdentity,
{ readonly strategy: "source-map-original" }
>,
observation: ApplicationNode["observations"][number],
): boolean => {
const { evidence, source_map_reference: reference } = observation;
const sourceDigests = [
observation.properties.content_sha256,
// Older graph producers used `source_sha256` for the same source-map fact.
observation.properties.source_sha256,
];
const hasKnownSourceDigest = identity.source_sha256 !== null;
const sourceDigestMatches =
sourceDigests.every(
(digest) =>
digest === undefined ||
digest === null ||
digest === identity.source_sha256,
) &&
(!hasKnownSourceDigest || sourceDigests.includes(identity.source_sha256));
const contentAvailabilityMatches =
(observation.properties.content_available !== true ||
hasKnownSourceDigest) &&
(observation.properties.content_available !== false ||
!hasKnownSourceDigest);
return (
sourceDigestMatches &&
contentAvailabilityMatches &&
reference !== null &&
evidence.state === "observed" &&
evidence.artifact.available &&
evidence.artifact.sha256 === identity.source_map_sha256 &&
reference.source_name === identity.original_source &&
reference.source_root === identity.source_root &&
evidence.location.available &&
evidence.location.value.kind === "artifact-path" &&
reference.map_path === evidence.location.value.path
);
};
const observationMatchesIdentity = (
node: ApplicationNode,
observation: ApplicationNode["observations"][number],
): boolean => {
const { identity } = node;
const { evidence } = observation;
if (identity.strategy === "content-digest")
return (
evidence.state === "observed" &&
evidence.artifact.available &&
evidence.artifact.sha256 === identity.sha256
);
if (identity.strategy === "source-map-original")
return sourceMapObservationMatchesIdentity(identity, observation);
if (identity.strategy === "canonical-path")
return (
evidence.artifact.available &&
evidence.artifact.sha256 === identity.artifact_sha256 &&
evidence.location.available &&
evidence.location.value.kind === "artifact-path" &&
evidence.location.value.path === identity.path
);
if (identity.strategy === "artifact-local-key")
return (
evidence.artifact.available &&
evidence.artifact.sha256 === identity.artifact_sha256
);
if (identity.strategy === "runtime-instance") {
if (
!["passive-cdp-runtime", "controlled-replay"].includes(
evidence.authority,
) ||
!evidence.location.available ||
evidence.location.value.kind !== "runtime"
)
return false;
const location = evidence.location.value;
return (
location.capture_sha256 === identity.capture_sha256 &&
[location.target_key, location.frame_key, location.script_key].includes(
identity.runtime_key,
)
);
}
if (identity.strategy === "structural-fingerprint")
return evidence.state === "inferred";
return evidence.state === "observed";
};
const checkNode = (
node: ApplicationNode,
index: number,
context: GraphIssueContext,
): void => {
if (node.node_id !== nodeId(node))
context.addIssue({
code: "custom",
message: "Node identifier does not match its kind and identity",
path: ["nodes", index, "node_id"],
});
if (
node.identity.strategy === "structural-fingerprint" &&
canonicalJson(node.identity.basis, "JavaScript Application Graph") !==
canonicalJson(
uniqueSorted(node.identity.basis),
"JavaScript Application Graph",
)
)
context.addIssue({
code: "custom",
message: "Structural fingerprint basis must be sorted and unique",
path: ["nodes", index, "identity", "basis"],
});
sortedUniqueIssue(
node.observations.map(({ observation_id: id }) => id),
["nodes", index, "observations"],
"Node observations",
context,
);
for (const [observationIndex, observation] of node.observations.entries()) {
const { observation_id: identifier, ...semantic } = observation;
if (identifier !== observationId(node.node_id, semantic))
context.addIssue({
code: "custom",
message: "Observation identifier does not match its semantic content",
path: [
"nodes",
index,
"observations",
observationIndex,
"observation_id",
],
});
if (
node.identity.strategy === "source-map-original" &&
!observationMatchesIdentity(node, observation)
)
context.addIssue({
code: "custom",
message:
"Source-map observation must retain its exact declaration and map location",
path: [
"nodes",
index,
"observations",
observationIndex,
"source_map_reference",
],
});
checkEvidenceNormalization(
observation.evidence,
["nodes", index, "observations", observationIndex, "evidence"],
context,
);
}
if (
!node.observations.some((observation) =>
observationMatchesIdentity(node, observation),
)
)
context.addIssue({
code: "custom",
message: "Node identity is not supported by a compatible observation",
path: ["nodes", index, "identity"],
});
};
type GraphRecord = z.infer<typeof javascriptApplicationGraphRecordSchema>;
const checkGraphContent = (
graph: JavaScriptApplicationGraphInput,
context: GraphIssueContext,
): void => {
const steps = checkGraphContentSteps(graph, context);
while (steps.next().done !== true);
};
function* checkGraphContentSteps(
graph: JavaScriptApplicationGraphInput,
context: GraphIssueContext,
): Generator<void, void> {
sortedUniqueIssue(
graph.root_node_ids,
["root_node_ids"],
"Root nodes",
context,
);
sortedUniqueIssue(
graph.nodes.map(({ node_id: id }) => id),
["nodes"],
"Nodes",
context,
);
sortedUniqueIssue(
graph.edges.map(({ edge_id: id }) => id),
["edges"],
"Edges",
context,
);
sortedUniqueIssue(
graph.limitations,
["limitations"],
"Graph limitations",
context,
);
sortedUniqueIssue(
graph.coverage.limits.map(limitKey),
["coverage", "limits"],
"Graph limits",
context,
);
if (graph.coverage.status !== "complete" && graph.limitations.length === 0)
context.addIssue({
code: "custom",
message: "Non-complete graph coverage requires an explicit limitation",
path: ["limitations"],
});
const nodesById = new Map(graph.nodes.map((node) => [node.node_id, node]));
for (const root of graph.root_node_ids)
if (!nodesById.has(root))
context.addIssue({
code: "custom",
message: "Root identifier must name a graph node",
path: ["root_node_ids"],
});
for (const [index, node] of graph.nodes.entries()) {
if (index % 64 === 0) yield;
checkNode(node, index, context);
}
for (const [index, edge] of graph.edges.entries()) {
if (index % 64 === 0) yield;
const { edge_id: identifier, ...semantic } = edge;
if (identifier !== edgeId(semantic))
context.addIssue({
code: "custom",
message: "Edge identifier does not match its semantic content",
path: ["edges", index, "edge_id"],
});
if (
!nodesById.has(edge.source_node_id) ||
!nodesById.has(edge.target_node_id)
)
context.addIssue({
code: "custom",
message: "Edge endpoints must name graph nodes",
path: ["edges", index],
});
if (edge.source_node_id === edge.target_node_id)
context.addIssue({
code: "custom",
message: "Application graph edges cannot be self-referential",
path: ["edges", index],
});
if (
edge.relation === "changed_from" &&
nodesById.get(edge.source_node_id)?.kind !==
nodesById.get(edge.target_node_id)?.kind
)
context.addIssue({
code: "custom",
message: "changed_from endpoints must have the same node kind",
path: ["edges", index, "relation"],
});
checkEvidenceNormalization(
edge.evidence,
["edges", index, "evidence"],
context,
);
}
}
const checkGraphInvariants = (
graph: GraphRecord,
context: GraphIssueContext,
): void => {
checkGraphContent(graph, context);
const { graph_id: identifier, ...semantic } = graph;
if (
identifier !==
`jag_${digestCanonicalValue(semantic, "JavaScript Application Graph")}`
)
context.addIssue({
code: "custom",
message: "Graph identifier does not match its semantic content",
path: ["graph_id"],
});
};
/** Strict JavaScript Application Graph with verified commitments. */
export const javascriptApplicationGraphSchema =
javascriptApplicationGraphRecordSchema.superRefine(checkGraphInvariants);
const javascriptApplicationGraphContentSchema =
javascriptApplicationGraphInputSchema.superRefine(checkGraphContent);
/** Fully validated JavaScript Application Graph. */
export type JavaScriptApplicationGraph = z.infer<
typeof javascriptApplicationGraphSchema
>;
/** Build one normalized application entity and derive all semantic IDs. */
export const createJavaScriptApplicationNode = (
input: unknown,
): ApplicationNode => {
const parsed = applicationNodeInputSchema.parse(input);
// These properties are newly parsed and exclusively owned. Authenticate
// them before the observation/node schemas and final graph validate again.
for (const observation of parsed.observations)
freezeJsonSnapshot(observation.properties);
const identity = normalizeIdentity(parsed.identity);
const identifier = nodeId({ kind: parsed.kind, identity });
const observations = parsed.observations
.map((observation) => {
const semantic = {
...observation,
evidence: normalizeEvidence(observation.evidence),
identifier_strategy: OBSERVATION_IDENTIFIER_STRATEGY,
};
return applicationNodeObservationSchema.parse({
...semantic,
observation_id: observationId(identifier, semantic),
});
})
.sort((left, right) =>
compareUnicodeCodePoints(left.observation_id, right.observation_id),
);
return applicationNodeSchema.parse({
node_id: identifier,
kind: parsed.kind,
identity,
observations,
});
};
/** Build one normalized directed relationship and derive its semantic ID. */
export const createJavaScriptApplicationEdge = (
input: unknown,
): ApplicationEdge => {
const parsed = applicationEdgeInputSchema.parse(input);
freezeJsonSnapshot(parsed.properties);
const semantic = {
...parsed,
evidence: normalizeEvidence(parsed.evidence),
identifier_strategy: EDGE_IDENTIFIER_STRATEGY,
};
return applicationEdgeSchema.parse({
...semantic,
edge_id: edgeId(semantic),
});
};
/** Normalize a complete graph and derive its top-level semantic ID. */
export const createJavaScriptApplicationGraph = (
input: unknown,
): JavaScriptApplicationGraph => {
const semantic = normalizeGraphInput(
javascriptApplicationGraphInputSchema.parse(input),
);
const content = javascriptApplicationGraphContentSchema.parse(semantic);
return {
...content,
graph_id: `jag_${digestCanonicalValue(content, "JavaScript Application Graph")}`,
};
};
const normalizeGraphInput = (
parsed: JavaScriptApplicationGraphInput,
): JavaScriptApplicationGraphInput => ({
...parsed,
root_node_ids: uniqueSorted(parsed.root_node_ids),
nodes: [...parsed.nodes].sort((left, right) =>
compareUnicodeCodePoints(left.node_id, right.node_id),
),
edges: [...parsed.edges].sort((left, right) =>
compareUnicodeCodePoints(left.edge_id, right.edge_id),
),
coverage: {
...parsed.coverage,
limits: normalizeLimits(parsed.coverage.limits),
},
limitations: uniqueSorted(parsed.limitations),
});
const validatedImmutableApplicationGraphs = new WeakSet<object>();
/** Clone and validate synchronously, then return steps to seal the owned graph. */
export const createImmutableJavaScriptApplicationGraphSteps = (
input: unknown,
): Generator<void, JavaScriptApplicationGraph> =>
sealValidatedApplicationGraphSteps(createJavaScriptApplicationGraph(input));
const applicationGraphShape = javascriptApplicationGraphInputSchema.shape;
const applicationGraphContainerSchema =
javascriptApplicationGraphInputSchema.extend({
nodes: arrayContainerSchema(applicationGraphShape.nodes),
edges: arrayContainerSchema(applicationGraphShape.edges),
});
/**
* Seal a graph whose input the caller transfers: the caller neither retains nor
* mutates any part of it afterwards. Only the graph's own fields are validated
* synchronously; nodes and edges are validated, checked, hashed and frozen in
* steps, because doing so at once held the event loop for large applications.
*/
export const sealTransferredJavaScriptApplicationGraphSteps = (
input: unknown,
): Generator<void, JavaScriptApplicationGraph> =>
parseAndSealApplicationGraphSteps(
applicationGraphContainerSchema.parse(input),
);
function* parseAndSealApplicationGraphSteps(
container: z.output<typeof applicationGraphContainerSchema>,
): Generator<void, JavaScriptApplicationGraph> {
const issues: z.core.$ZodIssue[] = [];
const parsed: JavaScriptApplicationGraphInput = {
...container,
nodes: yield* parseElementSteps(
applicationGraphShape.nodes.element,
container.nodes,
["nodes"],
issues,
),
edges: yield* parseElementSteps(
applicationGraphShape.edges.element,
container.edges,
["edges"],
issues,
),
};
if (issues.length > 0) throw new z.ZodError(issues);
const content = normalizeGraphInput(parsed);
let invalid = false;
yield* checkGraphContentSteps(content, {
addIssue: () => {
invalid = true;
},
});
// An invalid owned graph is a construction defect; report it with the exact
// issues the content schema produces.
if (invalid) {
javascriptApplicationGraphContentSchema.parse(content);
throw new TypeError("Application graph checks disagree with its schema");
}
return yield* sealValidatedApplicationGraphSteps({
...content,
graph_id: `jag_${yield* canonicalJsonDigestSteps(content)}`,
});
}
function* sealValidatedApplicationGraphSteps(
graph: JavaScriptApplicationGraph,
): Generator<void, JavaScriptApplicationGraph> {
yield* freezeOwnedJsonSnapshotSteps(graph);
validatedImmutableApplicationGraphs.add(graph);
return graph;
}
/** Recognize the exact completely sealed graph produced by the owned factory. */
export const isValidatedImmutableJavaScriptApplicationGraph = (
value: unknown,
): value is JavaScriptApplicationGraph =>
typeof value === "object" &&
value !== null &&
validatedImmutableApplicationGraphs.has(value);
/** Parse a stored graph and reject stale IDs. */
export const parseJavaScriptApplicationGraph = (
input: unknown,
): JavaScriptApplicationGraph => javascriptApplicationGraphSchema.parse(input);
/** Compute the byte-stable SHA-256 commitment of a verified graph. */
export const computeJavaScriptApplicationGraphSha256 = (
input: unknown,
): string =>
digestCanonicalValue(
parseJavaScriptApplicationGraph(input),
"JavaScript Application Graph",
);
/** Serialize a verified graph as RFC 8785 canonical JSON. */
export const serializeJavaScriptApplicationGraph = (input: unknown): string =>
canonicalJson(
parseJavaScriptApplicationGraph(input),
"JavaScript Application Graph",
);