@gtrabanco/agentic-workflow-schema 3.3.0 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.es.md +549 -3
- package/README.md +539 -3
- package/dist/canonical-json.d.ts +60 -0
- package/dist/canonical-json.js +119 -0
- package/dist/index.d.ts +263 -2
- package/dist/index.js +587 -52
- package/dist/pre-execution-contract.d.ts +175 -0
- package/dist/pre-execution-contract.js +676 -0
- package/dist/pre-execution-vectors.d.ts +19 -0
- package/dist/pre-execution-vectors.js +35 -0
- package/dist/pre-execution.d.ts +363 -0
- package/dist/pre-execution.js +838 -0
- package/dist/sha256.d.ts +43 -0
- package/dist/sha256.js +189 -0
- package/dist/verification-contract.d.ts +294 -0
- package/dist/verification-contract.js +1229 -0
- package/package.json +21 -3
- package/pre-execution-artifact-snapshot.schema.json +383 -0
- package/pre-execution-review-receipt.schema.json +457 -0
- package/verification-plan.schema.json +197 -0
- package/verification-receipt.schema.json +309 -0
|
@@ -0,0 +1,1229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical internal definition of the two staged-verification contracts.
|
|
3
|
+
*
|
|
4
|
+
* This module is the SINGLE source of the VerificationPlan v1 /
|
|
5
|
+
* VerificationReceipt v1 structural contract: the closed field lists,
|
|
6
|
+
* vocabularies, bounds, patterns and cross-field rules declared here are
|
|
7
|
+
* consumed by
|
|
8
|
+
* - runtime validation (`validateStructure` below, used by the two public
|
|
9
|
+
* authoritative entries in `index.ts`), and
|
|
10
|
+
* - the deterministic Draft-07 structural projection generator
|
|
11
|
+
* (`scripts/generate-verification-schemas.mjs`).
|
|
12
|
+
*
|
|
13
|
+
* It is package-internal: it is not re-exported from the package root and the
|
|
14
|
+
* published `exports` map does not expose it. The generated JSON Schemas are
|
|
15
|
+
* structural projections of this definition, never a second semantic authority.
|
|
16
|
+
*/
|
|
17
|
+
// Function-level import (used only inside `applyCrossRule`, never at module
|
|
18
|
+
// init): the canonical serializer is the shared authority in
|
|
19
|
+
// `canonical-json.ts`, which itself imports this module's capture primitives.
|
|
20
|
+
import { canonicalJSONValue } from "./canonical-json.js";
|
|
21
|
+
const NUL_MESSAGE = "must not contain NUL characters";
|
|
22
|
+
const ISO_8601_PATTERN = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?Z$";
|
|
23
|
+
const LOWERCASE_64HEX_PATTERN = "^[a-f0-9]{64}$";
|
|
24
|
+
/** Contract ids, declared once; the package root re-exports them publicly. */
|
|
25
|
+
export const VERIFICATION_PLAN_CONTRACT_ID = "agentic-workflow/verification-plan@1";
|
|
26
|
+
export const VERIFICATION_RECEIPT_CONTRACT_ID = "agentic-workflow/verification-receipt@1";
|
|
27
|
+
/** Closed vocabularies, declared once; the root re-exports them publicly. */
|
|
28
|
+
export const VERIFICATION_STAGES = Object.freeze(["fast", "full"]);
|
|
29
|
+
export const VERIFICATION_COST_CLASSES = Object.freeze(["cheap", "moderate", "expensive"]);
|
|
30
|
+
export const VERIFICATION_COMMAND_STATUSES = Object.freeze(["passed", "failed", "timed-out", "skipped", "infrastructure-error"]);
|
|
31
|
+
export const VERIFICATION_VERDICTS = Object.freeze(["pass", "fail", "incomplete"]);
|
|
32
|
+
export const WORKING_DIRECTORY_POLICIES = ["candidate-root", "relative-path"];
|
|
33
|
+
/**
|
|
34
|
+
* D14 — bounded usability: the SHAPE ceilings (cardinality + string length).
|
|
35
|
+
*
|
|
36
|
+
* Every number the validator enforces is read from this object, and the same
|
|
37
|
+
* object is published publicly so a consumer can size fixtures without
|
|
38
|
+
* restating a literal. `VERIFICATION_LIMITS` is extended with the payload
|
|
39
|
+
* budgets by P11 and the timeout budgets by P12; the canonical plan/receipt
|
|
40
|
+
* byte budgets and the aggregate stage sums are enforced at runtime and are
|
|
41
|
+
* listed here once those phases land.
|
|
42
|
+
*/
|
|
43
|
+
export const VERIFICATION_LIMITS = Object.freeze({
|
|
44
|
+
commands: 128,
|
|
45
|
+
results: 128,
|
|
46
|
+
argsPerCommand: 64,
|
|
47
|
+
idChars: 128,
|
|
48
|
+
pathChars: 1024,
|
|
49
|
+
argChars: 4096,
|
|
50
|
+
skipReasonChars: 1024,
|
|
51
|
+
evidenceRefChars: 1024,
|
|
52
|
+
planBytes: 256 * 1024,
|
|
53
|
+
receiptBytes: 512 * 1024,
|
|
54
|
+
// D14 time bounds, in milliseconds: 10/15 minutes for the fast stage and
|
|
55
|
+
// 60/120 minutes for the full stage (per command / whole stage).
|
|
56
|
+
fastCommandTimeoutMs: 10 * 60_000,
|
|
57
|
+
fastStageTimeoutMs: 15 * 60_000,
|
|
58
|
+
fullCommandTimeoutMs: 60 * 60_000,
|
|
59
|
+
fullStageTimeoutMs: 120 * 60_000,
|
|
60
|
+
diagnostics: 50,
|
|
61
|
+
});
|
|
62
|
+
/**
|
|
63
|
+
* D16 — the closed diagnostic vocabulary. Every validation failure row carries
|
|
64
|
+
* exactly one of these codes plus an RFC 6901 path; adding a code is a reviewed,
|
|
65
|
+
* versioned contract change, and no code ever names the submitted value.
|
|
66
|
+
*/
|
|
67
|
+
export const VERIFICATION_DIAGNOSTIC_CODES = Object.freeze([
|
|
68
|
+
"invalid-type",
|
|
69
|
+
"missing-field",
|
|
70
|
+
"unknown-field",
|
|
71
|
+
"invalid-value",
|
|
72
|
+
"limit-exceeded",
|
|
73
|
+
"duplicate-id",
|
|
74
|
+
"unknown-command",
|
|
75
|
+
"invalid-order",
|
|
76
|
+
"invalid-stage",
|
|
77
|
+
"invalid-exit-state",
|
|
78
|
+
"invalid-evidence",
|
|
79
|
+
"invalid-skip",
|
|
80
|
+
"invalid-fail-fast",
|
|
81
|
+
"digest-mismatch",
|
|
82
|
+
"verdict-mismatch",
|
|
83
|
+
"budget-exceeded",
|
|
84
|
+
]);
|
|
85
|
+
export function createVerificationDiagnosticSink(limit = VERIFICATION_LIMITS.diagnostics) {
|
|
86
|
+
const rows = [];
|
|
87
|
+
let dropped = 0;
|
|
88
|
+
return {
|
|
89
|
+
push(code, path) {
|
|
90
|
+
if (rows.length < limit)
|
|
91
|
+
rows.push({ code, path });
|
|
92
|
+
else
|
|
93
|
+
dropped += 1;
|
|
94
|
+
},
|
|
95
|
+
count() {
|
|
96
|
+
return rows.length + dropped;
|
|
97
|
+
},
|
|
98
|
+
finish() {
|
|
99
|
+
return {
|
|
100
|
+
diagnostics: Object.freeze(rows.map((row) => Object.freeze({ code: row.code, path: row.path }))),
|
|
101
|
+
truncated: dropped > 0,
|
|
102
|
+
};
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
// ---------------------------------------------------------------------------
|
|
107
|
+
// VerificationPlan v1
|
|
108
|
+
// ---------------------------------------------------------------------------
|
|
109
|
+
const COMMAND_SPEC = {
|
|
110
|
+
description: "A single verification command within a plan.",
|
|
111
|
+
fields: [
|
|
112
|
+
{
|
|
113
|
+
key: "id",
|
|
114
|
+
type: "string",
|
|
115
|
+
minLength: 1,
|
|
116
|
+
maxLength: VERIFICATION_LIMITS.idChars,
|
|
117
|
+
nulFree: true,
|
|
118
|
+
description: `Stable, non-empty, NUL-free, at most ${VERIFICATION_LIMITS.idChars} chars, unique within the plan.`,
|
|
119
|
+
},
|
|
120
|
+
{ key: "stage", type: "enum", enum: VERIFICATION_STAGES, description: "Verification stage." },
|
|
121
|
+
{
|
|
122
|
+
key: "executable",
|
|
123
|
+
type: "string",
|
|
124
|
+
minLength: 1,
|
|
125
|
+
maxLength: VERIFICATION_LIMITS.pathChars,
|
|
126
|
+
nulFree: true,
|
|
127
|
+
description: `Non-empty executable path; NUL-free, at most ${VERIFICATION_LIMITS.pathChars} chars. Never a shell string.`,
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
key: "args",
|
|
131
|
+
type: "stringArray",
|
|
132
|
+
itemNulFree: true,
|
|
133
|
+
maxItems: VERIFICATION_LIMITS.argsPerCommand,
|
|
134
|
+
itemMaxLength: VERIFICATION_LIMITS.argChars,
|
|
135
|
+
description: `Ordered arguments; at most ${VERIFICATION_LIMITS.argsPerCommand} items, each NUL-free and at most ${VERIFICATION_LIMITS.argChars} chars.`,
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
key: "workingDirectoryPolicy",
|
|
139
|
+
type: "enum",
|
|
140
|
+
enum: WORKING_DIRECTORY_POLICIES,
|
|
141
|
+
description: "Working directory policy.",
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
key: "workingDirectory",
|
|
145
|
+
type: "nullable",
|
|
146
|
+
nullableOf: "string",
|
|
147
|
+
minLength: 1,
|
|
148
|
+
maxLength: VERIFICATION_LIMITS.pathChars,
|
|
149
|
+
nulFree: true,
|
|
150
|
+
rules: [
|
|
151
|
+
{ regex: "^(?!/)", message: 'must not be an absolute path (leading "/")' },
|
|
152
|
+
{ regex: "^(?![A-Za-z]:)", message: "must not be a Windows drive-letter path" },
|
|
153
|
+
{ regex: "^(?!.*\\\\)", message: "must not contain backslashes" },
|
|
154
|
+
{ regex: "^(?!.*(\\/|^)\\.\\.($|\\/))", message: 'must not contain ".." segments (path traversal)' },
|
|
155
|
+
],
|
|
156
|
+
description: "Null iff candidate-root; validated relative path iff relative-path.",
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
key: "timeoutMs",
|
|
160
|
+
type: "integer",
|
|
161
|
+
exclusiveMinimum: 0,
|
|
162
|
+
// The stage-independent ceiling; `fast-command-timeout` tightens it for the
|
|
163
|
+
// fast stage, exactly as the projection's conditional fragment does (AC10).
|
|
164
|
+
maximum: VERIFICATION_LIMITS.fullCommandTimeoutMs,
|
|
165
|
+
description: `Positive integer timeout in milliseconds; at most ${VERIFICATION_LIMITS.fullCommandTimeoutMs} (full stage) or ${VERIFICATION_LIMITS.fastCommandTimeoutMs} (fast stage).`,
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
key: "stopOnFailure",
|
|
169
|
+
type: "boolean",
|
|
170
|
+
description: "Whether to stop executing subsequent commands on failure.",
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
key: "costClass",
|
|
174
|
+
type: "enum",
|
|
175
|
+
enum: VERIFICATION_COST_CLASSES,
|
|
176
|
+
description: "Project-declared cost class.",
|
|
177
|
+
},
|
|
178
|
+
],
|
|
179
|
+
rules: [
|
|
180
|
+
{
|
|
181
|
+
id: "working-directory-null-at-root",
|
|
182
|
+
kind: "null-when",
|
|
183
|
+
projectable: true,
|
|
184
|
+
when: { field: "workingDirectoryPolicy", equals: "candidate-root" },
|
|
185
|
+
fields: ["workingDirectory"],
|
|
186
|
+
description: "workingDirectory is null when workingDirectoryPolicy is candidate-root.",
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
id: "fast-command-timeout",
|
|
190
|
+
kind: "maximum-when",
|
|
191
|
+
projectable: true,
|
|
192
|
+
when: { field: "stage", equals: "fast" },
|
|
193
|
+
fields: ["timeoutMs"],
|
|
194
|
+
maximum: VERIFICATION_LIMITS.fastCommandTimeoutMs,
|
|
195
|
+
description: `A fast-stage command times out within ${VERIFICATION_LIMITS.fastCommandTimeoutMs} ms (D14).`,
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
id: "working-directory-set-for-relative-path",
|
|
199
|
+
kind: "non-null-when",
|
|
200
|
+
projectable: true,
|
|
201
|
+
when: { field: "workingDirectoryPolicy", equals: "relative-path" },
|
|
202
|
+
fields: ["workingDirectory"],
|
|
203
|
+
description: "workingDirectory is a validated relative path when workingDirectoryPolicy is relative-path.",
|
|
204
|
+
},
|
|
205
|
+
],
|
|
206
|
+
};
|
|
207
|
+
// ---------------------------------------------------------------------------
|
|
208
|
+
// VerificationReceipt v1
|
|
209
|
+
// ---------------------------------------------------------------------------
|
|
210
|
+
const EVIDENCE_SPEC = {
|
|
211
|
+
description: "Bounded reference to captured evidence (stdout/stderr output).",
|
|
212
|
+
fields: [
|
|
213
|
+
{
|
|
214
|
+
key: "ref",
|
|
215
|
+
type: "string",
|
|
216
|
+
minLength: 1,
|
|
217
|
+
maxLength: VERIFICATION_LIMITS.evidenceRefChars,
|
|
218
|
+
nulFree: true,
|
|
219
|
+
violationCode: "invalid-evidence",
|
|
220
|
+
description: "Non-empty opaque pointer to stored evidence; NUL-free.",
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
key: "bytes",
|
|
224
|
+
type: "integer",
|
|
225
|
+
minimum: 0,
|
|
226
|
+
description: "Size of the captured evidence in bytes.",
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
key: "sha256",
|
|
230
|
+
type: "string",
|
|
231
|
+
pattern: LOWERCASE_64HEX_PATTERN,
|
|
232
|
+
violationCode: "invalid-evidence",
|
|
233
|
+
description: "Lowercase 64-hex SHA-256 digest of the captured evidence.",
|
|
234
|
+
},
|
|
235
|
+
],
|
|
236
|
+
rules: [],
|
|
237
|
+
};
|
|
238
|
+
const RESULT_SPEC = {
|
|
239
|
+
description: "Per-command verification result.",
|
|
240
|
+
fields: [
|
|
241
|
+
{
|
|
242
|
+
key: "commandId",
|
|
243
|
+
type: "string",
|
|
244
|
+
minLength: 1,
|
|
245
|
+
maxLength: VERIFICATION_LIMITS.idChars,
|
|
246
|
+
nulFree: true,
|
|
247
|
+
description: `Must exist in the bound plan's commands; NUL-free, at most ${VERIFICATION_LIMITS.idChars} chars.`,
|
|
248
|
+
},
|
|
249
|
+
{
|
|
250
|
+
key: "status",
|
|
251
|
+
type: "enum",
|
|
252
|
+
enum: VERIFICATION_COMMAND_STATUSES,
|
|
253
|
+
description: "The terminal status of this command execution.",
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
key: "exitCode",
|
|
257
|
+
type: "nullable",
|
|
258
|
+
nullableOf: "integer",
|
|
259
|
+
description: "Per the D4 exit-code/signal matrix.",
|
|
260
|
+
},
|
|
261
|
+
{
|
|
262
|
+
key: "signal",
|
|
263
|
+
type: "nullable",
|
|
264
|
+
nullableOf: "string",
|
|
265
|
+
minLength: 1,
|
|
266
|
+
// F50 hardening, not a D14 limit: the SPEC's ceiling list does not name
|
|
267
|
+
// `signal`, so this bound stays local to the field it protects.
|
|
268
|
+
maxLength: 1024,
|
|
269
|
+
nulFree: true,
|
|
270
|
+
description: "Per the D4 exit-code/signal matrix; NUL-free, at most 1024 chars.",
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
key: "startedAt",
|
|
274
|
+
type: "string",
|
|
275
|
+
pattern: ISO_8601_PATTERN,
|
|
276
|
+
description: "ISO-8601 UTC start timestamp.",
|
|
277
|
+
},
|
|
278
|
+
{
|
|
279
|
+
key: "endedAt",
|
|
280
|
+
type: "string",
|
|
281
|
+
pattern: ISO_8601_PATTERN,
|
|
282
|
+
description: "ISO-8601 UTC end timestamp, >= startedAt.",
|
|
283
|
+
},
|
|
284
|
+
{
|
|
285
|
+
key: "stdout",
|
|
286
|
+
type: "nullable",
|
|
287
|
+
nullableOf: "object",
|
|
288
|
+
specName: "EvidenceReferenceV1",
|
|
289
|
+
description: "Bounded stdout evidence reference or null.",
|
|
290
|
+
},
|
|
291
|
+
{
|
|
292
|
+
key: "stderr",
|
|
293
|
+
type: "nullable",
|
|
294
|
+
nullableOf: "object",
|
|
295
|
+
specName: "EvidenceReferenceV1",
|
|
296
|
+
description: "Bounded stderr evidence reference or null.",
|
|
297
|
+
},
|
|
298
|
+
{
|
|
299
|
+
key: "skipReason",
|
|
300
|
+
type: "nullable",
|
|
301
|
+
nullableOf: "string",
|
|
302
|
+
minLength: 1,
|
|
303
|
+
maxLength: VERIFICATION_LIMITS.skipReasonChars,
|
|
304
|
+
nulFree: true,
|
|
305
|
+
description: "Null on non-skipped rows; non-empty NUL-free string ≤ 1024 chars on skipped.",
|
|
306
|
+
},
|
|
307
|
+
],
|
|
308
|
+
rules: [
|
|
309
|
+
{
|
|
310
|
+
id: "d4-exactly-one-terminal-result",
|
|
311
|
+
kind: "exactly-one-non-null",
|
|
312
|
+
projectable: true,
|
|
313
|
+
when: { field: "status", in: ["passed", "failed"] },
|
|
314
|
+
fields: ["exitCode", "signal"],
|
|
315
|
+
description: "D4 exit-code/signal matrix: passed/failed carry exactly one of exitCode (integer) or signal (non-empty string).",
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
id: "d4-timed-out-has-no-exit-code",
|
|
319
|
+
kind: "null-when",
|
|
320
|
+
projectable: true,
|
|
321
|
+
code: "invalid-exit-state",
|
|
322
|
+
when: { field: "status", equals: "timed-out" },
|
|
323
|
+
fields: ["exitCode"],
|
|
324
|
+
description: "D4: timed-out carries exitCode null and an optional captured kill signal.",
|
|
325
|
+
},
|
|
326
|
+
{
|
|
327
|
+
id: "d4-no-terminal-result",
|
|
328
|
+
kind: "null-when",
|
|
329
|
+
projectable: true,
|
|
330
|
+
code: "invalid-exit-state",
|
|
331
|
+
when: { field: "status", in: ["infrastructure-error", "skipped"] },
|
|
332
|
+
fields: ["exitCode", "signal"],
|
|
333
|
+
description: "D4: infrastructure-error and skipped carry neither exitCode nor signal.",
|
|
334
|
+
},
|
|
335
|
+
{
|
|
336
|
+
id: "skip-reason-only-when-skipped",
|
|
337
|
+
kind: "null-when",
|
|
338
|
+
projectable: true,
|
|
339
|
+
code: "invalid-skip",
|
|
340
|
+
when: { field: "status", notIn: ["skipped"] },
|
|
341
|
+
fields: ["skipReason"],
|
|
342
|
+
description: "Non-skipped rows carry skipReason null.",
|
|
343
|
+
},
|
|
344
|
+
{
|
|
345
|
+
id: "timestamp-order",
|
|
346
|
+
kind: "timestamp-order",
|
|
347
|
+
projectable: false,
|
|
348
|
+
fields: ["startedAt", "endedAt"],
|
|
349
|
+
description: "endedAt must not precede startedAt.",
|
|
350
|
+
},
|
|
351
|
+
{
|
|
352
|
+
id: "timestamp-calendar-valid",
|
|
353
|
+
kind: "calendar-roundtrip",
|
|
354
|
+
projectable: false,
|
|
355
|
+
fields: ["startedAt", "endedAt"],
|
|
356
|
+
description: "ISO-8601 timestamps must be calendar-valid (rejects 2025-99-99T99:99:99Z).",
|
|
357
|
+
},
|
|
358
|
+
],
|
|
359
|
+
};
|
|
360
|
+
/** VerificationPlan v1 root object: `contract` + non-empty `commands`. */
|
|
361
|
+
const PLAN_ROOT_SPEC = {
|
|
362
|
+
description: "An ordered, non-empty command list for staged verification (fast/full).",
|
|
363
|
+
fields: [
|
|
364
|
+
{
|
|
365
|
+
key: "contract",
|
|
366
|
+
type: "const",
|
|
367
|
+
value: VERIFICATION_PLAN_CONTRACT_ID,
|
|
368
|
+
description: "Contract identifier.",
|
|
369
|
+
},
|
|
370
|
+
{
|
|
371
|
+
key: "commands",
|
|
372
|
+
type: "array",
|
|
373
|
+
specName: "VerificationCommandV1",
|
|
374
|
+
minItems: 1,
|
|
375
|
+
maxItems: VERIFICATION_LIMITS.commands,
|
|
376
|
+
description: `Non-empty command list of at most ${VERIFICATION_LIMITS.commands} commands, in declared order.`,
|
|
377
|
+
},
|
|
378
|
+
],
|
|
379
|
+
rules: [
|
|
380
|
+
{
|
|
381
|
+
id: "unique-command-ids",
|
|
382
|
+
kind: "unique",
|
|
383
|
+
projectable: false,
|
|
384
|
+
collection: "commands",
|
|
385
|
+
fields: ["id"],
|
|
386
|
+
description: "Command ids are unique within the plan.",
|
|
387
|
+
},
|
|
388
|
+
{
|
|
389
|
+
id: "fast-stage-aggregate-budget",
|
|
390
|
+
kind: "stage-aggregate-budget",
|
|
391
|
+
projectable: false,
|
|
392
|
+
collection: "commands",
|
|
393
|
+
when: { field: "stage", equals: "fast" },
|
|
394
|
+
fields: ["timeoutMs"],
|
|
395
|
+
maximum: VERIFICATION_LIMITS.fastStageTimeoutMs,
|
|
396
|
+
description: `Declared fast-stage timeouts sum to at most ${VERIFICATION_LIMITS.fastStageTimeoutMs} ms (D14).`,
|
|
397
|
+
},
|
|
398
|
+
{
|
|
399
|
+
id: "full-stage-aggregate-budget",
|
|
400
|
+
kind: "stage-aggregate-budget",
|
|
401
|
+
projectable: false,
|
|
402
|
+
collection: "commands",
|
|
403
|
+
when: { field: "stage", equals: "full" },
|
|
404
|
+
fields: ["timeoutMs"],
|
|
405
|
+
maximum: VERIFICATION_LIMITS.fullStageTimeoutMs,
|
|
406
|
+
description: `Declared full-stage timeouts sum to at most ${VERIFICATION_LIMITS.fullStageTimeoutMs} ms (D14).`,
|
|
407
|
+
},
|
|
408
|
+
],
|
|
409
|
+
};
|
|
410
|
+
/** VerificationReceipt v1 root object. */
|
|
411
|
+
const RECEIPT_ROOT_SPEC = {
|
|
412
|
+
description: "A verification receipt bound to a plan, candidate snapshot, and acceptance fingerprint.",
|
|
413
|
+
fields: [
|
|
414
|
+
{
|
|
415
|
+
key: "contract",
|
|
416
|
+
type: "const",
|
|
417
|
+
value: VERIFICATION_RECEIPT_CONTRACT_ID,
|
|
418
|
+
description: "Contract identifier.",
|
|
419
|
+
},
|
|
420
|
+
{
|
|
421
|
+
key: "planDigest",
|
|
422
|
+
type: "string",
|
|
423
|
+
pattern: LOWERCASE_64HEX_PATTERN,
|
|
424
|
+
description: "Lowercase 64-hex SHA-256 digest of the bound plan.",
|
|
425
|
+
},
|
|
426
|
+
{
|
|
427
|
+
key: "candidateSnapshotDigest",
|
|
428
|
+
type: "string",
|
|
429
|
+
pattern: LOWERCASE_64HEX_PATTERN,
|
|
430
|
+
description: "Lowercase 64-hex SHA-256 from #138's candidate-snapshot digest.",
|
|
431
|
+
},
|
|
432
|
+
{
|
|
433
|
+
key: "acceptanceFingerprint",
|
|
434
|
+
type: "string",
|
|
435
|
+
pattern: LOWERCASE_64HEX_PATTERN,
|
|
436
|
+
description: "Lowercase 64-hex acceptance fingerprint from #138.",
|
|
437
|
+
},
|
|
438
|
+
{ key: "stageRequested", type: "enum", enum: VERIFICATION_STAGES, description: "Which stage was requested." },
|
|
439
|
+
{
|
|
440
|
+
key: "results",
|
|
441
|
+
type: "array",
|
|
442
|
+
specName: "VerificationResultV1",
|
|
443
|
+
maxItems: VERIFICATION_LIMITS.results,
|
|
444
|
+
description: `Ordered per-command results; at most ${VERIFICATION_LIMITS.results} rows in declared order.`,
|
|
445
|
+
},
|
|
446
|
+
{
|
|
447
|
+
key: "verdict",
|
|
448
|
+
type: "enum",
|
|
449
|
+
enum: VERIFICATION_VERDICTS,
|
|
450
|
+
description: "Must equal deriveVerificationVerdict.",
|
|
451
|
+
},
|
|
452
|
+
],
|
|
453
|
+
rules: [
|
|
454
|
+
{
|
|
455
|
+
id: "unique-result-command-ids",
|
|
456
|
+
kind: "unique",
|
|
457
|
+
projectable: false,
|
|
458
|
+
collection: "results",
|
|
459
|
+
fields: ["commandId"],
|
|
460
|
+
description: "Each declared command carries at most one result row.",
|
|
461
|
+
},
|
|
462
|
+
],
|
|
463
|
+
};
|
|
464
|
+
export const VERIFICATION_CONTRACT = Object.freeze({
|
|
465
|
+
plan: Object.freeze({
|
|
466
|
+
contractId: VERIFICATION_PLAN_CONTRACT_ID,
|
|
467
|
+
fileName: "verification-plan.schema.json",
|
|
468
|
+
title: "VerificationPlan v1",
|
|
469
|
+
description: "An ordered, non-empty command list for staged verification (fast/full).",
|
|
470
|
+
authority: "validateVerificationPlanV1",
|
|
471
|
+
rootLabel: "plan",
|
|
472
|
+
root: PLAN_ROOT_SPEC,
|
|
473
|
+
objects: Object.freeze({ VerificationCommandV1: COMMAND_SPEC }),
|
|
474
|
+
}),
|
|
475
|
+
receipt: Object.freeze({
|
|
476
|
+
contractId: VERIFICATION_RECEIPT_CONTRACT_ID,
|
|
477
|
+
fileName: "verification-receipt.schema.json",
|
|
478
|
+
title: "VerificationReceipt v1",
|
|
479
|
+
description: "A verification receipt bound to a plan, candidate snapshot, and acceptance fingerprint.",
|
|
480
|
+
authority: "validateVerificationReceiptAgainstPlan",
|
|
481
|
+
rootLabel: "receipt",
|
|
482
|
+
root: RECEIPT_ROOT_SPEC,
|
|
483
|
+
objects: Object.freeze({
|
|
484
|
+
EvidenceReferenceV1: EVIDENCE_SPEC,
|
|
485
|
+
VerificationResultV1: RESULT_SPEC,
|
|
486
|
+
}),
|
|
487
|
+
}),
|
|
488
|
+
});
|
|
489
|
+
/**
|
|
490
|
+
* Non-validating own-property projection of a contract value.
|
|
491
|
+
*
|
|
492
|
+
* `canonicalize*`/`digest*` run this first so a digest always describes the
|
|
493
|
+
* normalized DTO — never an accidental prototype value or an undeclared field.
|
|
494
|
+
* It performs no checks (callers validate first) and copies only declared own
|
|
495
|
+
* properties, so the walk is one pass with no regex and no error buffers.
|
|
496
|
+
*/
|
|
497
|
+
export function projectStructure(contract, spec, value) {
|
|
498
|
+
if (!isPlainRecord(value))
|
|
499
|
+
return value;
|
|
500
|
+
const projected = {};
|
|
501
|
+
for (const field of spec.fields) {
|
|
502
|
+
if (!Object.prototype.hasOwnProperty.call(value, field.key))
|
|
503
|
+
continue;
|
|
504
|
+
const raw = value[field.key];
|
|
505
|
+
if (field.type === "array") {
|
|
506
|
+
const nested = verificationObjectSpec(contract, field.specName ?? "");
|
|
507
|
+
projected[field.key] = Array.isArray(raw)
|
|
508
|
+
? raw.map((item) => projectStructure(contract, nested, item))
|
|
509
|
+
: raw;
|
|
510
|
+
}
|
|
511
|
+
else if (field.type === "nullable" && field.nullableOf === "object" && raw !== null) {
|
|
512
|
+
const nested = verificationObjectSpec(contract, field.specName ?? "");
|
|
513
|
+
projected[field.key] = projectStructure(contract, nested, raw);
|
|
514
|
+
}
|
|
515
|
+
else {
|
|
516
|
+
projected[field.key] = raw;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
return projected;
|
|
520
|
+
}
|
|
521
|
+
/** Nested object spec by `$defs` name. */
|
|
522
|
+
function verificationObjectSpec(contract, defName) {
|
|
523
|
+
const spec = contract.objects[defName];
|
|
524
|
+
if (!spec)
|
|
525
|
+
throw new Error(`unknown verification object ${defName}`);
|
|
526
|
+
return spec;
|
|
527
|
+
}
|
|
528
|
+
/** A JSON object literal (or null-prototype object) — never an array or a class instance. */
|
|
529
|
+
function isPlainRecord(value) {
|
|
530
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
531
|
+
return false;
|
|
532
|
+
const proto = Object.getPrototypeOf(value);
|
|
533
|
+
return proto === Object.prototype || proto === null;
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* F97 — the submitted accessor that cannot be observed as plain data.
|
|
537
|
+
*
|
|
538
|
+
* Carries the RFC 6901 pointer of the frame that owns the failing read plus the
|
|
539
|
+
* D16 code that frame earns: `invalid-type` for a walk failure (F92 parity) or
|
|
540
|
+
* `limit-exceeded` for the canonical byte budget (F99). The message never names
|
|
541
|
+
* the submitted key or value.
|
|
542
|
+
*/
|
|
543
|
+
class VerificationInputCaptureError extends Error {
|
|
544
|
+
pointer;
|
|
545
|
+
code;
|
|
546
|
+
constructor(pointer, code = "invalid-type") {
|
|
547
|
+
super(code === "limit-exceeded" ? "verification input: over canonical byte budget" : "verification input: unreadable accessor");
|
|
548
|
+
this.pointer = pointer;
|
|
549
|
+
this.code = code;
|
|
550
|
+
this.name = "VerificationInputCaptureError";
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
/** True when the fragment survives canonical serialization as pure ASCII. */
|
|
554
|
+
function isAscii(text) {
|
|
555
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
556
|
+
if (text.charCodeAt(index) > 0x7f)
|
|
557
|
+
return false;
|
|
558
|
+
}
|
|
559
|
+
return true;
|
|
560
|
+
}
|
|
561
|
+
function charge(context, units) {
|
|
562
|
+
if (!context.applicable)
|
|
563
|
+
return;
|
|
564
|
+
context.size += units;
|
|
565
|
+
if (context.budget !== null && context.size > context.budget) {
|
|
566
|
+
throw new VerificationInputCaptureError("", "limit-exceeded");
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
/** UTF-16 length of a leaf's canonical fragment — the unit `canonicalJSONValue` emits. */
|
|
570
|
+
function canonicalLeafUnits(value) {
|
|
571
|
+
return JSON.stringify(value).length;
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* F97 — capture one submitted value as data, reading every accessor exactly once.
|
|
575
|
+
*
|
|
576
|
+
* Plain objects become own-enumerable-key copies, arrays become element copies, and
|
|
577
|
+
* every container is frozen, so no later pass can observe a different document than
|
|
578
|
+
* the one the entry decided on. Two submissions are deliberately NOT rewritten by the
|
|
579
|
+
* capture, because the structural walk — not the capture — owns every refusal:
|
|
580
|
+
* - a non-plain object (class instance, array-like prototype) is copied by
|
|
581
|
+
* reference, so its frame still refuses it as `invalid-type`;
|
|
582
|
+
* - an unsupported leaf (function, symbol, bigint, `undefined`, non-finite number)
|
|
583
|
+
* is copied as submitted, so the field check still refuses it.
|
|
584
|
+
* An accessor that throws is reported at the frame that owns the read.
|
|
585
|
+
*/
|
|
586
|
+
function captureContractValue(value, pointer, context) {
|
|
587
|
+
if (value === null) {
|
|
588
|
+
charge(context, 4);
|
|
589
|
+
return null;
|
|
590
|
+
}
|
|
591
|
+
if (typeof value === "string") {
|
|
592
|
+
if (!context.nonAscii)
|
|
593
|
+
context.nonAscii = !isAscii(value);
|
|
594
|
+
charge(context, canonicalLeafUnits(value));
|
|
595
|
+
return value;
|
|
596
|
+
}
|
|
597
|
+
if (typeof value === "boolean") {
|
|
598
|
+
charge(context, canonicalLeafUnits(value));
|
|
599
|
+
return value;
|
|
600
|
+
}
|
|
601
|
+
if (typeof value === "number") {
|
|
602
|
+
if (!Number.isFinite(value)) {
|
|
603
|
+
// The canonical serializer has no form for it: the budget can no longer be
|
|
604
|
+
// measured exactly, and the walk owns the refusal (F91-era behaviour).
|
|
605
|
+
context.applicable = false;
|
|
606
|
+
return value;
|
|
607
|
+
}
|
|
608
|
+
charge(context, canonicalLeafUnits(value));
|
|
609
|
+
return value;
|
|
610
|
+
}
|
|
611
|
+
if (typeof value !== "object") {
|
|
612
|
+
// function / symbol / bigint / undefined — outside the canonical form.
|
|
613
|
+
context.applicable = false;
|
|
614
|
+
return value;
|
|
615
|
+
}
|
|
616
|
+
if (Array.isArray(value)) {
|
|
617
|
+
const items = [];
|
|
618
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
619
|
+
let item;
|
|
620
|
+
try {
|
|
621
|
+
item = Object.freeze(captureContractValue(value[index], `${pointer}/${index}`, context));
|
|
622
|
+
}
|
|
623
|
+
catch (failure) {
|
|
624
|
+
throw failure instanceof VerificationInputCaptureError
|
|
625
|
+
? failure
|
|
626
|
+
: new VerificationInputCaptureError(pointer);
|
|
627
|
+
}
|
|
628
|
+
items.push(item);
|
|
629
|
+
}
|
|
630
|
+
charge(context, 2 + Math.max(items.length - 1, 0));
|
|
631
|
+
return Object.freeze(items);
|
|
632
|
+
}
|
|
633
|
+
const proto = Object.getPrototypeOf(value);
|
|
634
|
+
if (proto !== Object.prototype && proto !== null) {
|
|
635
|
+
// Copied by reference; its canonical size is unknowable and its frame is
|
|
636
|
+
// refused by the walk, so the measure is no longer exact from here on.
|
|
637
|
+
context.applicable = false;
|
|
638
|
+
return value;
|
|
639
|
+
}
|
|
640
|
+
const source = value;
|
|
641
|
+
const keys = Object.keys(source);
|
|
642
|
+
const captured = {};
|
|
643
|
+
let keyUnits = 0;
|
|
644
|
+
for (const key of keys) {
|
|
645
|
+
let item;
|
|
646
|
+
try {
|
|
647
|
+
item = Object.freeze(captureContractValue(source[key], `${pointer}/${key}`, context));
|
|
648
|
+
}
|
|
649
|
+
catch (failure) {
|
|
650
|
+
// The frame that owns the read answers for it — the same pointer rule F92
|
|
651
|
+
// established for a walk failure, and it also bounds a self-referential
|
|
652
|
+
// submission (the recursion's RangeError lands on the frame that opened it).
|
|
653
|
+
throw failure instanceof VerificationInputCaptureError
|
|
654
|
+
? failure
|
|
655
|
+
: new VerificationInputCaptureError(pointer);
|
|
656
|
+
}
|
|
657
|
+
if (!context.nonAscii)
|
|
658
|
+
context.nonAscii = !isAscii(key);
|
|
659
|
+
keyUnits += JSON.stringify(key).length + 1;
|
|
660
|
+
// `defineProperty`, never assignment: a submitted own `__proto__` key (what
|
|
661
|
+
// `JSON.parse` produces) is an ordinary data property, but plain assignment
|
|
662
|
+
// would hit the inherited `Object.prototype.__proto__` setter and drop the key
|
|
663
|
+
// the structural pass exists to refuse (P7).
|
|
664
|
+
Object.defineProperty(captured, key, {
|
|
665
|
+
value: item,
|
|
666
|
+
enumerable: true,
|
|
667
|
+
writable: false,
|
|
668
|
+
configurable: false,
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
charge(context, 2 + keyUnits + Math.max(keys.length - 1, 0));
|
|
672
|
+
return Object.freeze(captured);
|
|
673
|
+
}
|
|
674
|
+
export function captureVerificationInput(value, budgetBytes) {
|
|
675
|
+
const context = {
|
|
676
|
+
budget: budgetBytes ?? null,
|
|
677
|
+
size: 0,
|
|
678
|
+
applicable: true,
|
|
679
|
+
nonAscii: false,
|
|
680
|
+
};
|
|
681
|
+
try {
|
|
682
|
+
return {
|
|
683
|
+
ok: true,
|
|
684
|
+
value: Object.freeze(captureContractValue(value, "", context)),
|
|
685
|
+
measureExact: context.applicable && !context.nonAscii,
|
|
686
|
+
};
|
|
687
|
+
}
|
|
688
|
+
catch (failure) {
|
|
689
|
+
if (failure instanceof VerificationInputCaptureError) {
|
|
690
|
+
return { ok: false, code: failure.code, pointer: failure.pointer };
|
|
691
|
+
}
|
|
692
|
+
return { ok: false, code: "invalid-type", pointer: "" };
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
function verificationFieldSpec(spec, key) {
|
|
696
|
+
return spec.fields.find((field) => field.key === key);
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* The `when` predicates of one rule as a list: the single-condition form is the
|
|
700
|
+
* one-element case, so every reader handles exactly one shape.
|
|
701
|
+
*
|
|
702
|
+
* A rule with no `when` applies unconditionally; `stage-aggregate-budget` and
|
|
703
|
+
* `exactly-one-non-null` report the discriminator they fired on, which only the
|
|
704
|
+
* single-condition form can name (see `VerificationCrossRule.when`).
|
|
705
|
+
*/
|
|
706
|
+
export function whenConditions(when) {
|
|
707
|
+
if (!when)
|
|
708
|
+
return [];
|
|
709
|
+
return "allOf" in when ? when.allOf : [when];
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* True when a cross rule's `when` predicate(s) match the submitted siblings.
|
|
713
|
+
*
|
|
714
|
+
* `notIn` only fires for values inside the sibling's declared vocabulary, so a
|
|
715
|
+
* garbage enum value yields one vocabulary error instead of a cascade. A
|
|
716
|
+
* conjunction (`allOf`) holds when EVERY condition holds, and a condition whose
|
|
717
|
+
* sibling is absent does not hold — an incomplete document is refused by the
|
|
718
|
+
* field walk, never by a rule that silently treated the missing sibling as equal.
|
|
719
|
+
*/
|
|
720
|
+
function ruleApplies(spec, rule, obj) {
|
|
721
|
+
const conditions = whenConditions(rule.when);
|
|
722
|
+
if (conditions.length === 0)
|
|
723
|
+
return true;
|
|
724
|
+
return conditions.every((when) => conditionApplies(spec, when, obj));
|
|
725
|
+
}
|
|
726
|
+
function conditionApplies(spec, when, obj) {
|
|
727
|
+
if (!Object.prototype.hasOwnProperty.call(obj, when.field))
|
|
728
|
+
return false;
|
|
729
|
+
const value = obj[when.field];
|
|
730
|
+
// A `minItems` predicate reads an ARRAY sibling, not a string one; the two
|
|
731
|
+
// predicate families never mix on one rule.
|
|
732
|
+
if (when.minItems !== undefined) {
|
|
733
|
+
return Array.isArray(value) && value.length >= when.minItems;
|
|
734
|
+
}
|
|
735
|
+
if (typeof value !== "string")
|
|
736
|
+
return false;
|
|
737
|
+
if (when.equals !== undefined)
|
|
738
|
+
return value === when.equals;
|
|
739
|
+
if (when.in !== undefined)
|
|
740
|
+
return when.in.includes(value);
|
|
741
|
+
if (when.notIn !== undefined) {
|
|
742
|
+
const vocabulary = verificationFieldSpec(spec, when.field)?.enum ?? [];
|
|
743
|
+
return vocabulary.includes(value) && !when.notIn.includes(value);
|
|
744
|
+
}
|
|
745
|
+
return false;
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* Diagnostic code for a value that is not the declared shape of `field`: a
|
|
749
|
+
* const/enum mismatch is a wrong VALUE of an otherwise correct type, everything
|
|
750
|
+
* else is a type failure. D16 keeps the row to code + path, so a submitted value
|
|
751
|
+
* is never carried out of the validator.
|
|
752
|
+
*/
|
|
753
|
+
function typeCode(field) {
|
|
754
|
+
return field.type === "const" || field.type === "enum" ? "invalid-value" : "invalid-type";
|
|
755
|
+
}
|
|
756
|
+
/** Specialize a value-content failure to the field's own code (D5 evidence). */
|
|
757
|
+
function valueCode(field) {
|
|
758
|
+
return field.violationCode ?? "invalid-value";
|
|
759
|
+
}
|
|
760
|
+
/**
|
|
761
|
+
* Compiled-pattern memo.
|
|
762
|
+
*
|
|
763
|
+
* Every pattern validated here is declared once in the frozen field-spec table
|
|
764
|
+
* above, so re-parsing it per string field of every walked document was pure
|
|
765
|
+
* repeat work; the cache holds that fixed handful of entries for the process
|
|
766
|
+
* lifetime. Safe to share instances: these patterns are compiled without `g`/`y`,
|
|
767
|
+
* so `test()` carries no lastIndex state between calls.
|
|
768
|
+
*/
|
|
769
|
+
const _compiledPatterns = new Map();
|
|
770
|
+
function compiledPattern(pattern) {
|
|
771
|
+
let re = _compiledPatterns.get(pattern);
|
|
772
|
+
if (re === undefined) {
|
|
773
|
+
re = new RegExp(pattern);
|
|
774
|
+
_compiledPatterns.set(pattern, re);
|
|
775
|
+
}
|
|
776
|
+
return re;
|
|
777
|
+
}
|
|
778
|
+
function checkString(field, value, path, sink) {
|
|
779
|
+
if (typeof value !== "string") {
|
|
780
|
+
sink.push(typeCode(field), path);
|
|
781
|
+
return undefined;
|
|
782
|
+
}
|
|
783
|
+
if (field.minLength !== undefined && value.length < field.minLength) {
|
|
784
|
+
sink.push("limit-exceeded", path);
|
|
785
|
+
return undefined;
|
|
786
|
+
}
|
|
787
|
+
if (field.nulFree && value.includes("\0")) {
|
|
788
|
+
sink.push(valueCode(field), path);
|
|
789
|
+
return undefined;
|
|
790
|
+
}
|
|
791
|
+
if (field.maxLength !== undefined && value.length > field.maxLength) {
|
|
792
|
+
sink.push("limit-exceeded", path);
|
|
793
|
+
}
|
|
794
|
+
if (field.pattern !== undefined && !compiledPattern(field.pattern).test(value)) {
|
|
795
|
+
sink.push(valueCode(field), path);
|
|
796
|
+
return undefined;
|
|
797
|
+
}
|
|
798
|
+
for (const rule of field.rules ?? []) {
|
|
799
|
+
if (!compiledPattern(rule.regex).test(value))
|
|
800
|
+
sink.push(valueCode(field), path);
|
|
801
|
+
}
|
|
802
|
+
return value;
|
|
803
|
+
}
|
|
804
|
+
function checkInteger(field, value, path, sink) {
|
|
805
|
+
if (typeof value !== "number" || !Number.isInteger(value)) {
|
|
806
|
+
sink.push("invalid-type", path);
|
|
807
|
+
return undefined;
|
|
808
|
+
}
|
|
809
|
+
// A declared bound that Draft-07 can express is a limit failure; the positivity
|
|
810
|
+
// rule for `timeoutMs` is a value rule, which is what the projection states as
|
|
811
|
+
// `exclusiveMinimum`.
|
|
812
|
+
if (field.exclusiveMinimum !== undefined && value <= field.exclusiveMinimum) {
|
|
813
|
+
sink.push("limit-exceeded", path);
|
|
814
|
+
return undefined;
|
|
815
|
+
}
|
|
816
|
+
if (field.minimum !== undefined && value < field.minimum) {
|
|
817
|
+
sink.push("limit-exceeded", path);
|
|
818
|
+
return undefined;
|
|
819
|
+
}
|
|
820
|
+
if (field.maximum !== undefined && value > field.maximum) {
|
|
821
|
+
sink.push("limit-exceeded", path);
|
|
822
|
+
return undefined;
|
|
823
|
+
}
|
|
824
|
+
return value;
|
|
825
|
+
}
|
|
826
|
+
/**
|
|
827
|
+
* Validate one contract object against its spec, recording D16 diagnostics in
|
|
828
|
+
* `sink`, and return its normalized own-property DTO — or `undefined` when
|
|
829
|
+
* anything in this object (or below it) failed.
|
|
830
|
+
*
|
|
831
|
+
* Only own properties of declared keys are read, so inherited or prototype
|
|
832
|
+
* values can never be mistaken for contract data; the returned object is a
|
|
833
|
+
* fresh plain literal, so mutating the input afterwards cannot change it.
|
|
834
|
+
*
|
|
835
|
+
* Every path is an RFC 6901 JSON Pointer built from declared contract property
|
|
836
|
+
* names and decimal array indices (`/commands/3/id`; `""` is the document root),
|
|
837
|
+
* so a diagnostic identifies the offending location without naming — let alone
|
|
838
|
+
* echoing — the submitted value.
|
|
839
|
+
*/
|
|
840
|
+
export function validateStructure(contract, spec, value, path, sink) {
|
|
841
|
+
// F92 — the walk reads only declared keys of a JSON document (the input
|
|
842
|
+
// domain every public authority documents). An object outside that domain —
|
|
843
|
+
// a live getter that throws, not constructible by JSON.parse — must never
|
|
844
|
+
// smuggle an attacker-chosen exception past the D16 contract: the failure
|
|
845
|
+
// is refused here as exactly one redacted `invalid-type` row on this frame.
|
|
846
|
+
// Because the throw is caught by the frame that owns the read, nested rows
|
|
847
|
+
// report their own pointer (e.g. `/results/0`) and nothing else re-throws.
|
|
848
|
+
try {
|
|
849
|
+
return validateStructureFields(contract, spec, value, path, sink);
|
|
850
|
+
}
|
|
851
|
+
catch {
|
|
852
|
+
sink.push("invalid-type", path);
|
|
853
|
+
return undefined;
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
function validateStructureFields(contract, spec, value, path, sink) {
|
|
857
|
+
if (!isPlainRecord(value)) {
|
|
858
|
+
sink.push("invalid-type", path);
|
|
859
|
+
return undefined;
|
|
860
|
+
}
|
|
861
|
+
const before = sink.count();
|
|
862
|
+
const at = (key) => `${path}/${key}`;
|
|
863
|
+
for (const key of Object.keys(value)) {
|
|
864
|
+
if (!spec.fields.some((field) => field.key === key)) {
|
|
865
|
+
// The path names the CONTAINER, never the submitted key: an undeclared key
|
|
866
|
+
// is input data, and D16 allows only contract property names and indices in
|
|
867
|
+
// a pointer (so a probe like `__proto__` cannot echo itself back out).
|
|
868
|
+
sink.push("unknown-field", path);
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
const normalized = {};
|
|
872
|
+
for (const field of spec.fields) {
|
|
873
|
+
const fieldPath = at(field.key);
|
|
874
|
+
if (!Object.prototype.hasOwnProperty.call(value, field.key)) {
|
|
875
|
+
sink.push("missing-field", fieldPath);
|
|
876
|
+
continue;
|
|
877
|
+
}
|
|
878
|
+
const raw = value[field.key];
|
|
879
|
+
switch (field.type) {
|
|
880
|
+
case "const": {
|
|
881
|
+
if (raw !== field.value)
|
|
882
|
+
sink.push(valueCode(field), fieldPath);
|
|
883
|
+
else
|
|
884
|
+
normalized[field.key] = raw;
|
|
885
|
+
break;
|
|
886
|
+
}
|
|
887
|
+
case "enum": {
|
|
888
|
+
const vocabulary = field.enum ?? [];
|
|
889
|
+
if (!vocabulary.includes(raw))
|
|
890
|
+
sink.push(valueCode(field), fieldPath);
|
|
891
|
+
else
|
|
892
|
+
normalized[field.key] = raw;
|
|
893
|
+
break;
|
|
894
|
+
}
|
|
895
|
+
case "boolean": {
|
|
896
|
+
if (typeof raw !== "boolean")
|
|
897
|
+
sink.push("invalid-type", fieldPath);
|
|
898
|
+
else
|
|
899
|
+
normalized[field.key] = raw;
|
|
900
|
+
break;
|
|
901
|
+
}
|
|
902
|
+
case "string": {
|
|
903
|
+
const checked = checkString(field, raw, fieldPath, sink);
|
|
904
|
+
if (checked !== undefined)
|
|
905
|
+
normalized[field.key] = checked;
|
|
906
|
+
break;
|
|
907
|
+
}
|
|
908
|
+
case "integer": {
|
|
909
|
+
const checked = checkInteger(field, raw, fieldPath, sink);
|
|
910
|
+
if (checked !== undefined)
|
|
911
|
+
normalized[field.key] = checked;
|
|
912
|
+
break;
|
|
913
|
+
}
|
|
914
|
+
case "stringArray": {
|
|
915
|
+
if (!Array.isArray(raw)) {
|
|
916
|
+
sink.push("invalid-type", fieldPath);
|
|
917
|
+
break;
|
|
918
|
+
}
|
|
919
|
+
// Cardinality first: the ceiling is expressible in Draft-07, so the
|
|
920
|
+
// projection and this validator must agree (AC10).
|
|
921
|
+
if (field.maxItems !== undefined && raw.length > field.maxItems) {
|
|
922
|
+
sink.push("limit-exceeded", fieldPath);
|
|
923
|
+
break;
|
|
924
|
+
}
|
|
925
|
+
const items = [];
|
|
926
|
+
for (let i = 0; i < raw.length; i++) {
|
|
927
|
+
const item = raw[i];
|
|
928
|
+
if (typeof item !== "string") {
|
|
929
|
+
sink.push("invalid-type", `${fieldPath}/${i}`);
|
|
930
|
+
continue;
|
|
931
|
+
}
|
|
932
|
+
if (field.itemNulFree && item.includes("\0")) {
|
|
933
|
+
sink.push("invalid-value", `${fieldPath}/${i}`);
|
|
934
|
+
continue;
|
|
935
|
+
}
|
|
936
|
+
if (field.itemMaxLength !== undefined && item.length > field.itemMaxLength) {
|
|
937
|
+
sink.push("limit-exceeded", `${fieldPath}/${i}`);
|
|
938
|
+
continue;
|
|
939
|
+
}
|
|
940
|
+
items.push(item);
|
|
941
|
+
}
|
|
942
|
+
normalized[field.key] = items;
|
|
943
|
+
break;
|
|
944
|
+
}
|
|
945
|
+
case "array": {
|
|
946
|
+
const nested = verificationObjectSpec(contract, field.specName ?? "");
|
|
947
|
+
if (!Array.isArray(raw)) {
|
|
948
|
+
sink.push("invalid-type", fieldPath);
|
|
949
|
+
break;
|
|
950
|
+
}
|
|
951
|
+
if (field.minItems !== undefined && raw.length < field.minItems) {
|
|
952
|
+
sink.push("limit-exceeded", fieldPath);
|
|
953
|
+
break;
|
|
954
|
+
}
|
|
955
|
+
if (field.maxItems !== undefined && raw.length > field.maxItems) {
|
|
956
|
+
sink.push("limit-exceeded", fieldPath);
|
|
957
|
+
break;
|
|
958
|
+
}
|
|
959
|
+
const children = [];
|
|
960
|
+
for (let i = 0; i < raw.length; i++) {
|
|
961
|
+
const child = validateStructure(contract, nested, raw[i], `${fieldPath}/${i}`, sink);
|
|
962
|
+
if (child)
|
|
963
|
+
children.push(child);
|
|
964
|
+
}
|
|
965
|
+
normalized[field.key] = children;
|
|
966
|
+
break;
|
|
967
|
+
}
|
|
968
|
+
case "nullable": {
|
|
969
|
+
if (raw === null) {
|
|
970
|
+
normalized[field.key] = null;
|
|
971
|
+
break;
|
|
972
|
+
}
|
|
973
|
+
if (field.nullableOf === "string") {
|
|
974
|
+
const checked = checkString(field, raw, fieldPath, sink);
|
|
975
|
+
if (checked !== undefined)
|
|
976
|
+
normalized[field.key] = checked;
|
|
977
|
+
break;
|
|
978
|
+
}
|
|
979
|
+
if (field.nullableOf === "integer") {
|
|
980
|
+
const checked = checkInteger(field, raw, fieldPath, sink);
|
|
981
|
+
if (checked !== undefined)
|
|
982
|
+
normalized[field.key] = checked;
|
|
983
|
+
break;
|
|
984
|
+
}
|
|
985
|
+
if (!isPlainRecord(raw)) {
|
|
986
|
+
sink.push("invalid-type", fieldPath);
|
|
987
|
+
break;
|
|
988
|
+
}
|
|
989
|
+
const nested = verificationObjectSpec(contract, field.specName ?? "");
|
|
990
|
+
const child = validateStructure(contract, nested, raw, fieldPath, sink);
|
|
991
|
+
if (child)
|
|
992
|
+
normalized[field.key] = child;
|
|
993
|
+
break;
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
for (const rule of spec.rules) {
|
|
998
|
+
// Binding-only rules describe how a verdict may be ACTED ON, not what
|
|
999
|
+
// well-formed data is: the plain walk skips them, the binding authority
|
|
1000
|
+
// applies them explicitly over the same captured document.
|
|
1001
|
+
if (rule.enforcement === "binding")
|
|
1002
|
+
continue;
|
|
1003
|
+
applyCrossRule(spec, rule, value, path, sink);
|
|
1004
|
+
}
|
|
1005
|
+
return sink.count() === before ? normalized : undefined;
|
|
1006
|
+
}
|
|
1007
|
+
/** Fallback code per rule kind; a spec may name a narrower one with `code`. */
|
|
1008
|
+
const CROSS_RULE_DEFAULT_CODE = Object.freeze({
|
|
1009
|
+
"exactly-one-non-null": "invalid-exit-state",
|
|
1010
|
+
"null-when": "invalid-value",
|
|
1011
|
+
"non-null-when": "invalid-value",
|
|
1012
|
+
unique: "duplicate-id",
|
|
1013
|
+
"timestamp-order": "invalid-value",
|
|
1014
|
+
"calendar-roundtrip": "invalid-value",
|
|
1015
|
+
"maximum-when": "limit-exceeded",
|
|
1016
|
+
"stage-aggregate-budget": "budget-exceeded",
|
|
1017
|
+
"unique-items": "duplicate-id",
|
|
1018
|
+
"enum-when": "invalid-value",
|
|
1019
|
+
});
|
|
1020
|
+
/**
|
|
1021
|
+
* Apply one cross-field rule to a captured, normalized document. Exported for a
|
|
1022
|
+
* family's binding authority, which re-applies the `binding`-enforcement rules
|
|
1023
|
+
* the shared walk deliberately skipped.
|
|
1024
|
+
*/
|
|
1025
|
+
export function applyCrossRule(spec, rule, value, path, sink) {
|
|
1026
|
+
const code = (rule.code ?? CROSS_RULE_DEFAULT_CODE[rule.kind]);
|
|
1027
|
+
const at = (key) => `${path}/${key}`;
|
|
1028
|
+
switch (rule.kind) {
|
|
1029
|
+
case "unique": {
|
|
1030
|
+
const collectionName = rule.collection ?? "";
|
|
1031
|
+
const collection = Array.isArray(value[collectionName]) ? value[collectionName] : [];
|
|
1032
|
+
const field = rule.fields?.[0] ?? "";
|
|
1033
|
+
const seen = new Set();
|
|
1034
|
+
for (let i = 0; i < collection.length; i++) {
|
|
1035
|
+
const item = collection[i];
|
|
1036
|
+
if (!isPlainRecord(item))
|
|
1037
|
+
continue;
|
|
1038
|
+
const key = item[field];
|
|
1039
|
+
if (typeof key !== "string" || key.length === 0)
|
|
1040
|
+
continue;
|
|
1041
|
+
if (seen.has(key))
|
|
1042
|
+
sink.push(code, `${at(collectionName)}/${i}/${field}`);
|
|
1043
|
+
else
|
|
1044
|
+
seen.add(key);
|
|
1045
|
+
}
|
|
1046
|
+
break;
|
|
1047
|
+
}
|
|
1048
|
+
case "exactly-one-non-null": {
|
|
1049
|
+
if (!ruleApplies(spec, rule, value))
|
|
1050
|
+
break;
|
|
1051
|
+
const fields = rule.fields ?? [];
|
|
1052
|
+
const whenField = whenConditions(rule.when)[0]?.field ?? "";
|
|
1053
|
+
const present = fields.filter((key) => Object.prototype.hasOwnProperty.call(value, key) && value[key] !== null);
|
|
1054
|
+
// One row at the discriminator: the combination of its value with the
|
|
1055
|
+
// nullness of `fields` is the violation, and D16 forbids naming the values.
|
|
1056
|
+
if (present.length === fields.length || present.length === 0)
|
|
1057
|
+
sink.push(code, at(whenField));
|
|
1058
|
+
break;
|
|
1059
|
+
}
|
|
1060
|
+
case "null-when": {
|
|
1061
|
+
if (!ruleApplies(spec, rule, value))
|
|
1062
|
+
break;
|
|
1063
|
+
for (const key of rule.fields ?? []) {
|
|
1064
|
+
if (!Object.prototype.hasOwnProperty.call(value, key))
|
|
1065
|
+
continue;
|
|
1066
|
+
if (value[key] !== null)
|
|
1067
|
+
sink.push(code, at(key));
|
|
1068
|
+
}
|
|
1069
|
+
break;
|
|
1070
|
+
}
|
|
1071
|
+
case "non-null-when": {
|
|
1072
|
+
if (!ruleApplies(spec, rule, value))
|
|
1073
|
+
break;
|
|
1074
|
+
for (const key of rule.fields ?? []) {
|
|
1075
|
+
const present = Object.prototype.hasOwnProperty.call(value, key) && value[key] !== null;
|
|
1076
|
+
if (!present)
|
|
1077
|
+
sink.push(code, at(key));
|
|
1078
|
+
}
|
|
1079
|
+
break;
|
|
1080
|
+
}
|
|
1081
|
+
case "enum-when": {
|
|
1082
|
+
if (!ruleApplies(spec, rule, value))
|
|
1083
|
+
break;
|
|
1084
|
+
const allowed = rule.values ?? [];
|
|
1085
|
+
for (const key of rule.fields ?? []) {
|
|
1086
|
+
const raw = value[key];
|
|
1087
|
+
// A non-string is refused by the field's own type/vocabulary check; this
|
|
1088
|
+
// rule only narrows the set of well-formed values.
|
|
1089
|
+
if (typeof raw === "string" && !allowed.includes(raw))
|
|
1090
|
+
sink.push(code, at(key));
|
|
1091
|
+
}
|
|
1092
|
+
break;
|
|
1093
|
+
}
|
|
1094
|
+
case "maximum-when": {
|
|
1095
|
+
if (!ruleApplies(spec, rule, value))
|
|
1096
|
+
break;
|
|
1097
|
+
for (const key of rule.fields ?? []) {
|
|
1098
|
+
const raw = value[key];
|
|
1099
|
+
// A numeric field is compared by value; an array field by row count, so
|
|
1100
|
+
// one kind states both "at most N" shapes (e.g. an empty-array ceiling).
|
|
1101
|
+
const count = typeof raw === "number" ? raw : Array.isArray(raw) ? raw.length : undefined;
|
|
1102
|
+
if (count !== undefined && rule.maximum !== undefined && count > rule.maximum) {
|
|
1103
|
+
sink.push(code, at(key));
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
break;
|
|
1107
|
+
}
|
|
1108
|
+
case "unique-items": {
|
|
1109
|
+
// The Draft-07 floor: no two rows of `collection` are the same VALUE. A
|
|
1110
|
+
// family that needs key-level uniqueness declares its own stronger rule
|
|
1111
|
+
// (the pre-execution receipt pins digest uniqueness beside this floor).
|
|
1112
|
+
const collectionName = rule.collection ?? "";
|
|
1113
|
+
const collection = Array.isArray(value[collectionName]) ? value[collectionName] : [];
|
|
1114
|
+
const seen = new Set();
|
|
1115
|
+
for (let i = 0; i < collection.length; i++) {
|
|
1116
|
+
const item = collection[i];
|
|
1117
|
+
if (!isPlainRecord(item))
|
|
1118
|
+
continue;
|
|
1119
|
+
// Canonical form, not JSON.stringify: two own-property copies of one
|
|
1120
|
+
// row may carry different key orders but must mean one identity. A row
|
|
1121
|
+
// outside the canonical domain is refused by the field walk anyway, so
|
|
1122
|
+
// it is skipped here instead of masking those rows with a throw.
|
|
1123
|
+
let key;
|
|
1124
|
+
try {
|
|
1125
|
+
key = canonicalJSONValue(item);
|
|
1126
|
+
}
|
|
1127
|
+
catch {
|
|
1128
|
+
continue;
|
|
1129
|
+
}
|
|
1130
|
+
if (seen.has(key))
|
|
1131
|
+
sink.push(code, `${at(collectionName)}/${i}`);
|
|
1132
|
+
else
|
|
1133
|
+
seen.add(key);
|
|
1134
|
+
}
|
|
1135
|
+
break;
|
|
1136
|
+
}
|
|
1137
|
+
case "stage-aggregate-budget": {
|
|
1138
|
+
// Sum the stage's declared timeouts in declared order and report the ONE
|
|
1139
|
+
// command that crossed — the earliest pointer that explains the overflow.
|
|
1140
|
+
const collectionName = rule.collection ?? "";
|
|
1141
|
+
const collection = Array.isArray(value[collectionName]) ? value[collectionName] : [];
|
|
1142
|
+
const discriminatorCondition = whenConditions(rule.when)[0];
|
|
1143
|
+
const discriminator = discriminatorCondition?.field ?? "";
|
|
1144
|
+
const stage = discriminatorCondition?.equals ?? "";
|
|
1145
|
+
const field = rule.fields?.[0] ?? "";
|
|
1146
|
+
const ceiling = rule.maximum;
|
|
1147
|
+
if (ceiling === undefined || !discriminator || !stage || !field)
|
|
1148
|
+
break;
|
|
1149
|
+
let total = 0;
|
|
1150
|
+
for (let i = 0; i < collection.length; i++) {
|
|
1151
|
+
const item = collection[i];
|
|
1152
|
+
if (!isPlainRecord(item) || item[discriminator] !== stage)
|
|
1153
|
+
continue;
|
|
1154
|
+
const timeout = item[field];
|
|
1155
|
+
if (typeof timeout !== "number")
|
|
1156
|
+
continue;
|
|
1157
|
+
total += timeout;
|
|
1158
|
+
if (total > ceiling) {
|
|
1159
|
+
sink.push(code, `${at(collectionName)}/${i}/${field}`);
|
|
1160
|
+
break;
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
break;
|
|
1164
|
+
}
|
|
1165
|
+
case "timestamp-order": {
|
|
1166
|
+
const [start, end] = rule.fields ?? [];
|
|
1167
|
+
if (!start || !end)
|
|
1168
|
+
break;
|
|
1169
|
+
const a = value[start];
|
|
1170
|
+
const b = value[end];
|
|
1171
|
+
if (typeof a === "string" && typeof b === "string" && new Date(b).getTime() < new Date(a).getTime()) {
|
|
1172
|
+
sink.push(code, at(end));
|
|
1173
|
+
}
|
|
1174
|
+
break;
|
|
1175
|
+
}
|
|
1176
|
+
case "calendar-roundtrip": {
|
|
1177
|
+
for (const key of rule.fields ?? []) {
|
|
1178
|
+
const raw = value[key];
|
|
1179
|
+
if (typeof raw !== "string" || !compiledPattern(ISO_8601_PATTERN).test(raw))
|
|
1180
|
+
continue;
|
|
1181
|
+
const date = new Date(raw);
|
|
1182
|
+
const parts = raw.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/);
|
|
1183
|
+
const ok = Number.isFinite(date.getTime()) &&
|
|
1184
|
+
parts !== null &&
|
|
1185
|
+
date.getUTCFullYear() === Number(parts[1]) &&
|
|
1186
|
+
date.getUTCMonth() + 1 === Number(parts[2]) &&
|
|
1187
|
+
date.getUTCDate() === Number(parts[3]) &&
|
|
1188
|
+
date.getUTCHours() === Number(parts[4]) &&
|
|
1189
|
+
date.getUTCMinutes() === Number(parts[5]) &&
|
|
1190
|
+
date.getUTCSeconds() === Number(parts[6]);
|
|
1191
|
+
if (!ok)
|
|
1192
|
+
sink.push(code, at(key));
|
|
1193
|
+
}
|
|
1194
|
+
break;
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
/**
|
|
1199
|
+
* Every cross rule that pins a diagnostic must pin a code the family publishes.
|
|
1200
|
+
*
|
|
1201
|
+
* `VerificationCrossRule.code` is widened to `string` on purpose — a contract family
|
|
1202
|
+
* reports one of its OWN codes there — and `applyCrossRule` casts the value back to
|
|
1203
|
+
* the closed code type without checking it, so the type said "closed vocabulary"
|
|
1204
|
+
* while the runtime said nothing: a mistyped code, or one borrowed from another
|
|
1205
|
+
* family, would reach a caller as a diagnostic no consumer can match (F71). This is
|
|
1206
|
+
* the checked half of that cast, run against the declarations at load time, where
|
|
1207
|
+
* the failure is a broken import rather than a wrong verdict. It answers the codes it
|
|
1208
|
+
* verified so a caller can prove the scan was not vacuous.
|
|
1209
|
+
*/
|
|
1210
|
+
export function assertCrossRuleVocabulary(contracts, vocabulary, owner) {
|
|
1211
|
+
const pinned = [];
|
|
1212
|
+
for (const [contractName, spec] of Object.entries(contracts)) {
|
|
1213
|
+
for (const object of [spec.root, ...Object.values(spec.objects)]) {
|
|
1214
|
+
for (const rule of object.rules) {
|
|
1215
|
+
if (rule.code === undefined)
|
|
1216
|
+
continue;
|
|
1217
|
+
if (!vocabulary.includes(rule.code)) {
|
|
1218
|
+
throw new TypeError(`${owner}/${contractName}: cross rule "${rule.id}" reports "${rule.code}", which ` +
|
|
1219
|
+
`the ${vocabulary.length}-code ${owner} vocabulary does not publish`);
|
|
1220
|
+
}
|
|
1221
|
+
pinned.push(rule.code);
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
return Object.freeze(pinned);
|
|
1226
|
+
}
|
|
1227
|
+
// The family declared in this file, checked against its own closed list. The
|
|
1228
|
+
// pre-execution family runs the same guard with its wider vocabulary.
|
|
1229
|
+
assertCrossRuleVocabulary(VERIFICATION_CONTRACT, VERIFICATION_DIAGNOSTIC_CODES, "verification");
|