-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathanalysisSnapshot.ts
More file actions
328 lines (310 loc) · 10.5 KB
/
Copy pathanalysisSnapshot.ts
File metadata and controls
328 lines (310 loc) · 10.5 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
import { createHash } from "node:crypto";
import canonicalize from "canonicalize";
import { z } from "zod";
import {
analysisProfileSchema,
analysisProfilesEqual,
committedProviderSchema,
type AnalysisProfileCommitment,
} from "./analysisProfile.js";
import type { BinaryTarget } from "./binaryTarget.js";
import {
evidenceBundleForTarget,
evidenceBundleSchema,
parseEvidenceBundle,
} from "./evidenceBundle.js";
import {
evidenceLocationSchema,
type Evidence,
type EvidenceLocation,
type EvidenceSubjectTarget,
} from "./evidence.js";
import {
jsonObjectSchema,
jsonValueSchema,
type JsonValue,
} from "./jsonValue.js";
const digestSchema = z.string().regex(/^[a-f0-9]{64}$/u);
const architectureSchema = z.enum(["x86", "x86_64", "arm", "arm64"]);
const formatSchema = z.enum([
"analysis-database",
"mach-o",
"elf",
"pe",
"zip",
"ipa",
"apk",
"msix",
"appx",
"asar",
"dmg",
"pkg",
"plist",
"javascript",
"source-map",
]);
const kindSchema = z.enum(["executable", "database", "archive", "artifact"]);
const subjectSchema = z.object({
path: z.string().min(1),
sha256: digestSchema,
format: z.enum([
...formatSchema.options,
"hopper",
"directory",
"file",
"unknown",
"mach-o-universal",
"javascript-bundle",
"entitlements",
]),
architecture: architectureSchema.nullable(),
});
const targetSchema = z.object({
sha256: digestSchema,
kind: kindSchema,
format: formatSchema,
architecture: architectureSchema.nullable(),
});
const bindingSchema = z
.object({
provider: committedProviderSchema,
analysis_profile: analysisProfileSchema,
})
.superRefine((binding, context) => {
if (
binding.provider.id !== binding.analysis_profile.provider.id ||
binding.provider.name !== binding.analysis_profile.provider.name ||
binding.provider.version !== binding.analysis_profile.provider.version
)
context.addIssue({
code: "custom",
path: ["provider"],
message: "Snapshot binding provider does not match analysis profile",
});
});
const entrySchema = z.object({
query_id: z.string().regex(/^query_[a-f0-9]{64}$/u),
operation: z.string().min(1),
parameters: jsonObjectSchema,
execution: z.object({
result: jsonValueSchema,
raw_result: jsonValueSchema.nullable(),
provider: committedProviderSchema,
limitations: z.array(z.string()),
locations: z.array(evidenceLocationSchema),
subject: subjectSchema.nullable(),
}),
});
/** Provider- and profile-exact cache of successful immutable analysis calls. */
export const analysisSnapshotSchema = z.object({
snapshot_version: z.literal(2),
target: targetSchema,
binding: bindingSchema,
entries: z.array(entrySchema).max(10_000),
evidence_bundle: evidenceBundleSchema,
});
export type AnalysisSnapshot = z.infer<typeof analysisSnapshotSchema>;
export type AnalysisSnapshotEntry = z.infer<typeof entrySchema>;
export type AnalysisSnapshotTarget = z.infer<typeof targetSchema>;
export type AnalysisSnapshotBinding = z.infer<typeof bindingSchema>;
interface SnapshotExecution {
readonly result: JsonValue;
readonly rawResult: JsonValue | null;
readonly provider: {
readonly id: string;
readonly name: string;
readonly version: string | null;
};
readonly analysisProfile?: AnalysisProfileCommitment;
readonly limitations: readonly string[];
readonly locations: readonly EvidenceLocation[];
readonly subject: EvidenceSubjectTarget | null;
}
const canonicalJson = (value: unknown): string => {
const json = jsonValueSchema.parse(value);
const encoded = canonicalize(json);
if (encoded === undefined)
throw new TypeError("RFC 8785 canonicalization rejected snapshot data");
return encoded;
};
/** Project a binary into the path-free identity used for cache invalidation. */
export const snapshotTarget = (
target: BinaryTarget,
): AnalysisSnapshotTarget => ({
sha256: target.sha256,
kind: target.kind,
format: target.format,
architecture: target.architecture ?? null,
});
/** Build the immutable provider/profile binding used by snapshot v2. */
export const snapshotBinding = (
profile: AnalysisProfileCommitment,
): AnalysisSnapshotBinding =>
bindingSchema.parse({
provider: profile.provider,
analysis_profile: profile,
});
/** Compare an open binary with the immutable target identity in a snapshot. */
export const snapshotMatchesTarget = (
snapshot: AnalysisSnapshotTarget,
target: BinaryTarget,
): boolean =>
snapshot.sha256 === target.sha256 &&
snapshot.kind === target.kind &&
snapshot.format === target.format &&
snapshot.architecture === (target.architecture ?? null);
/** Compare a snapshot binding with the selected concrete analysis profile. */
export const snapshotMatchesProfile = (
binding: AnalysisSnapshotBinding,
profile: AnalysisProfileCommitment,
): boolean =>
binding.provider.id === profile.provider.id &&
binding.provider.name === profile.provider.name &&
binding.provider.version === profile.provider.version &&
analysisProfilesEqual(binding.analysis_profile, profile);
/** Require an exact target and selected provider/profile cache partition. */
export const snapshotMatchesBinding = (
snapshot: Pick<AnalysisSnapshot, "target" | "binding">,
target: BinaryTarget,
profile: AnalysisProfileCommitment,
): boolean =>
snapshotMatchesTarget(snapshot.target, target) &&
snapshotMatchesProfile(snapshot.binding, profile);
/** Find exact persisted CLI Evidence without starting an analysis provider. */
export const snapshotEvidenceForQuery = (
snapshot: AnalysisSnapshot,
query: {
readonly target: BinaryTarget;
readonly bindingProfile: AnalysisProfileCommitment;
readonly operation: string;
readonly parameters: Readonly<Record<string, JsonValue>>;
readonly provider: SnapshotExecution["provider"];
readonly evidenceProfile: AnalysisProfileCommitment;
},
): Evidence | undefined => {
const {
target,
bindingProfile,
operation,
parameters,
provider,
evidenceProfile,
} = query;
if (!snapshotMatchesBinding(snapshot, target, bindingProfile))
return undefined;
const encodedParameters = canonicalJson(parameters);
return snapshot.evidence_bundle.records.find(
(record) =>
record.subject?.digest.sha256 === target.sha256 &&
record.operation === operation &&
record.provider.id === provider.id &&
record.provider.name === provider.name &&
record.provider.version === provider.version &&
"analysis_profile" in record &&
analysisProfilesEqual(record.analysis_profile, evidenceProfile) &&
canonicalJson(record.parameters) === encodedParameters,
);
};
/** Compute the stable lookup key for one provider/profile-specific query. */
export const analysisQueryId = (
target: AnalysisSnapshotTarget,
binding: AnalysisSnapshotBinding,
operation: string,
parameters: Readonly<Record<string, JsonValue>>,
): string =>
`query_${createHash("sha256")
.update(canonicalJson({ target, binding, operation, parameters }))
.digest("hex")}`;
/** Create one serializable snapshot entry from a successful provider call. */
export const createAnalysisSnapshotEntry = (input: {
readonly target: AnalysisSnapshotTarget;
readonly binding: AnalysisSnapshotBinding;
readonly operation: string;
readonly parameters: Readonly<Record<string, JsonValue>>;
readonly execution: SnapshotExecution;
}): AnalysisSnapshotEntry => {
const { target, binding, operation, parameters, execution } = input;
if (
execution.analysisProfile === undefined ||
!snapshotMatchesProfile(binding, execution.analysisProfile) ||
execution.provider.id !== binding.provider.id ||
execution.provider.name !== binding.provider.name ||
execution.provider.version !== binding.provider.version
)
throw new TypeError(
"Snapshot execution does not match its provider/profile binding",
);
return {
query_id: analysisQueryId(target, binding, operation, parameters),
operation,
parameters: jsonObjectSchema.parse(parameters),
execution: {
result: execution.result,
raw_result: execution.rawResult,
provider: binding.provider,
limitations: [...execution.limitations],
locations: [...execution.locations],
subject:
execution.subject === null
? null
: {
...execution.subject,
architecture: execution.subject.architecture ?? null,
},
},
};
};
/** Parse v2 and reject legacy snapshots, altered query IDs, or non-canonical order. */
export const parseAnalysisSnapshot = (input: unknown): AnalysisSnapshot => {
rejectLegacySnapshot(input);
const parsed = analysisSnapshotSchema.parse(input);
parseEvidenceBundle(parsed.evidence_bundle);
if (
JSON.stringify(
evidenceBundleForTarget(parsed.evidence_bundle, parsed.target.sha256),
) !== JSON.stringify(parsed.evidence_bundle)
)
throw new TypeError(
"Analysis snapshot evidence contains records for another target",
);
const ids = new Set<string>();
for (const entry of parsed.entries) {
if (
entry.execution.provider.id !== parsed.binding.provider.id ||
entry.execution.provider.name !== parsed.binding.provider.name ||
entry.execution.provider.version !== parsed.binding.provider.version
)
throw new TypeError("Analysis snapshot entry provider does not match");
const expected = analysisQueryId(
parsed.target,
parsed.binding,
entry.operation,
entry.parameters,
);
if (entry.query_id !== expected)
throw new TypeError("Analysis snapshot query identifier does not match");
if (ids.has(entry.query_id))
throw new TypeError("Analysis snapshot contains duplicate queries");
ids.add(entry.query_id);
}
const sorted = [...parsed.entries].sort((left, right) =>
left.query_id.localeCompare(right.query_id),
);
if (JSON.stringify(parsed.entries) !== JSON.stringify(sorted))
throw new TypeError("Analysis snapshot entries are not canonical");
return parsed;
};
/** Serialize a validated snapshot with byte-stable canonical entry ordering. */
export const serializeAnalysisSnapshot = (snapshot: AnalysisSnapshot): string =>
`${canonicalJson(parseAnalysisSnapshot(snapshot))}\n`;
const rejectLegacySnapshot = (input: unknown): void => {
if (
typeof input === "object" &&
input !== null &&
"snapshot_version" in input &&
input.snapshot_version === 1
)
throw new TypeError(
"Analysis snapshot v1 is incompatible with profile-exact replay; recapture it as snapshot v2. Its Evidence bundle may be imported separately.",
);
};