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
package/dist/redact.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Redaction runs before anything is written to the audit log or shown in an
|
|
3
|
+
* approval prompt. The same patterns back scanner rule HMCP004, which looks for
|
|
4
|
+
* secrets that leaked into an API spec.
|
|
5
|
+
*/
|
|
6
|
+
import { setOwn } from "./canonical.js";
|
|
7
|
+
/** Argument/field names whose values are never recorded. */
|
|
8
|
+
export const SECRET_KEY_PATTERN = /(^|[_\-.])(pass|passwd|password|passwords|passphrase|pwd|secret|secrets|token|tokens|apikey|api_key|accesskey|access_key|authorization|auth|credential|credentials|privatekey|private_key|session|sessionid|cookie|bearer|signature|otp|pin|ssn|cardnumber|card_number|creditcard|credit_card|cvv|cvc|client_secret|refresh_token|id_token)([_\-.]|$)/i;
|
|
9
|
+
/**
|
|
10
|
+
* A key with its camelCase boundaries spelled out: `userPassword` becomes
|
|
11
|
+
* `user_Password`, `AWSSecretAccessKey` becomes `AWS_Secret_Access_Key`.
|
|
12
|
+
*
|
|
13
|
+
* The pattern above looks for a secret word set off by a separator, and
|
|
14
|
+
* camelCase has none. Most JSON APIs use it, so without this `password` was
|
|
15
|
+
* redacted and `userPassword` was written to the audit log in clear.
|
|
16
|
+
*/
|
|
17
|
+
function withWordBreaks(key) {
|
|
18
|
+
return key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2");
|
|
19
|
+
}
|
|
20
|
+
/** True when an argument or field name says its value is a secret. */
|
|
21
|
+
export function isSecretKeyName(key) {
|
|
22
|
+
return SECRET_KEY_PATTERN.test(key) || SECRET_KEY_PATTERN.test(withWordBreaks(key));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Value shapes that are secrets regardless of the key they sit under. Ordered
|
|
26
|
+
* most-specific first so the reported label is the useful one.
|
|
27
|
+
*/
|
|
28
|
+
export const SECRET_VALUE_PATTERNS = [
|
|
29
|
+
{ id: "private-key", label: "pem private key", regex: /-----BEGIN [A-Z ]*PRIVATE KEY-----/ },
|
|
30
|
+
{ id: "aws-access-key", label: "aws access key id", regex: /\b(?:AKIA|ASIA|ABIA|ACCA)[0-9A-Z]{16}\b/ },
|
|
31
|
+
{ id: "github-token", label: "github token", regex: /\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{36,}\b/ },
|
|
32
|
+
{ id: "github-pat", label: "github fine-grained pat", regex: /\bgithub_pat_[A-Za-z0-9_]{22,}\b/ },
|
|
33
|
+
{ id: "slack-token", label: "slack token", regex: /\bxox[abprs]-[A-Za-z0-9-]{10,}\b/ },
|
|
34
|
+
{ id: "google-api-key", label: "google api key", regex: /\bAIza[0-9A-Za-z_-]{35}\b/ },
|
|
35
|
+
{ id: "stripe-key", label: "stripe secret key", regex: /\b(?:sk|rk)_(?:live|test)_[A-Za-z0-9]{16,}\b/ },
|
|
36
|
+
{ id: "anthropic-key", label: "anthropic-style key", regex: /\bsk-ant-[A-Za-z0-9_-]{20,}\b/ },
|
|
37
|
+
{ id: "openai-key", label: "openai-style key", regex: /\bsk-(?:proj-)?[A-Za-z0-9_-]{20,}\b/ },
|
|
38
|
+
{ id: "jwt", label: "jwt", regex: /\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b/ },
|
|
39
|
+
{ id: "basic-auth-url", label: "credentials in url", regex: /\b[a-z][a-z0-9+.-]*:\/\/[^/\s:@]+:[^/\s@]+@/i },
|
|
40
|
+
{ id: "bearer-header", label: "bearer header", regex: /\bBearer\s+[A-Za-z0-9._~+/-]{16,}=*/ },
|
|
41
|
+
{
|
|
42
|
+
id: "high-entropy-hex",
|
|
43
|
+
label: "long hex string",
|
|
44
|
+
// Requires both a digit and a letter: a real hash or token has both, while
|
|
45
|
+
// a long run of one repeated character is almost never a secret.
|
|
46
|
+
regex: /\b(?=[0-9a-fA-F]{40,}\b)(?=[0-9a-fA-F]*[0-9])(?=[0-9a-fA-F]*[a-fA-F])[0-9a-fA-F]{40,}\b/
|
|
47
|
+
}
|
|
48
|
+
];
|
|
49
|
+
/** Finds secret-shaped substrings. Used by the scanner; returns [] for clean text. */
|
|
50
|
+
export function findSecrets(text) {
|
|
51
|
+
const hits = [];
|
|
52
|
+
for (const p of SECRET_VALUE_PATTERNS) {
|
|
53
|
+
const m = p.regex.exec(text);
|
|
54
|
+
if (m)
|
|
55
|
+
hits.push({ patternId: p.id, label: p.label, match: m[0] });
|
|
56
|
+
}
|
|
57
|
+
return hits;
|
|
58
|
+
}
|
|
59
|
+
const DEFAULTS = { maxStringLength: 512, maxDepth: 12, maxArrayLength: 100 };
|
|
60
|
+
function isSecretKey(key, extra) {
|
|
61
|
+
return extra.has(key.toLowerCase()) || isSecretKeyName(key);
|
|
62
|
+
}
|
|
63
|
+
function redactString(value, maxLen) {
|
|
64
|
+
for (const p of SECRET_VALUE_PATTERNS) {
|
|
65
|
+
if (p.regex.test(value))
|
|
66
|
+
return `[redacted:${p.id}]`;
|
|
67
|
+
}
|
|
68
|
+
if (value.length > maxLen) {
|
|
69
|
+
return `${value.slice(0, maxLen)}…[truncated ${value.length - maxLen} chars]`;
|
|
70
|
+
}
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Deep copy with secrets removed. Never mutates the input, tolerates cycles,
|
|
75
|
+
* and always returns something JSON-serializable - it sits on the path to the
|
|
76
|
+
* audit log, so it must not be able to throw.
|
|
77
|
+
*/
|
|
78
|
+
export function redact(value, options = {}) {
|
|
79
|
+
const maxStringLength = options.maxStringLength ?? DEFAULTS.maxStringLength;
|
|
80
|
+
const maxDepth = options.maxDepth ?? DEFAULTS.maxDepth;
|
|
81
|
+
const maxArrayLength = options.maxArrayLength ?? DEFAULTS.maxArrayLength;
|
|
82
|
+
const extra = new Set((options.extraKeys ?? []).map((k) => k.toLowerCase()));
|
|
83
|
+
const seen = new WeakSet();
|
|
84
|
+
const walk = (node, depth) => {
|
|
85
|
+
if (node === null || node === undefined)
|
|
86
|
+
return node ?? null;
|
|
87
|
+
const t = typeof node;
|
|
88
|
+
if (t === "string")
|
|
89
|
+
return redactString(node, maxStringLength);
|
|
90
|
+
if (t === "number")
|
|
91
|
+
return Number.isFinite(node) ? node : String(node);
|
|
92
|
+
if (t === "boolean")
|
|
93
|
+
return node;
|
|
94
|
+
if (t === "bigint")
|
|
95
|
+
return node.toString();
|
|
96
|
+
if (t === "function" || t === "symbol")
|
|
97
|
+
return `[${t}]`;
|
|
98
|
+
if (node instanceof Date)
|
|
99
|
+
return node.toISOString();
|
|
100
|
+
if (depth >= maxDepth)
|
|
101
|
+
return "[redacted:max-depth]";
|
|
102
|
+
const obj = node;
|
|
103
|
+
if (seen.has(obj))
|
|
104
|
+
return "[redacted:circular]";
|
|
105
|
+
seen.add(obj);
|
|
106
|
+
try {
|
|
107
|
+
if (Array.isArray(node)) {
|
|
108
|
+
const items = node.slice(0, maxArrayLength).map((v) => walk(v, depth + 1));
|
|
109
|
+
if (node.length > maxArrayLength)
|
|
110
|
+
items.push(`[truncated ${node.length - maxArrayLength} items]`);
|
|
111
|
+
return items;
|
|
112
|
+
}
|
|
113
|
+
if (node instanceof Map)
|
|
114
|
+
return walk(Object.fromEntries(node), depth);
|
|
115
|
+
if (node instanceof Set)
|
|
116
|
+
return walk([...node], depth);
|
|
117
|
+
const out = {};
|
|
118
|
+
for (const [key, v] of Object.entries(node)) {
|
|
119
|
+
// Not `out[key] = …`: a `__proto__` key has to survive into what a
|
|
120
|
+
// reviewer is shown. See `setOwn`.
|
|
121
|
+
setOwn(out, key, isSecretKey(key, extra) ? "[redacted:key]" : walk(v, depth + 1));
|
|
122
|
+
}
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
seen.delete(obj);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
return walk(value, 0);
|
|
130
|
+
}
|
|
131
|
+
/** Convenience wrapper for argument objects. */
|
|
132
|
+
export function redactArgs(args, extraKeys = []) {
|
|
133
|
+
const result = redact(args ?? {}, { extraKeys });
|
|
134
|
+
return result && typeof result === "object" && !Array.isArray(result)
|
|
135
|
+
? result
|
|
136
|
+
: {};
|
|
137
|
+
}
|
|
138
|
+
/** The longest piece of free text kept in a record. */
|
|
139
|
+
const MAX_TEXT_LENGTH = 2000;
|
|
140
|
+
/** Shorter than this and a value is too likely to be an ordinary word to blank everywhere. */
|
|
141
|
+
const MIN_SECRET_LENGTH = 4;
|
|
142
|
+
/**
|
|
143
|
+
* Redacts free text that was written by someone else and may quote a secret.
|
|
144
|
+
*
|
|
145
|
+
* `redact` works on structure: it knows which key a value sits under. An
|
|
146
|
+
* error message has no structure, and the commonest way one leaks is an
|
|
147
|
+
* upstream's validation error that quotes the value it rejected -- the same
|
|
148
|
+
* value `redactArgs` has just removed from the field beside it. So this takes
|
|
149
|
+
* the arguments too, and blanks the value of every secret-named argument
|
|
150
|
+
* wherever it is quoted, before looking for anything secret-*shaped*.
|
|
151
|
+
*
|
|
152
|
+
* Unlike `redact` on a string, a match replaces only itself: the rest of the
|
|
153
|
+
* message is what makes the record useful.
|
|
154
|
+
*/
|
|
155
|
+
export function redactText(text, options = {}) {
|
|
156
|
+
const extra = new Set((options.extraKeys ?? []).map((k) => k.toLowerCase()));
|
|
157
|
+
const secrets = new Set();
|
|
158
|
+
const seen = new WeakSet();
|
|
159
|
+
const collect = (node, underSecretKey, depth) => {
|
|
160
|
+
if (depth > DEFAULTS.maxDepth)
|
|
161
|
+
return;
|
|
162
|
+
if (typeof node === "string" || typeof node === "number") {
|
|
163
|
+
const value = String(node);
|
|
164
|
+
if (underSecretKey && value.length >= MIN_SECRET_LENGTH)
|
|
165
|
+
secrets.add(value);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (node === null || typeof node !== "object" || seen.has(node))
|
|
169
|
+
return;
|
|
170
|
+
seen.add(node);
|
|
171
|
+
if (Array.isArray(node)) {
|
|
172
|
+
for (const v of node)
|
|
173
|
+
collect(v, underSecretKey, depth + 1);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
for (const [key, v] of Object.entries(node)) {
|
|
177
|
+
collect(v, underSecretKey || isSecretKey(key, extra), depth + 1);
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
collect(options.args, false, 0);
|
|
181
|
+
let out = text;
|
|
182
|
+
// Longest first, so a value that contains a shorter one is removed whole.
|
|
183
|
+
for (const value of [...secrets].sort((a, b) => b.length - a.length)) {
|
|
184
|
+
out = out.split(value).join("[redacted:key]");
|
|
185
|
+
}
|
|
186
|
+
for (const p of SECRET_VALUE_PATTERNS) {
|
|
187
|
+
out = out.replace(new RegExp(p.regex.source, `${p.regex.flags.replace("g", "")}g`), `[redacted:${p.id}]`);
|
|
188
|
+
}
|
|
189
|
+
const max = options.maxLength ?? MAX_TEXT_LENGTH;
|
|
190
|
+
return out.length > max ? `${out.slice(0, max)}…[truncated ${out.length - max} chars]` : out;
|
|
191
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const RegistryEntrySchema: z.ZodObject<{
|
|
3
|
+
id: z.ZodString;
|
|
4
|
+
kind: z.ZodLiteral<"gateway">;
|
|
5
|
+
label: z.ZodString;
|
|
6
|
+
config_path: z.ZodString;
|
|
7
|
+
component: z.ZodOptional<z.ZodString>;
|
|
8
|
+
added_at: z.ZodString;
|
|
9
|
+
}, z.core.$strict>;
|
|
10
|
+
export type RegistryEntry = z.infer<typeof RegistryEntrySchema>;
|
|
11
|
+
export declare const RegistrySchema: z.ZodObject<{
|
|
12
|
+
version: z.ZodLiteral<1>;
|
|
13
|
+
servers: z.ZodDefault<z.ZodArray<z.ZodObject<{
|
|
14
|
+
id: z.ZodString;
|
|
15
|
+
kind: z.ZodLiteral<"gateway">;
|
|
16
|
+
label: z.ZodString;
|
|
17
|
+
config_path: z.ZodString;
|
|
18
|
+
component: z.ZodOptional<z.ZodString>;
|
|
19
|
+
added_at: z.ZodString;
|
|
20
|
+
}, z.core.$strict>>>;
|
|
21
|
+
}, z.core.$strict>;
|
|
22
|
+
export type Registry = z.infer<typeof RegistrySchema>;
|
|
23
|
+
export declare function defaultRegistryPath(): string;
|
|
24
|
+
export declare class RegistryError extends Error {
|
|
25
|
+
readonly issues?: readonly {
|
|
26
|
+
path: string;
|
|
27
|
+
message: string;
|
|
28
|
+
}[] | undefined;
|
|
29
|
+
constructor(message: string, issues?: readonly {
|
|
30
|
+
path: string;
|
|
31
|
+
message: string;
|
|
32
|
+
}[] | undefined);
|
|
33
|
+
}
|
|
34
|
+
export declare function parseRegistry(raw: unknown, source?: string): Registry;
|
|
35
|
+
/** A missing registry is an empty one, not an error. */
|
|
36
|
+
export declare function readRegistry(path?: string): Registry;
|
|
37
|
+
/**
|
|
38
|
+
* Writes via a temporary file and a rename, so a crash mid-write cannot leave a
|
|
39
|
+
* half-written registry that then fails to parse and hides every server.
|
|
40
|
+
*/
|
|
41
|
+
export declare function writeRegistry(registry: Registry, path?: string): void;
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { expandPath } from "./policy.js";
|
|
5
|
+
/**
|
|
6
|
+
* The set of servers a console knows about.
|
|
7
|
+
*
|
|
8
|
+
* Paths enter the system only here, through an explicit registration that is
|
|
9
|
+
* validated when it is made. Nothing else in a console takes a filesystem path
|
|
10
|
+
* from a caller, which is what keeps a read route from being talked into
|
|
11
|
+
* opening an arbitrary file.
|
|
12
|
+
*/
|
|
13
|
+
const ID = /^[a-z0-9][a-z0-9_-]{0,63}$/;
|
|
14
|
+
export const RegistryEntrySchema = z
|
|
15
|
+
.object({
|
|
16
|
+
id: z.string().regex(ID, "an id is lower-case letters, digits, dashes and underscores"),
|
|
17
|
+
kind: z.literal("gateway"),
|
|
18
|
+
label: z.string().min(1),
|
|
19
|
+
config_path: z.string().min(1),
|
|
20
|
+
/** Overrides the audit `component` used to attribute records. */
|
|
21
|
+
component: z.string().min(1).optional(),
|
|
22
|
+
added_at: z.string()
|
|
23
|
+
})
|
|
24
|
+
.strict();
|
|
25
|
+
export const RegistrySchema = z
|
|
26
|
+
.object({
|
|
27
|
+
version: z.literal(1),
|
|
28
|
+
servers: z.array(RegistryEntrySchema).default([])
|
|
29
|
+
})
|
|
30
|
+
.strict()
|
|
31
|
+
.superRefine((reg, ctx) => {
|
|
32
|
+
// Mirrors the duplicate-rule-id check in PolicySchema: a repeated id would
|
|
33
|
+
// make which server you were looking at depend on array order.
|
|
34
|
+
const seen = new Set();
|
|
35
|
+
for (const [index, entry] of reg.servers.entries()) {
|
|
36
|
+
if (seen.has(entry.id)) {
|
|
37
|
+
ctx.addIssue({ code: "custom", path: ["servers", index, "id"], message: `duplicate server id "${entry.id}"` });
|
|
38
|
+
}
|
|
39
|
+
seen.add(entry.id);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
export function defaultRegistryPath() {
|
|
43
|
+
return expandPath(process.env["HMCP_REGISTRY"] ?? "~/.hmcp/servers.json");
|
|
44
|
+
}
|
|
45
|
+
export class RegistryError extends Error {
|
|
46
|
+
issues;
|
|
47
|
+
constructor(message, issues) {
|
|
48
|
+
super(message);
|
|
49
|
+
this.issues = issues;
|
|
50
|
+
this.name = "RegistryError";
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The entry kinds this build understands. An entry of any other kind is
|
|
55
|
+
* skipped rather than rejected -- see `parseRegistry`.
|
|
56
|
+
*/
|
|
57
|
+
const KNOWN_KINDS = new Set(["gateway"]);
|
|
58
|
+
/**
|
|
59
|
+
* Drops entries whose `kind` this build does not understand.
|
|
60
|
+
*
|
|
61
|
+
* The registry is long-lived and shared between versions: `~/.hmcp/servers.json`
|
|
62
|
+
* outlives any one build, and it held `kind: "generated"` entries until the
|
|
63
|
+
* generator was removed. A strict parse of the whole file would make one stale
|
|
64
|
+
* entry hide every other server -- the console would come up with nothing and
|
|
65
|
+
* blame the file rather than the entry. So an unrecognised kind costs its own
|
|
66
|
+
* row and a line on stderr, not the registry.
|
|
67
|
+
*
|
|
68
|
+
* Note this is deliberately only about `kind`. An entry of a *known* kind that
|
|
69
|
+
* is malformed is still an error: that is a file someone wrote wrongly now,
|
|
70
|
+
* not one written correctly by a different version.
|
|
71
|
+
*/
|
|
72
|
+
function dropUnknownKinds(raw, source) {
|
|
73
|
+
if (typeof raw !== "object" || raw === null)
|
|
74
|
+
return raw;
|
|
75
|
+
const servers = raw.servers;
|
|
76
|
+
if (!Array.isArray(servers))
|
|
77
|
+
return raw;
|
|
78
|
+
const kept = [];
|
|
79
|
+
const skipped = [];
|
|
80
|
+
for (const entry of servers) {
|
|
81
|
+
const kind = entry?.kind;
|
|
82
|
+
if (typeof kind === "string" && !KNOWN_KINDS.has(kind)) {
|
|
83
|
+
const id = entry.id;
|
|
84
|
+
skipped.push(`${typeof id === "string" ? id : "<no id>"} (kind "${kind}")`);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
kept.push(entry);
|
|
88
|
+
}
|
|
89
|
+
if (skipped.length === 0)
|
|
90
|
+
return raw;
|
|
91
|
+
process.stderr.write(`hmcp: ignoring ${skipped.length} registry ${skipped.length === 1 ? "entry" : "entries"} ` +
|
|
92
|
+
`of an unknown kind in ${source}: ${skipped.join(", ")}\n`);
|
|
93
|
+
return { ...raw, servers: kept };
|
|
94
|
+
}
|
|
95
|
+
export function parseRegistry(raw, source = "<inline>") {
|
|
96
|
+
const result = RegistrySchema.safeParse(dropUnknownKinds(raw, source));
|
|
97
|
+
if (!result.success) {
|
|
98
|
+
const issues = result.error.issues.map((i) => ({ path: i.path.join(".") || "(root)", message: i.message }));
|
|
99
|
+
throw new RegistryError(`invalid server registry ${source}:\n${issues.map((i) => ` ${i.path}: ${i.message}`).join("\n")}`, issues);
|
|
100
|
+
}
|
|
101
|
+
return result.data;
|
|
102
|
+
}
|
|
103
|
+
/** A missing registry is an empty one, not an error. */
|
|
104
|
+
export function readRegistry(path = defaultRegistryPath()) {
|
|
105
|
+
const full = expandPath(path);
|
|
106
|
+
if (!existsSync(full))
|
|
107
|
+
return { version: 1, servers: [] };
|
|
108
|
+
return parseRegistry(JSON.parse(readFileSync(full, "utf8")), full);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Writes via a temporary file and a rename, so a crash mid-write cannot leave a
|
|
112
|
+
* half-written registry that then fails to parse and hides every server.
|
|
113
|
+
*/
|
|
114
|
+
export function writeRegistry(registry, path = defaultRegistryPath()) {
|
|
115
|
+
const full = expandPath(path);
|
|
116
|
+
parseRegistry(registry, full);
|
|
117
|
+
mkdirSync(dirname(full), { recursive: true });
|
|
118
|
+
const tmp = join(dirname(full), `.${Date.now()}.servers.json.tmp`);
|
|
119
|
+
writeFileSync(tmp, JSON.stringify(registry, null, 2) + "\n", { encoding: "utf8", mode: 0o600 });
|
|
120
|
+
renameSync(tmp, full);
|
|
121
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Defaults for a remembered approval, in a file of their own because both the
|
|
3
|
+
* approval broker (which names them in its prompt) and the code that builds
|
|
4
|
+
* the grant need them, and those two already depend on each other.
|
|
5
|
+
*/
|
|
6
|
+
/** How long a remembered approval lasts unless the caller says otherwise. */
|
|
7
|
+
export declare const REMEMBER_DEFAULT_TTL_SECONDS: number;
|
|
8
|
+
/** How many calls it releases unless the caller says otherwise. */
|
|
9
|
+
export declare const REMEMBER_DEFAULT_USES = 20;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Defaults for a remembered approval, in a file of their own because both the
|
|
3
|
+
* approval broker (which names them in its prompt) and the code that builds
|
|
4
|
+
* the grant need them, and those two already depend on each other.
|
|
5
|
+
*/
|
|
6
|
+
/** How long a remembered approval lasts unless the caller says otherwise. */
|
|
7
|
+
export const REMEMBER_DEFAULT_TTL_SECONDS = 60 * 60;
|
|
8
|
+
/** How many calls it releases unless the caller says otherwise. */
|
|
9
|
+
export const REMEMBER_DEFAULT_USES = 20;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { AuditLog } from "./audit.js";
|
|
2
|
+
import type { ApprovalRow, ScopedApprovals } from "./approvals.js";
|
|
3
|
+
import { type StandingGrantDraft, type StandingGrantRow } from "./grants.js";
|
|
4
|
+
import type { ArgConstraint, Effect, Policy } from "./policy.js";
|
|
5
|
+
/**
|
|
6
|
+
* "Approve, and allow calls like this for a while."
|
|
7
|
+
*
|
|
8
|
+
* An approval covers one exact call. That is the right default and it is also
|
|
9
|
+
* why the same question comes back a minute later, and a person asked the same
|
|
10
|
+
* thing ten times stops reading it. Remembering turns their answer into a
|
|
11
|
+
* standing grant -- but one built from the call they were just shown rather
|
|
12
|
+
* than from a pattern they had to write:
|
|
13
|
+
*
|
|
14
|
+
* - this tool, by its exact name;
|
|
15
|
+
* - for a stated time and a stated number of calls;
|
|
16
|
+
* - inside every bound the governing rule already sets;
|
|
17
|
+
* - and where the call broke one of those bounds, relaxed only as far as the
|
|
18
|
+
* call that was approved. Someone who approved a transfer of 3,000 against
|
|
19
|
+
* a limit of 500 has pre-approved transfers up to 3,000, not transfers.
|
|
20
|
+
*
|
|
21
|
+
* Two things cannot be remembered at all. A tool nothing has classified has
|
|
22
|
+
* to be classified first, because a grant is checked against an effect. And
|
|
23
|
+
* a destructive tool stays in front of a person every time: making deletes
|
|
24
|
+
* routine is the one thing this should never be a shortcut to.
|
|
25
|
+
*/
|
|
26
|
+
export { REMEMBER_DEFAULT_TTL_SECONDS, REMEMBER_DEFAULT_USES } from "./remember-defaults.js";
|
|
27
|
+
export declare class RememberError extends Error {
|
|
28
|
+
constructor(message: string);
|
|
29
|
+
}
|
|
30
|
+
export interface RememberInput {
|
|
31
|
+
readonly tool: string;
|
|
32
|
+
readonly effect: Effect | null;
|
|
33
|
+
/**
|
|
34
|
+
* The call's arguments as far as they are known. From a stored approval
|
|
35
|
+
* these are the redacted ones, which is fine: only the arguments that broke
|
|
36
|
+
* a bound are read, and one of those that the record does not actually hold
|
|
37
|
+
* is refused rather than guessed.
|
|
38
|
+
*/
|
|
39
|
+
readonly args: Record<string, unknown>;
|
|
40
|
+
/** Which arguments broke a bound: concrete paths, or the bound's own path. */
|
|
41
|
+
readonly heldArgs: readonly string[];
|
|
42
|
+
/** The bounds of the rule that governs this tool, if it sets any. */
|
|
43
|
+
readonly ruleArgs?: Record<string, ArgConstraint> | undefined;
|
|
44
|
+
readonly ttlSeconds: number;
|
|
45
|
+
/** null for no cap on calls within the time. */
|
|
46
|
+
readonly maxUses: number | null;
|
|
47
|
+
readonly createdBy: string;
|
|
48
|
+
/** For the grant's reason. */
|
|
49
|
+
readonly approvalId?: string | undefined;
|
|
50
|
+
readonly now?: number | undefined;
|
|
51
|
+
}
|
|
52
|
+
/** The grant an approved call would be remembered as, or a `RememberError` saying why not. */
|
|
53
|
+
export declare function rememberDraft(input: RememberInput): StandingGrantDraft;
|
|
54
|
+
export interface CreateRemembered {
|
|
55
|
+
readonly scoped: ScopedApprovals;
|
|
56
|
+
/** Where the grant's creation is recorded. Without one, nothing is created. */
|
|
57
|
+
readonly audit: AuditLog;
|
|
58
|
+
readonly draft: StandingGrantDraft;
|
|
59
|
+
readonly approvalId: string;
|
|
60
|
+
/** How it was asked for, in prose: "hmcp approve", "the approval prompt". */
|
|
61
|
+
readonly via?: string | undefined;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Creates the grant and records it. No pre-approval exists unaudited: if the
|
|
65
|
+
* record cannot be written the grant is withdrawn, the same rule the console
|
|
66
|
+
* applies to a grant made by hand.
|
|
67
|
+
*/
|
|
68
|
+
export declare function createRememberedGrant(options: CreateRemembered): StandingGrantRow;
|
|
69
|
+
export interface RememberOnApprove {
|
|
70
|
+
/** The scope the held call's row lives in, which is where the grant is created. */
|
|
71
|
+
readonly scoped: ScopedApprovals;
|
|
72
|
+
/** Where the grant's creation is recorded. */
|
|
73
|
+
readonly audit: AuditLog;
|
|
74
|
+
readonly row: ApprovalRow;
|
|
75
|
+
/** The policy as it is now, for the bounds of the rule that held the call. */
|
|
76
|
+
readonly policy: Policy;
|
|
77
|
+
readonly ttlSeconds: number;
|
|
78
|
+
readonly maxUses: number | null;
|
|
79
|
+
readonly actor: string;
|
|
80
|
+
/** How it was asked for, in prose: "hmcp approve", "the console". */
|
|
81
|
+
readonly via: string;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Remembers an approval: builds the grant from the held call and creates it.
|
|
85
|
+
*
|
|
86
|
+
* Shared by `hmcp approve --remember` and the console, so the two cannot come
|
|
87
|
+
* to disagree about what remembering a call may release. It does not decide
|
|
88
|
+
* the approval: the caller does that next, so a grant that cannot be written
|
|
89
|
+
* stops before anything changes. Throws `RememberError` for every refusal.
|
|
90
|
+
*/
|
|
91
|
+
export declare function rememberOnApprove(options: RememberOnApprove): StandingGrantRow;
|