@agent-custody/state 0.15.0 → 0.16.1

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 (2) hide show
  1. package/dist/verify-web.js +120 -2
  2. package/package.json +2 -2
@@ -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";
@@ -136,6 +224,9 @@ const nodeHash = (l, r) => sha256(concat(new Uint8Array([1]), l, r));
136
224
  const HASH_HEX = /^[0-9a-f]{64}$/;
137
225
  const isHash = (x) => typeof x === "string" && HASH_HEX.test(x);
138
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; };
139
230
  const EMPTY_ROOT = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
140
231
  export async function verifyInclusion(leaf, proof, rootHex) {
141
232
  if (!isSize(proof.leafIndex) || !isSize(proof.treeSize))
@@ -179,7 +270,7 @@ export async function verifyConsistency(oldSize, oldRoot, newSize, newRoot, proo
179
270
  if (proofHex.length === 0)
180
271
  return false;
181
272
  const proof = proofHex.map(unhex);
182
- if ((oldSize & (oldSize - 1)) === 0)
273
+ if (isPowerOfTwo(oldSize))
183
274
  proof.unshift(unhex(oldRoot));
184
275
  let fn = oldSize - 1, sn = newSize - 1;
185
276
  while (fn % 2 === 1) {
@@ -209,6 +300,8 @@ const ISSUER_NOTE = {
209
300
  gateway: "enforced outside the agent's process; the agent could neither skip nor forge this receipt",
210
301
  sdk: "self-reported by the agent's own process; tamper-evident after issue, but nothing here was enforced outside the agent",
211
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",
212
305
  };
213
306
  const short = (s) => s.slice(0, 12);
214
307
  async function hmacHex(secret, data) {
@@ -465,7 +558,7 @@ export async function verifyBundle(bundle, opts) {
465
558
  if (!add("structural parts in clear", !broken, broken ?? undefined))
466
559
  return done(null);
467
560
  const p = st.predicate;
468
- add("issuer kind is known", p.issuer.kind === "gateway" || p.issuer.kind === "sdk" || p.issuer.kind === "door", `${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}` : ""}`);
469
562
  add("issuer keyid matches signer", p.issuer.keyid === sig.keyid);
470
563
  if (opts.requireStrict) {
471
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");
@@ -490,6 +583,31 @@ export async function verifyBundle(bundle, opts) {
490
583
  }
491
584
  if (p.issuer.kind === "door")
492
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
+ }
493
611
  if (p.delegation) {
494
612
  const del = await verifyDelegationChain(p.delegation.envelope, opts.principalKeys);
495
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.15.0",
3
+ "version": "0.16.1",
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,7 +33,7 @@
33
33
  "node": ">=22"
34
34
  },
35
35
  "dependencies": {
36
- "@agent-custody/receipts": "0.15.0",
36
+ "@agent-custody/receipts": "0.16.1",
37
37
  "@modelcontextprotocol/sdk": "1.30.0",
38
38
  "zod": "4.5.4"
39
39
  },