@agent-custody/state 0.14.0 → 0.16.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/dist/verify-web.js +141 -7
- package/package.json +9 -9
package/dist/verify-web.js
CHANGED
|
@@ -1,6 +1,94 @@
|
|
|
1
1
|
const RECEIPT_TYPE = "application/vnd.in-toto+json";
|
|
2
2
|
const PREDICATE_TYPE = "https://agent-custody.dev/receipt/v0.2";
|
|
3
3
|
const TREEHEAD_TYPE = "application/vnd.agent-custody.treehead+json";
|
|
4
|
+
// A copy of packages/receipts/src/addresses.ts: this file has no imports. Keep the two lists identical.
|
|
5
|
+
const V4 = [
|
|
6
|
+
["0.0.0.0", 8], ["10.0.0.0", 8], ["100.64.0.0", 10], ["127.0.0.0", 8], ["169.254.0.0", 16], ["172.16.0.0", 12],
|
|
7
|
+
["192.0.0.0", 24], ["192.168.0.0", 16], ["198.18.0.0", 15], ["224.0.0.0", 4], ["240.0.0.0", 4],
|
|
8
|
+
];
|
|
9
|
+
const V6 = [["::", 128], ["::1", 128], ["fc00::", 7], ["fe80::", 10], ["ff00::", 8], ["64:ff9b:1::", 48]];
|
|
10
|
+
function v4(ip) {
|
|
11
|
+
const parts = ip.split(".");
|
|
12
|
+
if (parts.length !== 4)
|
|
13
|
+
return null;
|
|
14
|
+
let n = 0;
|
|
15
|
+
for (const p of parts) {
|
|
16
|
+
// no leading zeros: `010` is octal to getaddrinfo and an error to Python's ipaddress, so it is not an address here either
|
|
17
|
+
if (!/^(0|[1-9]\d{0,2})$/.test(p) || Number(p) > 255)
|
|
18
|
+
return null;
|
|
19
|
+
n = n * 256 + Number(p);
|
|
20
|
+
}
|
|
21
|
+
return n;
|
|
22
|
+
}
|
|
23
|
+
function v6(ip) {
|
|
24
|
+
let s = ip.toLowerCase();
|
|
25
|
+
// an embedded IPv4 tail (::ffff:10.0.0.1) becomes its two hex groups
|
|
26
|
+
if (s.includes(".")) {
|
|
27
|
+
const at = s.lastIndexOf(":");
|
|
28
|
+
const n = at < 0 ? null : v4(s.slice(at + 1));
|
|
29
|
+
if (n === null)
|
|
30
|
+
return null;
|
|
31
|
+
s = `${s.slice(0, at + 1)}${Math.floor(n / 65536).toString(16)}:${(n % 65536).toString(16)}`;
|
|
32
|
+
}
|
|
33
|
+
const halves = s.split("::");
|
|
34
|
+
if (halves.length > 2)
|
|
35
|
+
return null;
|
|
36
|
+
const read = (h) => (h === "" ? [] : h.split(":").map((g) => (/^[0-9a-f]{1,4}$/.test(g) ? parseInt(g, 16) : NaN)));
|
|
37
|
+
const head = read(halves[0]);
|
|
38
|
+
const rest = halves.length === 2 ? read(halves[1]) : [];
|
|
39
|
+
const fill = 8 - head.length - rest.length;
|
|
40
|
+
if (halves.length === 1 ? fill !== 0 : fill < 1)
|
|
41
|
+
return null;
|
|
42
|
+
const groups = [...head, ...Array(halves.length === 2 ? fill : 0).fill(0), ...rest];
|
|
43
|
+
return groups.every((g) => Number.isInteger(g)) ? groups : null;
|
|
44
|
+
}
|
|
45
|
+
function inV6(g, base, bits) {
|
|
46
|
+
for (let i = 0; i < 8 && bits > 0; i++, bits -= 16) {
|
|
47
|
+
const mask = bits >= 16 ? 0xffff : (0xffff << (16 - bits)) & 0xffff;
|
|
48
|
+
if ((g[i] & mask) !== (base[i] & mask))
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
/** True when `ip` is not a public address, or is not an address at all: a caller that cannot read it must not dial it. */
|
|
54
|
+
function internalAddress(ip) {
|
|
55
|
+
const n = v4(ip);
|
|
56
|
+
if (n !== null)
|
|
57
|
+
return V4.some(([base, bits]) => Math.floor(n / 2 ** (32 - bits)) === Math.floor(v4(base) / 2 ** (32 - bits)));
|
|
58
|
+
const g = v6(ip.replace(/^\[|\]$/g, "").replace(/%.*$/, ""));
|
|
59
|
+
if (!g)
|
|
60
|
+
return true;
|
|
61
|
+
const embedded = embeddedV4(g);
|
|
62
|
+
if (embedded)
|
|
63
|
+
return internalAddress(embedded);
|
|
64
|
+
return V6.some(([base, bits]) => inV6(g, v6(base), bits));
|
|
65
|
+
}
|
|
66
|
+
/** The IPv4 address inside an IPv4-mapped (::ffff:0:0/96) or NAT64 (64:ff9b::/96) address, else null: a DNS64 answer can wrap an internal IPv4 address. */
|
|
67
|
+
function embeddedV4(g) {
|
|
68
|
+
const mapped = g.slice(0, 5).every((x) => x === 0) && g[5] === 0xffff;
|
|
69
|
+
const nat64 = g[0] === 0x64 && g[1] === 0xff9b && g.slice(2, 6).every((x) => x === 0);
|
|
70
|
+
return mapped || nat64 ? `${g[6] >> 8}.${g[6] & 255}.${g[7] >> 8}.${g[7] & 255}` : null;
|
|
71
|
+
}
|
|
72
|
+
/** The address in a canonical form (IPv4 dotted, or eight IPv6 groups; a mapped IPv4 address as IPv4), or null when it is not one. */
|
|
73
|
+
function canonicalAddress(ip) {
|
|
74
|
+
if (v4(ip) !== null)
|
|
75
|
+
return ip.split(".").map(Number).join(".");
|
|
76
|
+
const g = v6(ip.replace(/^\[|\]$/g, "").replace(/%.*$/, ""));
|
|
77
|
+
if (!g)
|
|
78
|
+
return null;
|
|
79
|
+
if (g.slice(0, 5).every((x) => x === 0) && g[5] === 0xffff)
|
|
80
|
+
return embeddedV4(g);
|
|
81
|
+
return g.map((x) => x.toString(16)).join(":");
|
|
82
|
+
}
|
|
83
|
+
/** True when `a` and `b` are the same address, written however (::1 and 0:0::1; 10.0.0.1 and ::ffff:10.0.0.1). */
|
|
84
|
+
function sameAddress(a, b) {
|
|
85
|
+
const x = canonicalAddress(a);
|
|
86
|
+
return x !== null && x === canonicalAddress(b);
|
|
87
|
+
}
|
|
88
|
+
/** True when `host` is an IP address rather than a name. */
|
|
89
|
+
function isAddressLiteral(host) {
|
|
90
|
+
return canonicalAddress(host) !== null;
|
|
91
|
+
}
|
|
4
92
|
const DELEGATION_TYPE = "application/vnd.agent-custody.delegation+json";
|
|
5
93
|
const UPSTREAM_TYPE = "application/vnd.agent-custody.upstream+json";
|
|
6
94
|
const AUTHORIZATION_TYPE = "https://agent-custody.dev/authorization/v0.1";
|
|
@@ -131,10 +219,23 @@ export async function verifyDelegationChain(env, trusted, depth = 1) {
|
|
|
131
219
|
// ---- RFC 6962 / 9162 ----
|
|
132
220
|
const leafHash = (data) => sha256(concat(new Uint8Array([0]), enc.encode(data)));
|
|
133
221
|
const nodeHash = (l, r) => sha256(concat(new Uint8Array([1]), l, r));
|
|
222
|
+
// every hash a proof check reads is 32 bytes: a proof hash and a root are each a string of 64 lowercase hex digits;
|
|
223
|
+
// sizes and indices are integers, since the loops halve them with Math.floor
|
|
224
|
+
const HASH_HEX = /^[0-9a-f]{64}$/;
|
|
225
|
+
const isHash = (x) => typeof x === "string" && HASH_HEX.test(x);
|
|
226
|
+
const isSize = (n) => Number.isSafeInteger(n) && n >= 0;
|
|
227
|
+
// exact for every safe integer: `(n & (n - 1)) === 0` reads only the low 32 bits of n
|
|
228
|
+
const isPowerOfTwo = (n) => { let k = 1; while (k < n)
|
|
229
|
+
k *= 2; return k === n; };
|
|
230
|
+
const EMPTY_ROOT = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
|
|
134
231
|
export async function verifyInclusion(leaf, proof, rootHex) {
|
|
232
|
+
if (!isSize(proof.leafIndex) || !isSize(proof.treeSize))
|
|
233
|
+
return false;
|
|
135
234
|
let fn = proof.leafIndex, sn = proof.treeSize - 1;
|
|
136
235
|
if (fn < 0 || sn < 0 || fn > sn)
|
|
137
236
|
return false;
|
|
237
|
+
if (leaf.length !== 32 || !isHash(rootHex) || !Array.isArray(proof.hashes) || !proof.hashes.every(isHash))
|
|
238
|
+
return false;
|
|
138
239
|
let r = leaf;
|
|
139
240
|
for (const h of proof.hashes) {
|
|
140
241
|
if (sn === 0)
|
|
@@ -157,16 +258,19 @@ export async function verifyInclusion(leaf, proof, rootHex) {
|
|
|
157
258
|
return sn === 0 && hex(r) === rootHex;
|
|
158
259
|
}
|
|
159
260
|
export async function verifyConsistency(oldSize, oldRoot, newSize, newRoot, proofHex) {
|
|
160
|
-
if (oldSize
|
|
261
|
+
if (!isSize(oldSize) || !isSize(newSize) || oldSize > newSize)
|
|
262
|
+
return false;
|
|
263
|
+
if (!isHash(oldRoot) || !isHash(newRoot) || !Array.isArray(proofHex) || !proofHex.every(isHash))
|
|
161
264
|
return false;
|
|
265
|
+
// the empty tree has one root, sha256 of nothing; any other root at size 0, old or new, is a head no log could have signed
|
|
266
|
+
if (oldSize === 0)
|
|
267
|
+
return proofHex.length === 0 && oldRoot === EMPTY_ROOT && (newSize > 0 || newRoot === EMPTY_ROOT);
|
|
162
268
|
if (oldSize === newSize)
|
|
163
269
|
return proofHex.length === 0 && oldRoot === newRoot;
|
|
164
|
-
if (oldSize === 0)
|
|
165
|
-
return proofHex.length === 0;
|
|
166
270
|
if (proofHex.length === 0)
|
|
167
271
|
return false;
|
|
168
272
|
const proof = proofHex.map(unhex);
|
|
169
|
-
if ((oldSize
|
|
273
|
+
if (isPowerOfTwo(oldSize))
|
|
170
274
|
proof.unshift(unhex(oldRoot));
|
|
171
275
|
let fn = oldSize - 1, sn = newSize - 1;
|
|
172
276
|
while (fn % 2 === 1) {
|
|
@@ -195,6 +299,9 @@ export async function verifyConsistency(oldSize, oldRoot, newSize, newRoot, proo
|
|
|
195
299
|
const ISSUER_NOTE = {
|
|
196
300
|
gateway: "enforced outside the agent's process; the agent could neither skip nor forge this receipt",
|
|
197
301
|
sdk: "self-reported by the agent's own process; tamper-evident after issue, but nothing here was enforced outside the agent",
|
|
302
|
+
door: "enforced in front of the business's own HTTP surface; the arriving party could neither skip nor forge this receipt, and is attested only when it presented a grant",
|
|
303
|
+
guardian: "decided by an ACS Guardian outside the agent's process and signed with its own key, so the decision is attributable to it; the call is the agent's own report, and the agent could skip the Guardian or ignore its answer",
|
|
304
|
+
egress: "observed by a forward proxy in the agent's network path, for a connection that went around the gateway: where it went and how much, never what was sent; the agent is known only by the proxy credential the witness checked, and a connection that skipped the proxy (no lockdown) leaves no receipt at all",
|
|
198
305
|
};
|
|
199
306
|
const short = (s) => s.slice(0, 12);
|
|
200
307
|
async function hmacHex(secret, data) {
|
|
@@ -281,8 +388,8 @@ async function verifyOriginalAuthorization(bundle, opts) {
|
|
|
281
388
|
add("structural parts in clear", !problem, problem ?? undefined);
|
|
282
389
|
if (problem)
|
|
283
390
|
return { ok: false, checks };
|
|
284
|
-
add("authorization issued by the gateway", p.issuer.kind === "gateway" && p.issuer.keyid === sig.keyid);
|
|
285
|
-
add("authorization operation is submitted", p.operation?.status === "submitted");
|
|
391
|
+
add("authorization issued by the gateway", (p.issuer.kind === "gateway" || p.issuer.kind === "door") && p.issuer.keyid === sig.keyid, p.issuer.kind === "door" ? "a door commits a request before relaying it, as a gateway commits a call" : undefined);
|
|
392
|
+
add("authorization operation is submitted", p.issuer.kind === "door" || p.operation?.status === "submitted", p.issuer.kind === "door" ? "a door has no operations" : undefined);
|
|
286
393
|
add("authorization permits the original call", p.policy?.decision === "allow" ? Array.isArray(p.policy.errors) && p.policy.errors.length === 0 : p.policy?.decision === "deny" && p.policy.enforced === false);
|
|
287
394
|
const del = p.delegation && await verifyDelegationChain(p.delegation.envelope, opts.principalKeys);
|
|
288
395
|
add("delegation signature (principal key)", !!del?.ok);
|
|
@@ -451,7 +558,7 @@ export async function verifyBundle(bundle, opts) {
|
|
|
451
558
|
if (!add("structural parts in clear", !broken, broken ?? undefined))
|
|
452
559
|
return done(null);
|
|
453
560
|
const p = st.predicate;
|
|
454
|
-
add("issuer kind is known", p.issuer.kind === "gateway" || p.issuer.kind === "sdk", `${p.issuer.kind}${p.issuer.framework ? ` / ${p.issuer.framework}` : ""}`);
|
|
561
|
+
add("issuer kind is known", p.issuer.kind === "gateway" || p.issuer.kind === "sdk" || p.issuer.kind === "door" || p.issuer.kind === "egress" || p.issuer.kind === "guardian", `${p.issuer.kind}${p.issuer.framework ? ` / ${p.issuer.framework}` : ""}`);
|
|
455
562
|
add("issuer keyid matches signer", p.issuer.keyid === sig.keyid);
|
|
456
563
|
if (opts.requireStrict) {
|
|
457
564
|
add("strict mode in force", p.issuer.kind === "gateway" && p.issuer.strict === true, p.issuer.strict === true ? undefined : "the issuer does not say it ran in strict mode");
|
|
@@ -474,6 +581,33 @@ export async function verifyBundle(bundle, opts) {
|
|
|
474
581
|
add("gateway receipt carries a delegation", p.delegation !== undefined);
|
|
475
582
|
add("gateway receipt carries a policy decision", p.policy !== null);
|
|
476
583
|
}
|
|
584
|
+
if (p.issuer.kind === "door")
|
|
585
|
+
add("door receipt carries a policy decision", p.policy !== null);
|
|
586
|
+
// An egress witness saw a connection that went around the gateway: where it went and how much, never what was sent.
|
|
587
|
+
// Its destination is its own observation; its agent is only as good as the proxy credential it checked.
|
|
588
|
+
if (p.issuer.kind === "egress") {
|
|
589
|
+
add("egress receipt carries a policy decision", p.policy !== null);
|
|
590
|
+
const dest = Object.hasOwn(p.facts, "destination") ? p.facts.destination : undefined;
|
|
591
|
+
const seen = dest?.value;
|
|
592
|
+
const name = typeof seen?.host === "string" && typeof seen.port === "number" ? `${seen.host.includes(":") ? `[${seen.host}]` : seen.host}:${seen.port}` : undefined;
|
|
593
|
+
add("egress destination observed", p.tool.provenance === "observed" && dest?.provenance === "observed" && name === p.tool.name, name === p.tool.name ? `${p.tool.name}; an observed bypass of the gateway, not a record of what was sent` : `the destination fact names ${name ?? "nothing"}, the receipt ${p.tool.name}`);
|
|
594
|
+
const relayed = p.execution.status === "executed" || p.execution.status === "failed" || p.execution.status === "error";
|
|
595
|
+
add("egress relayed only for a checked agent", !relayed || p.agent.provenance === "observed", p.agent.provenance === "observed" ? `${p.agent.id}, by the proxy credential the witness checked` : relayed ? `the connection was ${p.execution.status} for an agent the witness did not check` : "no agent was checked and nothing was relayed");
|
|
596
|
+
add("egress open committed before relaying", !relayed || !!p.authorization, relayed ? (p.authorization ? "the connection was in the log before any byte moved" : `the connection was ${p.execution.status} with nothing committed first`) : "nothing was relayed");
|
|
597
|
+
// An IP literal is the action itself: the peer must be that very address. A host name must have led to a public address.
|
|
598
|
+
const host = typeof seen?.host === "string" ? seen.host : "";
|
|
599
|
+
const literal = isAddressLiteral(host);
|
|
600
|
+
const reached = p.execution.status === "executed" || p.execution.status === "failed";
|
|
601
|
+
const peer = p.execution.status === "executed" || p.execution.status === "failed" ? p.execution.result?.peer : undefined;
|
|
602
|
+
const peerOk = typeof peer === "string" && (literal ? sameAddress(peer, host) : !internalAddress(peer));
|
|
603
|
+
add("egress peer matches the destination", !reached || peerOk, !reached ? "no connection was made" : literal ? `${host}, an address the policy named, reached ${typeof peer === "string" ? peer : "no recorded peer"}` : `${host} reached ${typeof peer === "string" ? peer : "no recorded peer"}${peerOk ? ", a public address" : ""}`);
|
|
604
|
+
}
|
|
605
|
+
// A Guardian decides and carries out nothing: an allow is withheld (nothing forwarded), a deny is denied.
|
|
606
|
+
if (p.issuer.kind === "guardian") {
|
|
607
|
+
add("guardian receipt carries a policy decision", !!p.policy && typeof p.policy === "object" && !Array.isArray(p.policy));
|
|
608
|
+
const decided = p.execution.status === "denied" ? p.policy?.decision === "deny" : p.execution.status === "withheld" && p.policy?.decision === "allow";
|
|
609
|
+
add("guardian decided, executed nothing", decided, decided ? `${p.policy?.decision}; the agent asked, reports the call itself, and is trusted to honor the answer` : `the receipt says ${p.execution.status} with a policy ${p.policy?.decision ?? "absent"}`);
|
|
610
|
+
}
|
|
477
611
|
if (p.delegation) {
|
|
478
612
|
const del = await verifyDelegationChain(p.delegation.envelope, opts.principalKeys);
|
|
479
613
|
const d = del.ok ? del.delegation : null;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agent-custody/state",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"description": "Governed memory for AI agents: a fact ledger with provenance, valid time, rollback, lineage, certified forget, and a store that is a JSONL file, SQLite, or Postgres, built on @agent-custody/receipts",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -33,17 +33,17 @@
|
|
|
33
33
|
"node": ">=22"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@agent-custody/receipts": "0.
|
|
37
|
-
"@modelcontextprotocol/sdk": "
|
|
38
|
-
"zod": "
|
|
36
|
+
"@agent-custody/receipts": "0.16.0",
|
|
37
|
+
"@modelcontextprotocol/sdk": "1.30.0",
|
|
38
|
+
"zod": "4.5.4"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@electric-sql/pglite": "0.5.8",
|
|
42
|
-
"@getzep/zep-cloud": "
|
|
43
|
-
"@types/node": "
|
|
44
|
-
"mem0ai": "
|
|
45
|
-
"typescript": "
|
|
46
|
-
"vitest": "
|
|
42
|
+
"@getzep/zep-cloud": "3.28.0",
|
|
43
|
+
"@types/node": "26.4.1",
|
|
44
|
+
"mem0ai": "3.1.8",
|
|
45
|
+
"typescript": "7.0.2",
|
|
46
|
+
"vitest": "5.0.0"
|
|
47
47
|
},
|
|
48
48
|
"peerDependencies": {
|
|
49
49
|
"pg": ">=8"
|