@agent-custody/receipts 0.6.19 → 0.7.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.md +9 -6
- package/dist/a2a.js +19 -7
- package/dist/capture.d.ts +153 -0
- package/dist/capture.js +559 -0
- package/dist/cli.js +98 -4
- package/dist/config.d.ts +59 -0
- package/dist/config.js +21 -2
- package/dist/crypto.d.ts +2 -2
- package/dist/crypto.js +7 -4
- package/dist/delegation.d.ts +7 -0
- package/dist/delegation.js +15 -1
- package/dist/gateway-http.js +13 -1
- package/dist/gateway.d.ts +10 -0
- package/dist/gateway.js +132 -13
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/issue.d.ts +14 -4
- package/dist/issue.js +66 -7
- package/dist/log-sink.d.ts +11 -1
- package/dist/log-sink.js +24 -5
- package/dist/otel.js +5 -4
- package/dist/portal.d.ts +23 -2
- package/dist/portal.js +181 -36
- package/dist/receipt.d.ts +19 -8
- package/dist/retention.d.ts +8 -1
- package/dist/retention.js +21 -2
- package/dist/sdk/index.js +10 -1
- package/dist/splunk.js +6 -4
- package/dist/upstream.d.ts +9 -7
- package/dist/upstream.js +16 -8
- package/dist/verify.d.ts +16 -0
- package/dist/verify.js +171 -13
- package/docs/compliance.md +1 -1
- package/docs/tutorials.md +2 -0
- package/docs/usage.md +44 -4
- package/docs/verification.md +11 -4
- package/package.json +1 -1
- package/vectors/audit.json +27 -27
- package/vectors/canonical.json +5 -5
- package/vectors/receipts.json +1394 -270
package/dist/capture.js
ADDED
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
// Private evidence: what a receipt records of a call, decided in one place. The signature and the digests are over
|
|
2
|
+
// the whole call as it happened; what the receipt file carries in clear is a choice, made per tool in the config, and
|
|
3
|
+
// applied by the issuer to the whole statement just before it is signed (`privateView`). A fixed list of structural
|
|
4
|
+
// paths stays in clear: ids, key ids, timestamps, the tool name, the grant, the digests, the decision
|
|
5
|
+
// and the ids of the policies behind it, the status, the provenance fields, and the note itself. Every other value
|
|
6
|
+
// under a private rule leaves a commitment in its place, sha256 over a random salt and the canonical value, so a
|
|
7
|
+
// reviewer who is later shown the value and the salt can check it belongs; an encrypted commitment carries both as
|
|
8
|
+
// well, under a key the customer holds (AES-256-GCM). The salt never appears in the receipt: without it a reviewer
|
|
9
|
+
// cannot confirm a guess of a withheld value against the commitment (issue #126), and the digests over withheld
|
|
10
|
+
// values are salted with material derived from the field salts, so nothing public confirms a guess against them
|
|
11
|
+
// either. A field or path added to the receipt later starts withheld, never in clear. The signed note lists the
|
|
12
|
+
// withheld paths; a verifier trusts the note, not the shape of the data.
|
|
13
|
+
import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
|
|
14
|
+
import { canonicalize, digestOf, sha256Hex } from "./crypto.js";
|
|
15
|
+
// used only inside functions, so the cycle with upstream.ts (which takes isCommitment from here) is resolved before either runs
|
|
16
|
+
import { contentDigest, UPSTREAM_TYPE } from "./upstream.js";
|
|
17
|
+
/** A 32-byte key from hex (64 chars) or base64 (44 chars), as an environment variable holds it. */
|
|
18
|
+
export function captureKeyFrom(text) {
|
|
19
|
+
const t = text.trim();
|
|
20
|
+
// strict forms only: a base64 string with stray characters would decode silently to a different key
|
|
21
|
+
if (!/^[0-9a-fA-F]{64}$/.test(t) && !/^[A-Za-z0-9+/]{43}=$/.test(t))
|
|
22
|
+
throw new Error("capture key: expected 32 bytes as 64 hex characters or 44 characters of base64");
|
|
23
|
+
const key = t.length === 64 ? Buffer.from(t, "hex") : Buffer.from(t, "base64");
|
|
24
|
+
if (key.length !== 32)
|
|
25
|
+
throw new Error("capture key: expected 32 bytes as 64 hex characters or base64");
|
|
26
|
+
return key;
|
|
27
|
+
}
|
|
28
|
+
export function captureKeyId(key) {
|
|
29
|
+
return sha256Hex(key);
|
|
30
|
+
}
|
|
31
|
+
const HEX64 = /^[0-9a-f]{64}$/;
|
|
32
|
+
const HEX32 = /^[0-9a-f]{32}$/;
|
|
33
|
+
const B64 = /^[A-Za-z0-9+/]+=*$/;
|
|
34
|
+
/** A well-formed commitment: the sha256 a verifier checks, and for an encrypted one the fields the key needs. A `$custody` tag alone is not one. */
|
|
35
|
+
export function isCommitment(v) {
|
|
36
|
+
if (!v || typeof v !== "object" || Array.isArray(v))
|
|
37
|
+
return false;
|
|
38
|
+
const c = v;
|
|
39
|
+
if (typeof c.sha256 !== "string" || !HEX64.test(c.sha256))
|
|
40
|
+
return false;
|
|
41
|
+
if (c.$custody === "withheld")
|
|
42
|
+
return true;
|
|
43
|
+
if (c.$custody !== "encrypted")
|
|
44
|
+
return false;
|
|
45
|
+
// a 12-byte iv and a 16-byte tag, as AES-256-GCM is used here; a shorter tag would be accepted by the cipher and must not be
|
|
46
|
+
return c.alg === "A256GCM" && typeof c.keyid === "string" && HEX64.test(c.keyid) && [c.iv, c.ciphertext, c.tag].every((f) => typeof f === "string" && f.length > 0 && B64.test(f)) && c.iv.length === 16 && c.tag.length === 24;
|
|
47
|
+
}
|
|
48
|
+
/** What an encrypted commitment is bound to: the receipt and the path, so a ciphertext moved to another receipt or path does not decrypt. */
|
|
49
|
+
export function aadFor(receiptId, path) {
|
|
50
|
+
return canonicalize({ receiptId: typeof receiptId === "string" ? receiptId : null, path });
|
|
51
|
+
}
|
|
52
|
+
/** A disclosed value as a disclosure carries it: the salt, and the value when the holder has filled it in. */
|
|
53
|
+
export function isDisclosed(v) {
|
|
54
|
+
if (!v || typeof v !== "object" || Array.isArray(v))
|
|
55
|
+
return false;
|
|
56
|
+
const d = v;
|
|
57
|
+
return d.$custody === "disclosed" && typeof d.salt === "string" && HEX32.test(d.salt);
|
|
58
|
+
}
|
|
59
|
+
function commit(value, salt) {
|
|
60
|
+
return sha256Hex(Buffer.concat([Buffer.from(salt, "hex"), Buffer.from(canonicalize(value), "utf8")]));
|
|
61
|
+
}
|
|
62
|
+
/** Replaces a value with its commitment, carrying the value and the salt encrypted when a key is given (bound to `aad`, see `aadFor`); the salt is returned to the caller, never written into the receipt. */
|
|
63
|
+
export function withhold(value, key, aad) {
|
|
64
|
+
const salt = randomBytes(16).toString("hex");
|
|
65
|
+
const sha256 = commit(value, salt);
|
|
66
|
+
if (!key)
|
|
67
|
+
return { commitment: { $custody: "withheld", sha256 }, salt };
|
|
68
|
+
const iv = randomBytes(12);
|
|
69
|
+
const cipher = createCipheriv("aes-256-gcm", key, iv);
|
|
70
|
+
if (aad)
|
|
71
|
+
cipher.setAAD(Buffer.from(aad, "utf8"));
|
|
72
|
+
const ciphertext = Buffer.concat([cipher.update(canonicalize({ salt, value }), "utf8"), cipher.final()]);
|
|
73
|
+
return { commitment: { $custody: "encrypted", alg: "A256GCM", keyid: captureKeyId(key), iv: iv.toString("base64"), ciphertext: ciphertext.toString("base64"), tag: cipher.getAuthTag().toString("base64"), sha256 }, salt };
|
|
74
|
+
}
|
|
75
|
+
/** True when `value` under `salt` is what the commitment was made over. */
|
|
76
|
+
export function checkCommitment(c, value, salt) {
|
|
77
|
+
return HEX32.test(salt) && commit(value, salt) === c.sha256;
|
|
78
|
+
}
|
|
79
|
+
/** The value and the salt behind an encrypted commitment, under the key that made it; checked against the commitment too. */
|
|
80
|
+
export function reveal(c, key, aad) {
|
|
81
|
+
if (c.$custody !== "encrypted")
|
|
82
|
+
throw new Error("this value was withheld without its value; only the party that holds it can supply it, with its salt");
|
|
83
|
+
if (c.keyid !== captureKeyId(key))
|
|
84
|
+
throw new Error(`this value was encrypted under key ${c.keyid.slice(0, 12)}…, not the one given`);
|
|
85
|
+
const decipher = createDecipheriv("aes-256-gcm", key, Buffer.from(c.iv, "base64"));
|
|
86
|
+
if (aad)
|
|
87
|
+
decipher.setAAD(Buffer.from(aad, "utf8"));
|
|
88
|
+
decipher.setAuthTag(Buffer.from(c.tag, "base64"));
|
|
89
|
+
const text = Buffer.concat([decipher.update(Buffer.from(c.ciphertext, "base64")), decipher.final()]).toString("utf8");
|
|
90
|
+
const inner = JSON.parse(text);
|
|
91
|
+
if (!inner || typeof inner !== "object" || typeof inner.salt !== "string" || !("value" in inner))
|
|
92
|
+
throw new Error("the decrypted content is not a salt and a value");
|
|
93
|
+
if (!checkCommitment(c, inner.value, inner.salt))
|
|
94
|
+
throw new Error("the decrypted value does not match its commitment");
|
|
95
|
+
return { value: inner.value, salt: inner.salt };
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The salt of a digest over a part that holds withheld values: derived from the salts of those values, in note order,
|
|
99
|
+
* so whoever can restore the values can recompute the digest and nobody else can confirm a guess against it. No salt
|
|
100
|
+
* when nothing under the part is withheld: the values are public, and so may the digest be.
|
|
101
|
+
*/
|
|
102
|
+
export function digestSalt(salts) {
|
|
103
|
+
return salts.length ? sha256Hex(Buffer.from(salts.join(""), "hex")) : undefined;
|
|
104
|
+
}
|
|
105
|
+
/** The tool's rule, else the "*" rule: by own property, so a tool named `constructor` or `toString` does not find an inherited member and fall through to clear. */
|
|
106
|
+
export function ruleFor(capture, tool) {
|
|
107
|
+
if (!capture)
|
|
108
|
+
return undefined;
|
|
109
|
+
if (Object.hasOwn(capture, tool))
|
|
110
|
+
return capture[tool];
|
|
111
|
+
return Object.hasOwn(capture, "*") ? capture["*"] : undefined;
|
|
112
|
+
}
|
|
113
|
+
const ownOf = (o, k) => (o && Object.hasOwn(o, k) ? o[k] : undefined);
|
|
114
|
+
const PRIVACY = { clear: 0, digest: 1, encrypted: 2 };
|
|
115
|
+
/** A side as the rule captures it: its mode, and the fields it keeps in clear (null for all of them, a side in clear whole). */
|
|
116
|
+
function sideRule(rule, side) {
|
|
117
|
+
const mode = ruleMode(rule);
|
|
118
|
+
const s = rule[side];
|
|
119
|
+
if (mode === "clear" || s?.mode === "clear")
|
|
120
|
+
return { mode: "clear", clear: null };
|
|
121
|
+
return { mode: s?.mode ?? mode, clear: s?.clear ?? [] };
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* The rule for a call: the tool's own, else the wildcard. For a name no upstream offers, which is text the agent
|
|
125
|
+
* chose, both together, side by side: the more private mode, and only the fields both keep in clear, so that a rule
|
|
126
|
+
* left for a retired name can never be selected by an agent to widen what is in clear. A mode does not order which
|
|
127
|
+
* fields are public, so the clear lists are intersected, not taken from the more private rule. The gateway and the
|
|
128
|
+
* issuer both call this, so the fact arguments kept in clear and the view agree.
|
|
129
|
+
*/
|
|
130
|
+
export function effectiveRule(capture, tool, unknownTool = false) {
|
|
131
|
+
const named = ruleFor(capture, tool);
|
|
132
|
+
if (!unknownTool)
|
|
133
|
+
return named;
|
|
134
|
+
const star = ownOf(capture, "*");
|
|
135
|
+
const own_ = ownOf(capture, tool);
|
|
136
|
+
if (!star || !own_)
|
|
137
|
+
return named ?? star;
|
|
138
|
+
const both = {};
|
|
139
|
+
for (const side of ["args", "result", "facts"]) {
|
|
140
|
+
const a = sideRule(own_, side);
|
|
141
|
+
const b = sideRule(star, side);
|
|
142
|
+
const mode = PRIVACY[a.mode] >= PRIVACY[b.mode] ? a.mode : b.mode;
|
|
143
|
+
if (mode === "clear")
|
|
144
|
+
both[side] = { mode };
|
|
145
|
+
else
|
|
146
|
+
both[side] = { mode, clear: a.clear === null ? (b.clear ?? []) : b.clear === null ? a.clear : a.clear.filter((f) => b.clear.includes(f)) };
|
|
147
|
+
}
|
|
148
|
+
return both;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* An upstream's attestation envelope exactly as `signResult` writes it for this call: the three envelope fields, the
|
|
152
|
+
* upstream payload type, one signature entry of a key id and a signature, and a payload that is the canonical bytes of
|
|
153
|
+
* this receipt's id, this tool, and the digest of this result, nothing else. Any other field, claim, or encoding is
|
|
154
|
+
* upstream-authored data that may repeat the withheld arguments. The key id and the signature are bytes the upstream
|
|
155
|
+
* chose and the gateway, holding no upstream key, checks for shape only; they carry no more than the clear result the
|
|
156
|
+
* rule publishes from the same upstream.
|
|
157
|
+
*/
|
|
158
|
+
function isPlainAttestation(envelope, receiptId, tool, result) {
|
|
159
|
+
if (!isObject(envelope) || !exactKeys(envelope, ["payload", "payloadType", "signatures"]))
|
|
160
|
+
return false;
|
|
161
|
+
if (envelope.payloadType !== UPSTREAM_TYPE || typeof envelope.payload !== "string" || !isObject(result))
|
|
162
|
+
return false;
|
|
163
|
+
const sigs = envelope.signatures;
|
|
164
|
+
if (!Array.isArray(sigs) || sigs.length !== 1)
|
|
165
|
+
return false;
|
|
166
|
+
const sig = sigs[0];
|
|
167
|
+
if (!isObject(sig) || !exactKeys(sig, ["keyid", "sig"]) || typeof sig.keyid !== "string" || !/^[0-9a-f]{64}$/.test(sig.keyid))
|
|
168
|
+
return false;
|
|
169
|
+
if (typeof sig.sig !== "string" || !canonicalBase64(sig.sig, 64))
|
|
170
|
+
return false;
|
|
171
|
+
if (!canonicalBase64(envelope.payload))
|
|
172
|
+
return false;
|
|
173
|
+
try {
|
|
174
|
+
const bytes = Buffer.from(envelope.payload, "base64");
|
|
175
|
+
const p = JSON.parse(bytes.toString("utf8"));
|
|
176
|
+
// compared as bytes: decoding to text would turn an invalid byte into U+FFFD and let it through
|
|
177
|
+
if (!isObject(p) || !exactKeys(p, ["contentDigest", "receiptId", "tool"]) || !bytes.equals(Buffer.from(canonicalize(p), "utf8")))
|
|
178
|
+
return false;
|
|
179
|
+
return p.receiptId === receiptId && p.tool === tool && p.contentDigest === contentDigest(result);
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/** exactly these own keys: a joined list would let one key named "a,b" stand for two, and an inherited one would not count */
|
|
186
|
+
const exactKeys = (o, keys) => Object.keys(o).length === keys.length && keys.every((k) => Object.hasOwn(o, k));
|
|
187
|
+
/** base64 that decodes and encodes back to itself, so no character rides along unread; of `length` bytes when given */
|
|
188
|
+
function canonicalBase64(s, length) {
|
|
189
|
+
const b = Buffer.from(s, "base64");
|
|
190
|
+
return b.toString("base64") === s && (length === undefined || b.length === length);
|
|
191
|
+
}
|
|
192
|
+
/** The one mode a rule withholds in: encrypted when any side is, else digest; clear when no side is private. */
|
|
193
|
+
export function ruleMode(rule) {
|
|
194
|
+
const modes = [rule?.args, rule?.result, rule?.facts].map((s) => s?.mode ?? "clear");
|
|
195
|
+
if (modes.includes("encrypted"))
|
|
196
|
+
return "encrypted";
|
|
197
|
+
if (modes.includes("digest"))
|
|
198
|
+
return "digest";
|
|
199
|
+
return "clear";
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* How a side is captured: null when it is in clear whole (set to clear, or every side clear); else the fields kept in
|
|
203
|
+
* clear and whether its commitments carry the value encrypted. A side the rule does not set is withheld whole, field
|
|
204
|
+
* by field, in the rule's mode.
|
|
205
|
+
*/
|
|
206
|
+
function sideOf(rule, side, key) {
|
|
207
|
+
const mode = ruleMode(rule);
|
|
208
|
+
if (mode === "clear")
|
|
209
|
+
return null;
|
|
210
|
+
const s = rule?.[side];
|
|
211
|
+
if (s?.mode === "clear")
|
|
212
|
+
return null;
|
|
213
|
+
return { clear: new Set(s?.clear ?? []), key: (s?.mode ?? mode) === "encrypted" ? key : undefined };
|
|
214
|
+
}
|
|
215
|
+
// ---- paths ----
|
|
216
|
+
export const pathKey = (p) => JSON.stringify(p);
|
|
217
|
+
export const pathText = (p) => p.map((s) => (/^[A-Za-z0-9_$-]+$/.test(s) ? s : JSON.stringify(s))).join(".");
|
|
218
|
+
export const under = (p, prefix) => prefix.length <= p.length && prefix.every((s, i) => p[i] === s);
|
|
219
|
+
const isObject = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
220
|
+
const own = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
|
|
221
|
+
/** The value at a path, by own properties only; undefined when the path leaves the data. */
|
|
222
|
+
export function getAt(root, path) {
|
|
223
|
+
let v = root;
|
|
224
|
+
for (const s of path) {
|
|
225
|
+
if (!isObject(v) || !own(v, s))
|
|
226
|
+
return undefined;
|
|
227
|
+
v = v[s];
|
|
228
|
+
}
|
|
229
|
+
return v;
|
|
230
|
+
}
|
|
231
|
+
/** A copy with a null prototype, so a field named __proto__ stays a field. */
|
|
232
|
+
const plain = (o) => {
|
|
233
|
+
const out = Object.create(null);
|
|
234
|
+
for (const [k, v] of Object.entries(o))
|
|
235
|
+
out[k] = v;
|
|
236
|
+
return out;
|
|
237
|
+
};
|
|
238
|
+
/** A copy of `root` with `value` at `path`, every object on the way copied (null-prototype), the rest shared. Creates objects for missing segments. */
|
|
239
|
+
export function setAt(root, path, value) {
|
|
240
|
+
if (path.length === 0)
|
|
241
|
+
return value;
|
|
242
|
+
const [head, ...rest] = path;
|
|
243
|
+
const o = isObject(root) ? plain(root) : Object.create(null);
|
|
244
|
+
o[head] = setAt(own(o, head) ? o[head] : undefined, rest, value);
|
|
245
|
+
return o;
|
|
246
|
+
}
|
|
247
|
+
// ---- the choke point ----
|
|
248
|
+
/** The structural top-level parts of a statement: ids, keys, times, the grant, the names. Never withheld. */
|
|
249
|
+
const CLEAR_TOP = new Set(["receiptId", "timestamp", "issuer", "principal", "agent", "delegation", "tool", "authorization", "capture"]);
|
|
250
|
+
/** Inside the parts that carry values: what stays in clear beside them. */
|
|
251
|
+
const CLEAR_IN = {
|
|
252
|
+
request: new Set(["argsDigest", "provenance"]),
|
|
253
|
+
fact: new Set(["tool", "resultDigest", "provenance"]),
|
|
254
|
+
policy: new Set(["decision", "reasons", "policyDigest", "enforced", "provenance"]),
|
|
255
|
+
execution: new Set(["status", "resultDigest", "provenance"]),
|
|
256
|
+
upstream: new Set(["provider"]),
|
|
257
|
+
consumed: new Set(["provenance"]),
|
|
258
|
+
/** the host's correlation ids are supplied by the agent's process or its call: values, withheld; the provenance stays */
|
|
259
|
+
session: new Set(["provenance"]),
|
|
260
|
+
model: new Set(["provenance"]),
|
|
261
|
+
};
|
|
262
|
+
/** The result's error flag is its status, so it stays in clear when it is the boolean flag and nothing else; its metadata carries whatever an upstream put there, so it never does. */
|
|
263
|
+
const RESULT_ALWAYS_CLEAR = new Set(["isError"]);
|
|
264
|
+
const RESULT_NEVER_CLEAR = new Set(["_meta"]);
|
|
265
|
+
/** the slot in a captured side's view that holds the commitment over every field not kept in clear */
|
|
266
|
+
export const REST = "$rest";
|
|
267
|
+
/** The parts an authorization and its receipt share; the receipt's view takes them from the authorization's, commitments and salts alike. */
|
|
268
|
+
const SHARED_WITH_AUTHORIZATION = ["request", "facts", "policy", "consumed"];
|
|
269
|
+
/**
|
|
270
|
+
* The statement a receipt carries under a tool's rule: the predicate with every non-structural value withheld as the
|
|
271
|
+
* rule says, the three digests salted from the field salts, and the note of what is withheld at `capture`. Under a
|
|
272
|
+
* clear rule, the predicate itself and no salts. Applied by the issuer to every statement it signs, the authorization
|
|
273
|
+
* included; given the authorization's view as `prior`, the receipt's view carries the same request, facts, and policy,
|
|
274
|
+
* so that the receipt still names the authorization and nothing is withheld twice.
|
|
275
|
+
*/
|
|
276
|
+
export function privateView(predicate, rule, key, ctx = {}, prior) {
|
|
277
|
+
const mode = ruleMode(rule);
|
|
278
|
+
if (mode === "clear")
|
|
279
|
+
return { predicate, salts: {} };
|
|
280
|
+
if (mode === "encrypted" && !key)
|
|
281
|
+
throw new Error("capture mode encrypted needs captureKeyEnv");
|
|
282
|
+
/** every value outside the three sides is withheld in the rule's mode: encrypted when any side is */
|
|
283
|
+
const k = mode === "encrypted" ? key : undefined;
|
|
284
|
+
const withheld = [];
|
|
285
|
+
const salts = {};
|
|
286
|
+
let out = plain(predicate);
|
|
287
|
+
// the note is this view's to write: one that came in with the predicate names nothing here
|
|
288
|
+
delete out.capture;
|
|
289
|
+
if (ctx.unknownTool && isObject(out.tool))
|
|
290
|
+
out.tool = { ...out.tool, name: "(unknown tool)" };
|
|
291
|
+
if (prior) {
|
|
292
|
+
for (const part of SHARED_WITH_AUTHORIZATION)
|
|
293
|
+
if (own(prior.predicate, part))
|
|
294
|
+
out[part] = prior.predicate[part];
|
|
295
|
+
for (const w of prior.predicate.capture?.withheld ?? []) {
|
|
296
|
+
withheld.push(w);
|
|
297
|
+
const s = ownOf(prior.salts, pathKey(w));
|
|
298
|
+
if (s)
|
|
299
|
+
salts[pathKey(w)] = s;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
const receiptId = predicate.receiptId;
|
|
303
|
+
const hide = (path, value, withKey) => {
|
|
304
|
+
// nothing carries nothing; a null does (that a value was null), and is withheld like any other
|
|
305
|
+
if (value === undefined)
|
|
306
|
+
return;
|
|
307
|
+
const w = withhold(value, withKey, aadFor(receiptId, path));
|
|
308
|
+
out = setAt(out, path, w.commitment);
|
|
309
|
+
salts[pathKey(path)] = w.salt;
|
|
310
|
+
withheld.push(path);
|
|
311
|
+
};
|
|
312
|
+
/** the digest of a part, salted from the salts of the withheld values under it */
|
|
313
|
+
const digest = (value, prefix) => digestOf(value, digestSalt(withheld.filter((w) => under(w, prefix)).map((w) => salts[pathKey(w)])));
|
|
314
|
+
/**
|
|
315
|
+
* A side captured under its rule: the listed fields are kept beside one commitment, at `$rest`, over an object of
|
|
316
|
+
* every other field. Field by field would leave the field names in clear, in the view and in the signed note, and
|
|
317
|
+
* names are chosen by the agent or the upstream like the values are. A value that is not a plain object is withheld
|
|
318
|
+
* whole. `always` names a boolean status flag that stays in clear only when it is one; `never` names fields that are
|
|
319
|
+
* withheld even when listed. A field literally named `$rest` is never clear: it is withheld with the rest and comes
|
|
320
|
+
* back with it.
|
|
321
|
+
*/
|
|
322
|
+
const object = (path, value, side, always = new Set(), never = new Set()) => {
|
|
323
|
+
if (!isObject(value))
|
|
324
|
+
return hide(path, value, side.key);
|
|
325
|
+
const view = Object.create(null);
|
|
326
|
+
const rest = Object.create(null);
|
|
327
|
+
let any = false;
|
|
328
|
+
for (const [f, v] of Object.entries(value)) {
|
|
329
|
+
if (f !== REST && !never.has(f) && (side.clear.has(f) || (always.has(f) && typeof v === "boolean")))
|
|
330
|
+
view[f] = v;
|
|
331
|
+
else {
|
|
332
|
+
rest[f] = v;
|
|
333
|
+
any = true;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
if (!any)
|
|
337
|
+
return;
|
|
338
|
+
out = setAt(out, path, view);
|
|
339
|
+
hide([...path, REST], rest, side.key);
|
|
340
|
+
};
|
|
341
|
+
/** the lookup arguments of a fact, field by field: the names are the fact config's, not the call's */
|
|
342
|
+
const fields = (path, value, side) => {
|
|
343
|
+
if (!isObject(value))
|
|
344
|
+
return hide(path, value, side.key);
|
|
345
|
+
for (const [f, v] of Object.entries(value))
|
|
346
|
+
if (!side.clear.has(f))
|
|
347
|
+
hide([...path, f], v, side.key);
|
|
348
|
+
};
|
|
349
|
+
const argsSide = sideOf(rule, "args", key);
|
|
350
|
+
const resultSide = sideOf(rule, "result", key);
|
|
351
|
+
const factsSide = sideOf(rule, "facts", key);
|
|
352
|
+
for (const [key_, value] of Object.entries(predicate)) {
|
|
353
|
+
if (CLEAR_TOP.has(key_))
|
|
354
|
+
continue;
|
|
355
|
+
if (prior && SHARED_WITH_AUTHORIZATION.includes(key_))
|
|
356
|
+
continue;
|
|
357
|
+
// no policy was evaluated: structural, not a value
|
|
358
|
+
if (key_ === "policy" && value === null)
|
|
359
|
+
continue;
|
|
360
|
+
if (key_ === "request" && isObject(value)) {
|
|
361
|
+
for (const [f, v] of Object.entries(value)) {
|
|
362
|
+
if (f === "args") {
|
|
363
|
+
if (argsSide)
|
|
364
|
+
object(["request", "args"], v, argsSide);
|
|
365
|
+
}
|
|
366
|
+
else if (!CLEAR_IN.request.has(f))
|
|
367
|
+
hide(["request", f], v, k);
|
|
368
|
+
}
|
|
369
|
+
if (own(value, "args"))
|
|
370
|
+
out = setAt(out, ["request", "argsDigest"], digest(value.args, ["request", "args"]));
|
|
371
|
+
}
|
|
372
|
+
else if (key_ === "facts" && isObject(value)) {
|
|
373
|
+
for (const [name, fact] of Object.entries(value)) {
|
|
374
|
+
if (!isObject(fact)) {
|
|
375
|
+
hide(["facts", name], fact, k);
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
for (const [f, v] of Object.entries(fact)) {
|
|
379
|
+
if (f === "value") {
|
|
380
|
+
if (factsSide)
|
|
381
|
+
object(["facts", name, "value"], v, factsSide);
|
|
382
|
+
}
|
|
383
|
+
else if (f === "args") {
|
|
384
|
+
if (argsSide)
|
|
385
|
+
fields(["facts", name, "args"], v, { clear: new Set(ownOf(ctx.clearFactArgs, name) ?? []), key: argsSide.key });
|
|
386
|
+
}
|
|
387
|
+
else if (!CLEAR_IN.fact.has(f))
|
|
388
|
+
hide(["facts", name, f], v, k);
|
|
389
|
+
}
|
|
390
|
+
// The fact's digest is over the lookup's full result, of which the receipt shows only the selected value: the
|
|
391
|
+
// rest (metadata, other content) is never public under a private rule, whatever the selected fields are. So
|
|
392
|
+
// the digest becomes a commitment to the full result (disclosed or decrypted like any value, and checked at
|
|
393
|
+
// `withheld facts.<name>.resultDigest`), or, when the producer did not give the full result, the digest
|
|
394
|
+
// string itself is withheld: an unsalted digest over private data would confirm a guess.
|
|
395
|
+
const full = ownOf(ctx.factResults, name);
|
|
396
|
+
const digestPath = ["facts", name, "resultDigest"];
|
|
397
|
+
if (full !== undefined) {
|
|
398
|
+
const w = withhold(full, factsSide ? factsSide.key : k, aadFor(receiptId, digestPath));
|
|
399
|
+
out = setAt(out, digestPath, w.commitment);
|
|
400
|
+
salts[pathKey(digestPath)] = w.salt;
|
|
401
|
+
withheld.push(digestPath);
|
|
402
|
+
}
|
|
403
|
+
else
|
|
404
|
+
hide(digestPath, fact.resultDigest, k);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
else if (key_ === "policy" && isObject(value)) {
|
|
408
|
+
for (const [f, v] of Object.entries(value)) {
|
|
409
|
+
// an empty error list is structural (there were none); a non-empty one carries the evaluator's messages
|
|
410
|
+
if (f === "errors" && Array.isArray(v) && v.length === 0)
|
|
411
|
+
continue;
|
|
412
|
+
if (!CLEAR_IN.policy.has(f))
|
|
413
|
+
hide(["policy", f], v, k);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
else if (key_ === "execution" && isObject(value)) {
|
|
417
|
+
for (const [f, v] of Object.entries(value)) {
|
|
418
|
+
if (f === "result") {
|
|
419
|
+
if (resultSide)
|
|
420
|
+
object(["execution", "result"], v, resultSide, RESULT_ALWAYS_CLEAR, RESULT_NEVER_CLEAR);
|
|
421
|
+
// a result left in clear by the rule still carries its metadata, which an upstream wrote and which may repeat the arguments: withheld under any private rule
|
|
422
|
+
else if (isObject(v) && own(v, "_meta"))
|
|
423
|
+
hide(["execution", "result", "_meta"], v._meta, k);
|
|
424
|
+
}
|
|
425
|
+
else if (f === "upstream" && isObject(v)) {
|
|
426
|
+
// Everything an upstream or a provider wrote in the evidence is withheld under any private rule: the delivery
|
|
427
|
+
// body and the signed envelope (a signed payload may carry any claim), and the provider's signature and bind
|
|
428
|
+
// too, since the signature is a MAC over the withheld body (whoever holds the secret would confirm a guess of
|
|
429
|
+
// the body against it) and the bind is a string the upstream chose. Only the provider's name stays. The
|
|
430
|
+
// verifier checks the delivery once the body, the signature, and the bind are back.
|
|
431
|
+
for (const [uf, uv] of Object.entries(v)) {
|
|
432
|
+
// beside a result the rule leaves in clear, an envelope that is exactly the plain attestation for this call
|
|
433
|
+
// (isPlainAttestation: every field, the signature entry, and the payload's bytes) stays, so the signature over
|
|
434
|
+
// that public result can be checked; any other field or claim is upstream-authored data and withholds it, and
|
|
435
|
+
// the verifier then says so rather than skipping the check. A masked tool name is never kept beside its envelope.
|
|
436
|
+
if (uf === "envelope" && !resultSide && !ctx.unknownTool && isPlainAttestation(uv, predicate.receiptId, predicate.tool.name, value.result))
|
|
437
|
+
continue;
|
|
438
|
+
if (uf === "rawBody" || uf === "envelope" || uf === "signature" || uf === "bind")
|
|
439
|
+
hide(["execution", "upstream", uf], uv, resultSide ? resultSide.key : k);
|
|
440
|
+
else if (!CLEAR_IN.upstream.has(uf))
|
|
441
|
+
hide(["execution", "upstream", uf], uv, k);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
else if (!CLEAR_IN.execution.has(f))
|
|
445
|
+
hide(["execution", f], v, k);
|
|
446
|
+
}
|
|
447
|
+
if (own(value, "result"))
|
|
448
|
+
out = setAt(out, ["execution", "resultDigest"], digest(value.result, ["execution", "result"]));
|
|
449
|
+
}
|
|
450
|
+
else if ((key_ === "session" || key_ === "model") && isObject(value)) {
|
|
451
|
+
// correlation ids the host or the agent supplied: a value when there is one; none is structural
|
|
452
|
+
for (const [f, v] of Object.entries(value)) {
|
|
453
|
+
if (v === null)
|
|
454
|
+
continue;
|
|
455
|
+
if (!CLEAR_IN[key_].has(f))
|
|
456
|
+
hide([key_, f], v, k);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
else if (key_ === "consumed" && isObject(value)) {
|
|
460
|
+
// the fact ids an upstream declared it served are upstream-chosen strings: withheld under a private rule, an empty list being structural
|
|
461
|
+
for (const [f, v] of Object.entries(value)) {
|
|
462
|
+
if (f === "factIds" && Array.isArray(v) && v.length === 0)
|
|
463
|
+
continue;
|
|
464
|
+
if (!CLEAR_IN.consumed.has(f))
|
|
465
|
+
hide(["consumed", f], v, k);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
else
|
|
469
|
+
hide([key_], value, k);
|
|
470
|
+
}
|
|
471
|
+
if (withheld.length)
|
|
472
|
+
out.capture = { mode, withheld };
|
|
473
|
+
return { predicate: out, salts };
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* The full statement behind a view, under its note: a path the note names must hold a commitment; the disclosure, when
|
|
477
|
+
* it carries that path as `{ $custody: "disclosed", salt, value }`, must match the commitment; else the key reveals
|
|
478
|
+
* what was encrypted under it; else the path stays withheld. A disclosure is a partial predicate with those at the
|
|
479
|
+
* withheld paths, as `agent-custody reveal` writes it. Nothing here throws: the problems are reported, so a verifier
|
|
480
|
+
* fails the right check.
|
|
481
|
+
*/
|
|
482
|
+
export function restorePaths(predicate, note, disclosed, key) {
|
|
483
|
+
const r = { predicate, restored: new Map(), salts: new Map(), values: new Map(), missing: [], problems: [] };
|
|
484
|
+
if (!note)
|
|
485
|
+
return r;
|
|
486
|
+
const keyid = key ? captureKeyId(key) : null;
|
|
487
|
+
for (const path of note.withheld) {
|
|
488
|
+
const c = getAt(predicate, path);
|
|
489
|
+
if (!isCommitment(c)) {
|
|
490
|
+
r.problems.push({ path, error: `the capture note says ${pathText(path)} is withheld, but the receipt carries no commitment for it` });
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
const d = disclosed === undefined ? undefined : getAt(disclosed, path);
|
|
494
|
+
if (d !== undefined) {
|
|
495
|
+
if (!isDisclosed(d)) {
|
|
496
|
+
r.problems.push({ path, error: `the disclosed ${pathText(path)} must be { "$custody": "disclosed", "salt", "value" }, as agent-custody reveal writes it; a value alone cannot be checked` });
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
// a salt without its value is what reveal --include-salts writes for the holder to complete: handed over as it
|
|
500
|
+
// is, it would let a reviewer confirm guesses, so an unfinished disclosure fails rather than passes as committed
|
|
501
|
+
if (!("value" in d)) {
|
|
502
|
+
r.problems.push({ path, error: `the disclosed ${pathText(path)} carries a salt but no value; fill in the value or remove the entry, since a salt alone lets a reviewer confirm guesses` });
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
if (!checkCommitment(c, d.value, d.salt)) {
|
|
506
|
+
r.problems.push({ path, error: `the disclosed ${pathText(path)} does not match the receipt's commitment` });
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
509
|
+
r.predicate = setAt(r.predicate, path, d.value);
|
|
510
|
+
r.restored.set(pathKey(path), "disclosed");
|
|
511
|
+
r.salts.set(pathKey(path), d.salt);
|
|
512
|
+
r.values.set(pathKey(path), d.value);
|
|
513
|
+
}
|
|
514
|
+
else if (key && c.$custody === "encrypted" && c.keyid !== keyid) {
|
|
515
|
+
// a key was given and it is not this value's: said so, rather than passing as committed with a hint to give the key
|
|
516
|
+
r.problems.push({ path, error: `${pathText(path)} is encrypted under key ${c.keyid.slice(0, 12)}…, not the one given` });
|
|
517
|
+
}
|
|
518
|
+
else if (key && c.$custody === "encrypted") {
|
|
519
|
+
try {
|
|
520
|
+
const back = reveal(c, key, aadFor(getAt(predicate, ["receiptId"]), path));
|
|
521
|
+
r.predicate = setAt(r.predicate, path, back.value);
|
|
522
|
+
r.restored.set(pathKey(path), "decrypted");
|
|
523
|
+
r.salts.set(pathKey(path), back.salt);
|
|
524
|
+
r.values.set(pathKey(path), back.value);
|
|
525
|
+
}
|
|
526
|
+
catch (e) {
|
|
527
|
+
r.problems.push({ path, error: `${pathText(path)}: ${e instanceof Error ? e.message : String(e)}` });
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
else
|
|
531
|
+
r.missing.push(path);
|
|
532
|
+
}
|
|
533
|
+
// a restored `$rest` is the rest of its parent: folded back in, so the parent is the object the digest was made over.
|
|
534
|
+
// Only where a side is captured as one (the arguments, the result, a fact's value): a fact's lookup arguments are
|
|
535
|
+
// captured field by field, and one of them may be named $rest.
|
|
536
|
+
const aggregate = (parent) => pathKey(parent) === pathKey(["request", "args"]) || pathKey(parent) === pathKey(["execution", "result"]) || (parent.length === 3 && parent[0] === "facts" && parent[2] === "value");
|
|
537
|
+
for (const path of note.withheld) {
|
|
538
|
+
if (path[path.length - 1] !== REST || !r.restored.has(pathKey(path)) || !aggregate(path.slice(0, -1)))
|
|
539
|
+
continue;
|
|
540
|
+
const parentPath = path.slice(0, -1);
|
|
541
|
+
const parent = getAt(r.predicate, parentPath);
|
|
542
|
+
if (!isObject(parent))
|
|
543
|
+
continue;
|
|
544
|
+
const merged = Object.create(null);
|
|
545
|
+
for (const [k, v] of Object.entries(parent))
|
|
546
|
+
if (k !== REST)
|
|
547
|
+
merged[k] = v;
|
|
548
|
+
const rest = parent[REST];
|
|
549
|
+
if (isObject(rest))
|
|
550
|
+
for (const [k, v] of Object.entries(rest))
|
|
551
|
+
merged[k] = v;
|
|
552
|
+
r.predicate = setAt(r.predicate, parentPath, merged);
|
|
553
|
+
}
|
|
554
|
+
return r;
|
|
555
|
+
}
|
|
556
|
+
/** The salt a digest over `prefix` was made with, once every withheld path under it is restored: see `digestSalt`. */
|
|
557
|
+
export function digestSaltFor(r, note, prefix) {
|
|
558
|
+
return digestSalt((note?.withheld ?? []).filter((w) => under(w, prefix)).map((w) => r.salts.get(pathKey(w)) ?? ""));
|
|
559
|
+
}
|