hmcp-core 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/README.md +19 -3
- package/dist/approvals.d.ts +508 -0
- package/dist/approvals.js +1445 -0
- package/dist/argpath.d.ts +75 -0
- package/dist/argpath.js +189 -0
- package/dist/audit.d.ts +289 -0
- package/dist/audit.js +512 -0
- package/dist/canonical.d.ts +78 -0
- package/dist/canonical.js +146 -0
- package/dist/classify-held.d.ts +47 -0
- package/dist/classify-held.js +60 -0
- package/dist/classify-name.d.ts +55 -0
- package/dist/classify-name.js +255 -0
- package/dist/decide.d.ts +168 -0
- package/dist/decide.js +518 -0
- package/dist/egress.d.ts +123 -0
- package/dist/egress.js +544 -0
- package/dist/enforce.d.ts +78 -0
- package/dist/enforce.js +231 -0
- package/dist/exposure-apply.d.ts +51 -0
- package/dist/exposure-apply.js +69 -0
- package/dist/exposure.d.ts +81 -0
- package/dist/exposure.js +65 -0
- package/dist/file-lock.d.ts +70 -0
- package/dist/file-lock.js +165 -0
- package/dist/glob.d.ts +11 -0
- package/dist/glob.js +55 -0
- package/dist/grants.d.ts +204 -0
- package/dist/grants.js +372 -0
- package/dist/http-util.d.ts +52 -0
- package/dist/http-util.js +81 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +36 -0
- package/dist/inventory.d.ts +100 -0
- package/dist/inventory.js +88 -0
- package/dist/ip.d.ts +15 -0
- package/dist/ip.js +186 -0
- package/dist/jwt.d.ts +57 -0
- package/dist/jwt.js +199 -0
- package/dist/live-state.d.ts +76 -0
- package/dist/live-state.js +57 -0
- package/dist/migrate.d.ts +22 -0
- package/dist/migrate.js +318 -0
- package/dist/pins.d.ts +119 -0
- package/dist/pins.js +125 -0
- package/dist/policy-batch.d.ts +89 -0
- package/dist/policy-batch.js +340 -0
- package/dist/policy-source.d.ts +161 -0
- package/dist/policy-source.js +299 -0
- package/dist/policy-write.d.ts +627 -0
- package/dist/policy-write.js +1697 -0
- package/dist/policy.d.ts +456 -0
- package/dist/policy.js +448 -0
- package/dist/redact.d.ts +65 -0
- package/dist/redact.js +191 -0
- package/dist/registry.d.ts +41 -0
- package/dist/registry.js +121 -0
- package/dist/remember-defaults.d.ts +9 -0
- package/dist/remember-defaults.js +9 -0
- package/dist/remember.d.ts +91 -0
- package/dist/remember.js +371 -0
- package/dist/resolve.d.ts +22 -0
- package/dist/resolve.js +34 -0
- package/dist/runtime-state.d.ts +65 -0
- package/dist/runtime-state.js +35 -0
- package/dist/secrets.d.ts +159 -0
- package/dist/secrets.js +361 -0
- package/dist/tenant.d.ts +83 -0
- package/dist/tenant.js +182 -0
- package/dist/unicode.d.ts +40 -0
- package/dist/unicode.js +103 -0
- package/dist/within-host.d.ts +33 -0
- package/dist/within-host.js +91 -0
- package/dist/within-real.d.ts +7 -0
- package/dist/within-real.js +28 -0
- package/package.json +33 -4
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { DatabaseSync } from "node:sqlite";
|
|
2
|
+
export declare const LATEST_SCHEMA_VERSION: number;
|
|
3
|
+
/**
|
|
4
|
+
* Brings one database up to `LATEST_SCHEMA_VERSION`.
|
|
5
|
+
*
|
|
6
|
+
* `createLatest` is the caller's "fresh database" path: the current
|
|
7
|
+
* `CREATE TABLE` statements, at their newest shape. It matters that a fresh
|
|
8
|
+
* database takes that path rather than replaying history, because replaying
|
|
9
|
+
* would mean every migration has to stay runnable against the schema of its
|
|
10
|
+
* own era for ever.
|
|
11
|
+
*
|
|
12
|
+
* The awkward case this has to get right is that a brand-new database and one
|
|
13
|
+
* written before versioning existed *both* report version 0. They are told
|
|
14
|
+
* apart by looking for a table only the second would have.
|
|
15
|
+
*
|
|
16
|
+
* Concurrency is real here, not theoretical: the default store path is shared,
|
|
17
|
+
* and a console, a CLI and a server may all open it at once. Each step takes
|
|
18
|
+
* the write lock with BEGIN IMMEDIATE and re-reads the version inside the
|
|
19
|
+
* transaction, so a process that loses the race finds the work already done
|
|
20
|
+
* and skips it rather than applying it twice.
|
|
21
|
+
*/
|
|
22
|
+
export declare function migrate(db: DatabaseSync, createLatest: (db: DatabaseSync) => void): void;
|
package/dist/migrate.js
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { TOOL_INVENTORY_SCHEMA } from "./inventory.js";
|
|
2
|
+
import { RUNTIME_STATE_SCHEMA } from "./runtime-state.js";
|
|
3
|
+
import { TOOL_CHANGE_SCHEMA, TOOL_PIN_SCHEMA } from "./pins.js";
|
|
4
|
+
/**
|
|
5
|
+
* Schema versioning for the approvals database.
|
|
6
|
+
*
|
|
7
|
+
* Until this existed the whole schema lifecycle was four `CREATE TABLE IF NOT
|
|
8
|
+
* EXISTS` statements, which is fine for adding a *table* and silently useless
|
|
9
|
+
* for adding a *column*: the statement is a no-op against a table that already
|
|
10
|
+
* exists, so an upgraded binary would open an old database, see none of its
|
|
11
|
+
* new columns, and fail at the first query with `no such column`. Nothing
|
|
12
|
+
* would have detected it and nothing would have repaired it.
|
|
13
|
+
*
|
|
14
|
+
* `PRAGMA user_version` is the version counter. It is an integer SQLite keeps
|
|
15
|
+
* in the database header, it costs nothing to read, and — the reason it is the
|
|
16
|
+
* right choice here rather than a bookkeeping table — it is transactional, so
|
|
17
|
+
* a migration and the stamp that records it commit or roll back together.
|
|
18
|
+
* There is no window in which the work is done but the version says otherwise.
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Adds a column unless the table already has it.
|
|
22
|
+
*
|
|
23
|
+
* `ALTER TABLE ... ADD COLUMN` throws on a duplicate, which would abort the
|
|
24
|
+
* migration and leave the version stamp behind it — a database that fails to
|
|
25
|
+
* open on every subsequent start. A column add is naturally idempotent and
|
|
26
|
+
* this makes it so, which matters whenever the schema and the stamp disagree:
|
|
27
|
+
* a database created fresh at the latest schema but carrying an older
|
|
28
|
+
* `user_version`, or one opened by two builds in either order.
|
|
29
|
+
*/
|
|
30
|
+
function addColumn(db, table, column, decl) {
|
|
31
|
+
const existing = db.prepare(`PRAGMA table_info(${table})`).all();
|
|
32
|
+
if (existing.some((c) => c.name === column))
|
|
33
|
+
return;
|
|
34
|
+
db.exec(`ALTER TABLE ${table} ADD COLUMN ${column} ${decl}`);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Index + 1 is the version a migration produces, so this array is append-only:
|
|
38
|
+
* editing a published entry changes what a version *means* on databases that
|
|
39
|
+
* already claim it.
|
|
40
|
+
*/
|
|
41
|
+
const MIGRATIONS = [
|
|
42
|
+
{
|
|
43
|
+
name: "scope approvals, grants and exposure by component and tenant",
|
|
44
|
+
up(db) {
|
|
45
|
+
/*
|
|
46
|
+
* `approvals` and `standing_grants` never carried a server identity at
|
|
47
|
+
* all, let alone a tenant, so every query against them returned every
|
|
48
|
+
* row in a database that is shared between servers by default. Two
|
|
49
|
+
* columns with a constant default is a cheap ALTER; SQLite allows it
|
|
50
|
+
* precisely because no existing row has to be rewritten.
|
|
51
|
+
*/
|
|
52
|
+
for (const table of ["approvals", "standing_grants"]) {
|
|
53
|
+
db.exec(`ALTER TABLE ${table} ADD COLUMN component TEXT NOT NULL DEFAULT ''`);
|
|
54
|
+
db.exec(`ALTER TABLE ${table} ADD COLUMN tenant TEXT NOT NULL DEFAULT ''`);
|
|
55
|
+
}
|
|
56
|
+
// The old indexes lead with the columns that are now the least
|
|
57
|
+
// selective, so they are replaced rather than supplemented.
|
|
58
|
+
db.exec("DROP INDEX IF EXISTS approvals_binding");
|
|
59
|
+
db.exec("DROP INDEX IF EXISTS approvals_state");
|
|
60
|
+
db.exec("DROP INDEX IF EXISTS grants_active");
|
|
61
|
+
db.exec("CREATE INDEX approvals_binding ON approvals (component, tenant, binding_hash, state)");
|
|
62
|
+
db.exec("CREATE INDEX approvals_state ON approvals (component, tenant, state, created_at)");
|
|
63
|
+
db.exec("CREATE INDEX grants_active ON standing_grants (component, tenant, state, expires_at)");
|
|
64
|
+
/*
|
|
65
|
+
* `tool_exposure` needs its PRIMARY KEY widened from (component, tool)
|
|
66
|
+
* to (component, tenant, tool), and SQLite cannot alter a primary key in
|
|
67
|
+
* place — the table has to be rebuilt. Safe to do bluntly here because
|
|
68
|
+
* the schema has no foreign keys, views or triggers pointing at it, so
|
|
69
|
+
* there is nothing for the rename to leave dangling.
|
|
70
|
+
*/
|
|
71
|
+
db.exec(`
|
|
72
|
+
CREATE TABLE tool_exposure_new (
|
|
73
|
+
component TEXT NOT NULL,
|
|
74
|
+
tenant TEXT NOT NULL DEFAULT '',
|
|
75
|
+
tool TEXT NOT NULL,
|
|
76
|
+
reason TEXT NOT NULL,
|
|
77
|
+
set_at INTEGER NOT NULL,
|
|
78
|
+
set_by TEXT NOT NULL,
|
|
79
|
+
PRIMARY KEY (component, tenant, tool)
|
|
80
|
+
)
|
|
81
|
+
`);
|
|
82
|
+
db.exec(`
|
|
83
|
+
INSERT INTO tool_exposure_new (component, tenant, tool, reason, set_at, set_by)
|
|
84
|
+
SELECT component, '', tool, reason, set_at, set_by FROM tool_exposure
|
|
85
|
+
`);
|
|
86
|
+
db.exec("DROP TABLE tool_exposure");
|
|
87
|
+
db.exec("ALTER TABLE tool_exposure_new RENAME TO tool_exposure");
|
|
88
|
+
/*
|
|
89
|
+
* `runtime_state` is deliberately untouched. Its columns are pid,
|
|
90
|
+
* started_at, last_seen and the policy digest: that is the liveness of
|
|
91
|
+
* an operating-system process, and one hosted gateway process serves
|
|
92
|
+
* every tenant at once. Tenants do not have processes, so a tenant
|
|
93
|
+
* column there would be the wrong axis and would cost a second rebuild
|
|
94
|
+
* to say nothing.
|
|
95
|
+
*/
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
name: "create runtime_state for databases versioned before it existed",
|
|
100
|
+
up(db) {
|
|
101
|
+
/*
|
|
102
|
+
* The table arrived after versioning did, so a database already stamped
|
|
103
|
+
* version 1 was never given it and the console failed on first read with
|
|
104
|
+
* `no such table`. Fresh databases get it from `createLatest`; this is
|
|
105
|
+
* the same statement for the ones that do not.
|
|
106
|
+
*/
|
|
107
|
+
db.exec(RUNTIME_STATE_SCHEMA);
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: "create tool_inventory so a gateway can publish the surface it serves",
|
|
112
|
+
up(db) {
|
|
113
|
+
/*
|
|
114
|
+
* New table, so the same two-line shape as the migration above: fresh
|
|
115
|
+
* databases get it from `createLatest`, and this is the statement for the
|
|
116
|
+
* ones already stamped at an earlier version.
|
|
117
|
+
*
|
|
118
|
+
* Nothing is backfilled, and there is nothing that could be: the rows
|
|
119
|
+
* describe what a *running* process is serving, and the only honest answer
|
|
120
|
+
* for a gateway that has not started since this build is "unknown". A
|
|
121
|
+
* reader already has to treat an absent inventory that way, because a
|
|
122
|
+
* gateway may have exited.
|
|
123
|
+
*/
|
|
124
|
+
db.exec(TOOL_INVENTORY_SCHEMA);
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
name: "record on each approval why it was held and under which bounds",
|
|
129
|
+
up(db) {
|
|
130
|
+
/*
|
|
131
|
+
* A pending approval said what the call was and never said why it was
|
|
132
|
+
* being asked about. Once an argument bound can *hold* a call rather
|
|
133
|
+
* than only refuse it, the reviewer's question becomes "which bound did
|
|
134
|
+
* this exceed, and by how much" — and the answer was nowhere, because
|
|
135
|
+
* `args_redacted` truncates arrays at 100 and collapses anything past
|
|
136
|
+
* depth 12, so the bounded value itself may not be in the row at all.
|
|
137
|
+
*
|
|
138
|
+
* `rule_id` and `policy_digest` are the other half: which rule held the
|
|
139
|
+
* call, and which policy text that rule came from. Without them an
|
|
140
|
+
* auditor reading a past grant cannot say what the bounds were at the
|
|
141
|
+
* time, only what they are now.
|
|
142
|
+
*
|
|
143
|
+
* Nullable with no default, because for a row written before this
|
|
144
|
+
* migration the honest answer is "not recorded" and not "none".
|
|
145
|
+
*/
|
|
146
|
+
addColumn(db, "approvals", "violations", "TEXT");
|
|
147
|
+
addColumn(db, "approvals", "rule_id", "TEXT");
|
|
148
|
+
addColumn(db, "approvals", "policy_digest", "TEXT");
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
name: "stamp each approval with the binding scheme it was written under",
|
|
153
|
+
up(db) {
|
|
154
|
+
/*
|
|
155
|
+
* `bindingHash` changed framing: the tool and the arguments became
|
|
156
|
+
* separate fields of one canonical document instead of being
|
|
157
|
+
* concatenated into the digest, so that no tool name could stand in for
|
|
158
|
+
* a different call. Every digest therefore changed.
|
|
159
|
+
*
|
|
160
|
+
* Without this column that would silently strand every approval already
|
|
161
|
+
* in flight. A reviewer approves a call, the binary is upgraded, the
|
|
162
|
+
* agent retries with identical arguments, and the retry computes a digest
|
|
163
|
+
* that matches nothing -- so the call is asked about again. Teaching an
|
|
164
|
+
* agent that retrying produces a second prompt is the opposite of what
|
|
165
|
+
* approvals are for.
|
|
166
|
+
*
|
|
167
|
+
* `DEFAULT 1` is correct for every existing row by definition: they can
|
|
168
|
+
* only have been written under the old framing. New rows write the
|
|
169
|
+
* current version explicitly, and redemption compares a digest only
|
|
170
|
+
* against rows stamped with the scheme that digest came from.
|
|
171
|
+
*/
|
|
172
|
+
addColumn(db, "approvals", "binding_version", "INTEGER NOT NULL DEFAULT 1");
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
name: "record on each approval which arguments broke a bound",
|
|
177
|
+
up(db) {
|
|
178
|
+
/*
|
|
179
|
+
* `violations` already says *why* a call was held, as prose for a
|
|
180
|
+
* reviewer. This says *which arguments* it was held over, as paths: what
|
|
181
|
+
* a reviewer needs in order to say "and allow calls like this", because
|
|
182
|
+
* the grant that remembers their answer has to bound exactly those
|
|
183
|
+
* arguments and no others.
|
|
184
|
+
*
|
|
185
|
+
* NULL on every existing row, which means "not recorded" and not
|
|
186
|
+
* "none". Nothing is backfilled: the paths cannot be recovered from the
|
|
187
|
+
* prose, and a guess here would become the bounds of a pre-approval.
|
|
188
|
+
*/
|
|
189
|
+
addColumn(db, "approvals", "held_args", "TEXT");
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
name: "pin what each upstream advertised, so a change to it can be noticed",
|
|
194
|
+
up(db) {
|
|
195
|
+
/*
|
|
196
|
+
* A new table, so `CREATE TABLE IF NOT EXISTS` would have been enough
|
|
197
|
+
* on its own -- but a database opened by this build and then by an
|
|
198
|
+
* older one and then by this one again must still arrive here, and the
|
|
199
|
+
* version stamp is what guarantees that.
|
|
200
|
+
*
|
|
201
|
+
* Nothing is backfilled. Every tool is "first seen" on the next start,
|
|
202
|
+
* which is the honest state: this gateway has no record of what the
|
|
203
|
+
* upstream said before today, and inventing one would mean reporting no
|
|
204
|
+
* change where it cannot know.
|
|
205
|
+
*/
|
|
206
|
+
db.exec(TOOL_PIN_SCHEMA);
|
|
207
|
+
}
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
name: "publish the changed contracts a gateway is holding, so the console can review them",
|
|
211
|
+
up(db) {
|
|
212
|
+
/*
|
|
213
|
+
* A new table, for the same reason as the inventory: the console is a
|
|
214
|
+
* separate process and could only learn of a changed tool from the audit
|
|
215
|
+
* log, which says that it changed and not what to accept.
|
|
216
|
+
*
|
|
217
|
+
* Nothing is backfilled. A change is pending only in a running gateway's
|
|
218
|
+
* memory, and the next one to start will publish what it finds.
|
|
219
|
+
*/
|
|
220
|
+
db.exec(TOOL_CHANGE_SCHEMA);
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
name: "let a remembered approval cover only the arguments that were approved",
|
|
225
|
+
up(db) {
|
|
226
|
+
/*
|
|
227
|
+
* "Approve and don't ask again" pinned each value the approved call
|
|
228
|
+
* sent, and let through any argument it had not sent: approving a push
|
|
229
|
+
* of a branch released the same push with `force: true` (R2 N4). A
|
|
230
|
+
* closed grant covers only calls whose every argument it names.
|
|
231
|
+
*
|
|
232
|
+
* 0 on every existing row: a grant written before this was made under
|
|
233
|
+
* the old meaning, and closing it now would quietly change what someone
|
|
234
|
+
* already agreed to -- in the safe direction, but a grant that stops
|
|
235
|
+
* matching is one more question nobody expected. Remembered grants last
|
|
236
|
+
* at most a day by default, so the old ones age out.
|
|
237
|
+
*/
|
|
238
|
+
addColumn(db, "standing_grants", "closed", "INTEGER NOT NULL DEFAULT 0");
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
];
|
|
242
|
+
export const LATEST_SCHEMA_VERSION = MIGRATIONS.length;
|
|
243
|
+
function userVersion(db) {
|
|
244
|
+
const row = db.prepare("PRAGMA user_version").get();
|
|
245
|
+
return Number(row?.user_version ?? 0);
|
|
246
|
+
}
|
|
247
|
+
/** Whether this database predates versioning, as opposed to being empty. */
|
|
248
|
+
function hasLegacyTables(db) {
|
|
249
|
+
const row = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='approvals'").get();
|
|
250
|
+
return row !== undefined;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Brings one database up to `LATEST_SCHEMA_VERSION`.
|
|
254
|
+
*
|
|
255
|
+
* `createLatest` is the caller's "fresh database" path: the current
|
|
256
|
+
* `CREATE TABLE` statements, at their newest shape. It matters that a fresh
|
|
257
|
+
* database takes that path rather than replaying history, because replaying
|
|
258
|
+
* would mean every migration has to stay runnable against the schema of its
|
|
259
|
+
* own era for ever.
|
|
260
|
+
*
|
|
261
|
+
* The awkward case this has to get right is that a brand-new database and one
|
|
262
|
+
* written before versioning existed *both* report version 0. They are told
|
|
263
|
+
* apart by looking for a table only the second would have.
|
|
264
|
+
*
|
|
265
|
+
* Concurrency is real here, not theoretical: the default store path is shared,
|
|
266
|
+
* and a console, a CLI and a server may all open it at once. Each step takes
|
|
267
|
+
* the write lock with BEGIN IMMEDIATE and re-reads the version inside the
|
|
268
|
+
* transaction, so a process that loses the race finds the work already done
|
|
269
|
+
* and skips it rather than applying it twice.
|
|
270
|
+
*/
|
|
271
|
+
export function migrate(db, createLatest) {
|
|
272
|
+
const current = userVersion(db);
|
|
273
|
+
if (current > LATEST_SCHEMA_VERSION) {
|
|
274
|
+
/*
|
|
275
|
+
* A newer build has already upgraded this file. Carrying on would mean
|
|
276
|
+
* querying columns this binary does not know about, or worse, writing rows
|
|
277
|
+
* that the newer one will misread. Refusing to open is the honest failure:
|
|
278
|
+
* it names the cause, where `no such column` three calls later does not.
|
|
279
|
+
*/
|
|
280
|
+
throw new Error(`approvals database is at schema version ${current}, but this build only understands ` +
|
|
281
|
+
`${LATEST_SCHEMA_VERSION}. It was written by a newer hardened-mcp; upgrade this one.`);
|
|
282
|
+
}
|
|
283
|
+
if (current === 0 && !hasLegacyTables(db)) {
|
|
284
|
+
db.exec("BEGIN IMMEDIATE");
|
|
285
|
+
try {
|
|
286
|
+
// Re-check under the lock: another process may have created it meanwhile.
|
|
287
|
+
if (userVersion(db) === 0 && !hasLegacyTables(db)) {
|
|
288
|
+
createLatest(db);
|
|
289
|
+
db.exec(`PRAGMA user_version = ${LATEST_SCHEMA_VERSION}`);
|
|
290
|
+
}
|
|
291
|
+
db.exec("COMMIT");
|
|
292
|
+
}
|
|
293
|
+
catch (err) {
|
|
294
|
+
db.exec("ROLLBACK");
|
|
295
|
+
throw err;
|
|
296
|
+
}
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
for (let version = current; version < LATEST_SCHEMA_VERSION; version++) {
|
|
300
|
+
const migration = MIGRATIONS[version];
|
|
301
|
+
db.exec("BEGIN IMMEDIATE");
|
|
302
|
+
try {
|
|
303
|
+
if (userVersion(db) !== version) {
|
|
304
|
+
// Someone else applied this step while we waited for the lock.
|
|
305
|
+
db.exec("ROLLBACK");
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
migration.up(db);
|
|
309
|
+
db.exec(`PRAGMA user_version = ${version + 1}`);
|
|
310
|
+
db.exec("COMMIT");
|
|
311
|
+
}
|
|
312
|
+
catch (err) {
|
|
313
|
+
db.exec("ROLLBACK");
|
|
314
|
+
throw new Error(`approvals database migration ${version + 1} ("${migration.name}") failed: ` +
|
|
315
|
+
`${err.message}`);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
package/dist/pins.d.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type { Effect } from "./policy.js";
|
|
2
|
+
/**
|
|
3
|
+
* What a server advertised the last time this gateway looked.
|
|
4
|
+
*
|
|
5
|
+
* A tool is not a fixed thing. The same name can come back with a different
|
|
6
|
+
* description, a wider schema, or an annotation it did not have before -- and
|
|
7
|
+
* a description is read by the model as instructions, which makes changing it
|
|
8
|
+
* the whole of the "rug pull": be harmless while being wrapped, become
|
|
9
|
+
* something else afterwards. The injection scan catches text that looks like
|
|
10
|
+
* an instruction. It cannot catch a tool that was honest yesterday.
|
|
11
|
+
*
|
|
12
|
+
* Nothing detects that from a single listing, so the previous one is written
|
|
13
|
+
* down and compared. A pin is per gateway, because two gateways may front
|
|
14
|
+
* different servers under the same tool name, and it survives a restart,
|
|
15
|
+
* which is the only condition under which it is worth anything at all.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* The rule id on the audit record for a tool whose advertised contract
|
|
19
|
+
* changed. A structural finding of this gateway's own, like HMCP005 for
|
|
20
|
+
* injected text, so a reader can search for it.
|
|
21
|
+
*/
|
|
22
|
+
export declare const TOOL_CHANGED_RULE = "HMCP011";
|
|
23
|
+
/**
|
|
24
|
+
* The rule id for a finding in what a tool *returned*, as opposed to what it
|
|
25
|
+
* advertised. Separate from HMCP005 so a reader can tell the two apart: one
|
|
26
|
+
* is about the server, the other about whatever the server was asked to
|
|
27
|
+
* fetch.
|
|
28
|
+
*/
|
|
29
|
+
export declare const RESULT_SCAN_RULE = "HMCP012";
|
|
30
|
+
export declare const TOOL_PIN_SCHEMA = "\nCREATE TABLE IF NOT EXISTS tool_pins (\n component TEXT NOT NULL,\n tool TEXT NOT NULL,\n digest TEXT NOT NULL,\n /*\n * The contract itself, canonicalised, so a change can be described rather\n * than merely detected. Two digests tell a reviewer nothing they can act\n * on, and \"the description changed\" tells them where to look. One version\n * only: this is a pin, not a history of every wording an upstream ever had.\n */\n contract TEXT,\n pinned_at INTEGER NOT NULL,\n pinned_by TEXT NOT NULL,\n PRIMARY KEY (component, tool)\n)\n";
|
|
31
|
+
/** The parts of a tool that the model is told, and that a pin covers. */
|
|
32
|
+
export interface ToolContract {
|
|
33
|
+
readonly effect: Effect | null;
|
|
34
|
+
/** The upstream's own text, before any rewriting: the pin is on what it said. */
|
|
35
|
+
readonly description: string;
|
|
36
|
+
readonly inputSchema: unknown;
|
|
37
|
+
readonly annotations: unknown;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A stable digest of one tool's advertised contract.
|
|
41
|
+
*
|
|
42
|
+
* Canonicalised first, because a server that serialises its schema in a
|
|
43
|
+
* different key order between releases has not changed anything, and a pin
|
|
44
|
+
* that reported that would be noise -- and a warning nobody reads protects
|
|
45
|
+
* nothing.
|
|
46
|
+
*/
|
|
47
|
+
export declare function contractDigest(contract: ToolContract): string;
|
|
48
|
+
export type PinOutcome = {
|
|
49
|
+
readonly kind: "first-seen";
|
|
50
|
+
} | {
|
|
51
|
+
readonly kind: "unchanged";
|
|
52
|
+
} | {
|
|
53
|
+
readonly kind: "changed";
|
|
54
|
+
readonly was: string;
|
|
55
|
+
/** The contract as it was pinned, when this build recorded it. */
|
|
56
|
+
readonly wasContract: ToolContract | undefined;
|
|
57
|
+
readonly pinnedAt: number;
|
|
58
|
+
readonly pinnedBy: string;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Which parts of a contract differ, for a reader.
|
|
62
|
+
*
|
|
63
|
+
* Two digests tell a reviewer nothing they can act on. "description" and
|
|
64
|
+
* "input schema" tell them where to look, and whether to care.
|
|
65
|
+
*/
|
|
66
|
+
export declare function describeContractChange(before: ToolContract, after: ToolContract): string[];
|
|
67
|
+
/**
|
|
68
|
+
* Changes a running gateway is holding, published so the console can review
|
|
69
|
+
* them.
|
|
70
|
+
*
|
|
71
|
+
* `checkPins` finds a changed contract in the gateway's own memory, and until
|
|
72
|
+
* this table existed that was the only place the finding lived outside the
|
|
73
|
+
* audit log. The console is a different process and never speaks MCP to an
|
|
74
|
+
* upstream, so it had no way to show *what* changed or to accept it without a
|
|
75
|
+
* terminal. The gateway now writes what it found here, beside its inventory,
|
|
76
|
+
* and replaces the set wholesale each time it publishes.
|
|
77
|
+
*
|
|
78
|
+
* The contract a reviewer accepts is the one stored in this row -- the
|
|
79
|
+
* gateway's own look at the upstream -- never one a client supplies. Both
|
|
80
|
+
* contracts are kept because under `on_tool_change: warn` the pin has already
|
|
81
|
+
* advanced, so the previous text exists nowhere else.
|
|
82
|
+
*/
|
|
83
|
+
export declare const TOOL_CHANGE_SCHEMA = "\nCREATE TABLE IF NOT EXISTS tool_changes (\n component TEXT NOT NULL,\n tool TEXT NOT NULL,\n pid INTEGER NOT NULL,\n digest TEXT NOT NULL,\n contract TEXT NOT NULL,\n was_digest TEXT NOT NULL,\n was_contract TEXT,\n pinned_at INTEGER NOT NULL,\n pinned_by TEXT NOT NULL,\n withheld INTEGER NOT NULL,\n detected_at INTEGER NOT NULL,\n PRIMARY KEY (component, tool)\n);\n";
|
|
84
|
+
/** One changed tool, as a gateway hands it to the store. */
|
|
85
|
+
export interface ToolChangeInput {
|
|
86
|
+
readonly tool: string;
|
|
87
|
+
/** What the upstream advertises now. */
|
|
88
|
+
readonly digest: string;
|
|
89
|
+
readonly contract: ToolContract;
|
|
90
|
+
/** The pin it was compared against. */
|
|
91
|
+
readonly was: string;
|
|
92
|
+
readonly wasContract: ToolContract | undefined;
|
|
93
|
+
readonly pinnedAt: number;
|
|
94
|
+
readonly pinnedBy: string;
|
|
95
|
+
/** True when `on_tool_change: deny` took the tool off the menu. */
|
|
96
|
+
readonly withheld: boolean;
|
|
97
|
+
}
|
|
98
|
+
/** One published change, as a reader gets it back. */
|
|
99
|
+
export interface ToolChangeRow {
|
|
100
|
+
readonly component: string;
|
|
101
|
+
readonly tool: string;
|
|
102
|
+
readonly pid: number;
|
|
103
|
+
readonly digest: string;
|
|
104
|
+
readonly contract: ToolContract;
|
|
105
|
+
readonly wasDigest: string;
|
|
106
|
+
readonly wasContract: ToolContract | null;
|
|
107
|
+
readonly pinnedAt: number;
|
|
108
|
+
readonly pinnedBy: string;
|
|
109
|
+
readonly withheld: boolean;
|
|
110
|
+
readonly detectedAt: number;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The audit rule id for a person accepting a changed contract. Distinct from
|
|
114
|
+
* HMCP011, which is the gateway's finding: one record says the tool changed,
|
|
115
|
+
* the other says who looked at it and said it was fine.
|
|
116
|
+
*/
|
|
117
|
+
export declare const PIN_ACCEPT_RULE = "pin.accept";
|
|
118
|
+
/** The `pinned_by` a gateway writes for itself, as against a person. */
|
|
119
|
+
export declare const GATEWAY_PINNER = "gateway";
|
package/dist/pins.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { canonicalJson } from "./canonical.js";
|
|
3
|
+
/**
|
|
4
|
+
* What a server advertised the last time this gateway looked.
|
|
5
|
+
*
|
|
6
|
+
* A tool is not a fixed thing. The same name can come back with a different
|
|
7
|
+
* description, a wider schema, or an annotation it did not have before -- and
|
|
8
|
+
* a description is read by the model as instructions, which makes changing it
|
|
9
|
+
* the whole of the "rug pull": be harmless while being wrapped, become
|
|
10
|
+
* something else afterwards. The injection scan catches text that looks like
|
|
11
|
+
* an instruction. It cannot catch a tool that was honest yesterday.
|
|
12
|
+
*
|
|
13
|
+
* Nothing detects that from a single listing, so the previous one is written
|
|
14
|
+
* down and compared. A pin is per gateway, because two gateways may front
|
|
15
|
+
* different servers under the same tool name, and it survives a restart,
|
|
16
|
+
* which is the only condition under which it is worth anything at all.
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* The rule id on the audit record for a tool whose advertised contract
|
|
20
|
+
* changed. A structural finding of this gateway's own, like HMCP005 for
|
|
21
|
+
* injected text, so a reader can search for it.
|
|
22
|
+
*/
|
|
23
|
+
export const TOOL_CHANGED_RULE = "HMCP011";
|
|
24
|
+
/**
|
|
25
|
+
* The rule id for a finding in what a tool *returned*, as opposed to what it
|
|
26
|
+
* advertised. Separate from HMCP005 so a reader can tell the two apart: one
|
|
27
|
+
* is about the server, the other about whatever the server was asked to
|
|
28
|
+
* fetch.
|
|
29
|
+
*/
|
|
30
|
+
export const RESULT_SCAN_RULE = "HMCP012";
|
|
31
|
+
export const TOOL_PIN_SCHEMA = `
|
|
32
|
+
CREATE TABLE IF NOT EXISTS tool_pins (
|
|
33
|
+
component TEXT NOT NULL,
|
|
34
|
+
tool TEXT NOT NULL,
|
|
35
|
+
digest TEXT NOT NULL,
|
|
36
|
+
/*
|
|
37
|
+
* The contract itself, canonicalised, so a change can be described rather
|
|
38
|
+
* than merely detected. Two digests tell a reviewer nothing they can act
|
|
39
|
+
* on, and "the description changed" tells them where to look. One version
|
|
40
|
+
* only: this is a pin, not a history of every wording an upstream ever had.
|
|
41
|
+
*/
|
|
42
|
+
contract TEXT,
|
|
43
|
+
pinned_at INTEGER NOT NULL,
|
|
44
|
+
pinned_by TEXT NOT NULL,
|
|
45
|
+
PRIMARY KEY (component, tool)
|
|
46
|
+
)
|
|
47
|
+
`;
|
|
48
|
+
/**
|
|
49
|
+
* A stable digest of one tool's advertised contract.
|
|
50
|
+
*
|
|
51
|
+
* Canonicalised first, because a server that serialises its schema in a
|
|
52
|
+
* different key order between releases has not changed anything, and a pin
|
|
53
|
+
* that reported that would be noise -- and a warning nobody reads protects
|
|
54
|
+
* nothing.
|
|
55
|
+
*/
|
|
56
|
+
export function contractDigest(contract) {
|
|
57
|
+
return createHash("sha256")
|
|
58
|
+
.update(canonicalJson({
|
|
59
|
+
effect: contract.effect ?? null,
|
|
60
|
+
description: contract.description,
|
|
61
|
+
inputSchema: contract.inputSchema ?? null,
|
|
62
|
+
annotations: contract.annotations ?? null
|
|
63
|
+
}))
|
|
64
|
+
.digest("hex");
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Which parts of a contract differ, for a reader.
|
|
68
|
+
*
|
|
69
|
+
* Two digests tell a reviewer nothing they can act on. "description" and
|
|
70
|
+
* "input schema" tell them where to look, and whether to care.
|
|
71
|
+
*/
|
|
72
|
+
export function describeContractChange(before, after) {
|
|
73
|
+
const parts = [];
|
|
74
|
+
if (before.effect !== after.effect)
|
|
75
|
+
parts.push("effect");
|
|
76
|
+
if (before.description !== after.description)
|
|
77
|
+
parts.push("description");
|
|
78
|
+
if (canonicalJson(before.inputSchema ?? null) !== canonicalJson(after.inputSchema ?? null)) {
|
|
79
|
+
parts.push("input schema");
|
|
80
|
+
}
|
|
81
|
+
if (canonicalJson(before.annotations ?? null) !== canonicalJson(after.annotations ?? null)) {
|
|
82
|
+
parts.push("annotations");
|
|
83
|
+
}
|
|
84
|
+
return parts;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Changes a running gateway is holding, published so the console can review
|
|
88
|
+
* them.
|
|
89
|
+
*
|
|
90
|
+
* `checkPins` finds a changed contract in the gateway's own memory, and until
|
|
91
|
+
* this table existed that was the only place the finding lived outside the
|
|
92
|
+
* audit log. The console is a different process and never speaks MCP to an
|
|
93
|
+
* upstream, so it had no way to show *what* changed or to accept it without a
|
|
94
|
+
* terminal. The gateway now writes what it found here, beside its inventory,
|
|
95
|
+
* and replaces the set wholesale each time it publishes.
|
|
96
|
+
*
|
|
97
|
+
* The contract a reviewer accepts is the one stored in this row -- the
|
|
98
|
+
* gateway's own look at the upstream -- never one a client supplies. Both
|
|
99
|
+
* contracts are kept because under `on_tool_change: warn` the pin has already
|
|
100
|
+
* advanced, so the previous text exists nowhere else.
|
|
101
|
+
*/
|
|
102
|
+
export const TOOL_CHANGE_SCHEMA = `
|
|
103
|
+
CREATE TABLE IF NOT EXISTS tool_changes (
|
|
104
|
+
component TEXT NOT NULL,
|
|
105
|
+
tool TEXT NOT NULL,
|
|
106
|
+
pid INTEGER NOT NULL,
|
|
107
|
+
digest TEXT NOT NULL,
|
|
108
|
+
contract TEXT NOT NULL,
|
|
109
|
+
was_digest TEXT NOT NULL,
|
|
110
|
+
was_contract TEXT,
|
|
111
|
+
pinned_at INTEGER NOT NULL,
|
|
112
|
+
pinned_by TEXT NOT NULL,
|
|
113
|
+
withheld INTEGER NOT NULL,
|
|
114
|
+
detected_at INTEGER NOT NULL,
|
|
115
|
+
PRIMARY KEY (component, tool)
|
|
116
|
+
);
|
|
117
|
+
`;
|
|
118
|
+
/**
|
|
119
|
+
* The audit rule id for a person accepting a changed contract. Distinct from
|
|
120
|
+
* HMCP011, which is the gateway's finding: one record says the tool changed,
|
|
121
|
+
* the other says who looked at it and said it was fine.
|
|
122
|
+
*/
|
|
123
|
+
export const PIN_ACCEPT_RULE = "pin.accept";
|
|
124
|
+
/** The `pinned_by` a gateway writes for itself, as against a person. */
|
|
125
|
+
export const GATEWAY_PINNER = "gateway";
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { type EgressConfig, type Policy } from "./policy.js";
|
|
2
|
+
import { type LoosenAcknowledged, type PolicyEditRefusal, type PolicyEditStale, type PolicyToolRef } from "./policy-write.js";
|
|
3
|
+
/**
|
|
4
|
+
* Several policy changes as one edit: what installing a server needs.
|
|
5
|
+
*
|
|
6
|
+
* Adding a server from the console touches the policy in three places at
|
|
7
|
+
* once -- its hosts go into `egress.allow`, `egress.methods` gains POST (an MCP
|
|
8
|
+
* call over HTTP is a POST), and a rule pack classifies the tools the test
|
|
9
|
+
* actually saw. Written as three separate edits, a failure between them leaves
|
|
10
|
+
* a policy that lets the gateway reach a server whose tools nothing
|
|
11
|
+
* classified, or classifies tools it cannot reach; and a gateway reloading in
|
|
12
|
+
* between adopts the half-way state. So they are one text edit, validated
|
|
13
|
+
* whole, graded whole and written once.
|
|
14
|
+
*
|
|
15
|
+
* The edit goes through the YAML document rather than a re-serialisation, so
|
|
16
|
+
* the comments people wrote in policy.yaml survive it. Grading reuses
|
|
17
|
+
* `policy-write.ts` (`settingLoosens`, `assessPolicyChange`) so this path can
|
|
18
|
+
* never call something tightening that the settings page calls loosening.
|
|
19
|
+
*/
|
|
20
|
+
/** The egress settings a batch may set directly. `allow` and `methods` only grow, through the `*Add` lists. */
|
|
21
|
+
export type PolicyBatchEgress = Partial<Omit<EgressConfig, "allow" | "methods">>;
|
|
22
|
+
export interface PolicyBatchChanges {
|
|
23
|
+
/** Hosts to add to `egress.allow` (`api.example.com`, `*.example.com`, optionally `:port`). Present ones are skipped. */
|
|
24
|
+
readonly egressAllowAdd?: readonly string[] | undefined;
|
|
25
|
+
/** Methods to add to `egress.methods`, e.g. `["POST"]`. Present ones are skipped. */
|
|
26
|
+
readonly egressMethodsAdd?: readonly string[] | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Egress settings to set, for a server on this computer or the local
|
|
29
|
+
* network: `{ block_private_ips: false, allow_http: true, allow_ip_literals: true }`.
|
|
30
|
+
*/
|
|
31
|
+
readonly egress?: PolicyBatchEgress | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Rules to write, typically a pack's (`applyPack(...).rules`). A rule whose
|
|
34
|
+
* id is already in the policy replaces it in place; a new one is placed just
|
|
35
|
+
* above the first existing rule whose glob matches its tool, so it actually
|
|
36
|
+
* takes effect (rules are first-match-wins), or at the end when none does.
|
|
37
|
+
* A rule for a tool a person already wrote a literal rule for is skipped and
|
|
38
|
+
* reported -- their choice wins over a pack's.
|
|
39
|
+
*/
|
|
40
|
+
readonly rules?: readonly unknown[] | undefined;
|
|
41
|
+
/** One line put above each newly added rule, e.g. "added with GitHub from the catalog". */
|
|
42
|
+
readonly comment?: string | undefined;
|
|
43
|
+
/** The tools a gateway serves (or "Test it" saw), so verdict changes are graded too. */
|
|
44
|
+
readonly tools?: readonly PolicyToolRef[] | undefined;
|
|
45
|
+
}
|
|
46
|
+
export interface PolicyBatchResult {
|
|
47
|
+
/** The new policy.yaml text. Identical to the input when nothing changed. */
|
|
48
|
+
readonly text: string;
|
|
49
|
+
readonly changed: boolean;
|
|
50
|
+
/** Plain sentences, one per thing that lets more through. Empty when nothing loosens. */
|
|
51
|
+
readonly loosens: string[];
|
|
52
|
+
readonly policy: Policy;
|
|
53
|
+
readonly digest: string;
|
|
54
|
+
readonly previousDigest: string;
|
|
55
|
+
readonly egressAdded: string[];
|
|
56
|
+
readonly methodsAdded: string[];
|
|
57
|
+
readonly rulesAdded: string[];
|
|
58
|
+
readonly rulesReplaced: string[];
|
|
59
|
+
/** Rule ids not written, with why. */
|
|
60
|
+
readonly rulesSkipped: {
|
|
61
|
+
id: string;
|
|
62
|
+
why: string;
|
|
63
|
+
}[];
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Applies `changes` to a policy file's text. Pure: reads and writes nothing.
|
|
67
|
+
* Throws `PolicyWriteError` when the text is not a valid policy, a change is
|
|
68
|
+
* invalid, or the edited text would not say exactly what was asked.
|
|
69
|
+
*/
|
|
70
|
+
export declare function applyPolicyBatch(policyText: string, changes: PolicyBatchChanges): PolicyBatchResult;
|
|
71
|
+
export interface PolicyBatchWrite extends PolicyBatchResult {
|
|
72
|
+
readonly kind: "written" | "unchanged";
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Reads policy.yaml, applies the batch, refuses a loosening that was not
|
|
76
|
+
* acknowledged, and writes the result atomically (temp file and rename, so a
|
|
77
|
+
* gateway polling the file never reads half of it).
|
|
78
|
+
*
|
|
79
|
+
* `expectedDigest`, when given, is the digest the person was shown; a policy
|
|
80
|
+
* changed since is `stale` rather than silently edited on top of.
|
|
81
|
+
*/
|
|
82
|
+
export declare function writePolicyBatch(options: {
|
|
83
|
+
readonly policyPath: string;
|
|
84
|
+
readonly changes: PolicyBatchChanges;
|
|
85
|
+
readonly expectedDigest?: string | undefined;
|
|
86
|
+
readonly writesAllowed?: boolean | undefined;
|
|
87
|
+
readonly overridesAllowed?: boolean | undefined;
|
|
88
|
+
readonly acknowledged?: LoosenAcknowledged | undefined;
|
|
89
|
+
}): PolicyBatchWrite | PolicyEditRefusal | PolicyEditStale;
|