hmcp-web 0.2.2 → 0.2.3
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/api.d.ts +6 -0
- package/dist/api.js +36 -8
- package/dist/model/app-connect-note.d.ts +114 -0
- package/dist/model/app-connect-note.js +199 -0
- package/dist/model/app-servers.d.ts +24 -1
- package/dist/model/app-servers.js +54 -6
- package/dist/model/apps.d.ts +24 -0
- package/dist/model/apps.js +18 -0
- package/dist/model/bring-in.d.ts +185 -0
- package/dist/model/bring-in.js +755 -0
- package/dist/model/console-settings.d.ts +2 -2
- package/dist/model/import-record.d.ts +4 -0
- package/dist/model/import-record.js +22 -1
- package/dist/model/new-toolbox.d.ts +84 -0
- package/dist/model/new-toolbox.js +209 -0
- package/dist/model/protection.d.ts +4 -1
- package/dist/model/protection.js +12 -9
- package/dist/model/provisional-tools.d.ts +56 -0
- package/dist/model/provisional-tools.js +145 -0
- package/dist/model/server-names.d.ts +25 -0
- package/dist/model/server-names.js +158 -0
- package/dist/model/toolbox-label.d.ts +22 -0
- package/dist/model/toolbox-label.js +42 -0
- package/dist/model/tools.d.ts +20 -5
- package/dist/model/tools.js +22 -11
- package/dist/routes/connect-app.d.ts +54 -0
- package/dist/routes/connect-app.js +1018 -0
- package/dist/routes/connect-import.d.ts +21 -6
- package/dist/routes/connect-import.js +162 -546
- package/dist/routes/connect.d.ts +11 -0
- package/dist/routes/connect.js +28 -116
- package/dist/routes/github.js +2 -1
- package/dist/routes/guard.js +4 -0
- package/dist/routes/runtime.js +21 -22
- package/dist/routes/servers-add.d.ts +1 -0
- package/dist/routes/servers-add.js +6 -5
- package/dist-ui/assets/Activity-BSf4lBm-.js +1 -0
- package/dist-ui/assets/Approvals-CDz3zYpl.js +1 -0
- package/dist-ui/assets/AttachApp-COr1547c.js +5 -0
- package/dist-ui/assets/Connections-ClW5R6ea.js +2 -0
- package/dist-ui/assets/{Gallery-DB_OO7Vw.js → Gallery-0iiv1H9e.js} +3 -3
- package/dist-ui/assets/{PreApprovals-C-2ZGYfc.js → PreApprovals-DYDKgSpr.js} +1 -1
- package/dist-ui/assets/Protection-DMtABpM7.js +1 -0
- package/dist-ui/assets/Safety-BLTF2fqV.js +1 -0
- package/dist-ui/assets/{Settings-CSyTOe8X.js → Settings-DZ4P3Us6.js} +1 -1
- package/dist-ui/assets/StartPage-Bke6mrv5.js +1 -0
- package/dist-ui/assets/{Suggestions-CaQ_ifWR.js → Suggestions-kAWUILyw.js} +1 -1
- package/dist-ui/assets/Tools-BdXSeKeL.js +4 -0
- package/dist-ui/assets/connect-app-words-CT8Fytzv.js +1 -0
- package/dist-ui/assets/index-Cm4wG2zz.js +16 -0
- package/dist-ui/assets/{kit-DRVm92_t.js → kit-BHB_ha4i.js} +1 -1
- package/dist-ui/assets/server-name-D9u6r07i.js +1 -0
- package/dist-ui/index.html +3 -2
- package/package.json +4 -4
- package/dist-ui/assets/Activity-CoVSFaRd.js +0 -1
- package/dist-ui/assets/Approvals-BSEDgiNu.js +0 -1
- package/dist-ui/assets/AttachApp-Cpm6DOaX.js +0 -5
- package/dist-ui/assets/Connections-D5Xd9vH8.js +0 -2
- package/dist-ui/assets/Protection-B4Ltb5iP.js +0 -1
- package/dist-ui/assets/Safety-CSQhoL9D.js +0 -1
- package/dist-ui/assets/StartPage-JBVzYXce.js +0 -1
- package/dist-ui/assets/Tools-CDl6irG1.js +0 -4
- package/dist-ui/assets/index-CTvq_isK.js +0 -16
|
@@ -6,19 +6,19 @@ export declare const SettingsPatchSchema: z.ZodObject<{
|
|
|
6
6
|
onboarding: z.ZodOptional<z.ZodObject<{
|
|
7
7
|
step: z.ZodOptional<z.ZodEnum<{
|
|
8
8
|
server: "server";
|
|
9
|
+
app: "app";
|
|
9
10
|
done: "done";
|
|
10
11
|
welcome: "welcome";
|
|
11
12
|
computer: "computer";
|
|
12
|
-
app: "app";
|
|
13
13
|
try: "try";
|
|
14
14
|
protect: "protect";
|
|
15
15
|
}>>;
|
|
16
16
|
done: z.ZodOptional<z.ZodArray<z.ZodEnum<{
|
|
17
17
|
server: "server";
|
|
18
|
+
app: "app";
|
|
18
19
|
done: "done";
|
|
19
20
|
welcome: "welcome";
|
|
20
21
|
computer: "computer";
|
|
21
|
-
app: "app";
|
|
22
22
|
try: "try";
|
|
23
23
|
protect: "protect";
|
|
24
24
|
}>>>;
|
|
@@ -71,6 +71,10 @@ export declare function deleteImportRecord(registryPath: string, target: ScopeTa
|
|
|
71
71
|
* - The connection itself gone (no undo record, or one for another toolbox):
|
|
72
72
|
* the whole record goes. Undoing the connection said the servers brought
|
|
73
73
|
* in stay in the toolbox.
|
|
74
|
+
* - Except that a record naming a toolbox id the console doesn't have, or
|
|
75
|
+
* beside an undo record that does, is kept as it is: a toolbox whose id
|
|
76
|
+
* changed is being pointed at its new one (`adoptToolboxId`), and a step
|
|
77
|
+
* of that left half-done must not lose what Undo needs.
|
|
74
78
|
* - A server the app's record no longer holds is back in the app already.
|
|
75
79
|
* If the toolbox still has it as the import added it, it is kept, marked
|
|
76
80
|
* so: an Undo that put it back and then failed on the toolbox must still
|
|
@@ -125,6 +125,10 @@ export function deleteImportRecord(registryPath, target) {
|
|
|
125
125
|
* - The connection itself gone (no undo record, or one for another toolbox):
|
|
126
126
|
* the whole record goes. Undoing the connection said the servers brought
|
|
127
127
|
* in stay in the toolbox.
|
|
128
|
+
* - Except that a record naming a toolbox id the console doesn't have, or
|
|
129
|
+
* beside an undo record that does, is kept as it is: a toolbox whose id
|
|
130
|
+
* changed is being pointed at its new one (`adoptToolboxId`), and a step
|
|
131
|
+
* of that left half-done must not lose what Undo needs.
|
|
128
132
|
* - A server the app's record no longer holds is back in the app already.
|
|
129
133
|
* If the toolbox still has it as the import added it, it is kept, marked
|
|
130
134
|
* so: an Undo that put it back and then failed on the toolbox must still
|
|
@@ -143,12 +147,29 @@ export function readLiveImportRecord(registryPath, target) {
|
|
|
143
147
|
// An unreadable record is not evidence that anything was given back.
|
|
144
148
|
return rec;
|
|
145
149
|
}
|
|
150
|
+
if (backup && backup.scope === target.scope && backup.registry_id !== rec.gateway_id) {
|
|
151
|
+
let ids;
|
|
152
|
+
try {
|
|
153
|
+
ids = new Set(readRegistry(registryPath).servers.map((s) => s.id));
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
// No list to check against is no evidence the toolbox is another one.
|
|
157
|
+
return rec;
|
|
158
|
+
}
|
|
159
|
+
if (!ids.has(rec.gateway_id) || (backup.registry_id !== undefined && !ids.has(backup.registry_id)))
|
|
160
|
+
return rec;
|
|
161
|
+
}
|
|
146
162
|
const held = backup && backup.scope === target.scope && backup.registry_id === rec.gateway_id ? backup.replaced : undefined;
|
|
147
163
|
const upstreams = held ? toolboxUpstreams(registryPath, rec.gateway_id) : undefined;
|
|
148
164
|
const items = held
|
|
149
165
|
? rec.items.flatMap((i) => {
|
|
150
|
-
if (i.source !== "config"
|
|
166
|
+
if (i.source !== "config")
|
|
151
167
|
return [i];
|
|
168
|
+
if (Object.hasOwn(held, i.key)) {
|
|
169
|
+
// Out of the app again (brought in once more): no longer back in it, so Undo puts it back.
|
|
170
|
+
const { back_in_app: _back, ...rest } = i;
|
|
171
|
+
return [rest];
|
|
172
|
+
}
|
|
152
173
|
// A toolbox that can't be read is no evidence the server left it: kept.
|
|
153
174
|
const still = upstreams === undefined || upstreams.some((u) => u.name === i.upstream && (i.digest === undefined || upstreamDigest(u) === i.digest));
|
|
154
175
|
return i.added && still ? [{ ...i, back_in_app: true }] : [];
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { type RegistryEntry } from "hmcp-core";
|
|
2
|
+
import { type Upstream } from "hmcp-gateway";
|
|
3
|
+
import { type PolicyChange } from "./gateway-config.js";
|
|
4
|
+
/**
|
|
5
|
+
* A toolbox from nothing: its gateway.yaml, its policy.yaml and its row in
|
|
6
|
+
* the console's list. What POST /api/v1/gateways writes, and what "Bring in
|
|
7
|
+
* servers" writes when the app has no toolbox yet.
|
|
8
|
+
*
|
|
9
|
+
* Worked out first (`planNewToolbox`, which writes nothing) and written
|
|
10
|
+
* after (`writeNewToolbox`), so a caller can test servers in between and
|
|
11
|
+
* still be told about a name already taken before anything starts.
|
|
12
|
+
*/
|
|
13
|
+
export type Posture = "read-only" | "approve-writes" | "locked";
|
|
14
|
+
export interface NewToolboxInput {
|
|
15
|
+
/** Also the folder name under ~/.hmcp/gateways, checked by the caller. */
|
|
16
|
+
readonly name: string;
|
|
17
|
+
readonly upstreams: readonly Upstream[];
|
|
18
|
+
readonly posture?: Posture;
|
|
19
|
+
readonly mode?: "observe" | "enforce";
|
|
20
|
+
readonly onUnclassified?: "deny" | "approve";
|
|
21
|
+
/** The folder, already checked by the caller. Default ~/.hmcp/gateways/<name>. */
|
|
22
|
+
readonly dir?: string;
|
|
23
|
+
readonly id?: string;
|
|
24
|
+
readonly label?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Changes made to the policy after it is written out, the way adding a
|
|
27
|
+
* server to an existing toolbox makes them. "Bring in servers" passes the
|
|
28
|
+
* network changes its servers were tested with, so the toolbox lets
|
|
29
|
+
* through what the test let through. POST /gateways passes none.
|
|
30
|
+
*/
|
|
31
|
+
readonly policyChanges?: readonly PolicyChange[];
|
|
32
|
+
}
|
|
33
|
+
export interface NewToolboxPlan {
|
|
34
|
+
readonly name: string;
|
|
35
|
+
readonly id: string;
|
|
36
|
+
readonly label: string;
|
|
37
|
+
readonly dir: string;
|
|
38
|
+
readonly configPath: string;
|
|
39
|
+
readonly policyPath: string;
|
|
40
|
+
readonly gatewayYaml: string;
|
|
41
|
+
readonly policyYaml: string;
|
|
42
|
+
readonly upstreamNames: readonly string[];
|
|
43
|
+
readonly posture: Posture;
|
|
44
|
+
readonly mode: "observe" | "enforce";
|
|
45
|
+
/** A value in env or headers is written into gateway.yaml as it is. */
|
|
46
|
+
readonly literals: boolean;
|
|
47
|
+
/** Hosts on this computer or the local network, which need the private-address protection off. */
|
|
48
|
+
readonly local: readonly string[];
|
|
49
|
+
/** What `policyChanges` let through, in plain words. */
|
|
50
|
+
readonly loosens: readonly string[];
|
|
51
|
+
}
|
|
52
|
+
export interface NewToolbox {
|
|
53
|
+
readonly entry: RegistryEntry;
|
|
54
|
+
readonly id: string;
|
|
55
|
+
readonly configPath: string;
|
|
56
|
+
readonly policyPath: string;
|
|
57
|
+
readonly dir: string;
|
|
58
|
+
/** The folder was made for this toolbox, so taking the toolbox away again removes it (when nothing else is in it). */
|
|
59
|
+
readonly createdDir: boolean;
|
|
60
|
+
/** The files this toolbox wrote: the only ones taking it away deletes. */
|
|
61
|
+
readonly files: readonly string[];
|
|
62
|
+
}
|
|
63
|
+
export declare function uniqueId(base: string, taken: ReadonlySet<string>): string;
|
|
64
|
+
/** What switching the private-address protection off lets through, as POST /gateways words it. */
|
|
65
|
+
export declare function localLoosens(local: readonly string[]): string[];
|
|
66
|
+
/**
|
|
67
|
+
* The new toolbox's files as text, and where they go. Writes nothing.
|
|
68
|
+
*
|
|
69
|
+
* Refuses a folder that already holds a gateway.yaml or policy.yaml, and an
|
|
70
|
+
* id already in the console's list.
|
|
71
|
+
*/
|
|
72
|
+
export declare function planNewToolbox(input: NewToolboxInput, registryPath: string): NewToolboxPlan;
|
|
73
|
+
/**
|
|
74
|
+
* Writes a planned toolbox: both files (never over one that appeared since
|
|
75
|
+
* the plan), then its row in the console's list. Either all of it is there
|
|
76
|
+
* afterwards, or none of it.
|
|
77
|
+
*/
|
|
78
|
+
export declare function writeNewToolbox(plan: NewToolboxPlan, registryPath: string): NewToolbox;
|
|
79
|
+
/**
|
|
80
|
+
* Takes a toolbox `writeNewToolbox` made away again: its row (only while it
|
|
81
|
+
* still names these files), then its files and the folder made for it. The
|
|
82
|
+
* files go even when the row can't be taken out; that refusal is still thrown.
|
|
83
|
+
*/
|
|
84
|
+
export declare function removeNewToolbox(made: NewToolbox, registryPath: string): void;
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, rmdirSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { stringify } from "yaml";
|
|
5
|
+
import { readRegistry, writeRegistry } from "hmcp-core";
|
|
6
|
+
import { emitPolicyYaml, isLocalTarget, loadGatewayConfig } from "hmcp-gateway";
|
|
7
|
+
import { conflict, unprocessable } from "../http.js";
|
|
8
|
+
import { applyPolicyChanges, unchangedBatch } from "./gateway-config.js";
|
|
9
|
+
/** An id is at most 64 characters, the registry's rule. */
|
|
10
|
+
const MAX_ID = 64;
|
|
11
|
+
/** How many numbered ids to try before saying so. */
|
|
12
|
+
const MAX_ID_TRIES = 1000;
|
|
13
|
+
export function uniqueId(base, taken) {
|
|
14
|
+
if (!taken.has(base))
|
|
15
|
+
return base;
|
|
16
|
+
for (let n = 2; n <= MAX_ID_TRIES; n++) {
|
|
17
|
+
// The name is shortened to make room, so a 64-character name still gets a different id.
|
|
18
|
+
const suffix = `-${n}`;
|
|
19
|
+
const candidate = `${base.slice(0, MAX_ID - suffix.length)}${suffix}`;
|
|
20
|
+
if (!taken.has(candidate))
|
|
21
|
+
return candidate;
|
|
22
|
+
}
|
|
23
|
+
throw conflict(`every id from "${base}" to "${base}-${MAX_ID_TRIES}" is taken; choose another name`);
|
|
24
|
+
}
|
|
25
|
+
/** Drops the keys `UpstreamSchema` filled with nothing, so the file says only what was chosen. */
|
|
26
|
+
function prune(upstream) {
|
|
27
|
+
const out = {};
|
|
28
|
+
for (const [key, value] of Object.entries(upstream)) {
|
|
29
|
+
if (Array.isArray(value) && value.length === 0)
|
|
30
|
+
continue;
|
|
31
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0)
|
|
32
|
+
continue;
|
|
33
|
+
if (key === "credential_passthrough" && value.enabled === false)
|
|
34
|
+
continue;
|
|
35
|
+
out[key] = value;
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
/** Whether a value in env or headers is written as itself rather than as a locked-key reference. */
|
|
40
|
+
function hasLiteral(upstream) {
|
|
41
|
+
const values = Object.values(upstream.transport === "stdio" ? upstream.env : upstream.headers);
|
|
42
|
+
return values.some((v) => typeof v === "string");
|
|
43
|
+
}
|
|
44
|
+
/** What switching the private-address protection off lets through, as POST /gateways words it. */
|
|
45
|
+
export function localLoosens(local) {
|
|
46
|
+
if (local.length === 0)
|
|
47
|
+
return [];
|
|
48
|
+
return [`allow connections to this computer and the local network (${local.join(", ")})`, "allow plain http:// and bare IP addresses"];
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The new toolbox's files as text, and where they go. Writes nothing.
|
|
52
|
+
*
|
|
53
|
+
* Refuses a folder that already holds a gateway.yaml or policy.yaml, and an
|
|
54
|
+
* id already in the console's list.
|
|
55
|
+
*/
|
|
56
|
+
export function planNewToolbox(input, registryPath) {
|
|
57
|
+
const posture = input.posture ?? "approve-writes";
|
|
58
|
+
const mode = input.mode ?? "observe";
|
|
59
|
+
const onUnclassified = input.onUnclassified ?? "approve";
|
|
60
|
+
const upstreams = input.upstreams;
|
|
61
|
+
const names = upstreams.map((u) => u.name);
|
|
62
|
+
const dir = input.dir ?? join(homedir(), ".hmcp", "gateways", input.name);
|
|
63
|
+
const configPath = join(dir, "gateway.yaml");
|
|
64
|
+
const policyPath = join(dir, "policy.yaml");
|
|
65
|
+
for (const path of [configPath, policyPath]) {
|
|
66
|
+
if (existsSync(path))
|
|
67
|
+
throw conflict(`${path} already exists; choose another name or folder`);
|
|
68
|
+
}
|
|
69
|
+
const taken = new Set(readRegistry(registryPath).servers.map((s) => s.id));
|
|
70
|
+
if (input.id && taken.has(input.id))
|
|
71
|
+
throw conflict(`a server with id "${input.id}" is already registered`);
|
|
72
|
+
const id = input.id ?? uniqueId(input.name, taken);
|
|
73
|
+
// A `{ secret }` reference is a name, not a value, so only literals earn the warning.
|
|
74
|
+
const literals = upstreams.some(hasLiteral);
|
|
75
|
+
const hosts = upstreams.flatMap((u) => (u.transport === "http" ? [new URL(u.url).hostname] : []));
|
|
76
|
+
/*
|
|
77
|
+
* A server on this computer or the local network is reachable only with
|
|
78
|
+
* the private-address protections off, for every HTTP server of this
|
|
79
|
+
* gateway. That is a loosening, so the caller has it confirmed like any other.
|
|
80
|
+
*/
|
|
81
|
+
const local = [...new Set(hosts.filter((h) => isLocalTarget(h)))];
|
|
82
|
+
const plan = {
|
|
83
|
+
upstreams: upstreams.map((upstream) => ({ upstream, originalName: upstream.name, warnings: [] })),
|
|
84
|
+
skipped: [],
|
|
85
|
+
gatewayEnv: {},
|
|
86
|
+
egressAllow: [...new Set(hosts)].sort(),
|
|
87
|
+
egressNeedsLocalHttp: local.length > 0,
|
|
88
|
+
secretsOnDisk: literals,
|
|
89
|
+
nameMap: new Map(),
|
|
90
|
+
malformed: []
|
|
91
|
+
};
|
|
92
|
+
const gatewayYaml = [
|
|
93
|
+
"# Created in the hardened-mcp console. policy.yaml beside this file decides",
|
|
94
|
+
"# what each tool may do.",
|
|
95
|
+
...(literals
|
|
96
|
+
? ["#", "# A value set in env or headers below is stored here as written. If it is a", "# credential, do not share or commit this file."]
|
|
97
|
+
: []),
|
|
98
|
+
"",
|
|
99
|
+
stringify({ version: 1, name: input.name, policy: "./policy.yaml", upstreams: upstreams.map(prune), on_injection: "strip", namespace: true }, { lineWidth: 0 })
|
|
100
|
+
].join("\n");
|
|
101
|
+
const emitted = emitPolicyYaml(plan, {
|
|
102
|
+
name: input.name,
|
|
103
|
+
posture,
|
|
104
|
+
enforcement: mode,
|
|
105
|
+
onUnclassified
|
|
106
|
+
})
|
|
107
|
+
.replace("# Generated by `hmcp-gateway adopt`.", "# Created in the hardened-mcp console.")
|
|
108
|
+
.replace(`\nname: ${input.name}-adopted\n`, `\nname: ${input.name}\n`);
|
|
109
|
+
const batch = input.policyChanges && input.policyChanges.length > 0 ? applyPolicyChanges(emitted, input.policyChanges, policyPath) : unchangedBatch(emitted);
|
|
110
|
+
return {
|
|
111
|
+
name: input.name,
|
|
112
|
+
id,
|
|
113
|
+
label: input.label ?? input.name,
|
|
114
|
+
dir,
|
|
115
|
+
configPath,
|
|
116
|
+
policyPath,
|
|
117
|
+
gatewayYaml,
|
|
118
|
+
policyYaml: batch.text,
|
|
119
|
+
upstreamNames: names,
|
|
120
|
+
posture,
|
|
121
|
+
mode,
|
|
122
|
+
literals,
|
|
123
|
+
local,
|
|
124
|
+
loosens: batch.loosens
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Writes a planned toolbox: both files (never over one that appeared since
|
|
129
|
+
* the plan), then its row in the console's list. Either all of it is there
|
|
130
|
+
* afterwards, or none of it.
|
|
131
|
+
*/
|
|
132
|
+
export function writeNewToolbox(plan, registryPath) {
|
|
133
|
+
const { dir, configPath, policyPath } = plan;
|
|
134
|
+
// A toolbox can't be empty (gateway.yaml needs at least one server): refused before anything is written.
|
|
135
|
+
if (plan.upstreamNames.length === 0)
|
|
136
|
+
throw unprocessable("a toolbox needs at least one server, so nothing was written");
|
|
137
|
+
const createdDir = !existsSync(dir);
|
|
138
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
139
|
+
const files = [];
|
|
140
|
+
try {
|
|
141
|
+
// `wx`: a file that appeared since the check above is still not overwritten.
|
|
142
|
+
writeFileSync(configPath, plan.gatewayYaml, { encoding: "utf8", mode: 0o600, flag: "wx" });
|
|
143
|
+
files.push(configPath);
|
|
144
|
+
writeFileSync(policyPath, plan.policyYaml, { encoding: "utf8", mode: 0o600, flag: "wx" });
|
|
145
|
+
files.push(policyPath);
|
|
146
|
+
loadGatewayConfig(configPath);
|
|
147
|
+
}
|
|
148
|
+
catch (err) {
|
|
149
|
+
// Only what this wrote goes: a file that appeared meanwhile (EEXIST) is someone else's.
|
|
150
|
+
removeToolboxFiles({ dir, createdDir, files });
|
|
151
|
+
if (err.code === "EEXIST")
|
|
152
|
+
throw conflict(`${err.message}; nothing was written`);
|
|
153
|
+
throw unprocessable(`the gateway could not be written: ${err.message}`);
|
|
154
|
+
}
|
|
155
|
+
const written = { id: plan.id, configPath, policyPath, dir, createdDir, files };
|
|
156
|
+
const entry = {
|
|
157
|
+
id: plan.id,
|
|
158
|
+
kind: "gateway",
|
|
159
|
+
label: plan.label,
|
|
160
|
+
config_path: configPath,
|
|
161
|
+
added_at: new Date().toISOString()
|
|
162
|
+
};
|
|
163
|
+
try {
|
|
164
|
+
const reg = readRegistry(registryPath);
|
|
165
|
+
// Taken while servers were being tested: the files go again, and the row is not written twice.
|
|
166
|
+
if (reg.servers.some((s) => s.id === plan.id))
|
|
167
|
+
throw conflict(`a server with id "${plan.id}" is already registered`);
|
|
168
|
+
writeRegistry({ ...reg, servers: [...reg.servers, entry] }, registryPath);
|
|
169
|
+
}
|
|
170
|
+
catch (err) {
|
|
171
|
+
removeToolboxFiles(written);
|
|
172
|
+
throw err;
|
|
173
|
+
}
|
|
174
|
+
return { ...written, entry };
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* The files this toolbox wrote, and the folder when it was made for it and
|
|
178
|
+
* is empty now. Anything else in the folder, and the folder holding it, stays.
|
|
179
|
+
*/
|
|
180
|
+
function removeToolboxFiles(made) {
|
|
181
|
+
for (const path of made.files)
|
|
182
|
+
if (existsSync(path))
|
|
183
|
+
unlinkSync(path);
|
|
184
|
+
if (!made.createdDir)
|
|
185
|
+
return;
|
|
186
|
+
try {
|
|
187
|
+
rmdirSync(made.dir);
|
|
188
|
+
}
|
|
189
|
+
catch (err) {
|
|
190
|
+
if (err.code !== "ENOTEMPTY" && err.code !== "EEXIST")
|
|
191
|
+
throw err;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Takes a toolbox `writeNewToolbox` made away again: its row (only while it
|
|
196
|
+
* still names these files), then its files and the folder made for it. The
|
|
197
|
+
* files go even when the row can't be taken out; that refusal is still thrown.
|
|
198
|
+
*/
|
|
199
|
+
export function removeNewToolbox(made, registryPath) {
|
|
200
|
+
try {
|
|
201
|
+
const reg = readRegistry(registryPath);
|
|
202
|
+
const kept = reg.servers.filter((s) => !(s.id === made.id && resolve(s.config_path) === resolve(made.configPath)));
|
|
203
|
+
if (kept.length !== reg.servers.length)
|
|
204
|
+
writeRegistry({ ...reg, servers: kept }, registryPath);
|
|
205
|
+
}
|
|
206
|
+
finally {
|
|
207
|
+
removeToolboxFiles(made);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
@@ -2,6 +2,7 @@ import { type Decision, type StandingGrantRow, type ArgConstraint, type ToolSurf
|
|
|
2
2
|
import { type LoadedServer } from "./server.js";
|
|
3
3
|
import { ENFORCEMENT_PIPELINE } from "./pipeline.js";
|
|
4
4
|
import { type ToolSurfaceView } from "./tools.js";
|
|
5
|
+
import type { ProvisionalTool } from "./provisional-tools.js";
|
|
5
6
|
/**
|
|
6
7
|
* The protection view model.
|
|
7
8
|
*
|
|
@@ -238,6 +239,8 @@ export interface ServerProtection {
|
|
|
238
239
|
readonly hiddenCount: number;
|
|
239
240
|
/** Tools the gateway could not classify, which it refuses. */
|
|
240
241
|
readonly unclassifiedCount: number;
|
|
242
|
+
/** What the servers offered when tested on the way in, read-only until the toolbox runs (see `ToolSurfaceView`). */
|
|
243
|
+
readonly provisional: readonly ProvisionalTool[];
|
|
241
244
|
};
|
|
242
245
|
readonly onUnclassified: string;
|
|
243
246
|
/**
|
|
@@ -391,6 +394,6 @@ export type Reach = "read-only" | "approve-writes" | "locked";
|
|
|
391
394
|
*/
|
|
392
395
|
export declare function effectiveReach(server: LoadedServer, tenant: string, surface?: ToolSurfaceView): Reach | null;
|
|
393
396
|
export declare function toolProtection(server: LoadedServer, tool: ToolSurfaceEntry, grants: readonly StandingGrantRow[]): ToolProtection;
|
|
394
|
-
export declare function buildProtection(server: LoadedServer, tenant: string, explicit?: boolean): ServerProtection;
|
|
397
|
+
export declare function buildProtection(server: LoadedServer, tenant: string, explicit?: boolean, registryPath?: string): ServerProtection;
|
|
395
398
|
/** The last few decisions recorded for one tool. */
|
|
396
399
|
export declare function recentActivity(server: LoadedServer, tool: string, limit?: number, otherComponents?: readonly string[]): import("hmcp-core").AuditRecord[];
|
package/dist/model/protection.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
|
-
import { decide, globMatch, parseArgPath, grantBoundGaps, grantConstraints, grantCoversTool, policyDigest, queryAuditLog, resolveEffect,
|
|
2
|
+
import { decide, globMatch, parseArgPath, grantBoundGaps, grantConstraints, grantCoversTool, policyDigest, queryAuditLog, resolveEffect, verifyAuditLog } from "hmcp-core";
|
|
3
3
|
import { describeTenantSource, scopedFor, storeFor } from "./server.js";
|
|
4
4
|
import { ENFORCEMENT_PIPELINE } from "./pipeline.js";
|
|
5
|
+
import { toolboxLabel } from "./toolbox-label.js";
|
|
5
6
|
import { toolSurfaceFor } from "./tools.js";
|
|
6
7
|
/** Which of `decide()`'s four stages produced a verdict, named from its rule id. */
|
|
7
8
|
function verdictStage(ruleId) {
|
|
@@ -390,9 +391,9 @@ function ruleRows(policy, toolNames) {
|
|
|
390
391
|
};
|
|
391
392
|
});
|
|
392
393
|
}
|
|
393
|
-
export function buildProtection(server, tenant, explicit = false) {
|
|
394
|
+
export function buildProtection(server, tenant, explicit = false, registryPath) {
|
|
394
395
|
const policy = server.policy;
|
|
395
|
-
const surface = toolSurfaceFor(server, tenant);
|
|
396
|
+
const surface = toolSurfaceFor(server, tenant, registryPath);
|
|
396
397
|
const toolNames = surface.entries.map((t) => t.name);
|
|
397
398
|
const store = storeFor(server);
|
|
398
399
|
const scoped = scopedFor(server, tenant);
|
|
@@ -421,19 +422,20 @@ export function buildProtection(server, tenant, explicit = false) {
|
|
|
421
422
|
* A row from a process that has stopped refreshing is ignored rather than
|
|
422
423
|
* trusted: it describes a server that is no longer deciding anything, and
|
|
423
424
|
* quoting it would be a second way of saying something untrue about what is
|
|
424
|
-
* in force.
|
|
425
|
+
* in force. Whether anything is running is the shared judgement
|
|
426
|
+
* (`liveProcesses`): when only a twin process is, the page says running and
|
|
427
|
+
* leaves the start time and policy unknown, as `status` does.
|
|
425
428
|
*/
|
|
426
|
-
const
|
|
427
|
-
const live = announced && runtimeStateIsLive(announced) ? announced : undefined;
|
|
429
|
+
const live = store.liveRuntimeState(server.component);
|
|
428
430
|
const runtime = {
|
|
429
|
-
running:
|
|
431
|
+
running: store.liveProcesses(server.component).length > 0,
|
|
430
432
|
startedAt: live?.started_at ?? null,
|
|
431
433
|
policyApplied: live ? live.policy_digest === policyDigest(policy) : null,
|
|
432
434
|
policyPath: live?.policy_path || server.policyPath
|
|
433
435
|
};
|
|
434
436
|
return {
|
|
435
437
|
id: server.entry.id,
|
|
436
|
-
label: server.entry.
|
|
438
|
+
label: toolboxLabel(server.entry, server.gateway.config.name),
|
|
437
439
|
kind: server.entry.kind,
|
|
438
440
|
tenant_scope: tenant,
|
|
439
441
|
explicitTenant: explicit,
|
|
@@ -447,7 +449,8 @@ export function buildProtection(server, tenant, explicit = false) {
|
|
|
447
449
|
toolCount: surface.entries.length,
|
|
448
450
|
injectionCount: surface.entries.filter((t) => t.injection && t.injection.length > 0).length,
|
|
449
451
|
hiddenCount: surface.entries.filter((t) => t.hiddenByPolicy).length,
|
|
450
|
-
unclassifiedCount: surface.entries.filter((t) => t.effect === null).length
|
|
452
|
+
unclassifiedCount: surface.entries.filter((t) => t.effect === null).length,
|
|
453
|
+
provisional: surface.provisional
|
|
451
454
|
},
|
|
452
455
|
onUnclassified: policy.defaults.on_unclassified,
|
|
453
456
|
enforcement: policy.enforcement,
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { ProbeTool } from "hmcp-gateway";
|
|
3
|
+
import type { LoadedServer } from "./server.js";
|
|
4
|
+
/**
|
|
5
|
+
* The tools a server offered when it was tested on the way into a toolbox,
|
|
6
|
+
* kept so the console can show them before the toolbox has ever run.
|
|
7
|
+
*
|
|
8
|
+
* A toolbox's real tool list is the one its running process publishes, and
|
|
9
|
+
* only that list is trusted for a switch or a rule. This list is not that: it
|
|
10
|
+
* is what a test saw, read-only, and shown only until the toolbox is live. It
|
|
11
|
+
* never joins `entries`, so nothing that writes protection can name a tool
|
|
12
|
+
* from it.
|
|
13
|
+
*
|
|
14
|
+
* Each server's list carries what the server ran as it was tested
|
|
15
|
+
* (`upstreamDigest`), and is dropped the moment gateway.yaml says something
|
|
16
|
+
* else: a list for a server changed since is a list for a server that isn't
|
|
17
|
+
* there. Names only: no settings, keys, command lines or addresses.
|
|
18
|
+
*/
|
|
19
|
+
/** A server's own text, so a server offering huge names or thousands of tools can't make the note, or the page, unbounded. */
|
|
20
|
+
export declare const MAX_NAME = 200;
|
|
21
|
+
export declare const MAX_TOOLS_PER_SERVER = 500;
|
|
22
|
+
export declare const MAX_SERVERS = 100;
|
|
23
|
+
export declare const ProvisionalToolSchema: z.ZodObject<{
|
|
24
|
+
name: z.ZodString;
|
|
25
|
+
upstream: z.ZodString;
|
|
26
|
+
effect: z.ZodEnum<{
|
|
27
|
+
read: "read";
|
|
28
|
+
write: "write";
|
|
29
|
+
destructive: "destructive";
|
|
30
|
+
unknown: "unknown";
|
|
31
|
+
}>;
|
|
32
|
+
verdict: z.ZodEnum<{
|
|
33
|
+
run: "run";
|
|
34
|
+
ask: "ask";
|
|
35
|
+
blocked: "blocked";
|
|
36
|
+
}>;
|
|
37
|
+
withheld: z.ZodOptional<z.ZodString>;
|
|
38
|
+
}, z.core.$strict>;
|
|
39
|
+
export type ProvisionalTool = z.infer<typeof ProvisionalToolSchema>;
|
|
40
|
+
/** `<registry folder>/provisional/<toolbox id>.json`; an id that isn't plain is hashed, so no id becomes a path. */
|
|
41
|
+
export declare function provisionalPath(registryPath: string, gatewayId: string): string;
|
|
42
|
+
/** Keeps what these servers offered when tested; a server listed before is replaced. */
|
|
43
|
+
export declare function saveProvisional(registryPath: string, gatewayId: string, probed: readonly {
|
|
44
|
+
upstream: string;
|
|
45
|
+
digest: string;
|
|
46
|
+
tools: readonly ProbeTool[];
|
|
47
|
+
}[], now?: Date): void;
|
|
48
|
+
/** Drops these servers' lists, or the whole file when `upstreams` is left out. */
|
|
49
|
+
export declare function removeProvisional(registryPath: string, gatewayId: string, upstreams?: readonly string[]): void;
|
|
50
|
+
/**
|
|
51
|
+
* The tools to show for a toolbox that isn't live, each only while
|
|
52
|
+
* gateway.yaml still has its server exactly as it was tested, and only when
|
|
53
|
+
* the test is newer than the last list the toolbox published (`publishedAt`,
|
|
54
|
+
* null when it never published): an older test says less than that list.
|
|
55
|
+
*/
|
|
56
|
+
export declare function provisionalFor(registryPath: string, server: LoadedServer, publishedAt?: number | null): ProvisionalTool[];
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { sha256 } from "hmcp-core";
|
|
5
|
+
import { upstreamDigest } from "./import-record.js";
|
|
6
|
+
/**
|
|
7
|
+
* The tools a server offered when it was tested on the way into a toolbox,
|
|
8
|
+
* kept so the console can show them before the toolbox has ever run.
|
|
9
|
+
*
|
|
10
|
+
* A toolbox's real tool list is the one its running process publishes, and
|
|
11
|
+
* only that list is trusted for a switch or a rule. This list is not that: it
|
|
12
|
+
* is what a test saw, read-only, and shown only until the toolbox is live. It
|
|
13
|
+
* never joins `entries`, so nothing that writes protection can name a tool
|
|
14
|
+
* from it.
|
|
15
|
+
*
|
|
16
|
+
* Each server's list carries what the server ran as it was tested
|
|
17
|
+
* (`upstreamDigest`), and is dropped the moment gateway.yaml says something
|
|
18
|
+
* else: a list for a server changed since is a list for a server that isn't
|
|
19
|
+
* there. Names only: no settings, keys, command lines or addresses.
|
|
20
|
+
*/
|
|
21
|
+
/** A server's own text, so a server offering huge names or thousands of tools can't make the note, or the page, unbounded. */
|
|
22
|
+
export const MAX_NAME = 200;
|
|
23
|
+
export const MAX_TOOLS_PER_SERVER = 500;
|
|
24
|
+
export const MAX_SERVERS = 100;
|
|
25
|
+
const MAX_WITHHELD = 500;
|
|
26
|
+
export const ProvisionalToolSchema = z
|
|
27
|
+
.object({
|
|
28
|
+
/** As the AI app will see it: <upstream>__<tool>. */
|
|
29
|
+
name: z.string().min(1).max(MAX_NAME),
|
|
30
|
+
upstream: z.string().min(1).max(MAX_NAME),
|
|
31
|
+
effect: z.enum(["read", "write", "destructive", "unknown"]),
|
|
32
|
+
verdict: z.enum(["run", "ask", "blocked"]),
|
|
33
|
+
withheld: z.string().max(MAX_WITHHELD).optional()
|
|
34
|
+
})
|
|
35
|
+
.strict();
|
|
36
|
+
const ProvisionalUpstreamSchema = z
|
|
37
|
+
.object({
|
|
38
|
+
upstream: z.string().min(1).max(MAX_NAME),
|
|
39
|
+
digest: z.string().min(1),
|
|
40
|
+
tools: z.array(ProvisionalToolSchema).max(MAX_TOOLS_PER_SERVER),
|
|
41
|
+
probed_at: z.string()
|
|
42
|
+
})
|
|
43
|
+
.strict();
|
|
44
|
+
const ProvisionalFileSchema = z
|
|
45
|
+
.object({
|
|
46
|
+
version: z.literal(1),
|
|
47
|
+
gateway_id: z.string().min(1),
|
|
48
|
+
upstreams: z.array(ProvisionalUpstreamSchema).max(MAX_SERVERS)
|
|
49
|
+
})
|
|
50
|
+
.strict();
|
|
51
|
+
const SAFE_ID = /^[a-z0-9][a-z0-9_-]{0,63}$/;
|
|
52
|
+
/** `<registry folder>/provisional/<toolbox id>.json`; an id that isn't plain is hashed, so no id becomes a path. */
|
|
53
|
+
export function provisionalPath(registryPath, gatewayId) {
|
|
54
|
+
const file = SAFE_ID.test(gatewayId) ? gatewayId : `id-${sha256(gatewayId).slice(0, 32)}`;
|
|
55
|
+
return join(dirname(registryPath), "provisional", `${file}.json`);
|
|
56
|
+
}
|
|
57
|
+
function readFile(registryPath, gatewayId) {
|
|
58
|
+
const path = provisionalPath(registryPath, gatewayId);
|
|
59
|
+
if (!existsSync(path))
|
|
60
|
+
return undefined;
|
|
61
|
+
try {
|
|
62
|
+
const parsed = ProvisionalFileSchema.safeParse(JSON.parse(readFileSync(path, "utf8")));
|
|
63
|
+
return parsed.success && parsed.data.gateway_id === gatewayId ? parsed.data : undefined;
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** Atomic and 0600, like the console's other notes. An empty list deletes the file. */
|
|
70
|
+
function writeFile(registryPath, file) {
|
|
71
|
+
const path = provisionalPath(registryPath, file.gateway_id);
|
|
72
|
+
if (file.upstreams.length === 0) {
|
|
73
|
+
if (existsSync(path))
|
|
74
|
+
unlinkSync(path);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
78
|
+
const tmp = join(dirname(path), `.${Date.now()}.${Math.random().toString(36).slice(2)}.tmp`);
|
|
79
|
+
writeFileSync(tmp, `${JSON.stringify(file, null, 2)}\n`, { encoding: "utf8", mode: 0o600 });
|
|
80
|
+
try {
|
|
81
|
+
chmodSync(tmp, 0o600);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
/* best effort: Windows has no such modes */
|
|
85
|
+
}
|
|
86
|
+
renameSync(tmp, path);
|
|
87
|
+
}
|
|
88
|
+
/** Keeps what these servers offered when tested; a server listed before is replaced. */
|
|
89
|
+
export function saveProvisional(registryPath, gatewayId, probed, now = new Date()) {
|
|
90
|
+
if (probed.length === 0)
|
|
91
|
+
return;
|
|
92
|
+
const before = readFile(registryPath, gatewayId)?.upstreams ?? [];
|
|
93
|
+
const names = new Set(probed.map((p) => p.upstream));
|
|
94
|
+
const added = probed
|
|
95
|
+
.filter((p) => p.upstream.length <= MAX_NAME)
|
|
96
|
+
.map((p) => ({
|
|
97
|
+
upstream: p.upstream,
|
|
98
|
+
digest: p.digest,
|
|
99
|
+
// A name too long to show is left out rather than cut, so no row names a tool that isn't there.
|
|
100
|
+
tools: p.tools
|
|
101
|
+
.filter((t) => t.exposedName.length > 0 && t.exposedName.length <= MAX_NAME)
|
|
102
|
+
.slice(0, MAX_TOOLS_PER_SERVER)
|
|
103
|
+
.map((t) => ({
|
|
104
|
+
name: t.exposedName,
|
|
105
|
+
upstream: p.upstream,
|
|
106
|
+
effect: t.effect,
|
|
107
|
+
verdict: t.verdict,
|
|
108
|
+
...(t.withheld !== undefined ? { withheld: t.withheld.slice(0, MAX_WITHHELD) } : {})
|
|
109
|
+
})),
|
|
110
|
+
probed_at: now.toISOString()
|
|
111
|
+
}));
|
|
112
|
+
const upstreams = [...before.filter((u) => !names.has(u.upstream)), ...added].slice(-MAX_SERVERS);
|
|
113
|
+
writeFile(registryPath, { version: 1, gateway_id: gatewayId, upstreams });
|
|
114
|
+
}
|
|
115
|
+
/** Drops these servers' lists, or the whole file when `upstreams` is left out. */
|
|
116
|
+
export function removeProvisional(registryPath, gatewayId, upstreams) {
|
|
117
|
+
const file = readFile(registryPath, gatewayId);
|
|
118
|
+
if (!file) {
|
|
119
|
+
const path = provisionalPath(registryPath, gatewayId);
|
|
120
|
+
// One that can't be read is dropped as well when the whole list goes.
|
|
121
|
+
if (upstreams === undefined && existsSync(path))
|
|
122
|
+
unlinkSync(path);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const gone = upstreams === undefined ? null : new Set(upstreams);
|
|
126
|
+
writeFile(registryPath, { ...file, upstreams: gone === null ? [] : file.upstreams.filter((u) => !gone.has(u.upstream)) });
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The tools to show for a toolbox that isn't live, each only while
|
|
130
|
+
* gateway.yaml still has its server exactly as it was tested, and only when
|
|
131
|
+
* the test is newer than the last list the toolbox published (`publishedAt`,
|
|
132
|
+
* null when it never published): an older test says less than that list.
|
|
133
|
+
*/
|
|
134
|
+
export function provisionalFor(registryPath, server, publishedAt = null) {
|
|
135
|
+
const file = readFile(registryPath, server.entry.id);
|
|
136
|
+
if (!file)
|
|
137
|
+
return [];
|
|
138
|
+
const now = server.gateway.config.upstreams;
|
|
139
|
+
return file.upstreams.flatMap((p) => {
|
|
140
|
+
if (publishedAt !== null && !(Date.parse(p.probed_at) > publishedAt))
|
|
141
|
+
return [];
|
|
142
|
+
const u = now.find((x) => x.name === p.upstream);
|
|
143
|
+
return u && upstreamDigest(u) === p.digest ? p.tools : [];
|
|
144
|
+
});
|
|
145
|
+
}
|