@agent-custody/receipts 0.15.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 +29 -15
- 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/cli.js +67 -3
- package/dist/config.d.ts +70 -0
- package/dist/config.js +62 -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/index.d.ts +9 -0
- package/dist/index.js +5 -0
- package/dist/log.js +8 -1
- package/dist/policy-rego.js +3 -1
- package/dist/portal.js +147 -41
- package/dist/receipt.d.ts +6 -2
- package/dist/simulate.js +1 -1
- package/dist/verify.js +30 -2
- package/docs/acs.md +107 -0
- package/docs/egress.md +177 -0
- package/docs/github-action.md +3 -3
- package/docs/policies.md +1 -1
- package/docs/threat-model.md +1 -1
- package/docs/tutorials.md +4 -0
- package/docs/verification.md +9 -2
- package/package.json +2 -2
- package/vectors/receipts.json +693 -0
package/dist/config.d.ts
CHANGED
|
@@ -483,7 +483,77 @@ export declare const DoorConfigSchema: z.ZodObject<{
|
|
|
483
483
|
captureKeyEnv: z.ZodOptional<z.ZodString>;
|
|
484
484
|
}, z.core.$strip>;
|
|
485
485
|
export type DoorConfig = z.infer<typeof DoorConfigSchema>;
|
|
486
|
+
/**
|
|
487
|
+
* The egress witness: a forward proxy in the agent's network path for every connection that does not go to the gateway.
|
|
488
|
+
* Each agent proves who it is with its own proxy credential (`Proxy-Authorization: Basic`, id and secret); the secret
|
|
489
|
+
* comes from the environment, never from the file. The Cedar policy decides per destination, and every connection,
|
|
490
|
+
* relayed or refused, is receipted. It records where traffic went, never what was sent: there is no TLS interception.
|
|
491
|
+
*/
|
|
492
|
+
export declare const EgressConfigSchema: z.ZodObject<{
|
|
493
|
+
identity: z.ZodObject<{
|
|
494
|
+
keyFile: z.ZodString;
|
|
495
|
+
}, z.core.$strip>;
|
|
496
|
+
agents: z.ZodArray<z.ZodObject<{
|
|
497
|
+
id: z.ZodString;
|
|
498
|
+
secretEnv: z.ZodString;
|
|
499
|
+
}, z.core.$strip>>;
|
|
500
|
+
policyFile: z.ZodString;
|
|
501
|
+
mode: z.ZodOptional<z.ZodEnum<{
|
|
502
|
+
enforce: "enforce";
|
|
503
|
+
observe: "observe";
|
|
504
|
+
}>>;
|
|
505
|
+
connectTimeoutMs: z.ZodDefault<z.ZodNumber>;
|
|
506
|
+
idleTimeoutMs: z.ZodDefault<z.ZodNumber>;
|
|
507
|
+
maxInFlight: z.ZodDefault<z.ZodNumber>;
|
|
508
|
+
receiptsDir: z.ZodString;
|
|
509
|
+
logFile: z.ZodOptional<z.ZodString>;
|
|
510
|
+
log: z.ZodOptional<z.ZodObject<{
|
|
511
|
+
url: z.ZodString;
|
|
512
|
+
tokenEnv: z.ZodOptional<z.ZodString>;
|
|
513
|
+
hashOnly: z.ZodOptional<z.ZodBoolean>;
|
|
514
|
+
timeoutMs: z.ZodOptional<z.ZodNumber>;
|
|
515
|
+
keys: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
516
|
+
logId: z.ZodOptional<z.ZodString>;
|
|
517
|
+
}, z.core.$strip>>;
|
|
518
|
+
otel: z.ZodOptional<z.ZodObject<{
|
|
519
|
+
url: z.ZodString;
|
|
520
|
+
headersEnv: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
521
|
+
serviceName: z.ZodOptional<z.ZodString>;
|
|
522
|
+
}, z.core.$strip>>;
|
|
523
|
+
splunk: z.ZodOptional<z.ZodObject<{
|
|
524
|
+
url: z.ZodString;
|
|
525
|
+
tokenEnv: z.ZodString;
|
|
526
|
+
index: z.ZodOptional<z.ZodString>;
|
|
527
|
+
source: z.ZodOptional<z.ZodString>;
|
|
528
|
+
sourcetype: z.ZodOptional<z.ZodString>;
|
|
529
|
+
host: z.ZodOptional<z.ZodString>;
|
|
530
|
+
}, z.core.$strip>>;
|
|
531
|
+
}, z.core.$strip>;
|
|
532
|
+
export type EgressConfig = z.infer<typeof EgressConfigSchema>;
|
|
533
|
+
export declare function loadEgressConfig(path: string): EgressConfig;
|
|
486
534
|
export declare function loadDoorConfig(path: string): DoorConfig;
|
|
535
|
+
/**
|
|
536
|
+
* An ACS Guardian: answers OWASP Agent Control Standard `steps/toolCallRequest` and `steps/toolCallResult` with the
|
|
537
|
+
* Cedar policy's decision, and receipts each decision. The agent ids on the wire are claimed: nothing checks them.
|
|
538
|
+
*/
|
|
539
|
+
export declare const AcsGuardianConfigSchema: z.ZodObject<{
|
|
540
|
+
identity: z.ZodObject<{
|
|
541
|
+
keyFile: z.ZodString;
|
|
542
|
+
}, z.core.$strip>;
|
|
543
|
+
policyFile: z.ZodString;
|
|
544
|
+
receiptsDir: z.ZodString;
|
|
545
|
+
logFile: z.ZodOptional<z.ZodString>;
|
|
546
|
+
log: z.ZodOptional<z.ZodObject<{
|
|
547
|
+
url: z.ZodString;
|
|
548
|
+
tokenEnv: z.ZodOptional<z.ZodString>;
|
|
549
|
+
hashOnly: z.ZodOptional<z.ZodBoolean>;
|
|
550
|
+
timeoutMs: z.ZodOptional<z.ZodNumber>;
|
|
551
|
+
keys: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
552
|
+
logId: z.ZodOptional<z.ZodString>;
|
|
553
|
+
}, z.core.$strip>>;
|
|
554
|
+
}, z.core.$strip>;
|
|
555
|
+
export type AcsGuardianConfig = z.infer<typeof AcsGuardianConfigSchema>;
|
|
556
|
+
export declare function loadAcsGuardianConfig(path: string): AcsGuardianConfig;
|
|
487
557
|
export declare const SdkConfigSchema: z.ZodObject<{
|
|
488
558
|
agentId: z.ZodString;
|
|
489
559
|
principalId: z.ZodOptional<z.ZodString>;
|
package/dist/config.js
CHANGED
|
@@ -289,6 +289,44 @@ export const DoorConfigSchema = z.object({
|
|
|
289
289
|
captureKeyEnv: z.string().min(1).optional(),
|
|
290
290
|
}).refine(hasOneLog, oneLog).refine((c) => new Set(c.routes.map((r) => r.name)).size === c.routes.length, { message: "route names must be unique" })
|
|
291
291
|
.refine((c) => !c.log || !!c.log.keys?.length, { message: "a remote log needs log.keys: the log's public key files, pinned, to check its answer before a request is relayed" });
|
|
292
|
+
/**
|
|
293
|
+
* The egress witness: a forward proxy in the agent's network path for every connection that does not go to the gateway.
|
|
294
|
+
* Each agent proves who it is with its own proxy credential (`Proxy-Authorization: Basic`, id and secret); the secret
|
|
295
|
+
* comes from the environment, never from the file. The Cedar policy decides per destination, and every connection,
|
|
296
|
+
* relayed or refused, is receipted. It records where traffic went, never what was sent: there is no TLS interception.
|
|
297
|
+
*/
|
|
298
|
+
export const EgressConfigSchema = z.object({
|
|
299
|
+
identity: z.object({ keyFile: z.string() }),
|
|
300
|
+
/** the agents the witness knows, each with the environment variable that holds its proxy secret */
|
|
301
|
+
agents: z.array(z.object({ id: z.string().min(1).max(200).regex(/^[^:\s]+$/, "an agent id has no colon or whitespace: Basic credentials split at the first colon"), secretEnv: z.string().min(1) })).min(1),
|
|
302
|
+
policyFile: z.string(),
|
|
303
|
+
/** "enforce" (default) refuses what the policy denies; "observe" relays every connection from a known agent and records the decision with `enforced: false` */
|
|
304
|
+
mode: z.enum(["enforce", "observe"]).optional(),
|
|
305
|
+
/** how long the witness waits for the destination to accept the connection. Default 10 s */
|
|
306
|
+
connectTimeoutMs: z.number().int().positive().default(10_000),
|
|
307
|
+
/** a relayed connection with no bytes either way for this long is closed. Default 5 minutes */
|
|
308
|
+
idleTimeoutMs: z.number().int().positive().default(300_000),
|
|
309
|
+
/** connections handled at once; past this the next is answered 503 and is not relayed or receipted. Default 256 */
|
|
310
|
+
maxInFlight: z.number().int().positive().default(256),
|
|
311
|
+
receiptsDir: z.string(),
|
|
312
|
+
logFile: z.string().optional(),
|
|
313
|
+
log: LogSchema.optional(),
|
|
314
|
+
otel: OtelSchema.optional(),
|
|
315
|
+
splunk: SplunkSchema.optional(),
|
|
316
|
+
}).refine(hasOneLog, oneLog).refine((c) => new Set(c.agents.map((a) => a.id)).size === c.agents.length, { message: "agent ids must be unique" });
|
|
317
|
+
export function loadEgressConfig(path) {
|
|
318
|
+
const cfg = EgressConfigSchema.parse(JSON.parse(readFileSync(path, "utf8")));
|
|
319
|
+
const base = dirname(resolve(path));
|
|
320
|
+
const r = (p) => resolve(base, p);
|
|
321
|
+
return {
|
|
322
|
+
...cfg,
|
|
323
|
+
identity: { keyFile: r(cfg.identity.keyFile) },
|
|
324
|
+
policyFile: r(cfg.policyFile),
|
|
325
|
+
receiptsDir: r(cfg.receiptsDir),
|
|
326
|
+
...(cfg.logFile ? { logFile: r(cfg.logFile) } : {}),
|
|
327
|
+
...(cfg.log?.keys ? { log: { ...cfg.log, keys: cfg.log.keys.map(r) } } : {}),
|
|
328
|
+
};
|
|
329
|
+
}
|
|
292
330
|
export function loadDoorConfig(path) {
|
|
293
331
|
const cfg = DoorConfigSchema.parse(JSON.parse(readFileSync(path, "utf8")));
|
|
294
332
|
const base = dirname(resolve(path));
|
|
@@ -303,6 +341,30 @@ export function loadDoorConfig(path) {
|
|
|
303
341
|
...(cfg.log?.keys ? { log: { ...cfg.log, keys: cfg.log.keys.map(r) } } : {}),
|
|
304
342
|
};
|
|
305
343
|
}
|
|
344
|
+
/**
|
|
345
|
+
* An ACS Guardian: answers OWASP Agent Control Standard `steps/toolCallRequest` and `steps/toolCallResult` with the
|
|
346
|
+
* Cedar policy's decision, and receipts each decision. The agent ids on the wire are claimed: nothing checks them.
|
|
347
|
+
*/
|
|
348
|
+
export const AcsGuardianConfigSchema = z.object({
|
|
349
|
+
identity: z.object({ keyFile: z.string() }),
|
|
350
|
+
policyFile: z.string(),
|
|
351
|
+
receiptsDir: z.string(),
|
|
352
|
+
logFile: z.string().optional(),
|
|
353
|
+
log: LogSchema.optional(),
|
|
354
|
+
}).refine(hasOneLog, oneLog);
|
|
355
|
+
export function loadAcsGuardianConfig(path) {
|
|
356
|
+
const cfg = AcsGuardianConfigSchema.parse(JSON.parse(readFileSync(path, "utf8")));
|
|
357
|
+
const base = dirname(resolve(path));
|
|
358
|
+
const r = (p) => resolve(base, p);
|
|
359
|
+
return {
|
|
360
|
+
...cfg,
|
|
361
|
+
identity: { keyFile: r(cfg.identity.keyFile) },
|
|
362
|
+
policyFile: r(cfg.policyFile),
|
|
363
|
+
receiptsDir: r(cfg.receiptsDir),
|
|
364
|
+
...(cfg.logFile ? { logFile: r(cfg.logFile) } : {}),
|
|
365
|
+
...(cfg.log?.keys ? { log: { ...cfg.log, keys: cfg.log.keys.map(r) } } : {}),
|
|
366
|
+
};
|
|
367
|
+
}
|
|
306
368
|
export const SdkConfigSchema = z.object({
|
|
307
369
|
/** identity the receipts will name; nothing checks it, so it is recorded as claimed */
|
|
308
370
|
agentId: z.string().min(1),
|
|
@@ -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>;
|