@agent-custody/receipts 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/README.md +48 -21
- package/dist/acs.d.ts +47 -0
- package/dist/acs.js +235 -0
- package/dist/addresses.d.ts +6 -0
- package/dist/addresses.js +90 -0
- package/dist/check.d.ts +10 -0
- package/dist/check.js +179 -3
- package/dist/cli.js +147 -12
- package/dist/config.d.ts +198 -1
- package/dist/config.js +154 -3
- package/dist/console.js +16 -7
- package/dist/deny-report.js +1 -1
- package/dist/door.d.ts +203 -0
- package/dist/door.js +712 -0
- package/dist/egress-draft.d.ts +43 -0
- package/dist/egress-draft.js +153 -0
- package/dist/egress.d.ts +116 -0
- package/dist/egress.js +585 -0
- package/dist/export.d.ts +39 -0
- package/dist/export.js +95 -0
- package/dist/gateway.js +53 -21
- package/dist/index.d.ts +15 -0
- package/dist/index.js +10 -0
- package/dist/issue.js +5 -65
- package/dist/lock.d.ts +12 -0
- package/dist/lock.js +124 -0
- package/dist/log.d.ts +11 -5
- package/dist/log.js +63 -43
- package/dist/opa-server.d.ts +34 -0
- package/dist/opa-server.js +93 -0
- package/dist/policy-rego.d.ts +30 -0
- package/dist/policy-rego.js +116 -0
- package/dist/portal.js +147 -41
- package/dist/pr-summary.d.ts +25 -0
- package/dist/pr-summary.js +87 -0
- package/dist/receipt.d.ts +8 -2
- package/dist/retention.d.ts +1 -1
- package/dist/retention.js +14 -4
- package/dist/sdk/index.js +10 -3
- package/dist/simulate.d.ts +3 -1
- package/dist/simulate.js +15 -7
- package/dist/verify.js +35 -4
- package/docs/acs.md +107 -0
- package/docs/configuration.md +3 -3
- package/docs/door-signals.md +66 -0
- package/docs/door.md +168 -0
- package/docs/egress.md +177 -0
- package/docs/github-action.md +164 -0
- package/docs/policies.md +112 -1
- package/docs/sdk.md +25 -9
- package/docs/threat-model.md +1 -1
- package/docs/tutorials.md +7 -0
- package/docs/usage.md +2 -2
- package/docs/verification.md +16 -2
- package/package.json +14 -13
- package/templates/claude-code.cedar +287 -10
- package/vectors/audit.json +27 -27
- package/vectors/canonical.json +5 -5
- package/vectors/merkle.json +123 -0
- package/vectors/receipts.json +1951 -1005
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type Destination } from "./egress.ts";
|
|
2
|
+
import { type VerifyOptions } from "./verify.ts";
|
|
3
|
+
export interface DraftDestination {
|
|
4
|
+
/** `host:port`, the Cedar action */
|
|
5
|
+
name: string;
|
|
6
|
+
/** `connect` (a tunnel) or `http` (a plain request): the same action covers both, so the draft keeps them apart */
|
|
7
|
+
scheme: Destination["scheme"];
|
|
8
|
+
count: number;
|
|
9
|
+
first: string;
|
|
10
|
+
last: string;
|
|
11
|
+
/** an IP literal in an internal range: the witness dials a literal a rule names, so only the platform refuses it */
|
|
12
|
+
internal: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface DraftAgent {
|
|
15
|
+
id: string;
|
|
16
|
+
count: number;
|
|
17
|
+
first: string;
|
|
18
|
+
last: string;
|
|
19
|
+
destinations: DraftDestination[];
|
|
20
|
+
}
|
|
21
|
+
export interface DraftSkip {
|
|
22
|
+
file: string;
|
|
23
|
+
kind: "not-a-file" | "unverified" | "not-egress" | "no-agent" | "not-relayed" | "unsafe-name";
|
|
24
|
+
reason: string;
|
|
25
|
+
}
|
|
26
|
+
export interface EgressPolicyDraft {
|
|
27
|
+
/** the Cedar text, which starts with the review warning */
|
|
28
|
+
policy: string;
|
|
29
|
+
agents: DraftAgent[];
|
|
30
|
+
/** receipt files read */
|
|
31
|
+
read: number;
|
|
32
|
+
/** relayed connections from a verified agent, the ones the draft permits */
|
|
33
|
+
used: number;
|
|
34
|
+
skipped: DraftSkip[];
|
|
35
|
+
}
|
|
36
|
+
export declare const DRAFT_WARNING: string[];
|
|
37
|
+
/**
|
|
38
|
+
* Reads every regular receipt file in `receiptsDir`, verifies each with `keys`, and drafts one `permit` per verified
|
|
39
|
+
* agent and scheme over the `host:port` destinations of the connections the witness relayed for it. A symlink, a
|
|
40
|
+
* receipt that does not verify, names no verified agent, or was not relayed is skipped and listed with its reason.
|
|
41
|
+
* Agents and destinations are sorted.
|
|
42
|
+
*/
|
|
43
|
+
export declare function draftEgressPolicy(receiptsDir: string, keys: Pick<VerifyOptions, "issuerKeys" | "logKeys" | "logFile">): EgressPolicyDraft;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// A first egress policy, drafted from what the agents did while the witness observed them. It is a record of the
|
|
2
|
+
// window, not a judgment: a destination an agent should never have reached is permitted like any other.
|
|
3
|
+
import { closeSync, constants as fsConstants, fstatSync, openSync, readdirSync, readFileSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { internalAddress, isAddressLiteral } from "./addresses.js";
|
|
6
|
+
import { DESTINATION_FACT } from "./egress.js";
|
|
7
|
+
import { verifyBundle } from "./verify.js";
|
|
8
|
+
const SKIP_LABEL = {
|
|
9
|
+
"not-a-file": "not a regular file (a symlink is not followed)",
|
|
10
|
+
unverified: "did not verify",
|
|
11
|
+
"not-egress": "not an egress receipt",
|
|
12
|
+
"no-agent": "no verified agent",
|
|
13
|
+
"not-relayed": "not relayed (denied, error, or withheld)",
|
|
14
|
+
"unsafe-name": "a name a Cedar string cannot carry",
|
|
15
|
+
};
|
|
16
|
+
export const DRAFT_WARNING = [
|
|
17
|
+
"// DRAFT. Review every line before you enforce it.",
|
|
18
|
+
"// This policy permits every destination the agents below reached while the witness observed them,",
|
|
19
|
+
"// including any they should not have reached. A permit here records what happened, not that it was right.",
|
|
20
|
+
"// Delete each destination you do not recognise. A provider that needs a credential (api.stripe.com,",
|
|
21
|
+
"// api.openai.com, ...) belongs in the gateway, which holds the key, not in a permit here.",
|
|
22
|
+
];
|
|
23
|
+
const AUTHORIZATION = ".authorization.json";
|
|
24
|
+
// A name goes into a Cedar string and a line comment: a quote, a backslash, or a control character could end either.
|
|
25
|
+
const UNSAFE = /["\\\u0000-\u001f\u007f]/;
|
|
26
|
+
/** Why a verified receipt does not count towards the draft, or null when it does. */
|
|
27
|
+
function notUsed(p) {
|
|
28
|
+
if (p.issuer.kind !== "egress")
|
|
29
|
+
return { kind: "not-egress", reason: `issuer kind ${p.issuer.kind}` };
|
|
30
|
+
if (p.agent.provenance !== "observed")
|
|
31
|
+
return { kind: "no-agent", reason: "the proxy credential was absent or did not verify" };
|
|
32
|
+
if (p.execution.status !== "executed")
|
|
33
|
+
return { kind: "not-relayed", reason: p.execution.status };
|
|
34
|
+
if (UNSAFE.test(p.agent.id) || UNSAFE.test(p.tool.name))
|
|
35
|
+
return { kind: "unsafe-name", reason: "the agent id or destination holds a quote, a backslash, or a control character" };
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Reads every regular receipt file in `receiptsDir`, verifies each with `keys`, and drafts one `permit` per verified
|
|
40
|
+
* agent and scheme over the `host:port` destinations of the connections the witness relayed for it. A symlink, a
|
|
41
|
+
* receipt that does not verify, names no verified agent, or was not relayed is skipped and listed with its reason.
|
|
42
|
+
* Agents and destinations are sorted.
|
|
43
|
+
*/
|
|
44
|
+
export function draftEgressPolicy(receiptsDir, keys) {
|
|
45
|
+
// Symlinks are listed so that they are reported, and never followed: one could lead outside the directory, to a
|
|
46
|
+
// receipt the listing does not show, or to a fifo that stalls the read.
|
|
47
|
+
const files = readdirSync(receiptsDir, { withFileTypes: true })
|
|
48
|
+
.filter((e) => !e.isDirectory() && e.name.endsWith(".json") && !e.name.endsWith(AUTHORIZATION))
|
|
49
|
+
.map((e) => e.name)
|
|
50
|
+
.sort();
|
|
51
|
+
const skipped = [];
|
|
52
|
+
const byAgent = new Map();
|
|
53
|
+
let used = 0;
|
|
54
|
+
for (const file of files) {
|
|
55
|
+
let p;
|
|
56
|
+
let text = null;
|
|
57
|
+
try {
|
|
58
|
+
// O_NOFOLLOW refuses a symlink, also one swapped in after the listing; O_NONBLOCK keeps a fifo from stalling the open
|
|
59
|
+
const fd = openSync(join(receiptsDir, file), fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW | fsConstants.O_NONBLOCK);
|
|
60
|
+
try {
|
|
61
|
+
if (fstatSync(fd).isFile())
|
|
62
|
+
text = readFileSync(fd, "utf8");
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
closeSync(fd);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
if (e.code !== "ELOOP")
|
|
70
|
+
throw e;
|
|
71
|
+
}
|
|
72
|
+
if (text === null) {
|
|
73
|
+
skipped.push({ file, kind: "not-a-file", reason: "a symlink or a special file; only regular files in the directory are read" });
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
const result = verifyBundle(JSON.parse(text), { ...keys, principalKeys: [] });
|
|
78
|
+
if (!result.ok || !result.statement) {
|
|
79
|
+
skipped.push({ file, kind: "unverified", reason: `failed ${result.checks.filter((c) => !c.ok).map((c) => c.name).join("; ")}` });
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
p = result.statement.predicate;
|
|
83
|
+
}
|
|
84
|
+
catch (e) {
|
|
85
|
+
skipped.push({ file, kind: "unverified", reason: `${e instanceof Error ? e.message : String(e)}` });
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const why = notUsed(p);
|
|
89
|
+
if (why) {
|
|
90
|
+
skipped.push({ file, ...why });
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
used++;
|
|
94
|
+
const observed = p.facts[DESTINATION_FACT]?.value;
|
|
95
|
+
const host = observed?.host ?? "";
|
|
96
|
+
const scheme = observed?.scheme === "http" ? "http" : "connect";
|
|
97
|
+
const dests = byAgent.get(p.agent.id) ?? new Map();
|
|
98
|
+
byAgent.set(p.agent.id, dests);
|
|
99
|
+
const key = `${scheme} ${p.tool.name}`;
|
|
100
|
+
const d = dests.get(key);
|
|
101
|
+
if (d) {
|
|
102
|
+
d.count++;
|
|
103
|
+
if (p.timestamp < d.first)
|
|
104
|
+
d.first = p.timestamp;
|
|
105
|
+
if (p.timestamp > d.last)
|
|
106
|
+
d.last = p.timestamp;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
dests.set(key, { name: p.tool.name, scheme, count: 1, first: p.timestamp, last: p.timestamp, internal: isAddressLiteral(host) && internalAddress(host) });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const order = (x, y) => (x < y ? -1 : x > y ? 1 : 0);
|
|
113
|
+
const agents = [...byAgent].map(([id, dests]) => {
|
|
114
|
+
const destinations = [...dests.values()].sort((a, b) => order(a.scheme, b.scheme) || order(a.name, b.name));
|
|
115
|
+
return {
|
|
116
|
+
id,
|
|
117
|
+
count: destinations.reduce((n, d) => n + d.count, 0),
|
|
118
|
+
first: destinations.map((d) => d.first).sort()[0],
|
|
119
|
+
last: destinations.map((d) => d.last).sort().at(-1),
|
|
120
|
+
destinations,
|
|
121
|
+
};
|
|
122
|
+
}).sort((a, b) => order(a.id, b.id));
|
|
123
|
+
return { policy: formatDraft(agents, files.length, used, skipped), agents, read: files.length, used, skipped };
|
|
124
|
+
}
|
|
125
|
+
const times = (n, first, last) => `${n} connection${n === 1 ? "" : "s"}, first ${first}, last ${last}`;
|
|
126
|
+
function formatDraft(agents, read, used, skipped) {
|
|
127
|
+
const lines = [...DRAFT_WARNING, "//", `// Drafted by agent-custody egress draft-policy from ${read} receipt${read === 1 ? "" : "s"}: ${used} relayed connection${used === 1 ? "" : "s"} from verified agents used, ${skipped.length} skipped.`];
|
|
128
|
+
for (const kind of Object.keys(SKIP_LABEL)) {
|
|
129
|
+
const n = skipped.filter((s) => s.kind === kind).length;
|
|
130
|
+
if (n)
|
|
131
|
+
lines.push(`// skipped, ${SKIP_LABEL[kind]}: ${n}`);
|
|
132
|
+
}
|
|
133
|
+
// One permit per agent and scheme: the action is host:port for a tunnel and a plain request alike, so a permit that
|
|
134
|
+
// left the scheme open would turn an observed plain GET into a permitted CONNECT tunnel to the same host and port.
|
|
135
|
+
for (const a of agents) {
|
|
136
|
+
for (const scheme of ["connect", "http"]) {
|
|
137
|
+
const ds = a.destinations.filter((d) => d.scheme === scheme);
|
|
138
|
+
if (!ds.length)
|
|
139
|
+
continue;
|
|
140
|
+
const n = ds.reduce((k, d) => k + d.count, 0);
|
|
141
|
+
lines.push("", `// ${a.id}, ${scheme === "connect" ? "CONNECT tunnels" : "plain HTTP requests"}: ${times(n, ds.map((d) => d.first).sort()[0], ds.map((d) => d.last).sort().at(-1))}`);
|
|
142
|
+
lines.push(`permit(principal == Agent::"${a.id}", action in [`);
|
|
143
|
+
ds.forEach((d, i) => {
|
|
144
|
+
const note = d.internal ? "; INTERNAL ADDRESS: the witness dials an IP literal a rule names, and only the platform's deny list refuses it" : "";
|
|
145
|
+
lines.push(` Action::"${d.name}"${i < ds.length - 1 ? "," : ""} // ${times(d.count, d.first, d.last)}${note}`);
|
|
146
|
+
});
|
|
147
|
+
lines.push(`], resource) when { context.facts.destination.scheme == "${scheme}" };`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (!agents.length)
|
|
151
|
+
lines.push("", "// No relayed connection from a verified agent: nothing to permit.");
|
|
152
|
+
return lines.join("\n") + "\n";
|
|
153
|
+
}
|
package/dist/egress.d.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { type IncomingMessage, type ServerResponse } from "node:http";
|
|
2
|
+
import { type LookupFunction } from "node:net";
|
|
3
|
+
import type { Duplex } from "node:stream";
|
|
4
|
+
import type { EgressConfig } from "./config.ts";
|
|
5
|
+
import { type FlushResult } from "./issue.ts";
|
|
6
|
+
import { type LogSink } from "./log-sink.ts";
|
|
7
|
+
import { type ReceiptExporter } from "./otel.ts";
|
|
8
|
+
export declare const EGRESS_VERSION = "0.1.0";
|
|
9
|
+
/** the fact every egress receipt carries, and the policy reads as `context.facts.destination` */
|
|
10
|
+
export declare const DESTINATION_FACT = "destination";
|
|
11
|
+
export declare const PROXY_REALM = "agent-custody egress";
|
|
12
|
+
/** What the witness observed about one connection before it decided. Only `declared` is the agent's word, and says so. */
|
|
13
|
+
export interface Destination {
|
|
14
|
+
host: string;
|
|
15
|
+
port: number;
|
|
16
|
+
/** `connect`: a CONNECT tunnel, opaque to the witness. `http`: a plain HTTP request in absolute form */
|
|
17
|
+
scheme: "connect" | "http";
|
|
18
|
+
/** what became of the proxy credential: it matched the secret the witness holds for that agent, it did not, or none was sent */
|
|
19
|
+
credential: "verified" | "invalid" | "absent";
|
|
20
|
+
/** the agent id in a credential that did not verify; a claim, never checked */
|
|
21
|
+
declared?: string;
|
|
22
|
+
/** the socket's address: a forward proxy has nothing in front of it, so no header the agent wrote is believed */
|
|
23
|
+
remote: string;
|
|
24
|
+
}
|
|
25
|
+
/** How a relayed connection ended: both sides closed, one side failed, no bytes for `idleTimeoutMs`, or the witness stopped. */
|
|
26
|
+
export type EgressEnd = "closed" | "reset" | "idle-timeout" | "witness-stopped";
|
|
27
|
+
/** What a receipt records of a relayed connection: where the witness actually connected, when, how many bytes, how it ended. */
|
|
28
|
+
export interface EgressResult {
|
|
29
|
+
/** the address the witness connected to, after its own name resolution */
|
|
30
|
+
peer: string;
|
|
31
|
+
openedAt: string;
|
|
32
|
+
closedAt: string;
|
|
33
|
+
/** bytes from the agent to the destination: the tunnel's payload, or a plain request's body */
|
|
34
|
+
bytesUp: number;
|
|
35
|
+
/** bytes from the destination to the agent: the tunnel's payload, or a plain response's body */
|
|
36
|
+
bytesDown: number;
|
|
37
|
+
end: EgressEnd;
|
|
38
|
+
/** a plain HTTP request only: the status the destination answered */
|
|
39
|
+
status?: number;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Lowercase, without a final dot or IPv6 brackets; refused unless it is an IP address or a DNS name. A name whose last
|
|
43
|
+
* label is all digits or starts with `0x` is refused: the URL parser and `getaddrinfo` read it as an IPv4 address
|
|
44
|
+
* (`2130706433`, `0x7f.1`), so the policy would decide on a string that is not the address dialled.
|
|
45
|
+
*/
|
|
46
|
+
export declare function normalizeHost(raw: string): string | null;
|
|
47
|
+
/**
|
|
48
|
+
* A CONNECT authority, `host:port` or `[v6]:port`; the port is required. The host goes through the same URL parser as a
|
|
49
|
+
* plain request's, so both paths decide on one canonical string: `010.0.0.1` is `8.0.0.1` and `2130706433` is
|
|
50
|
+
* `127.0.0.1`, the addresses `getaddrinfo` would dial.
|
|
51
|
+
*/
|
|
52
|
+
export declare function parseAuthority(authority: string): {
|
|
53
|
+
host: string;
|
|
54
|
+
port: number;
|
|
55
|
+
} | null;
|
|
56
|
+
/** The policy's action for a destination: `host:port`, with an IPv6 address in brackets. */
|
|
57
|
+
export declare const destinationName: (host: string, port: number) => string;
|
|
58
|
+
export interface AgentCredential {
|
|
59
|
+
id: string;
|
|
60
|
+
secret: string;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Checks `Proxy-Authorization: Basic base64(id:secret)` against the secrets the witness holds. The comparison is over
|
|
64
|
+
* sha256 digests in constant time, and runs against one stand-in, hashed once, when the id is unknown, so the answer
|
|
65
|
+
* time does not say which ids exist.
|
|
66
|
+
*/
|
|
67
|
+
export declare function checkProxyCredential(header: string | undefined, agents: ReadonlyMap<string, Buffer>): {
|
|
68
|
+
credential: Destination["credential"];
|
|
69
|
+
agent?: string;
|
|
70
|
+
declared?: string;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Wraps a resolver so that a host name never leads to an internal address: the policy decided on the name, and a name
|
|
74
|
+
* the agent chose (or a DNS answer someone else chose) must not turn a permit for a public host into a request to the
|
|
75
|
+
* metadata service, loopback, or the private network. Addresses that are internal are dropped before any connection is
|
|
76
|
+
* made; a name with none left fails. An IP literal is never looked up: it is the Cedar action itself, so a rule permits
|
|
77
|
+
* it by name. The check sits in the resolver the socket connects with, so there is no second lookup to rebind.
|
|
78
|
+
*/
|
|
79
|
+
export declare function publicOnlyLookup(resolve?: LookupFunction): LookupFunction;
|
|
80
|
+
export interface EgressOptions {
|
|
81
|
+
/** the log to append to, in place of the one the config names; for embedding and tests */
|
|
82
|
+
log?: LogSink;
|
|
83
|
+
exporter?: ReceiptExporter;
|
|
84
|
+
/** appends the outbox at start and every 30 seconds; only a serving witness sets it */
|
|
85
|
+
autoFlush?: boolean;
|
|
86
|
+
/** the resolver for host names, before the internal-address filter; for tests */
|
|
87
|
+
lookup?: LookupFunction;
|
|
88
|
+
}
|
|
89
|
+
export interface EgressHost {
|
|
90
|
+
keyid: string;
|
|
91
|
+
policyDigest(): string;
|
|
92
|
+
/** a plain HTTP request in absolute form */
|
|
93
|
+
handleRequest(req: IncomingMessage, res: ServerResponse): Promise<void>;
|
|
94
|
+
/** a CONNECT request: the tunnel, if the policy allows it */
|
|
95
|
+
handleConnect(req: IncomingMessage, client: Duplex, head: Buffer): Promise<void>;
|
|
96
|
+
flushOutbox(): Promise<FlushResult>;
|
|
97
|
+
pendingReceipts(): number;
|
|
98
|
+
/** closes every relayed connection (each is receipted as `witness-stopped`) and waits for their receipts */
|
|
99
|
+
close(): Promise<void>;
|
|
100
|
+
}
|
|
101
|
+
export declare function createEgressHost(cfg: EgressConfig, options?: EgressOptions): Promise<EgressHost>;
|
|
102
|
+
export interface EgressServerOptions {
|
|
103
|
+
port: number;
|
|
104
|
+
host?: string;
|
|
105
|
+
log?: (message: string) => void;
|
|
106
|
+
}
|
|
107
|
+
export interface RunningEgress {
|
|
108
|
+
url: string;
|
|
109
|
+
close(): Promise<void>;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Serves an egress host as an HTTP forward proxy. A request in origin form is for the witness itself:
|
|
113
|
+
* `GET /.well-known/agent-custody-egress` answers the key id and the policy digest, anything else 404. Bind it where
|
|
114
|
+
* only the locked-down agents can reach it.
|
|
115
|
+
*/
|
|
116
|
+
export declare function serveEgress(host: EgressHost, opts: EgressServerOptions): Promise<RunningEgress>;
|