hmcp-core 0.0.1 → 0.2.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 +297 -0
- package/dist/audit.js +524 -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 +386 -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/tenant.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { expandPath } from "./policy.js";
|
|
3
|
+
export class TenantError extends Error {
|
|
4
|
+
constructor(message) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = "TenantError";
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
function decodeJwtClaim(token, claim) {
|
|
10
|
+
const parts = token.split(".");
|
|
11
|
+
if (parts.length < 2)
|
|
12
|
+
return undefined;
|
|
13
|
+
try {
|
|
14
|
+
const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
|
|
15
|
+
const value = payload[claim];
|
|
16
|
+
return value === undefined || value === null ? undefined : String(value);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Produces the tenant identifier bound to the running credential. This value is
|
|
24
|
+
* never taken from agent input - that is the whole point of tenant scoping, and
|
|
25
|
+
* why a tenant argument in an agent-facing schema is a finding (HMCP002) rather
|
|
26
|
+
* than a parameter the gateway fills in.
|
|
27
|
+
*
|
|
28
|
+
* Note the `jwt-claim` source reads the claim without verifying the token's
|
|
29
|
+
* signature. The token is our own credential, not attacker-supplied input, so
|
|
30
|
+
* there is nothing to forge here; it is a way to avoid duplicating the tenant
|
|
31
|
+
* id in a second environment variable.
|
|
32
|
+
*/
|
|
33
|
+
export function resolveTenant(config, options = {}) {
|
|
34
|
+
const env = options.env ?? process.env;
|
|
35
|
+
const source = config.source;
|
|
36
|
+
/*
|
|
37
|
+
* A hosted deployment resolves the tenant from the request, so the sources
|
|
38
|
+
* that read their value from somewhere the caller can influence stop being
|
|
39
|
+
* safe. `header` is a value the caller simply types, and `jwt-claim`
|
|
40
|
+
* decodes without checking the signature -- both correct when the value
|
|
41
|
+
* comes from the operator's own environment, both forgeable when it does
|
|
42
|
+
* not. Refusing here makes that an enforced invariant rather than a comment
|
|
43
|
+
* someone has to read.
|
|
44
|
+
*/
|
|
45
|
+
if (options.trust === "request" && (source.kind === "header" || source.kind === "jwt-claim")) {
|
|
46
|
+
throw new TenantError(`tenant.source.kind "${source.kind}" cannot be used where the tenant comes from the request: ` +
|
|
47
|
+
`${source.kind === "header" ? "a caller can set any header" : "the signature is not verified"}. ` +
|
|
48
|
+
`Use kind "jwt-verified", which checks the token's signature, issuer and audience.`);
|
|
49
|
+
}
|
|
50
|
+
switch (source.kind) {
|
|
51
|
+
case "static":
|
|
52
|
+
return source.value;
|
|
53
|
+
case "env": {
|
|
54
|
+
const raw = env[source.name];
|
|
55
|
+
return raw && raw.length > 0 ? raw : undefined;
|
|
56
|
+
}
|
|
57
|
+
case "header": {
|
|
58
|
+
const headers = options.headers ?? {};
|
|
59
|
+
const wanted = source.name.toLowerCase();
|
|
60
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
61
|
+
if (key.toLowerCase() !== wanted)
|
|
62
|
+
continue;
|
|
63
|
+
const v = Array.isArray(value) ? value[0] : value;
|
|
64
|
+
if (v && v.length > 0)
|
|
65
|
+
return v;
|
|
66
|
+
}
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
case "jwt-verified":
|
|
70
|
+
/*
|
|
71
|
+
* Verification is asynchronous -- it may fetch a key set -- and this
|
|
72
|
+
* function is synchronous and on the hot path for every other kind.
|
|
73
|
+
* `TenantVerifier` does it, and the caller hands the result in rather
|
|
74
|
+
* than this quietly returning undefined and looking like "not
|
|
75
|
+
* configured".
|
|
76
|
+
*/
|
|
77
|
+
return options.verified;
|
|
78
|
+
case "jwt-claim": {
|
|
79
|
+
const raw = env[source.token_env];
|
|
80
|
+
if (!raw)
|
|
81
|
+
return undefined;
|
|
82
|
+
// Accept either a bare token, an "Authorization: Bearer x" value, or a
|
|
83
|
+
// path to a file holding the token.
|
|
84
|
+
let token = raw.replace(/^Bearer\s+/i, "").trim();
|
|
85
|
+
if (!token.includes(".") && token.length > 0) {
|
|
86
|
+
try {
|
|
87
|
+
token = readFileSync(expandPath(token), "utf8").trim().replace(/^Bearer\s+/i, "");
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return decodeJwtClaim(token, source.name);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/** Resolves and fails loudly when the policy marks tenant scoping required. */
|
|
98
|
+
export function requireTenant(config, options = {}) {
|
|
99
|
+
const value = resolveTenant(config, options);
|
|
100
|
+
if (value === undefined && config.required) {
|
|
101
|
+
const where = config.source.kind === "static"
|
|
102
|
+
? "static policy value"
|
|
103
|
+
: config.source.kind === "jwt-claim"
|
|
104
|
+
? `claim "${config.source.name}" of the token in ${config.source.token_env}`
|
|
105
|
+
: config.source.kind === "jwt-verified"
|
|
106
|
+
? `verified claim "${config.source.claim}" of the caller's token`
|
|
107
|
+
: `${config.source.kind} "${config.source.name}"`;
|
|
108
|
+
throw new TenantError(`tenant scoping is required but ${config.field} could not be resolved from ${where}. ` +
|
|
109
|
+
`Set it, or set tenant.required to false if this deployment is genuinely single-tenant.`);
|
|
110
|
+
}
|
|
111
|
+
return value;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Builds the server-side values to splice into an upstream request. The agent
|
|
115
|
+
* never supplies these and cannot see them in a tool schema.
|
|
116
|
+
*/
|
|
117
|
+
export function tenantInjection(config, value) {
|
|
118
|
+
const injection = { path: {}, query: {}, body: {}, headers: {} };
|
|
119
|
+
for (const target of config.inject) {
|
|
120
|
+
switch (target) {
|
|
121
|
+
case "path":
|
|
122
|
+
for (const name of [config.field, ...config.aliases])
|
|
123
|
+
injection.path[name] = value;
|
|
124
|
+
break;
|
|
125
|
+
case "query":
|
|
126
|
+
injection.query[config.field] = value;
|
|
127
|
+
break;
|
|
128
|
+
case "body":
|
|
129
|
+
injection.body[config.field] = value;
|
|
130
|
+
break;
|
|
131
|
+
case "header":
|
|
132
|
+
if (config.header_name)
|
|
133
|
+
injection.headers[config.header_name] = value;
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return injection;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* The reserved tenant key for "this deployment has no tenant configured".
|
|
141
|
+
*
|
|
142
|
+
* Empty string rather than NULL, because SQLite permits several NULLs in a
|
|
143
|
+
* non-rowid PRIMARY KEY column — `tool_exposure` would quietly stop being
|
|
144
|
+
* unique. Rather than a sentinel like "-", which a real tenant could be
|
|
145
|
+
* called, because `resolveTenant` already rejects an empty value on both the
|
|
146
|
+
* env and header branches, so no real tenant can ever be "".
|
|
147
|
+
*/
|
|
148
|
+
export const NO_TENANT = "";
|
|
149
|
+
/** The longest tenant key accepted. Generous for an id, short of a payload. */
|
|
150
|
+
const MAX_TENANT_KEY = 256;
|
|
151
|
+
/**
|
|
152
|
+
* Canonicalises a resolved tenant before it is used as a storage key.
|
|
153
|
+
*
|
|
154
|
+
* Single-tenant deployments read this value from their own environment and it
|
|
155
|
+
* is whatever the operator typed. Once a hosted gateway resolves it per
|
|
156
|
+
* request it is attacker-influenced, and a key that can be spelled two ways is
|
|
157
|
+
* a key that can be made to collide or to split: `acme` and `acme ` must not
|
|
158
|
+
* become two scopes, and two different-looking strings must not become one.
|
|
159
|
+
*
|
|
160
|
+
* So: NFC, because the same name composed two ways in Unicode must compare
|
|
161
|
+
* equal; trimmed; length-capped; and restricted to an unambiguous alphabet. A
|
|
162
|
+
* control character has no business in an identifier and is how a value gets
|
|
163
|
+
* smuggled past a log line or a terminal.
|
|
164
|
+
*/
|
|
165
|
+
export function normalizeTenantKey(raw) {
|
|
166
|
+
const value = raw.normalize("NFC").trim();
|
|
167
|
+
if (value.length === 0)
|
|
168
|
+
return NO_TENANT;
|
|
169
|
+
if (value.length > MAX_TENANT_KEY) {
|
|
170
|
+
throw new TenantError(`tenant identifier is ${value.length} characters; the maximum is ${MAX_TENANT_KEY}`);
|
|
171
|
+
}
|
|
172
|
+
if (!/^[A-Za-z0-9._:@-]+$/.test(value)) {
|
|
173
|
+
throw new TenantError(`tenant identifier ${JSON.stringify(raw)} contains characters that are not allowed. ` +
|
|
174
|
+
`Letters, digits and ".", "_", ":", "@", "-" only, so that one tenant cannot be ` +
|
|
175
|
+
`spelled two ways or disguised as another.`);
|
|
176
|
+
}
|
|
177
|
+
return value;
|
|
178
|
+
}
|
|
179
|
+
/** A scope for a deployment with no tenant configured. */
|
|
180
|
+
export function componentScope(component) {
|
|
181
|
+
return { component, tenant: NO_TENANT };
|
|
182
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text that does not look like what it is.
|
|
3
|
+
*
|
|
4
|
+
* Two different problems share this file because every surface that shows
|
|
5
|
+
* untrusted text to a person has both. Some characters render as nothing, so a
|
|
6
|
+
* reviewer reads a description or a tool name and never sees part of it. Some
|
|
7
|
+
* render as a different, familiar character, so a reviewer sees `delete` and
|
|
8
|
+
* the bytes say something else. The scanner needs both to find an injected
|
|
9
|
+
* instruction; the gateway needs both to decide whether two tool names are the
|
|
10
|
+
* same name to a human. Two copies of these tables is how those two come to
|
|
11
|
+
* disagree.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Anything invisible, with no exceptions. For an identifier, where there is no
|
|
15
|
+
* legitimate reason for any of these to appear.
|
|
16
|
+
*/
|
|
17
|
+
export declare const INVISIBLE_ANYWHERE: RegExp;
|
|
18
|
+
/**
|
|
19
|
+
* Anything invisible that is not doing its ordinary job in prose.
|
|
20
|
+
*
|
|
21
|
+
* A presentation selector directly after a pictograph (`⚠️`), or inside a
|
|
22
|
+
* keycap sequence (`1️⃣`), is how an emoji is spelled, and flagging it would
|
|
23
|
+
* make every description with a warning sign a finding. Anywhere else -- after
|
|
24
|
+
* a letter, inside a word -- it has nothing to select and is only there to
|
|
25
|
+
* make two strings differ invisibly.
|
|
26
|
+
*/
|
|
27
|
+
export declare const INVISIBLE_IN_PROSE: RegExp;
|
|
28
|
+
/** Removes every invisible character, including presentation selectors. */
|
|
29
|
+
export declare function stripInvisibleCharacters(text: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* What a string reads as: compatibility forms unified (fullwidth, mathematical
|
|
32
|
+
* alphabets, ligatures), invisible characters gone, and the look-alike letters
|
|
33
|
+
* above replaced by the Latin letter they are drawn as.
|
|
34
|
+
*
|
|
35
|
+
* For comparison only. Nothing should ever store or display the result: it is
|
|
36
|
+
* a lossy answer to "what would a person take this for", not a canonical form.
|
|
37
|
+
*/
|
|
38
|
+
export declare function readsAs(text: string): string;
|
|
39
|
+
/** True when a string contains a letter that is drawn like a different Latin one. */
|
|
40
|
+
export declare function hasLookalikeLetters(text: string): boolean;
|
package/dist/unicode.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text that does not look like what it is.
|
|
3
|
+
*
|
|
4
|
+
* Two different problems share this file because every surface that shows
|
|
5
|
+
* untrusted text to a person has both. Some characters render as nothing, so a
|
|
6
|
+
* reviewer reads a description or a tool name and never sees part of it. Some
|
|
7
|
+
* render as a different, familiar character, so a reviewer sees `delete` and
|
|
8
|
+
* the bytes say something else. The scanner needs both to find an injected
|
|
9
|
+
* instruction; the gateway needs both to decide whether two tool names are the
|
|
10
|
+
* same name to a human. Two copies of these tables is how those two come to
|
|
11
|
+
* disagree.
|
|
12
|
+
*/
|
|
13
|
+
/*
|
|
14
|
+
* Code points that occupy no space or draw nothing:
|
|
15
|
+
*
|
|
16
|
+
* U+00AD soft hyphen
|
|
17
|
+
* U+034F combining grapheme joiner
|
|
18
|
+
* U+061C Arabic letter mark
|
|
19
|
+
* U+115F, U+1160 Hangul fillers
|
|
20
|
+
* U+17B4, U+17B5 Khmer inherent vowels
|
|
21
|
+
* U+180B-U+180F Mongolian variation selectors and vowel separator
|
|
22
|
+
* U+200B-U+200F zero-width space, joiners, directional marks
|
|
23
|
+
* U+202A-U+202E bidirectional embeddings and overrides
|
|
24
|
+
* U+2060-U+206F word joiner, invisible operators, isolates, deprecated
|
|
25
|
+
* format characters, and the unassigned U+2065
|
|
26
|
+
* U+2800 braille blank
|
|
27
|
+
* U+3164, U+FFA0 Hangul fillers
|
|
28
|
+
* U+FE00-U+FE0D variation selectors (the two emoji ones are separate)
|
|
29
|
+
* U+FEFF byte-order mark / zero-width no-break space
|
|
30
|
+
* U+FFF9-U+FFFB interlinear annotation controls
|
|
31
|
+
* U+1BCA0-U+1BCA3 shorthand format controls
|
|
32
|
+
* U+1D173-U+1D17A musical format controls
|
|
33
|
+
* U+E0000-U+E0FFF the Tags block and the supplementary variation
|
|
34
|
+
* selectors. Tags are the important one: a complete copy
|
|
35
|
+
* of ASCII that renders as nothing and that a model reads
|
|
36
|
+
* as fluently as the visible kind.
|
|
37
|
+
*/
|
|
38
|
+
const INVISIBLE = "\\u00AD\\u034F\\u061C\\u115F\\u1160\\u17B4\\u17B5\\u180B-\\u180F\\u200B-\\u200F\\u202A-\\u202E" +
|
|
39
|
+
"\\u2060-\\u206F\\u2800\\u3164\\uFE00-\\uFE0D\\uFEFF\\uFFA0\\uFFF9-\\uFFFB" +
|
|
40
|
+
"\\u{1BCA0}-\\u{1BCA3}\\u{1D173}-\\u{1D17A}\\u{E0000}-\\u{E0FFF}";
|
|
41
|
+
/** U+FE0E and U+FE0F: text and emoji presentation selectors. */
|
|
42
|
+
const PRESENTATION = "\\uFE0E\\uFE0F";
|
|
43
|
+
/**
|
|
44
|
+
* Anything invisible, with no exceptions. For an identifier, where there is no
|
|
45
|
+
* legitimate reason for any of these to appear.
|
|
46
|
+
*/
|
|
47
|
+
export const INVISIBLE_ANYWHERE = new RegExp(`[${INVISIBLE}${PRESENTATION}]`, "u");
|
|
48
|
+
/**
|
|
49
|
+
* Anything invisible that is not doing its ordinary job in prose.
|
|
50
|
+
*
|
|
51
|
+
* A presentation selector directly after a pictograph (`⚠️`), or inside a
|
|
52
|
+
* keycap sequence (`1️⃣`), is how an emoji is spelled, and flagging it would
|
|
53
|
+
* make every description with a warning sign a finding. Anywhere else -- after
|
|
54
|
+
* a letter, inside a word -- it has nothing to select and is only there to
|
|
55
|
+
* make two strings differ invisibly.
|
|
56
|
+
*/
|
|
57
|
+
export const INVISIBLE_IN_PROSE = new RegExp(`[${INVISIBLE}]|(?<!\\p{Extended_Pictographic})[${PRESENTATION}](?!\\u20E3)`, "u");
|
|
58
|
+
const INVISIBLE_ALL = new RegExp(`[${INVISIBLE}${PRESENTATION}]`, "gu");
|
|
59
|
+
/** Removes every invisible character, including presentation selectors. */
|
|
60
|
+
export function stripInvisibleCharacters(text) {
|
|
61
|
+
return text.replace(INVISIBLE_ALL, "");
|
|
62
|
+
}
|
|
63
|
+
/*
|
|
64
|
+
* Letters from other scripts that are drawn like a Latin letter in ordinary
|
|
65
|
+
* fonts. Deliberately the short, well-known list rather than Unicode's whole
|
|
66
|
+
* confusables table: these are the ones that survive a glance, and a table of
|
|
67
|
+
* thousands would fold genuinely different words together.
|
|
68
|
+
*/
|
|
69
|
+
const LOOKALIKES = {
|
|
70
|
+
// Cyrillic, lower case
|
|
71
|
+
"а": "a", "е": "e", "о": "o", "р": "p", "с": "c", "у": "y", "х": "x",
|
|
72
|
+
"і": "i", "ј": "j", "ѕ": "s", "һ": "h", "ԁ": "d", "ԛ": "q", "ԝ": "w",
|
|
73
|
+
"к": "k", "ё": "e",
|
|
74
|
+
// Cyrillic, upper case
|
|
75
|
+
"А": "A", "В": "B", "Е": "E", "К": "K", "М": "M", "Н": "H", "О": "O",
|
|
76
|
+
"Р": "P", "С": "C", "Т": "T", "У": "Y", "Х": "X", "І": "I", "Ј": "J",
|
|
77
|
+
"Ѕ": "S",
|
|
78
|
+
// Greek, lower case
|
|
79
|
+
"α": "a", "ο": "o", "ρ": "p", "ν": "v", "ι": "i", "κ": "k", "τ": "t",
|
|
80
|
+
"υ": "u", "χ": "x", "ε": "e",
|
|
81
|
+
// Greek, upper case
|
|
82
|
+
"Α": "A", "Β": "B", "Ε": "E", "Ζ": "Z", "Η": "H", "Ι": "I", "Κ": "K",
|
|
83
|
+
"Μ": "M", "Ν": "N", "Ο": "O", "Ρ": "P", "Τ": "T", "Υ": "Y", "Χ": "X",
|
|
84
|
+
// Latin letters that are not the ASCII one
|
|
85
|
+
"ı": "i", "ɑ": "a", "ɡ": "g"
|
|
86
|
+
};
|
|
87
|
+
const LOOKALIKE_PATTERN = new RegExp(`[${Object.keys(LOOKALIKES).join("")}]`, "g");
|
|
88
|
+
/**
|
|
89
|
+
* What a string reads as: compatibility forms unified (fullwidth, mathematical
|
|
90
|
+
* alphabets, ligatures), invisible characters gone, and the look-alike letters
|
|
91
|
+
* above replaced by the Latin letter they are drawn as.
|
|
92
|
+
*
|
|
93
|
+
* For comparison only. Nothing should ever store or display the result: it is
|
|
94
|
+
* a lossy answer to "what would a person take this for", not a canonical form.
|
|
95
|
+
*/
|
|
96
|
+
export function readsAs(text) {
|
|
97
|
+
return stripInvisibleCharacters(text.normalize("NFKC")).replace(LOOKALIKE_PATTERN, (ch) => LOOKALIKES[ch] ?? ch);
|
|
98
|
+
}
|
|
99
|
+
/** True when a string contains a letter that is drawn like a different Latin one. */
|
|
100
|
+
export function hasLookalikeLetters(text) {
|
|
101
|
+
LOOKALIKE_PATTERN.lastIndex = 0;
|
|
102
|
+
return LOOKALIKE_PATTERN.test(text);
|
|
103
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two bounds that are worst to write by hand.
|
|
3
|
+
*
|
|
4
|
+
* `within` and `host` exist because the alternative is a regular expression,
|
|
5
|
+
* and the regular expressions people write for these are nearly right. A path
|
|
6
|
+
* bound built on `startsWith` admits `/srv/project-evil` for a bound of
|
|
7
|
+
* `/srv/project`; a host bound built on `includes` admits
|
|
8
|
+
* `api.example.com.evil.test`. Both read as correct and both are open.
|
|
9
|
+
*
|
|
10
|
+
* Kept in a file of its own so the decision engine's import graph stays as it
|
|
11
|
+
* was: `node:path` is pure string work with no filesystem access, which is
|
|
12
|
+
* what lets the engine go on running in a browser.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* How an absolute path is turned into the one the filesystem really opens.
|
|
16
|
+
*
|
|
17
|
+
* A folder chosen as `/var/folders/x/my files` is `/private/var/folders/x/my
|
|
18
|
+
* files` on macOS, and that second spelling is the one a filesystem server
|
|
19
|
+
* reports and an AI then sends. Compared as strings, every call in the folder
|
|
20
|
+
* is "outside" it (UT4 N8). Compared as real paths, both spellings are the
|
|
21
|
+
* same folder, and a symlink inside the folder that points out of it is
|
|
22
|
+
* outside, as it really is.
|
|
23
|
+
*
|
|
24
|
+
* Kept as a hook so this file needs no filesystem access: the Node side
|
|
25
|
+
* installs one (`within-real.ts`, loaded by the package entry), and without
|
|
26
|
+
* one paths are compared as written.
|
|
27
|
+
*/
|
|
28
|
+
export type RealPath = (absolute: string) => string;
|
|
29
|
+
export declare function setRealPathResolver(fn: RealPath | null): void;
|
|
30
|
+
/** Why a value is not inside the bound (one directory, or any of several), or `null` when it is. */
|
|
31
|
+
export declare function pathWithin(value: unknown, within: string | readonly string[]): string | null;
|
|
32
|
+
/** Why a URL's host is not allowed, or `null` when it is. */
|
|
33
|
+
export declare function urlHostAllowed(value: unknown, hosts: readonly string[]): string | null;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { isAbsolute, resolve, sep } from "node:path";
|
|
2
|
+
let realPath = null;
|
|
3
|
+
export function setRealPathResolver(fn) {
|
|
4
|
+
realPath = fn;
|
|
5
|
+
}
|
|
6
|
+
/** Why a value is not inside the bound (one directory, or any of several), or `null` when it is. */
|
|
7
|
+
export function pathWithin(value, within) {
|
|
8
|
+
if (typeof value !== "string") {
|
|
9
|
+
return `must be a string path to be bounded by "within", and is ${value === null ? "null" : typeof value}`;
|
|
10
|
+
}
|
|
11
|
+
/*
|
|
12
|
+
* A NUL byte ends the string inside a C library, so what a filesystem call
|
|
13
|
+
* opens can be a prefix of what was checked here. Nothing legitimate sends
|
|
14
|
+
* one.
|
|
15
|
+
*/
|
|
16
|
+
if (value.includes("\u0000"))
|
|
17
|
+
return `contains a NUL byte, which would truncate the path inside the upstream`;
|
|
18
|
+
/*
|
|
19
|
+
* `file:///etc/shadow` is a path wearing a scheme. Resolving it as a
|
|
20
|
+
* relative path would put it under the bound and let it through, while the
|
|
21
|
+
* upstream may well parse the URL and open the real path.
|
|
22
|
+
*/
|
|
23
|
+
if (/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(value) && !/^[a-zA-Z]:[\\/]/.test(value)) {
|
|
24
|
+
return `looks like a URL rather than a path, and "within" bounds paths`;
|
|
25
|
+
}
|
|
26
|
+
const dirs = typeof within === "string" ? [within] : within;
|
|
27
|
+
const problems = [];
|
|
28
|
+
for (const dir of dirs) {
|
|
29
|
+
const problem = pathWithinOne(value, dir);
|
|
30
|
+
if (problem === null)
|
|
31
|
+
return null;
|
|
32
|
+
problems.push(problem);
|
|
33
|
+
}
|
|
34
|
+
return problems.join("; and it ");
|
|
35
|
+
}
|
|
36
|
+
function pathWithinOne(value, within) {
|
|
37
|
+
const written = resolve(within);
|
|
38
|
+
/*
|
|
39
|
+
* An absolute value is judged as it is. A relative one is resolved against
|
|
40
|
+
* the bound rather than against the process's working directory, which the
|
|
41
|
+
* caller does not know and which would make the same call mean different
|
|
42
|
+
* things in different places.
|
|
43
|
+
*/
|
|
44
|
+
const lexical = isAbsolute(value) ? resolve(value) : resolve(written, value);
|
|
45
|
+
const base = realPath ? realPath(written) : written;
|
|
46
|
+
const target = realPath ? realPath(lexical) : lexical;
|
|
47
|
+
if (target === base)
|
|
48
|
+
return null;
|
|
49
|
+
// The separator is what makes this different from `startsWith`: without it
|
|
50
|
+
// `/srv/project-evil` is inside `/srv/project`.
|
|
51
|
+
if (target.startsWith(base.endsWith(sep) ? base : `${base}${sep}`))
|
|
52
|
+
return null;
|
|
53
|
+
return `resolves to ${JSON.stringify(target)}, which is outside ${JSON.stringify(base)}`;
|
|
54
|
+
}
|
|
55
|
+
/** Why a URL's host is not allowed, or `null` when it is. */
|
|
56
|
+
export function urlHostAllowed(value, hosts) {
|
|
57
|
+
if (typeof value !== "string") {
|
|
58
|
+
return `must be a URL string to be bounded by "host", and is ${value === null ? "null" : typeof value}`;
|
|
59
|
+
}
|
|
60
|
+
let url;
|
|
61
|
+
try {
|
|
62
|
+
url = new URL(value);
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return `is not a URL, so its host cannot be bounded (did you mean to send an absolute URL?)`;
|
|
66
|
+
}
|
|
67
|
+
/*
|
|
68
|
+
* A scheme with no host has nothing for this bound to check, and letting it
|
|
69
|
+
* through would be a bound that is not doing its job: `file:` reads a local
|
|
70
|
+
* path and `javascript:` is not a fetch at all.
|
|
71
|
+
*/
|
|
72
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
73
|
+
return `uses the ${url.protocol} scheme; "host" bounds http and https URLs`;
|
|
74
|
+
}
|
|
75
|
+
// `URL` has already lowercased, punycoded and stripped any userinfo, so
|
|
76
|
+
// `https://api.example.com@evil.test/` arrives here as `evil.test`.
|
|
77
|
+
const host = url.hostname.replace(/\.$/, "");
|
|
78
|
+
for (const allowed of hosts) {
|
|
79
|
+
const want = allowed.toLowerCase().replace(/\.$/, "");
|
|
80
|
+
if (want.startsWith(".")) {
|
|
81
|
+
// ".example.com" is the domain and everything under it.
|
|
82
|
+
const domain = want.slice(1);
|
|
83
|
+
if (host === domain || host.endsWith(`.${domain}`))
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
else if (host === want) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return `host ${JSON.stringify(host)} is not one of ${JSON.stringify(hosts)}`;
|
|
91
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The real path of `absolute`, through every symlink on the way to it. A path
|
|
3
|
+
* that doesn't exist yet (a file about to be written) is the real path of its
|
|
4
|
+
* deepest existing ancestor with the rest added back, so `<link>/new.txt` is
|
|
5
|
+
* judged where it will really be created.
|
|
6
|
+
*/
|
|
7
|
+
export declare function realPathDeepest(absolute: string): string;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { realpathSync } from "node:fs";
|
|
2
|
+
import { basename, dirname, join } from "node:path";
|
|
3
|
+
import { setRealPathResolver } from "./within-host.js";
|
|
4
|
+
/**
|
|
5
|
+
* The real path of `absolute`, through every symlink on the way to it. A path
|
|
6
|
+
* that doesn't exist yet (a file about to be written) is the real path of its
|
|
7
|
+
* deepest existing ancestor with the rest added back, so `<link>/new.txt` is
|
|
8
|
+
* judged where it will really be created.
|
|
9
|
+
*/
|
|
10
|
+
export function realPathDeepest(absolute) {
|
|
11
|
+
const rest = [];
|
|
12
|
+
let at = absolute;
|
|
13
|
+
for (;;) {
|
|
14
|
+
try {
|
|
15
|
+
const real = realpathSync.native(at);
|
|
16
|
+
return rest.length ? join(real, ...rest.reverse()) : real;
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
const up = dirname(at);
|
|
20
|
+
// The root itself can't be resolved: compare as written.
|
|
21
|
+
if (up === at)
|
|
22
|
+
return absolute;
|
|
23
|
+
rest.push(basename(at));
|
|
24
|
+
at = up;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
setRealPathResolver(realPathDeepest);
|
package/package.json
CHANGED
|
@@ -1,16 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hmcp-core",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Policy schema, decision engine, approvals, standing grants, egress guard and hash-chained audit log for hardened-mcp.",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"author": "Aditya Das",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/aoutix/hardened-mcp.git"
|
|
9
|
+
"url": "git+https://github.com/aoutix/hardened-mcp.git",
|
|
10
|
+
"directory": "packages/core"
|
|
10
11
|
},
|
|
11
12
|
"homepage": "https://github.com/aoutix/hardened-mcp#readme",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"mcp",
|
|
15
|
+
"model-context-protocol",
|
|
16
|
+
"security",
|
|
17
|
+
"policy",
|
|
18
|
+
"gateway",
|
|
19
|
+
"audit"
|
|
20
|
+
],
|
|
21
|
+
"type": "module",
|
|
22
|
+
"main": "./dist/index.js",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": "./dist/index.js"
|
|
26
|
+
},
|
|
12
27
|
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"!dist/**/*.map",
|
|
13
30
|
"README.md",
|
|
14
31
|
"LICENSE"
|
|
15
|
-
]
|
|
32
|
+
],
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=22.19.0"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"yaml": "^2.7.0",
|
|
41
|
+
"zod": "^4.0.0",
|
|
42
|
+
"undici": "^8.0.0",
|
|
43
|
+
"jose": "^6.1.3"
|
|
44
|
+
}
|
|
16
45
|
}
|