hmcp-web 0.0.1 → 0.1.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/LICENSE +77 -0
- package/README.md +48 -2
- package/THIRD-PARTY-NOTICES.md +110 -0
- package/bin/hmcp-web.js +20 -0
- package/dist/api.d.ts +11 -0
- package/dist/api.js +1404 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +40 -0
- package/dist/contracts/onboarding.d.ts +233 -0
- package/dist/contracts/onboarding.js +1 -0
- package/dist/http.d.ts +72 -0
- package/dist/http.js +319 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +7 -0
- package/dist/model/console-settings.d.ts +37 -0
- package/dist/model/console-settings.js +74 -0
- package/dist/model/doctor.d.ts +68 -0
- package/dist/model/doctor.js +254 -0
- package/dist/model/fs-browse.d.ts +9 -0
- package/dist/model/fs-browse.js +184 -0
- package/dist/model/gateway-config.d.ts +287 -0
- package/dist/model/gateway-config.js +760 -0
- package/dist/model/metrics.d.ts +65 -0
- package/dist/model/metrics.js +198 -0
- package/dist/model/oauth-flows.d.ts +60 -0
- package/dist/model/oauth-flows.js +79 -0
- package/dist/model/pipeline.d.ts +58 -0
- package/dist/model/pipeline.js +84 -0
- package/dist/model/probe.d.ts +155 -0
- package/dist/model/probe.js +385 -0
- package/dist/model/process.d.ts +100 -0
- package/dist/model/process.js +278 -0
- package/dist/model/protection.d.ts +378 -0
- package/dist/model/protection.js +470 -0
- package/dist/model/registry-cache.d.ts +59 -0
- package/dist/model/registry-cache.js +238 -0
- package/dist/model/scan.d.ts +48 -0
- package/dist/model/scan.js +59 -0
- package/dist/model/server.d.ts +127 -0
- package/dist/model/server.js +243 -0
- package/dist/model/tools.d.ts +61 -0
- package/dist/model/tools.js +72 -0
- package/dist/routes/catalog.d.ts +8 -0
- package/dist/routes/catalog.js +47 -0
- package/dist/routes/connect.d.ts +14 -0
- package/dist/routes/connect.js +633 -0
- package/dist/routes/oauth.d.ts +55 -0
- package/dist/routes/oauth.js +406 -0
- package/dist/routes/policy.d.ts +34 -0
- package/dist/routes/policy.js +513 -0
- package/dist/routes/runtime.d.ts +6 -0
- package/dist/routes/runtime.js +921 -0
- package/dist/routes/servers-add.d.ts +24 -0
- package/dist/routes/servers-add.js +1091 -0
- package/dist/routes/system.d.ts +4 -0
- package/dist/routes/system.js +13 -0
- package/dist/start.d.ts +56 -0
- package/dist/start.js +164 -0
- package/dist-ui/assets/Activity-BqKGpXMN.js +1 -0
- package/dist-ui/assets/Approvals-Qo7bCcli.js +1 -0
- package/dist-ui/assets/AttachApp-0zRSPgSY.js +5 -0
- package/dist-ui/assets/Connections-CE0JGe8c.js +2 -0
- package/dist-ui/assets/Gallery-B39L2Yma.js +17 -0
- package/dist-ui/assets/PreApprovals-BsIhjMqb.js +1 -0
- package/dist-ui/assets/Protection-DlB09ABa.js +1 -0
- package/dist-ui/assets/Safety-CN-oHO2h.js +1 -0
- package/dist-ui/assets/Settings-BLD7G1Ag.js +1 -0
- package/dist-ui/assets/StartPage-HuZSnmSr.js +1 -0
- package/dist-ui/assets/Suggestions-Dp7pofcs.js +1 -0
- package/dist-ui/assets/Tools-Bfot3bx5.js +4 -0
- package/dist-ui/assets/index-Khp0l1zp.js +16 -0
- package/dist-ui/assets/index-Ylyt4DEr.css +2 -0
- package/dist-ui/assets/kit-DQ7Oxf5n.js +4 -0
- package/dist-ui/brand/logo-256.png +0 -0
- package/dist-ui/brand/logo-512.png +0 -0
- package/dist-ui/brand/logo.svg +6 -0
- package/dist-ui/brand/mark.svg +5 -0
- package/dist-ui/fonts/OFL.txt +99 -0
- package/dist-ui/fonts/README.md +21 -0
- package/dist-ui/fonts/jetbrainsmono-latin.woff2 +0 -0
- package/dist-ui/fonts/manrope-latin.woff2 +0 -0
- package/dist-ui/index.html +51 -0
- package/package.json +69 -6
- package/bin/notice.js +0 -7
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { type DecisionKind } from "hmcp-core";
|
|
2
|
+
import type { LoadedServer } from "./server.js";
|
|
3
|
+
export type Window = "1h" | "24h" | "7d" | "30d" | "all";
|
|
4
|
+
export declare function isWindow(value: string): value is Window;
|
|
5
|
+
export interface Bucket {
|
|
6
|
+
/** Start of the bucket, epoch ms. */
|
|
7
|
+
readonly at: number;
|
|
8
|
+
readonly allow: number;
|
|
9
|
+
readonly approve: number;
|
|
10
|
+
readonly deny: number;
|
|
11
|
+
}
|
|
12
|
+
export interface ToolCount {
|
|
13
|
+
readonly tool: string;
|
|
14
|
+
readonly count: number;
|
|
15
|
+
}
|
|
16
|
+
export interface Coverage {
|
|
17
|
+
/** Records actually read and counted. */
|
|
18
|
+
readonly records: number;
|
|
19
|
+
/** Lines the backward scan touched, which is what the read cost. */
|
|
20
|
+
readonly scanned: number;
|
|
21
|
+
/** Lines that could not be parsed. Counted, never thrown on. */
|
|
22
|
+
readonly malformed: number;
|
|
23
|
+
/**
|
|
24
|
+
* True when the read reached the start of the window — so the counts are the
|
|
25
|
+
* window, and not merely its most recent `RECORD_BUDGET` records.
|
|
26
|
+
*/
|
|
27
|
+
readonly complete: boolean;
|
|
28
|
+
readonly budget: number;
|
|
29
|
+
/** Epoch ms of the oldest and newest record counted, or null at an empty log. */
|
|
30
|
+
readonly oldest: number | null;
|
|
31
|
+
readonly newest: number | null;
|
|
32
|
+
}
|
|
33
|
+
export interface Metrics {
|
|
34
|
+
readonly window: Window;
|
|
35
|
+
readonly bucketMs: number;
|
|
36
|
+
readonly buckets: readonly Bucket[];
|
|
37
|
+
readonly decisions: Readonly<Record<DecisionKind, number>>;
|
|
38
|
+
/** Calls asking for a tool the toolbox doesn't have. Not in `decisions`: no rule refused them. */
|
|
39
|
+
readonly unknownTool: number;
|
|
40
|
+
readonly outcomes: Readonly<Record<string, number>>;
|
|
41
|
+
readonly effects: Readonly<Record<string, number>>;
|
|
42
|
+
/** Most-refused tools first. Refusals only — the question a reviewer asks. */
|
|
43
|
+
readonly refusedTools: readonly ToolCount[];
|
|
44
|
+
readonly busiestTools: readonly ToolCount[];
|
|
45
|
+
/** Milliseconds; null where too few records carried a duration to say. */
|
|
46
|
+
readonly duration: {
|
|
47
|
+
readonly p50: number;
|
|
48
|
+
readonly p95: number;
|
|
49
|
+
readonly samples: number;
|
|
50
|
+
} | null;
|
|
51
|
+
/** Ages in ms of what is waiting for a human right now, oldest first. */
|
|
52
|
+
readonly queue: readonly {
|
|
53
|
+
readonly id: string;
|
|
54
|
+
readonly tool: string;
|
|
55
|
+
readonly ageMs: number;
|
|
56
|
+
readonly expiresAt: number;
|
|
57
|
+
}[];
|
|
58
|
+
readonly coverage: Coverage;
|
|
59
|
+
readonly auditPath: string;
|
|
60
|
+
/** False when `audit.enabled` is off, in which case every count above is a zero that means nothing. */
|
|
61
|
+
readonly auditing: boolean;
|
|
62
|
+
}
|
|
63
|
+
export declare function buildMetrics(server: LoadedServer, tenant: string | null, window: Window,
|
|
64
|
+
/** Other toolboxes writing to the same log: their calls are not this one's (UT5 round 4). */
|
|
65
|
+
otherComponents?: readonly string[]): Metrics;
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { queryAuditLog } from "hmcp-core";
|
|
2
|
+
import { scopedFor, storeFor } from "./server.js";
|
|
3
|
+
/**
|
|
4
|
+
* Aggregates for the console's overview, read out of the audit log.
|
|
5
|
+
*
|
|
6
|
+
* Every number here is a count over a *bounded* read of an append-only file,
|
|
7
|
+
* and that bound is the whole problem. `queryAuditLog` caps one call at 1000
|
|
8
|
+
* records, so an aggregate has to page backwards itself, and a log longer than
|
|
9
|
+
* the budget means the answer describes a suffix of history rather than all of
|
|
10
|
+
* it. A chart that cannot say which of the two it is drawn from is worse than
|
|
11
|
+
* no chart: it reads as a total and is a sample.
|
|
12
|
+
*
|
|
13
|
+
* So the bound is part of the payload. `complete` says whether the window was
|
|
14
|
+
* read to its end, `scanned` and `records` say what it cost and covered, and
|
|
15
|
+
* `oldest`/`newest` say which stretch of time the counts actually describe. The
|
|
16
|
+
* UI is expected to render that, not to drop it.
|
|
17
|
+
*/
|
|
18
|
+
/** How many records one metrics request will read before it stops. */
|
|
19
|
+
const RECORD_BUDGET = 5000;
|
|
20
|
+
/** One `queryAuditLog` call's own hard cap, mirrored here so paging matches it. */
|
|
21
|
+
const PAGE = 1000;
|
|
22
|
+
const WINDOW_MS = {
|
|
23
|
+
"1h": 3_600_000,
|
|
24
|
+
"24h": 86_400_000,
|
|
25
|
+
"7d": 604_800_000,
|
|
26
|
+
"30d": 2_592_000_000
|
|
27
|
+
};
|
|
28
|
+
export function isWindow(value) {
|
|
29
|
+
return value === "all" || value in WINDOW_MS;
|
|
30
|
+
}
|
|
31
|
+
/** The rule id the gateway records a call to a tool it doesn't have under. */
|
|
32
|
+
const UNKNOWN_TOOL_RULE = "gateway.unknown-tool";
|
|
33
|
+
/** One bucket per ~40 across the window, rounded to something a person reads. */
|
|
34
|
+
function bucketFor(window, spanMs) {
|
|
35
|
+
if (window === "1h")
|
|
36
|
+
return 60_000 * 2;
|
|
37
|
+
if (window === "24h")
|
|
38
|
+
return 3_600_000;
|
|
39
|
+
if (window === "7d")
|
|
40
|
+
return 3_600_000 * 6;
|
|
41
|
+
if (window === "30d")
|
|
42
|
+
return 86_400_000;
|
|
43
|
+
const target = Math.max(spanMs / 40, 60_000);
|
|
44
|
+
const steps = [60_000, 300_000, 3_600_000, 21_600_000, 86_400_000, 604_800_000];
|
|
45
|
+
return steps.find((s) => s >= target) ?? 604_800_000;
|
|
46
|
+
}
|
|
47
|
+
function percentile(sorted, p) {
|
|
48
|
+
if (sorted.length === 0)
|
|
49
|
+
return 0;
|
|
50
|
+
const idx = Math.min(sorted.length - 1, Math.max(0, Math.ceil((p / 100) * sorted.length) - 1));
|
|
51
|
+
return sorted[idx];
|
|
52
|
+
}
|
|
53
|
+
function topN(counts, n) {
|
|
54
|
+
return [...counts.entries()]
|
|
55
|
+
.sort((a, b) => (b[1] === a[1] ? a[0].localeCompare(b[0]) : b[1] - a[1]))
|
|
56
|
+
.slice(0, n)
|
|
57
|
+
.map(([tool, count]) => ({ tool, count }));
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Reads backwards from the end of the log until the window is covered or the
|
|
61
|
+
* budget is spent, whichever comes first.
|
|
62
|
+
*
|
|
63
|
+
* Paging rather than one big call because `queryAuditLog` caps a single read at
|
|
64
|
+
* 1000 records; asking for more silently gets 1000 and would make `complete`
|
|
65
|
+
* a lie.
|
|
66
|
+
*/
|
|
67
|
+
function collect(path, tenant, since, exclude) {
|
|
68
|
+
const records = [];
|
|
69
|
+
let scanned = 0;
|
|
70
|
+
let malformed = 0;
|
|
71
|
+
let cursor;
|
|
72
|
+
let complete = false;
|
|
73
|
+
while (records.length < RECORD_BUDGET) {
|
|
74
|
+
const page = queryAuditLog(path, {
|
|
75
|
+
limit: Math.min(PAGE, RECORD_BUDGET - records.length),
|
|
76
|
+
...(cursor !== undefined ? { beforeSeq: cursor } : {}),
|
|
77
|
+
...(since !== undefined ? { since } : {}),
|
|
78
|
+
...(exclude.length > 0 ? { excludeComponent: exclude } : {}),
|
|
79
|
+
// A null tenant means "every tenant" — the cross-tenant overview a
|
|
80
|
+
// single-tenant deployment wants. A named one, including the empty
|
|
81
|
+
// string, is one customer's rows and nobody else's.
|
|
82
|
+
...(tenant !== null ? { tenant: [tenant] } : {})
|
|
83
|
+
});
|
|
84
|
+
records.push(...page.records);
|
|
85
|
+
scanned += page.scanned;
|
|
86
|
+
malformed += page.malformed;
|
|
87
|
+
if (page.nextCursor === null) {
|
|
88
|
+
complete = true;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
cursor = page.nextCursor;
|
|
92
|
+
}
|
|
93
|
+
return { records, scanned, malformed, complete };
|
|
94
|
+
}
|
|
95
|
+
export function buildMetrics(server, tenant, window,
|
|
96
|
+
/** Other toolboxes writing to the same log: their calls are not this one's (UT5 round 4). */
|
|
97
|
+
otherComponents = []) {
|
|
98
|
+
const now = Date.now();
|
|
99
|
+
const since = window === "all" ? undefined : new Date(now - WINDOW_MS[window]).toISOString();
|
|
100
|
+
const auditing = server.policy.audit.enabled;
|
|
101
|
+
const read = collect(server.auditPath, tenant, since, otherComponents);
|
|
102
|
+
const decisions = { allow: 0, approve: 0, deny: 0 };
|
|
103
|
+
const outcomes = {};
|
|
104
|
+
const effects = {};
|
|
105
|
+
const refused = new Map();
|
|
106
|
+
const busiest = new Map();
|
|
107
|
+
const durations = [];
|
|
108
|
+
let unknownTool = 0;
|
|
109
|
+
const bucketed = new Map();
|
|
110
|
+
let oldest = null;
|
|
111
|
+
let newest = null;
|
|
112
|
+
const spanMs = window === "all" ? now - (Date.parse(read.records.at(-1)?.ts ?? "") || now) : WINDOW_MS[window];
|
|
113
|
+
const bucketMs = bucketFor(window, spanMs);
|
|
114
|
+
for (const record of read.records) {
|
|
115
|
+
// Only what an AI app asked for. The console writes its own actions to the
|
|
116
|
+
// same log -- turning protection on, adding a server -- under component
|
|
117
|
+
// "hmcp-web" or a "(policy)"/"(gateway)" tool, and counting those made
|
|
118
|
+
// Home say "Allowed 7, Blocked 1" before any AI had run.
|
|
119
|
+
if (record.component === "hmcp-web" || /^\(.*\)$/.test(record.tool ?? ""))
|
|
120
|
+
continue;
|
|
121
|
+
const at = Date.parse(record.ts);
|
|
122
|
+
if (!Number.isFinite(at))
|
|
123
|
+
continue;
|
|
124
|
+
if (oldest === null || at < oldest)
|
|
125
|
+
oldest = at;
|
|
126
|
+
if (newest === null || at > newest)
|
|
127
|
+
newest = at;
|
|
128
|
+
/*
|
|
129
|
+
* A call to a tool the toolbox doesn't have was refused because there is
|
|
130
|
+
* nothing to run, not by a rule: counted on its own, so "Blocked" (and,
|
|
131
|
+
* in watch-only mode, "would have been blocked") means what protection
|
|
132
|
+
* did (UT5 round 4).
|
|
133
|
+
*/
|
|
134
|
+
if (record.rule_id === UNKNOWN_TOOL_RULE) {
|
|
135
|
+
unknownTool++;
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const decision = record.decision;
|
|
139
|
+
if (decision === "allow" || decision === "approve" || decision === "deny")
|
|
140
|
+
decisions[decision]++;
|
|
141
|
+
outcomes[record.outcome] = (outcomes[record.outcome] ?? 0) + 1;
|
|
142
|
+
// An unclassified call is its own fact, not a missing one — the whole
|
|
143
|
+
// reason `on_unclassified: deny` exists — so it is counted under its own
|
|
144
|
+
// key rather than dropped.
|
|
145
|
+
effects[record.effect ?? "unclassified"] = (effects[record.effect ?? "unclassified"] ?? 0) + 1;
|
|
146
|
+
if (record.tool) {
|
|
147
|
+
busiest.set(record.tool, (busiest.get(record.tool) ?? 0) + 1);
|
|
148
|
+
if (decision === "deny")
|
|
149
|
+
refused.set(record.tool, (refused.get(record.tool) ?? 0) + 1);
|
|
150
|
+
}
|
|
151
|
+
if (typeof record.duration_ms === "number" && Number.isFinite(record.duration_ms)) {
|
|
152
|
+
durations.push(record.duration_ms);
|
|
153
|
+
}
|
|
154
|
+
const slot = Math.floor(at / bucketMs) * bucketMs;
|
|
155
|
+
const cell = bucketed.get(slot) ?? { allow: 0, approve: 0, deny: 0 };
|
|
156
|
+
if (decision === "allow" || decision === "approve" || decision === "deny")
|
|
157
|
+
cell[decision]++;
|
|
158
|
+
bucketed.set(slot, cell);
|
|
159
|
+
}
|
|
160
|
+
durations.sort((a, b) => a - b);
|
|
161
|
+
// `null` tenant means every tenant, so the queue has to come from the
|
|
162
|
+
// unscoped list — a scoped read with the empty tenant would silently show
|
|
163
|
+
// one tenant's queue under a heading that claims to cover all of them.
|
|
164
|
+
const pending = tenant === null ? storeFor(server).listPendingAll(50) : scopedFor(server, tenant).listPending(50);
|
|
165
|
+
const queue = pending
|
|
166
|
+
.map((row) => ({ id: row.id, tool: row.tool, ageMs: now - row.created_at, expiresAt: row.expires_at }))
|
|
167
|
+
.sort((a, b) => b.ageMs - a.ageMs);
|
|
168
|
+
return {
|
|
169
|
+
window,
|
|
170
|
+
bucketMs,
|
|
171
|
+
buckets: [...bucketed.entries()]
|
|
172
|
+
.sort((a, b) => a[0] - b[0])
|
|
173
|
+
.map(([at, cell]) => ({ at, ...cell })),
|
|
174
|
+
decisions,
|
|
175
|
+
unknownTool,
|
|
176
|
+
outcomes,
|
|
177
|
+
effects,
|
|
178
|
+
refusedTools: topN(refused, 8),
|
|
179
|
+
busiestTools: topN(busiest, 8),
|
|
180
|
+
// Fewer than five samples is not a percentile, it is two numbers with a
|
|
181
|
+
// label on them. Null so the UI shows nothing rather than something false.
|
|
182
|
+
duration: durations.length >= 5
|
|
183
|
+
? { p50: percentile(durations, 50), p95: percentile(durations, 95), samples: durations.length }
|
|
184
|
+
: null,
|
|
185
|
+
queue,
|
|
186
|
+
coverage: {
|
|
187
|
+
records: read.records.length,
|
|
188
|
+
scanned: read.scanned,
|
|
189
|
+
malformed: read.malformed,
|
|
190
|
+
complete: read.complete,
|
|
191
|
+
budget: RECORD_BUDGET,
|
|
192
|
+
oldest,
|
|
193
|
+
newest
|
|
194
|
+
},
|
|
195
|
+
auditPath: server.auditPath,
|
|
196
|
+
auditing
|
|
197
|
+
};
|
|
198
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { OAuthClientRegistration } from "hmcp-core";
|
|
2
|
+
import { type Discovered } from "hmcp-gateway";
|
|
3
|
+
/**
|
|
4
|
+
* Sign-ins in progress, held in this console process's memory only.
|
|
5
|
+
*
|
|
6
|
+
* A flow carries the PKCE verifier, which together with the code is enough to
|
|
7
|
+
* get a token, so it is never written to disk and never sent to the browser.
|
|
8
|
+
* The browser holds only the `state`: 32 random bytes that name the flow.
|
|
9
|
+
*
|
|
10
|
+
* Single use: the first `/complete` for a state takes the verifier out and
|
|
11
|
+
* nothing can complete it again. A flow that is not finished within ten
|
|
12
|
+
* minutes is expired, then forgotten.
|
|
13
|
+
*/
|
|
14
|
+
export declare const FLOW_TTL_MS: number;
|
|
15
|
+
export type FlowStatus = "waiting" | "completing" | "done" | "failed" | "expired";
|
|
16
|
+
export interface OAuthFlow {
|
|
17
|
+
readonly state: string;
|
|
18
|
+
readonly secretName: string;
|
|
19
|
+
readonly label?: string;
|
|
20
|
+
codeVerifier: string;
|
|
21
|
+
readonly redirectUri: string;
|
|
22
|
+
readonly discovered: Discovered;
|
|
23
|
+
readonly client: OAuthClientRegistration;
|
|
24
|
+
readonly serverUrl: string;
|
|
25
|
+
/** Plain name of the provider, for messages: "Notion". */
|
|
26
|
+
readonly provider: string;
|
|
27
|
+
status: FlowStatus;
|
|
28
|
+
problem?: string;
|
|
29
|
+
readonly expiresAt: number;
|
|
30
|
+
}
|
|
31
|
+
export type FlowInit = Omit<OAuthFlow, "state" | "status" | "expiresAt" | "problem">;
|
|
32
|
+
export type TakeResult = {
|
|
33
|
+
ok: true;
|
|
34
|
+
flow: OAuthFlow;
|
|
35
|
+
} | {
|
|
36
|
+
ok: false;
|
|
37
|
+
why: "unknown" | "expired" | "used";
|
|
38
|
+
flow?: OAuthFlow;
|
|
39
|
+
};
|
|
40
|
+
export declare class OAuthFlows {
|
|
41
|
+
private readonly opts;
|
|
42
|
+
private readonly flows;
|
|
43
|
+
constructor(opts?: {
|
|
44
|
+
ttlMs?: number;
|
|
45
|
+
now?: () => number;
|
|
46
|
+
});
|
|
47
|
+
private now;
|
|
48
|
+
create(init: FlowInit): OAuthFlow;
|
|
49
|
+
/** For status polling. A waiting flow past its time reads as expired. */
|
|
50
|
+
get(state: string): OAuthFlow | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Claims a waiting flow for completion. After this the flow cannot be
|
|
53
|
+
* claimed again, whatever the outcome.
|
|
54
|
+
*/
|
|
55
|
+
take(state: string): TakeResult;
|
|
56
|
+
finish(flow: OAuthFlow, problem?: string): void;
|
|
57
|
+
/** Drops flows long past their expiry; an expired flow loses its verifier at once. */
|
|
58
|
+
sweep(): void;
|
|
59
|
+
get size(): number;
|
|
60
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { newState } from "hmcp-gateway";
|
|
2
|
+
/**
|
|
3
|
+
* Sign-ins in progress, held in this console process's memory only.
|
|
4
|
+
*
|
|
5
|
+
* A flow carries the PKCE verifier, which together with the code is enough to
|
|
6
|
+
* get a token, so it is never written to disk and never sent to the browser.
|
|
7
|
+
* The browser holds only the `state`: 32 random bytes that name the flow.
|
|
8
|
+
*
|
|
9
|
+
* Single use: the first `/complete` for a state takes the verifier out and
|
|
10
|
+
* nothing can complete it again. A flow that is not finished within ten
|
|
11
|
+
* minutes is expired, then forgotten.
|
|
12
|
+
*/
|
|
13
|
+
export const FLOW_TTL_MS = 10 * 60 * 1000;
|
|
14
|
+
/** How long a finished or expired flow can still be asked about. */
|
|
15
|
+
const KEEP_AFTER_MS = 10 * 60 * 1000;
|
|
16
|
+
export class OAuthFlows {
|
|
17
|
+
opts;
|
|
18
|
+
flows = new Map();
|
|
19
|
+
constructor(opts = {}) {
|
|
20
|
+
this.opts = opts;
|
|
21
|
+
}
|
|
22
|
+
now() {
|
|
23
|
+
return (this.opts.now ?? Date.now)();
|
|
24
|
+
}
|
|
25
|
+
create(init) {
|
|
26
|
+
this.sweep();
|
|
27
|
+
const flow = { ...init, state: newState(), status: "waiting", expiresAt: this.now() + (this.opts.ttlMs ?? FLOW_TTL_MS) };
|
|
28
|
+
this.flows.set(flow.state, flow);
|
|
29
|
+
return flow;
|
|
30
|
+
}
|
|
31
|
+
/** For status polling. A waiting flow past its time reads as expired. */
|
|
32
|
+
get(state) {
|
|
33
|
+
this.sweep();
|
|
34
|
+
return this.flows.get(state);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Claims a waiting flow for completion. After this the flow cannot be
|
|
38
|
+
* claimed again, whatever the outcome.
|
|
39
|
+
*/
|
|
40
|
+
take(state) {
|
|
41
|
+
this.sweep();
|
|
42
|
+
const flow = this.flows.get(state);
|
|
43
|
+
if (!flow)
|
|
44
|
+
return { ok: false, why: "unknown" };
|
|
45
|
+
if (flow.status === "expired")
|
|
46
|
+
return { ok: false, why: "expired", flow };
|
|
47
|
+
if (flow.status !== "waiting")
|
|
48
|
+
return { ok: false, why: "used", flow };
|
|
49
|
+
if (this.now() > flow.expiresAt) {
|
|
50
|
+
flow.codeVerifier = "";
|
|
51
|
+
flow.status = "expired";
|
|
52
|
+
return { ok: false, why: "expired", flow };
|
|
53
|
+
}
|
|
54
|
+
flow.status = "completing";
|
|
55
|
+
return { ok: true, flow };
|
|
56
|
+
}
|
|
57
|
+
finish(flow, problem) {
|
|
58
|
+
flow.codeVerifier = "";
|
|
59
|
+
flow.status = problem ? "failed" : "done";
|
|
60
|
+
if (problem)
|
|
61
|
+
flow.problem = problem;
|
|
62
|
+
}
|
|
63
|
+
/** Drops flows long past their expiry; an expired flow loses its verifier at once. */
|
|
64
|
+
sweep() {
|
|
65
|
+
const now = this.now();
|
|
66
|
+
for (const [state, flow] of this.flows) {
|
|
67
|
+
if (now > flow.expiresAt) {
|
|
68
|
+
flow.codeVerifier = "";
|
|
69
|
+
if (flow.status === "waiting")
|
|
70
|
+
flow.status = "expired";
|
|
71
|
+
}
|
|
72
|
+
if (now > flow.expiresAt + KEEP_AFTER_MS)
|
|
73
|
+
this.flows.delete(state);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
get size() {
|
|
77
|
+
return this.flows.size;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The enforcement pipeline, in the order it actually runs.
|
|
3
|
+
*
|
|
4
|
+
* This is the one part of the protection view that is written ahead of time
|
|
5
|
+
* rather than derived, because it describes the runtime's code and not the
|
|
6
|
+
* user's configuration. Each step carries the file and lines it corresponds to
|
|
7
|
+
* so it can be checked against the source.
|
|
8
|
+
*
|
|
9
|
+
* KEEP IN SYNC with `packages/runtime/src/enforce.ts`, `request.ts`, and
|
|
10
|
+
* `packages/core/src/egress.ts`. If you change the order enforcement happens
|
|
11
|
+
* in, change it here in the same commit.
|
|
12
|
+
*/
|
|
13
|
+
export declare const ENFORCEMENT_PIPELINE: readonly [{
|
|
14
|
+
readonly step: 1;
|
|
15
|
+
readonly title: "Tenant resolved once, at startup";
|
|
16
|
+
readonly detail: string;
|
|
17
|
+
readonly source: "runtime/src/server.ts:78";
|
|
18
|
+
}, {
|
|
19
|
+
readonly step: 2;
|
|
20
|
+
readonly title: "Arguments validated against a closed schema";
|
|
21
|
+
readonly detail: string;
|
|
22
|
+
readonly source: "runtime/src/server.ts:131-137";
|
|
23
|
+
}, {
|
|
24
|
+
readonly step: 3;
|
|
25
|
+
readonly title: "Pagination ceiling applied before anything is recorded";
|
|
26
|
+
readonly detail: string;
|
|
27
|
+
readonly source: "runtime/src/request.ts:164-174";
|
|
28
|
+
}, {
|
|
29
|
+
readonly step: 4;
|
|
30
|
+
readonly title: "Policy decides, and the decision is pure";
|
|
31
|
+
readonly detail: string;
|
|
32
|
+
readonly source: "core/src/decide.ts:133";
|
|
33
|
+
}, {
|
|
34
|
+
readonly step: 5;
|
|
35
|
+
readonly title: "A refusal never reaches the upstream";
|
|
36
|
+
readonly detail: string;
|
|
37
|
+
readonly source: "runtime/src/enforce.ts:88-103";
|
|
38
|
+
}, {
|
|
39
|
+
readonly step: 6;
|
|
40
|
+
readonly title: "Approvals are bound to the exact arguments";
|
|
41
|
+
readonly detail: string;
|
|
42
|
+
readonly source: "core/src/approvals.ts:256";
|
|
43
|
+
}, {
|
|
44
|
+
readonly step: 7;
|
|
45
|
+
readonly title: "Tenant injected last, after the agent's arguments are placed";
|
|
46
|
+
readonly detail: string;
|
|
47
|
+
readonly source: "runtime/src/request.ts:63-90";
|
|
48
|
+
}, {
|
|
49
|
+
readonly step: 8;
|
|
50
|
+
readonly title: "Egress checked, then the resolved addresses pinned";
|
|
51
|
+
readonly detail: string;
|
|
52
|
+
readonly source: "core/src/egress.ts:84-140";
|
|
53
|
+
}, {
|
|
54
|
+
readonly step: 9;
|
|
55
|
+
readonly title: "The outcome is recorded either way";
|
|
56
|
+
readonly detail: string;
|
|
57
|
+
readonly source: "runtime/src/enforce.ts:137-162";
|
|
58
|
+
}];
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The enforcement pipeline, in the order it actually runs.
|
|
3
|
+
*
|
|
4
|
+
* This is the one part of the protection view that is written ahead of time
|
|
5
|
+
* rather than derived, because it describes the runtime's code and not the
|
|
6
|
+
* user's configuration. Each step carries the file and lines it corresponds to
|
|
7
|
+
* so it can be checked against the source.
|
|
8
|
+
*
|
|
9
|
+
* KEEP IN SYNC with `packages/runtime/src/enforce.ts`, `request.ts`, and
|
|
10
|
+
* `packages/core/src/egress.ts`. If you change the order enforcement happens
|
|
11
|
+
* in, change it here in the same commit.
|
|
12
|
+
*/
|
|
13
|
+
export const ENFORCEMENT_PIPELINE = [
|
|
14
|
+
{
|
|
15
|
+
step: 1,
|
|
16
|
+
title: "Tenant resolved once, at startup",
|
|
17
|
+
detail: "The tenant value is read from the credential when the server boots, not per call. A required " +
|
|
18
|
+
"tenant that cannot be resolved stops the server from starting at all, rather than failing " +
|
|
19
|
+
"later on a call that looked fine.",
|
|
20
|
+
source: "runtime/src/server.ts:78"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
step: 2,
|
|
24
|
+
title: "Arguments validated against a closed schema",
|
|
25
|
+
detail: "The MCP SDK checks arguments against the tool's schema, which declares additionalProperties: " +
|
|
26
|
+
"false. An argument the schema does not name is rejected with an error rather than quietly " +
|
|
27
|
+
"stripped, so an attempt to smuggle a tenant id in is visible instead of silent.",
|
|
28
|
+
source: "runtime/src/server.ts:131-137"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
step: 3,
|
|
32
|
+
title: "Pagination ceiling applied before anything is recorded",
|
|
33
|
+
detail: "A missing page-size argument is filled in with the server's ceiling, so no call is unbounded " +
|
|
34
|
+
"and the arguments written to the audit log are the ones that will actually be used.",
|
|
35
|
+
source: "runtime/src/request.ts:164-174"
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
step: 4,
|
|
39
|
+
title: "Policy decides, and the decision is pure",
|
|
40
|
+
detail: "decide() runs in this order: tenant integrity, then unclassified handling, then the first " +
|
|
41
|
+
"matching rule with its argument bounds checked before its decision is honoured, then the " +
|
|
42
|
+
"posture. It is synchronous and touches no network, so enforcement can never depend on a " +
|
|
43
|
+
"service being reachable.",
|
|
44
|
+
source: "core/src/decide.ts:133"
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
step: 5,
|
|
48
|
+
title: "A refusal never reaches the upstream",
|
|
49
|
+
detail: "On a deny the call is recorded and returned as an error. The upstream request is not built " +
|
|
50
|
+
"and no credential is read, so a refused call cannot have a side effect.",
|
|
51
|
+
source: "runtime/src/enforce.ts:88-103"
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
step: 6,
|
|
55
|
+
title: "Approvals are bound to the exact arguments",
|
|
56
|
+
detail: "A grant commits to sha256(tool + canonical arguments), so an approval for one set of " +
|
|
57
|
+
"arguments cannot release another. A standing grant is checked here too, and is bounded by " +
|
|
58
|
+
"its argument constraints, its expiry and its use cap.",
|
|
59
|
+
source: "core/src/approvals.ts:256"
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
step: 7,
|
|
63
|
+
title: "Tenant injected last, after the agent's arguments are placed",
|
|
64
|
+
detail: "Arguments are written into the request first and the tenant value after, so no argument can " +
|
|
65
|
+
"overwrite it. Path placeholders are percent-encoded, so a value cannot escape its segment.",
|
|
66
|
+
source: "runtime/src/request.ts:63-90"
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
step: 8,
|
|
70
|
+
title: "Egress checked, then the resolved addresses pinned",
|
|
71
|
+
detail: "Scheme, plaintext HTTP, credentials in the URL, method, IP literals and the host allowlist are " +
|
|
72
|
+
"checked first. Then every DNS answer is validated against private, loopback and link-local " +
|
|
73
|
+
"space and the addresses are pinned for the connection, which closes the rebinding window. " +
|
|
74
|
+
"Redirects are re-validated per hop rather than followed.",
|
|
75
|
+
source: "core/src/egress.ts:84-140"
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
step: 9,
|
|
79
|
+
title: "The outcome is recorded either way",
|
|
80
|
+
detail: "Completed, denied, held for approval or errored, one hash-chained record is appended with the " +
|
|
81
|
+
"arguments redacted and hashed. An edited, removed or reordered record is detectable.",
|
|
82
|
+
source: "runtime/src/enforce.ts:137-162"
|
|
83
|
+
}
|
|
84
|
+
];
|