@agledger/verify-core 2.2.0 → 4.0.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.
Files changed (49) hide show
  1. package/LICENSE +18 -18
  2. package/README.md +797 -29
  3. package/dist/acknowledgements.d.ts +32 -0
  4. package/dist/acknowledgements.js +88 -0
  5. package/dist/anchors.d.ts +349 -0
  6. package/dist/anchors.js +654 -0
  7. package/dist/attestation.d.ts +98 -0
  8. package/dist/attestation.js +145 -0
  9. package/dist/audit-export.d.ts +343 -20
  10. package/dist/audit-export.js +400 -22
  11. package/dist/batch-head.d.ts +28 -0
  12. package/dist/batch-head.js +169 -0
  13. package/dist/batch.d.ts +69 -0
  14. package/dist/batch.js +99 -0
  15. package/dist/bulk-export.d.ts +161 -0
  16. package/dist/bulk-export.js +711 -0
  17. package/dist/chain.d.ts +135 -13
  18. package/dist/chain.js +229 -24
  19. package/dist/claim.d.ts +23 -0
  20. package/dist/claim.js +100 -0
  21. package/dist/export-anchors.d.ts +84 -0
  22. package/dist/export-anchors.js +109 -0
  23. package/dist/export-evidence.d.ts +90 -0
  24. package/dist/export-evidence.js +186 -0
  25. package/dist/export-pages.d.ts +476 -0
  26. package/dist/export-pages.js +586 -0
  27. package/dist/failures.d.ts +20 -16
  28. package/dist/failures.js +44 -16
  29. package/dist/federation-wire.d.ts +78 -11
  30. package/dist/federation-wire.js +199 -57
  31. package/dist/history-seal.d.ts +100 -0
  32. package/dist/history-seal.js +301 -0
  33. package/dist/index.d.ts +36 -8
  34. package/dist/index.js +17 -4
  35. package/dist/key-statements.d.ts +138 -5
  36. package/dist/key-statements.js +191 -20
  37. package/dist/ml-dsa-kats.d.ts +18 -0
  38. package/dist/ml-dsa-kats.js +241 -0
  39. package/dist/primitives.d.ts +255 -18
  40. package/dist/primitives.js +612 -55
  41. package/dist/read-log.d.ts +350 -0
  42. package/dist/read-log.js +816 -0
  43. package/dist/schema-chain-export.d.ts +82 -0
  44. package/dist/schema-chain-export.js +474 -0
  45. package/dist/walk-anchors.d.ts +115 -0
  46. package/dist/walk-anchors.js +133 -0
  47. package/dist/walk-checkpoints.d.ts +125 -0
  48. package/dist/walk-checkpoints.js +118 -0
  49. package/package.json +6 -5
@@ -0,0 +1,98 @@
1
+ /**
2
+ * COMPLIANCE_ATTESTATION and AI_IMPACT_ASSESSMENT entries bind a body the
3
+ * chain does not carry: the entry signs `contentHash`, `sha256:` and the hex
4
+ * SHA-256 of the RFC 8785 form of the stored attestation, beside the fields
5
+ * of its compliance record (`complianceRecordId`, `recordId`, `recordType`,
6
+ * `attestedBy`, `attestedAt`). The body is served by
7
+ * `GET /v1/records/{id}/compliance-records/{complianceRecordId}`; given that
8
+ * response, it is held to the hash and the fields to the values the entry
9
+ * signs. The export's `integrity.contentBinding` is the Server's own answer
10
+ * to the same question, unsigned, and is reported beside the verdict.
11
+ */
12
+ import type { CheckApplicability } from './chain.js';
13
+ import type { FailureCode } from './failures.js';
14
+ /** The entry types whose signed payload binds a body by `contentHash`. */
15
+ export declare const CONTENT_BOUND_ENTRY_TYPES: ReadonlySet<string>;
16
+ /** The form of a signed `contentHash`. */
17
+ export declare const CONTENT_HASH_PATTERN: RegExp;
18
+ /** A compliance record as `GET /v1/records/{id}/compliance-records/{complianceRecordId}` serves it (the fields read here). */
19
+ export interface ComplianceRecordInput {
20
+ id: string;
21
+ recordId: string;
22
+ recordType: string;
23
+ /** The stored attestation body the entry's `contentHash` is taken over. */
24
+ attestation: unknown;
25
+ attestedBy: string;
26
+ attestedAt: string;
27
+ [key: string]: unknown;
28
+ }
29
+ /** The fields of one export entry the attestation check reads. */
30
+ export interface AttestationEntryView {
31
+ chainPosition: number;
32
+ coseSign1: unknown;
33
+ /** The export's `integrity.contentBinding`: the Server's answer, unsigned. */
34
+ contentBinding?: unknown;
35
+ }
36
+ /** What one content-bound entry showed. */
37
+ export interface AttestationEntryReport {
38
+ position: number;
39
+ entryType: string;
40
+ /** The `complianceRecordId` the entry signs, or null on one that signs none. */
41
+ complianceRecordId: string | null;
42
+ /** The signed `contentHash`, or null when the entry signs none (written before the Server bound bodies). */
43
+ contentHash: string | null;
44
+ /**
45
+ * `matched`: a supplied body hashes to the signed contentHash and its fields
46
+ * equal the signed ones. `mismatch`: see the finding. `not_supplied`: no
47
+ * body for it was given. `unbound`: the entry signs no contentHash, so no
48
+ * body is bound to it. `malformed`: the signed contentHash or attestedAt is
49
+ * not in the form the Server writes.
50
+ */
51
+ body: 'matched' | 'mismatch' | 'not_supplied' | 'unbound' | 'malformed';
52
+ /**
53
+ * The export's `integrity.contentBinding` for the entry: the Server's own
54
+ * statement, unsigned, kept out of the verdict. Null when the export
55
+ * carries none.
56
+ */
57
+ serverContentBinding: string | null;
58
+ detail: string;
59
+ }
60
+ /** What the attestation check found on an export. */
61
+ export interface AttestationReport {
62
+ /**
63
+ * `skipped_no_input` with no `complianceRecords` given, `applied` once a
64
+ * supplied body was held to an entry, `not_checked` when bodies were given
65
+ * and none belonged to this record.
66
+ */
67
+ applicability: CheckApplicability;
68
+ entries: AttestationEntryReport[];
69
+ /** Supplied compliance records that belong to another record: listed, never judged. */
70
+ otherRecords: Array<{
71
+ id: string;
72
+ recordId: string;
73
+ }>;
74
+ }
75
+ export interface AttestationFinding {
76
+ position: number;
77
+ code: FailureCode;
78
+ detail: string;
79
+ }
80
+ /**
81
+ * The compliance records a caller supplied, in any of the forms they are
82
+ * served in: one response, an array of them, or the list endpoint's
83
+ * `{ data: [...] }`. Throws `TypeError` on anything else, or on an item that
84
+ * is not a compliance record.
85
+ */
86
+ export declare function readComplianceRecords(input: unknown): ComplianceRecordInput[];
87
+ /**
88
+ * Check every content-bound entry of an export: the signed `contentHash` and
89
+ * `attestedAt` are in the forms the Server writes (CONTENT_BINDING_MALFORMED),
90
+ * and, for each supplied compliance record, the body hashes to the signed
91
+ * contentHash (CONTENT_HASH_MISMATCH) and its fields equal the signed ones
92
+ * (CONTENT_FIELD_DRIFT). A supplied record of this record that no entry names
93
+ * is CONTENT_ROW_UNSIGNED; one of another record is listed and not judged.
94
+ */
95
+ export declare function verifyAttestations(recordId: string, entries: readonly AttestationEntryView[], supplied: readonly ComplianceRecordInput[] | undefined): {
96
+ report: AttestationReport;
97
+ findings: AttestationFinding[];
98
+ };
@@ -0,0 +1,145 @@
1
+ import { decodeSignedClaim } from './claim.js';
2
+ import { canonicalizeJson } from './federation-wire.js';
3
+ import { instantMs } from './instant.js';
4
+ import { sha256HexString } from './primitives.js';
5
+ /** The entry types whose signed payload binds a body by `contentHash`. */
6
+ export const CONTENT_BOUND_ENTRY_TYPES = new Set(['COMPLIANCE_ATTESTATION', 'AI_IMPACT_ASSESSMENT']);
7
+ /** The form of a signed `contentHash`. */
8
+ export const CONTENT_HASH_PATTERN = /^sha256:[0-9a-f]{64}$/;
9
+ function isRecord(value) {
10
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
11
+ }
12
+ /**
13
+ * The compliance records a caller supplied, in any of the forms they are
14
+ * served in: one response, an array of them, or the list endpoint's
15
+ * `{ data: [...] }`. Throws `TypeError` on anything else, or on an item that
16
+ * is not a compliance record.
17
+ */
18
+ export function readComplianceRecords(input) {
19
+ const list = Array.isArray(input)
20
+ ? input.flatMap((x) => (isRecord(x) && Array.isArray(x['data']) && !('attestation' in x) ? x['data'] : [x]))
21
+ : isRecord(input) && Array.isArray(input['data']) && !('attestation' in input)
22
+ ? input['data']
23
+ : [input];
24
+ return list.map((item, i) => {
25
+ if (!isRecord(item) || typeof item['id'] !== 'string' || typeof item['recordId'] !== 'string'
26
+ || typeof item['recordType'] !== 'string' || !('attestation' in item)
27
+ || typeof item['attestedBy'] !== 'string' || typeof item['attestedAt'] !== 'string') {
28
+ throw new TypeError(`complianceRecords[${i}] is not a compliance record as GET /v1/records/{id}/compliance-records/{complianceRecordId} serves it: { id, recordId, recordType, attestation, attestedBy, attestedAt, ... }.`);
29
+ }
30
+ return item;
31
+ });
32
+ }
33
+ /** Whether two instants name the same millisecond, or are the same string. */
34
+ function sameInstant(a, b) {
35
+ if (a === b)
36
+ return true;
37
+ if (typeof a !== 'string' || typeof b !== 'string')
38
+ return false;
39
+ const x = instantMs(a);
40
+ return !Number.isNaN(x) && x === instantMs(b);
41
+ }
42
+ /**
43
+ * Check every content-bound entry of an export: the signed `contentHash` and
44
+ * `attestedAt` are in the forms the Server writes (CONTENT_BINDING_MALFORMED),
45
+ * and, for each supplied compliance record, the body hashes to the signed
46
+ * contentHash (CONTENT_HASH_MISMATCH) and its fields equal the signed ones
47
+ * (CONTENT_FIELD_DRIFT). A supplied record of this record that no entry names
48
+ * is CONTENT_ROW_UNSIGNED; one of another record is listed and not judged.
49
+ */
50
+ export function verifyAttestations(recordId, entries, supplied) {
51
+ const findings = [];
52
+ const report = {
53
+ applicability: supplied === undefined || supplied.length === 0 ? 'skipped_no_input' : 'not_checked',
54
+ entries: [],
55
+ otherRecords: [],
56
+ };
57
+ const byId = new Map((supplied ?? []).map((body) => [body.id.toLowerCase(), body]));
58
+ const named = new Set();
59
+ for (const e of entries) {
60
+ const claim = typeof e.coseSign1 === 'string' ? decodeSignedClaim(Buffer.from(e.coseSign1, 'base64')) : null;
61
+ const entryType = claim?.predicate['entry_type'];
62
+ if (claim === null || typeof entryType !== 'string' || !CONTENT_BOUND_ENTRY_TYPES.has(entryType))
63
+ continue;
64
+ const payload = isRecord(claim.predicate['payload']) ? claim.predicate['payload'] : {};
65
+ const crId = typeof payload['complianceRecordId'] === 'string' ? payload['complianceRecordId'] : null;
66
+ if (crId !== null)
67
+ named.add(crId.toLowerCase());
68
+ const contentHash = typeof payload['contentHash'] === 'string' ? payload['contentHash'] : null;
69
+ const at = e.chainPosition;
70
+ const server = typeof e.contentBinding === 'string' ? e.contentBinding : null;
71
+ const push = (body, detail) => {
72
+ report.entries.push({ position: at, entryType, complianceRecordId: crId, contentHash, body, serverContentBinding: server, detail });
73
+ };
74
+ const label = `${entryType} at position ${at}`;
75
+ const malformed = [];
76
+ if ('contentHash' in payload && (contentHash === null || !CONTENT_HASH_PATTERN.test(contentHash))) {
77
+ malformed.push(`contentHash ${JSON.stringify(payload['contentHash'])} is not sha256: and 64 lowercase hex characters`);
78
+ }
79
+ if ('attestedAt' in payload && (typeof payload['attestedAt'] !== 'string' || Number.isNaN(instantMs(payload['attestedAt'])))) {
80
+ malformed.push(`attestedAt ${JSON.stringify(payload['attestedAt'])} is not an RFC 3339 instant`);
81
+ }
82
+ if (malformed.length > 0) {
83
+ const detail = `${label}: the signed ${malformed.join(', and the signed ')}.`;
84
+ push('malformed', detail);
85
+ findings.push({ position: at, code: 'CONTENT_BINDING_MALFORMED', detail });
86
+ continue;
87
+ }
88
+ if (contentHash === null) {
89
+ push('unbound', `${label}: signs no contentHash, so no body is bound to it.`);
90
+ continue;
91
+ }
92
+ const body = crId !== null ? byId.get(crId.toLowerCase()) : undefined;
93
+ if (body === undefined) {
94
+ push('not_supplied', `${label}: signs contentHash ${contentHash} for compliance record ${crId ?? '(none named)'}; its body was not supplied.`);
95
+ continue;
96
+ }
97
+ report.applicability = 'applied';
98
+ let actual;
99
+ try {
100
+ actual = `sha256:${sha256HexString(canonicalizeJson(body.attestation))}`;
101
+ }
102
+ catch {
103
+ actual = '(not canonicalizable under RFC 8785)';
104
+ }
105
+ if (actual !== contentHash) {
106
+ const detail = `${label}: the supplied body of compliance record ${body.id} hashes to ${actual}, and the entry signs ${contentHash}.`;
107
+ push('mismatch', detail);
108
+ findings.push({ position: at, code: 'CONTENT_HASH_MISMATCH', detail });
109
+ continue;
110
+ }
111
+ const drift = [];
112
+ for (const field of ['recordId', 'recordType', 'attestedBy', 'attestedAt']) {
113
+ if (!(field in payload))
114
+ continue;
115
+ const signed = payload[field];
116
+ const same = field === 'attestedAt' ? sameInstant(signed, body[field])
117
+ : field === 'recordId' ? typeof signed === 'string' && signed.toLowerCase() === body.recordId.toLowerCase()
118
+ : signed === body[field];
119
+ if (!same)
120
+ drift.push(`${field} ${JSON.stringify(body[field])} where the entry signs ${JSON.stringify(signed)}`);
121
+ }
122
+ if (drift.length > 0) {
123
+ const detail = `${label}: the supplied compliance record ${body.id} has ${drift.join('; ')}.`;
124
+ push('mismatch', detail);
125
+ findings.push({ position: at, code: 'CONTENT_FIELD_DRIFT', detail });
126
+ continue;
127
+ }
128
+ push('matched', `${label}: the supplied body of compliance record ${body.id} hashes to the signed contentHash, and its fields are the signed ones.`);
129
+ }
130
+ for (const body of supplied ?? []) {
131
+ if (named.has(body.id.toLowerCase()))
132
+ continue;
133
+ if (body.recordId.toLowerCase() !== recordId.toLowerCase()) {
134
+ report.otherRecords.push({ id: body.id, recordId: body.recordId });
135
+ continue;
136
+ }
137
+ report.applicability = 'applied';
138
+ findings.push({
139
+ position: 0,
140
+ code: 'CONTENT_ROW_UNSIGNED',
141
+ detail: `Compliance record ${body.id} belongs to record ${recordId}, and no COMPLIANCE_ATTESTATION entry of its chain names it.`,
142
+ });
143
+ }
144
+ return { report, findings };
145
+ }