Repository navigation
Expand file tree
/
Copy pathjavascriptApplicationAnalysisSteps.test.ts
More file actions
326 lines (310 loc) · 11.7 KB
/
Copy pathjavascriptApplicationAnalysisSteps.test.ts
File metadata and controls
326 lines (310 loc) · 11.7 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
import { describe, expect, it } from "vitest";
import { z } from "zod";
import { JAVASCRIPT_APPLICATION_EVIDENCE_EXAMPLE } from "../../contracts/javascript/javascriptRuntimeReconciliationExample.js";
import { freezeJsonSnapshot } from "../immutableJson.js";
import {
javascriptApplicationAnalysisResultSchema,
parseOwnedJavaScriptApplicationAnalysisSteps,
} from "./javascriptApplicationAnalysis.js";
import {
createJavaScriptApplicationGraph,
createImmutableJavaScriptApplicationGraphSteps,
isValidatedImmutableJavaScriptApplicationGraph,
sealTransferredJavaScriptApplicationGraphSteps,
} from "./javascriptApplicationGraph.js";
import {
createJavaScriptSemanticGraph,
createImmutableJavaScriptSemanticGraphSteps,
isValidatedImmutableJavaScriptSemanticGraph,
sealTransferredJavaScriptSemanticGraphSteps,
} from "./javascriptSemanticGraph.js";
const complete = <Value>(steps: Iterator<void, Value>): Value => {
let next = steps.next();
while (!next.done) next = steps.next();
return next.value;
};
const example = () =>
javascriptApplicationAnalysisResultSchema.parse(
JAVASCRIPT_APPLICATION_EVIDENCE_EXAMPLE.normalized_result,
);
const ownedExample = () => {
const result = example();
const { graph_id: _applicationId, ...application } = result.graph;
const { graph_id: _semanticId, ...semantic } = result.semantic_graph;
return {
...result,
graph: complete(
createImmutableJavaScriptApplicationGraphSteps(application),
),
semantic_graph: complete(
createImmutableJavaScriptSemanticGraphSteps(semantic),
),
};
};
const issuesFrom = (action: () => unknown) => {
try {
action();
} catch (error: unknown) {
if (error instanceof z.ZodError) return error.issues;
throw error;
}
throw new Error("Expected complete result validation to reject the input");
};
describe("owned JavaScript graph validation", () => {
it("reuses exact sealed graphs and preserves all public result fields", () => {
const input = ownedExample();
const result = complete(
parseOwnedJavaScriptApplicationAnalysisSteps(input),
);
expect(result).toEqual(
javascriptApplicationAnalysisResultSchema.parse(input),
);
expect(result.graph).toBe(input.graph);
expect(result.semantic_graph).toBe(input.semantic_graph);
expect(result.summary).not.toBe(input.summary);
expect(result.statistics).not.toBe(input.statistics);
expect(Object.isFrozen(result.graph.nodes[0]?.observations)).toBe(true);
expect(Object.isFrozen(result.semantic_graph.nodes[0]?.properties)).toBe(
true,
);
expect(isValidatedImmutableJavaScriptApplicationGraph(result.graph)).toBe(
true,
);
expect(
isValidatedImmutableJavaScriptSemanticGraph(result.semantic_graph),
).toBe(true);
expect(
isValidatedImmutableJavaScriptApplicationGraph(result.graph.nodes),
).toBe(false);
});
it("keeps ordinary constructors mutable and clones inputs before sealing", () => {
const input = example();
const { graph_id: _applicationId, ...application } = input.graph;
const { graph_id: _semanticId, ...semantic } = input.semantic_graph;
const ordinaryApplication = createJavaScriptApplicationGraph(application);
const ordinarySemantic = createJavaScriptSemanticGraph(semantic);
expect(Object.isFrozen(ordinaryApplication)).toBe(false);
expect(Object.isFrozen(ordinarySemantic)).toBe(false);
const applicationSteps =
createImmutableJavaScriptApplicationGraphSteps(application);
const semanticSteps = createImmutableJavaScriptSemanticGraphSteps(semantic);
application.limitations.push("Changed after factory input was cloned.");
semantic.limitations.push("Changed after factory input was cloned.");
expect(complete(applicationSteps)).toEqual(ordinaryApplication);
expect(complete(semanticSteps)).toEqual(ordinarySemantic);
expect(
isValidatedImmutableJavaScriptApplicationGraph(ordinaryApplication),
).toBe(false);
expect(isValidatedImmutableJavaScriptSemanticGraph(ordinarySemantic)).toBe(
false,
);
});
it.each(["mutable", "shallow-frozen", "json-sealed"])(
"fully validates %s imported graphs instead of trusting their IDs",
(representation) => {
const input = example();
input.graph.graph_id = `jag_${"f".repeat(64)}`;
if (representation === "shallow-frozen") Object.freeze(input.graph);
if (representation === "json-sealed") freezeJsonSnapshot(input.graph);
expect(isValidatedImmutableJavaScriptApplicationGraph(input.graph)).toBe(
false,
);
const issues = issuesFrom(() =>
complete(parseOwnedJavaScriptApplicationAnalysisSteps(input)),
);
expect(issues).toEqual(
issuesFrom(() =>
javascriptApplicationAnalysisResultSchema.parse(input),
),
);
expect(
issues.some(({ path }) => path.join("/") === "graph/graph_id"),
).toBe(true);
},
);
it("fully parses valid imported graphs without conferring an owned proof", () => {
const input = example();
const parsed = complete(
parseOwnedJavaScriptApplicationAnalysisSteps(input),
);
expect(parsed).toEqual(input);
expect(parsed.graph).not.toBe(input.graph);
expect(parsed.semantic_graph).not.toBe(input.semantic_graph);
expect(
isValidatedImmutableJavaScriptSemanticGraph(parsed.semantic_graph),
).toBe(false);
});
it("rejects a forged semantic graph paired with an authenticated application graph", () => {
const input = ownedExample();
input.semantic_graph = freezeJsonSnapshot({
...input.semantic_graph,
graph_id: `jsrg_${"f".repeat(64)}`,
});
expect(
isValidatedImmutableJavaScriptSemanticGraph(input.semantic_graph),
).toBe(false);
expect(
issuesFrom(() =>
complete(parseOwnedJavaScriptApplicationAnalysisSteps(input)),
),
).toEqual(
issuesFrom(() => javascriptApplicationAnalysisResultSchema.parse(input)),
);
});
it("does not authenticate an abandoned sealing operation", () => {
const input = example();
const { graph_id: _semanticId, ...semantic } = input.semantic_graph;
const originalNode = semantic.nodes[0];
if (originalNode === undefined) throw new Error("Expected a semantic node");
semantic.nodes[0] = {
...originalNode,
properties: {
rows: Array.from({ length: 10_000 }, (_, value) => ({ value })),
},
};
const mutable = createJavaScriptSemanticGraph(semantic);
const { graph_id: _mutableId, ...mutableInput } = mutable;
const steps = createImmutableJavaScriptSemanticGraphSteps(mutableInput);
expect(steps.next().done).toBe(false);
steps.return(mutable);
expect(isValidatedImmutableJavaScriptSemanticGraph(mutable)).toBe(false);
const sealed = complete(
createImmutableJavaScriptSemanticGraphSteps(mutableInput),
);
expect(sealed).toEqual(mutable);
expect(isValidatedImmutableJavaScriptSemanticGraph(sealed)).toBe(true);
});
});
describe("owned JavaScript result bindings", () => {
it.each(["application", "root", "node"])(
"retains the %s binding check for individually authenticated graphs",
(binding) => {
const input = ownedExample();
if (binding === "root") input.root_artifact_sha256 = "e".repeat(64);
else {
const { graph_id: _semanticId, ...semantic } = input.semantic_graph;
if (binding === "application")
semantic.application_graph_id = `jag_${"e".repeat(64)}`;
else {
const node = semantic.nodes[0];
if (node === undefined) throw new Error("Expected a semantic node");
semantic.nodes = [
{ ...node, application_node_ids: [`jag_node_${"e".repeat(64)}`] },
...semantic.nodes.slice(1),
];
}
input.semantic_graph = complete(
createImmutableJavaScriptSemanticGraphSteps(semantic),
);
}
expect(
issuesFrom(() =>
complete(parseOwnedJavaScriptApplicationAnalysisSteps(input)),
),
).toEqual(
issuesFrom(() =>
javascriptApplicationAnalysisResultSchema.parse(input),
),
);
},
);
it("yields while checking a wide binding set and closes an abandoned iteration", () => {
const input = ownedExample();
const { graph_id: _semanticId, ...semantic } = input.semantic_graph;
const node = semantic.nodes[0];
if (node === undefined) throw new Error("Expected a semantic node");
semantic.nodes = [
{
...node,
application_node_ids: Array.from(
{ length: 10_000 },
(_, index) => `jag_node_${index.toString(16).padStart(64, "0")}`,
),
},
...semantic.nodes.slice(1),
];
input.semantic_graph = complete(
createImmutableJavaScriptSemanticGraphSteps(semantic),
);
const steps = parseOwnedJavaScriptApplicationAnalysisSteps(input);
expect(steps.next().done).toBe(false);
steps.return(input);
expect(steps.next().done).toBe(true);
expect(
issuesFrom(() =>
complete(parseOwnedJavaScriptApplicationAnalysisSteps(input)),
),
).toEqual(
issuesFrom(() => javascriptApplicationAnalysisResultSchema.parse(input)),
);
});
it("retains strict metadata validation alongside authenticated graphs", () => {
const input = {
...ownedExample(),
statistics: { ...example().statistics, findings: -1 },
extra: true,
};
expect(
issuesFrom(() =>
complete(parseOwnedJavaScriptApplicationAnalysisSteps(input)),
),
).toEqual(
issuesFrom(() => javascriptApplicationAnalysisResultSchema.parse(input)),
);
});
});
describe("graphs transferred to their sealing steps", () => {
it("seal the same validated graphs as the copying factories", () => {
const input = example();
const { graph_id: _applicationId, ...application } = input.graph;
const { graph_id: _semanticId, ...semantic } = input.semantic_graph;
const expectedApplication = createJavaScriptApplicationGraph(application);
const expectedSemantic = createJavaScriptSemanticGraph(semantic);
const sealedApplication = complete(
sealTransferredJavaScriptApplicationGraphSteps(
structuredClone(application),
),
);
const sealedSemantic = complete(
sealTransferredJavaScriptSemanticGraphSteps(structuredClone(semantic)),
);
expect(sealedApplication).toEqual(expectedApplication);
expect(sealedSemantic).toEqual(expectedSemantic);
expect(
isValidatedImmutableJavaScriptApplicationGraph(sealedApplication),
).toBe(true);
expect(isValidatedImmutableJavaScriptSemanticGraph(sealedSemantic)).toBe(
true,
);
});
it("report the issues a whole-graph parse reports", () => {
const input = example();
const { graph_id: _applicationId, ...application } = input.graph;
const { graph_id: _semanticId, ...semantic } = input.semantic_graph;
const node = semantic.nodes[0];
const applicationNode = application.nodes[0];
if (node === undefined || applicationNode === undefined)
throw new Error("Expected semantic and application nodes");
const malformed = {
...semantic,
nodes: [{ ...node, label: 7 }, ...semantic.nodes.slice(1)],
};
expect(
issuesFrom(() =>
complete(sealTransferredJavaScriptSemanticGraphSteps(malformed)),
),
).toEqual(issuesFrom(() => createJavaScriptSemanticGraph(malformed)));
const stale = {
...application,
nodes: [
{ ...applicationNode, node_id: `jag_node_${"f".repeat(64)}` },
...application.nodes.slice(1),
],
};
expect(
issuesFrom(() =>
complete(sealTransferredJavaScriptApplicationGraphSteps(stale)),
),
).toEqual(issuesFrom(() => createJavaScriptApplicationGraph(stale)));
});
});