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/decide.d.ts
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import type { ArgConstraint, Effect, Policy, TenantConfig } from "./policy.js";
|
|
2
|
+
/**
|
|
3
|
+
* `reason` is what the audit log records. `agentReason`, where a decision sets
|
|
4
|
+
* one, is what the caller is told instead.
|
|
5
|
+
*
|
|
6
|
+
* They are usually the same and the field is usually absent. It exists because
|
|
7
|
+
* one of them is not: a refusal naming the operator who switched a tool off,
|
|
8
|
+
* and the free text they typed, is the right thing to keep in the record and
|
|
9
|
+
* the wrong thing to hand a model. Defaulting `agentReason` to `reason` keeps
|
|
10
|
+
* every other decision unchanged, so the split costs nothing where it does not
|
|
11
|
+
* matter.
|
|
12
|
+
*/
|
|
13
|
+
interface DecisionBase {
|
|
14
|
+
readonly effect: Effect | null;
|
|
15
|
+
readonly ruleId: string;
|
|
16
|
+
readonly reason: string;
|
|
17
|
+
readonly agentReason?: string;
|
|
18
|
+
/**
|
|
19
|
+
* The individual bounds this call failed, when a bound is what produced the
|
|
20
|
+
* decision. Carried separately from `reason` so a reviewer can be shown
|
|
21
|
+
* which bound was exceeded and by how much, rather than one concatenated
|
|
22
|
+
* sentence -- and because `args_redacted` on a held call may not contain the
|
|
23
|
+
* bounded value at all.
|
|
24
|
+
*/
|
|
25
|
+
readonly violations?: readonly string[];
|
|
26
|
+
/**
|
|
27
|
+
* Which arguments those violations are about, as concrete paths
|
|
28
|
+
* (`transfer.amount`, `items[3].qty`), or the bound's own path where the
|
|
29
|
+
* argument was absent.
|
|
30
|
+
*
|
|
31
|
+
* For whatever decides whether something other than a human may release the
|
|
32
|
+
* hold: a standing grant has to have bounded these itself.
|
|
33
|
+
*/
|
|
34
|
+
readonly heldArgs?: readonly string[];
|
|
35
|
+
}
|
|
36
|
+
export type Decision = ({
|
|
37
|
+
readonly kind: "allow";
|
|
38
|
+
} & DecisionBase) | ({
|
|
39
|
+
readonly kind: "approve";
|
|
40
|
+
} & DecisionBase) | ({
|
|
41
|
+
readonly kind: "deny";
|
|
42
|
+
} & DecisionBase);
|
|
43
|
+
export interface TenantContext {
|
|
44
|
+
/** The tenant bound to the running credential. */
|
|
45
|
+
readonly expected: string | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* A tenant value present in the agent-supplied arguments. For generated
|
|
48
|
+
* servers this should always be absent, because the field is stripped from
|
|
49
|
+
* the agent-facing schema; its presence here is a defense-in-depth check that
|
|
50
|
+
* matters most for the gateway, where schemas are not ours to control.
|
|
51
|
+
*/
|
|
52
|
+
readonly supplied?: string | undefined;
|
|
53
|
+
}
|
|
54
|
+
export interface DecideInput {
|
|
55
|
+
readonly tool: string;
|
|
56
|
+
/** Declared effect from a manifest or upstream annotation; undefined means unclassified. */
|
|
57
|
+
readonly effect?: Effect | undefined;
|
|
58
|
+
readonly args?: Record<string, unknown> | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* There is no call to judge: describe what this tool's posture is, rather
|
|
61
|
+
* than what would happen to a call carrying no arguments.
|
|
62
|
+
*
|
|
63
|
+
* The console asks exactly this question for every tool it lists, and the
|
|
64
|
+
* two are not the same question. A rule bounding `amount` refuses a call
|
|
65
|
+
* that omits it, because the upstream would apply its own default -- but
|
|
66
|
+
* reporting a bounded tool as "refused" in a listing where no arguments
|
|
67
|
+
* exist describes a tool nobody can call, which is false. With this set,
|
|
68
|
+
* bounds are not weighed and the caller is expected to say separately that
|
|
69
|
+
* the tool is bounded.
|
|
70
|
+
*/
|
|
71
|
+
readonly argsUnknown?: boolean | undefined;
|
|
72
|
+
readonly policy: Policy;
|
|
73
|
+
readonly tenant?: TenantContext | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* An exposure override from the console has switched this tool off.
|
|
76
|
+
*
|
|
77
|
+
* Looked up by the caller rather than here, because that is a read from the
|
|
78
|
+
* approvals database and `decide()` stays pure and synchronous. It can only
|
|
79
|
+
* ever refuse: there is no value of this that permits a call the rules below
|
|
80
|
+
* would have refused.
|
|
81
|
+
*/
|
|
82
|
+
readonly disabled?: boolean | undefined;
|
|
83
|
+
/** Why it was switched off, for the refusal handed back to the agent. */
|
|
84
|
+
readonly disabledReason?: string | undefined;
|
|
85
|
+
}
|
|
86
|
+
export declare const UNCLASSIFIED_RULE = "defaults.on_unclassified";
|
|
87
|
+
/**
|
|
88
|
+
* What the caller is told when a bound refuses or holds a call. Deliberately
|
|
89
|
+
* says nothing about which bound or what the limit is.
|
|
90
|
+
*/
|
|
91
|
+
export declare const BOUNDS_AGENT_NOTICE = "arguments are outside the bounds policy sets for this tool";
|
|
92
|
+
/**
|
|
93
|
+
* The same, when what was wrong is only that a path is outside the folders the
|
|
94
|
+
* person shared: said in those words, so the AI can tell them plainly (UT4 N3).
|
|
95
|
+
* It still names neither the path nor the folders.
|
|
96
|
+
*/
|
|
97
|
+
export declare const OUTSIDE_FOLDER_AGENT_NOTICE = "that is outside the folders the person chose to share with this server";
|
|
98
|
+
/**
|
|
99
|
+
* What the caller is told when a tool has no effect classification, which is
|
|
100
|
+
* the one refusal in this product that is nobody's decision -- it is the
|
|
101
|
+
* absence of one. So it is the one that should say how to make a decision.
|
|
102
|
+
*/
|
|
103
|
+
export declare function unclassifiedAgentReason(tool: string): string;
|
|
104
|
+
/** Rules written from a ready-made rule pack, as opposed to a person's own. */
|
|
105
|
+
export declare function isPackRule(id: string): boolean;
|
|
106
|
+
/**
|
|
107
|
+
* Resolves the effective classification. A matching rule that carries an
|
|
108
|
+
* `effect` reclassifies the tool - that is how a `POST /search` gets moved into
|
|
109
|
+
* the read bucket deliberately, by a named rule, rather than by a heuristic.
|
|
110
|
+
*/
|
|
111
|
+
export declare function resolveEffect(input: DecideInput): Effect | null;
|
|
112
|
+
export interface ConstraintViolation {
|
|
113
|
+
/** The concrete path the violation is about: `items[3].qty`, not `items[].qty`. */
|
|
114
|
+
readonly arg: string;
|
|
115
|
+
readonly message: string;
|
|
116
|
+
/** Set when the value was a path outside its `within` folders. */
|
|
117
|
+
readonly outsideFolder?: true;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* One value written so that two values get the same text exactly when they
|
|
121
|
+
* are the same call: object keys in sorted order, types kept apart (`5` and
|
|
122
|
+
* `"5"`, `true` and `1`), and a list of objects or lists compared as a
|
|
123
|
+
* multiset -- its items sorted -- because a batch of records sent in another
|
|
124
|
+
* order is the same batch. A list holding any plain value keeps its order:
|
|
125
|
+
* `["cp", "a", "b"]` and `["cp", "b", "a"]` copy in opposite directions.
|
|
126
|
+
*
|
|
127
|
+
* Recursive on purpose. A value nested past the stack throws a `RangeError`,
|
|
128
|
+
* and the caller treats a throw as "not covered" (it fails closed).
|
|
129
|
+
*/
|
|
130
|
+
export declare function comparableValue(value: unknown): string;
|
|
131
|
+
/** True when `value` is a list holding exactly the approved items (see `comparableValue`). */
|
|
132
|
+
export declare function sameItems(value: unknown, approved: readonly unknown[]): boolean;
|
|
133
|
+
/**
|
|
134
|
+
* Checks a call's arguments against a rule's bounds.
|
|
135
|
+
*
|
|
136
|
+
* Each key in `constraints` is an argument path, so a bound can reach a nested
|
|
137
|
+
* or repeated value. A path that fans out is checked at every place it matches,
|
|
138
|
+
* and the violation names the place rather than the pattern.
|
|
139
|
+
*/
|
|
140
|
+
export declare function checkArgConstraints(args: Record<string, unknown>, constraints: Record<string, ArgConstraint>,
|
|
141
|
+
/** Argument names whose values must not appear in a violation message. */
|
|
142
|
+
redactKeys?: readonly string[]): ConstraintViolation[];
|
|
143
|
+
/**
|
|
144
|
+
* Arguments no constraint claims. This is what makes a bound a closed world
|
|
145
|
+
* rather than a note about one field: with `amount` bounded and nothing said
|
|
146
|
+
* about `force`, an open world passes `force: true` straight through.
|
|
147
|
+
*
|
|
148
|
+
* A constraint on a container claims everything beneath it, so bounding
|
|
149
|
+
* `items` with `maxItems` does not leave every element unclaimed.
|
|
150
|
+
*/
|
|
151
|
+
export declare function findUnknownArgs(args: Record<string, unknown>, constraints: Record<string, ArgConstraint>): string[];
|
|
152
|
+
/** Collects the tenant-identifying names a policy cares about. */
|
|
153
|
+
export declare function tenantFieldNames(tenant: TenantConfig): string[];
|
|
154
|
+
/**
|
|
155
|
+
* Pulls an agent-supplied tenant value out of an argument object, matching the
|
|
156
|
+
* canonical field name or any alias, case-insensitively.
|
|
157
|
+
*/
|
|
158
|
+
export declare function extractSuppliedTenant(args: Record<string, unknown> | undefined, tenant: TenantConfig): string | undefined;
|
|
159
|
+
/**
|
|
160
|
+
* The single decision point. Pure and synchronous by design: if enforcement
|
|
161
|
+
* ever needed to reach the network to decide, that would be a hole, not a
|
|
162
|
+
* feature. Generated servers and the gateway both route every call through
|
|
163
|
+
* this function, so the two cannot drift apart.
|
|
164
|
+
*/
|
|
165
|
+
export declare function decide(input: DecideInput): Decision;
|
|
166
|
+
/** True when a decision permits the call to reach the upstream. */
|
|
167
|
+
export declare function isPermitted(d: Decision): boolean;
|
|
168
|
+
export {};
|
package/dist/decide.js
ADDED
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
import { globMatch } from "./glob.js";
|
|
2
|
+
import { formatPath, hasWildcard, isAtOrUnder, leafPaths, parseArgPath, resolveArgPath } from "./argpath.js";
|
|
3
|
+
import { EXPOSURE_AGENT_NOTICE, EXPOSURE_RULE } from "./exposure.js";
|
|
4
|
+
import { pathWithin, urlHostAllowed } from "./within-host.js";
|
|
5
|
+
const POSTURE_RULE = "defaults.mode";
|
|
6
|
+
export const UNCLASSIFIED_RULE = "defaults.on_unclassified";
|
|
7
|
+
const TENANT_RULE = "tenant.on_mismatch";
|
|
8
|
+
/**
|
|
9
|
+
* What the caller is told when a bound refuses or holds a call. Deliberately
|
|
10
|
+
* says nothing about which bound or what the limit is.
|
|
11
|
+
*/
|
|
12
|
+
export const BOUNDS_AGENT_NOTICE = "arguments are outside the bounds policy sets for this tool";
|
|
13
|
+
/**
|
|
14
|
+
* The same, when what was wrong is only that a path is outside the folders the
|
|
15
|
+
* person shared: said in those words, so the AI can tell them plainly (UT4 N3).
|
|
16
|
+
* It still names neither the path nor the folders.
|
|
17
|
+
*/
|
|
18
|
+
export const OUTSIDE_FOLDER_AGENT_NOTICE = "that is outside the folders the person chose to share with this server";
|
|
19
|
+
/**
|
|
20
|
+
* What the caller is told when a tool has no effect classification, which is
|
|
21
|
+
* the one refusal in this product that is nobody's decision -- it is the
|
|
22
|
+
* absence of one. So it is the one that should say how to make a decision.
|
|
23
|
+
*/
|
|
24
|
+
export function unclassifiedAgentReason(tool) {
|
|
25
|
+
return (`nothing has said what this tool does, so the toolbox refuses it rather than guessing. ` +
|
|
26
|
+
`The person can say what it does in the hardened-mcp console, on the Tools page; nothing needs to be ` +
|
|
27
|
+
`typed in a terminal. (For developers: hmcp-gateway rule ${tool})`);
|
|
28
|
+
}
|
|
29
|
+
/** Rules written from a ready-made rule pack, as opposed to a person's own. */
|
|
30
|
+
export function isPackRule(id) {
|
|
31
|
+
return id.startsWith("pack-");
|
|
32
|
+
}
|
|
33
|
+
const STRICTNESS = { allow: 0, approve: 1, deny: 2 };
|
|
34
|
+
/** First rule whose glob matches, or undefined. */
|
|
35
|
+
function firstMatch(rules, tool) {
|
|
36
|
+
return rules.find((r) => globMatch(r.match, tool));
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Resolves the effective classification. A matching rule that carries an
|
|
40
|
+
* `effect` reclassifies the tool - that is how a `POST /search` gets moved into
|
|
41
|
+
* the read bucket deliberately, by a named rule, rather than by a heuristic.
|
|
42
|
+
*/
|
|
43
|
+
export function resolveEffect(input) {
|
|
44
|
+
const rule = firstMatch(input.policy.rules, input.tool);
|
|
45
|
+
return rule?.effect ?? input.effect ?? null;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Whether a resolved path lands on a value the policy keeps out of the record.
|
|
49
|
+
* Matched on any segment of the path, not only the last: a value under a
|
|
50
|
+
* redacted parent is redacted too, the same way `redactArgs` treats a subtree.
|
|
51
|
+
*/
|
|
52
|
+
function redactionTest(redactKeys) {
|
|
53
|
+
if (redactKeys.length === 0)
|
|
54
|
+
return () => false;
|
|
55
|
+
const wanted = new Set(redactKeys.map((k) => k.toLowerCase()));
|
|
56
|
+
return (path) => path.some((seg) => typeof seg === "string" && wanted.has(seg.toLowerCase()));
|
|
57
|
+
}
|
|
58
|
+
/** True when a constraint says anything about a value, as opposed to only its presence. */
|
|
59
|
+
function boundsAValue(c) {
|
|
60
|
+
return (c.max !== undefined ||
|
|
61
|
+
c.min !== undefined ||
|
|
62
|
+
c.enum !== undefined ||
|
|
63
|
+
c.pattern !== undefined ||
|
|
64
|
+
c.maxLength !== undefined ||
|
|
65
|
+
c.maxItems !== undefined ||
|
|
66
|
+
c.const !== undefined ||
|
|
67
|
+
c.same_items !== undefined ||
|
|
68
|
+
c.within !== undefined ||
|
|
69
|
+
c.host !== undefined);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* One value written so that two values get the same text exactly when they
|
|
73
|
+
* are the same call: object keys in sorted order, types kept apart (`5` and
|
|
74
|
+
* `"5"`, `true` and `1`), and a list of objects or lists compared as a
|
|
75
|
+
* multiset -- its items sorted -- because a batch of records sent in another
|
|
76
|
+
* order is the same batch. A list holding any plain value keeps its order:
|
|
77
|
+
* `["cp", "a", "b"]` and `["cp", "b", "a"]` copy in opposite directions.
|
|
78
|
+
*
|
|
79
|
+
* Recursive on purpose. A value nested past the stack throws a `RangeError`,
|
|
80
|
+
* and the caller treats a throw as "not covered" (it fails closed).
|
|
81
|
+
*/
|
|
82
|
+
export function comparableValue(value) {
|
|
83
|
+
if (Array.isArray(value)) {
|
|
84
|
+
const parts = value.map(comparableValue);
|
|
85
|
+
const positional = value.some((v) => v === null || typeof v !== "object");
|
|
86
|
+
return `[${(positional ? parts : [...parts].sort()).join(",")}]`;
|
|
87
|
+
}
|
|
88
|
+
if (value !== null && typeof value === "object") {
|
|
89
|
+
const record = value;
|
|
90
|
+
const keys = Object.keys(record)
|
|
91
|
+
.filter((k) => record[k] !== undefined)
|
|
92
|
+
.sort();
|
|
93
|
+
return `{${keys.map((k) => `${JSON.stringify(k)}:${comparableValue(record[k])}`).join(",")}}`;
|
|
94
|
+
}
|
|
95
|
+
if (typeof value === "number")
|
|
96
|
+
return Number.isFinite(value) ? JSON.stringify(value) : `#${String(value)}`;
|
|
97
|
+
if (typeof value === "string" || typeof value === "boolean" || value === null)
|
|
98
|
+
return JSON.stringify(value);
|
|
99
|
+
// undefined, a function, a bigint: nothing JSON sends, so nothing an approval can match.
|
|
100
|
+
return `?${typeof value}`;
|
|
101
|
+
}
|
|
102
|
+
/** True when `value` is a list holding exactly the approved items (see `comparableValue`). */
|
|
103
|
+
export function sameItems(value, approved) {
|
|
104
|
+
return Array.isArray(value) && comparableValue(value) === comparableValue(approved);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Checks one resolved value against one constraint.
|
|
108
|
+
*
|
|
109
|
+
* `null` is a value here, not an absence. A tool where `filter: null` means
|
|
110
|
+
* "every record" is one where treating null as nothing to check leaves the
|
|
111
|
+
* widest possible call unbounded.
|
|
112
|
+
*/
|
|
113
|
+
function checkConstraint(arg, value, c, redacted) {
|
|
114
|
+
const fail = (message) => ({ arg, message });
|
|
115
|
+
/*
|
|
116
|
+
* A violation message names the offending value, because that is what a
|
|
117
|
+
* reviewer needs in order to decide. For an argument the policy redacts it
|
|
118
|
+
* must not: the message goes into the audit log and in front of a human, and
|
|
119
|
+
* `audit.redact` saying to keep a value out of the record has to mean that
|
|
120
|
+
* wherever the value would otherwise appear.
|
|
121
|
+
*/
|
|
122
|
+
const show = (v) => (redacted ? "<redacted>" : JSON.stringify(v));
|
|
123
|
+
const shown = show(value);
|
|
124
|
+
if (c.const !== undefined && value !== c.const) {
|
|
125
|
+
return fail(`${arg} must equal ${JSON.stringify(c.const)}`);
|
|
126
|
+
}
|
|
127
|
+
if (c.same_items !== undefined && !sameItems(value, c.same_items)) {
|
|
128
|
+
return fail(Array.isArray(value)
|
|
129
|
+
? `${arg} must hold exactly the ${c.same_items.length} item(s) that were approved, each as often as it was approved`
|
|
130
|
+
: `${arg} must be a list of the items that were approved`);
|
|
131
|
+
}
|
|
132
|
+
if (c.enum && !c.enum.some((allowed) => allowed === value)) {
|
|
133
|
+
return fail(`${arg}=${shown} is not one of ${JSON.stringify(c.enum)}`);
|
|
134
|
+
}
|
|
135
|
+
if (c.max !== undefined || c.min !== undefined) {
|
|
136
|
+
// Not coerced by default. `Number("500")`, `Number(true)` and
|
|
137
|
+
// `Number([500])` all produce a number that satisfies a bound, while the
|
|
138
|
+
// upstream may read the value as a string, a flag or a list.
|
|
139
|
+
if (typeof value !== "number" && !c.coerce) {
|
|
140
|
+
return fail(`${arg} must be a number to be bounded by policy, and is ${typeofLabel(value)}`);
|
|
141
|
+
}
|
|
142
|
+
const n = typeof value === "number" ? value : Number(value);
|
|
143
|
+
if (!Number.isFinite(n))
|
|
144
|
+
return fail(`${arg} must be numeric to be bounded by policy`);
|
|
145
|
+
if (c.max !== undefined && n > c.max)
|
|
146
|
+
return fail(`${arg}=${show(n)} exceeds the policy maximum of ${c.max}`);
|
|
147
|
+
if (c.min !== undefined && n < c.min)
|
|
148
|
+
return fail(`${arg}=${show(n)} is below the policy minimum of ${c.min}`);
|
|
149
|
+
}
|
|
150
|
+
if (c.within !== undefined) {
|
|
151
|
+
const problem = pathWithin(value, c.within);
|
|
152
|
+
if (problem) {
|
|
153
|
+
// Only a real path that's elsewhere is "outside the folder"; a value that isn't a path is just wrong.
|
|
154
|
+
return typeof value === "string" ? { ...fail(`${arg} ${problem}`), outsideFolder: true } : fail(`${arg} ${problem}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (c.host !== undefined) {
|
|
158
|
+
const problem = urlHostAllowed(value, c.host);
|
|
159
|
+
if (problem)
|
|
160
|
+
return fail(`${arg} ${problem}`);
|
|
161
|
+
}
|
|
162
|
+
if (c.maxLength !== undefined) {
|
|
163
|
+
const len = typeof value === "string" ? value.length : Array.isArray(value) ? value.length : undefined;
|
|
164
|
+
if (len === undefined)
|
|
165
|
+
return fail(`${arg} must be a string or array to be length-bounded`);
|
|
166
|
+
if (len > c.maxLength)
|
|
167
|
+
return fail(`${arg} length ${len} exceeds the policy maximum of ${c.maxLength}`);
|
|
168
|
+
}
|
|
169
|
+
if (c.maxItems !== undefined) {
|
|
170
|
+
if (!Array.isArray(value))
|
|
171
|
+
return fail(`${arg} must be an array to be item-bounded`);
|
|
172
|
+
if (value.length > c.maxItems) {
|
|
173
|
+
return fail(`${arg} has ${value.length} items, exceeding the policy maximum of ${c.maxItems}`);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (c.pattern !== undefined) {
|
|
177
|
+
if (typeof value !== "string")
|
|
178
|
+
return fail(`${arg} must be a string to be pattern-matched`);
|
|
179
|
+
let re;
|
|
180
|
+
try {
|
|
181
|
+
re = new RegExp(c.pattern);
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
// An unparseable pattern is a closed door, not an open one.
|
|
185
|
+
return fail(`${arg} has an unparseable policy pattern`);
|
|
186
|
+
}
|
|
187
|
+
if (!re.test(value))
|
|
188
|
+
return fail(`${arg} does not match the required pattern`);
|
|
189
|
+
}
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
function typeofLabel(value) {
|
|
193
|
+
if (value === null)
|
|
194
|
+
return "null";
|
|
195
|
+
if (Array.isArray(value))
|
|
196
|
+
return "an array";
|
|
197
|
+
return `a ${typeof value}`;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Checks a call's arguments against a rule's bounds.
|
|
201
|
+
*
|
|
202
|
+
* Each key in `constraints` is an argument path, so a bound can reach a nested
|
|
203
|
+
* or repeated value. A path that fans out is checked at every place it matches,
|
|
204
|
+
* and the violation names the place rather than the pattern.
|
|
205
|
+
*/
|
|
206
|
+
export function checkArgConstraints(args, constraints,
|
|
207
|
+
/** Argument names whose values must not appear in a violation message. */
|
|
208
|
+
redactKeys = []) {
|
|
209
|
+
const violations = [];
|
|
210
|
+
const isRedacted = redactionTest(redactKeys);
|
|
211
|
+
for (const [spec, c] of Object.entries(constraints)) {
|
|
212
|
+
const parsed = parseArgPath(spec);
|
|
213
|
+
if (!parsed.ok) {
|
|
214
|
+
// A path the policy loader would have rejected. Reached only through a
|
|
215
|
+
// surface that stored one anyway, such as an older grant row, and a
|
|
216
|
+
// bound nobody can evaluate is a closed door rather than an open one.
|
|
217
|
+
violations.push({ arg: spec, message: `${spec} is not a usable argument path: ${parsed.error}` });
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
const matches = resolveArgPath(args, parsed.segments);
|
|
221
|
+
if (matches.length === 0) {
|
|
222
|
+
if (c.required) {
|
|
223
|
+
violations.push({ arg: spec, message: `${spec} is required by policy` });
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
// A bound on a value that is not there is not satisfied by its absence:
|
|
227
|
+
// the upstream applies its own default, which is the unbounded value the
|
|
228
|
+
// bound existed to prevent. A fanned-out path is exempt -- no elements to
|
|
229
|
+
// bound is not an absence, and that is what `maxItems` is for.
|
|
230
|
+
if (!c.optional && !hasWildcard(parsed.segments) && boundsAValue(c)) {
|
|
231
|
+
violations.push({
|
|
232
|
+
arg: spec,
|
|
233
|
+
message: `${spec} is bounded by policy but absent, so the upstream default would apply unbounded`
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
for (const match of matches) {
|
|
239
|
+
const v = checkConstraint(formatPath(match.path), match.value, c, isRedacted(match.path));
|
|
240
|
+
if (v)
|
|
241
|
+
violations.push(v);
|
|
242
|
+
}
|
|
243
|
+
if (c.count_max !== undefined && matches.length > c.count_max) {
|
|
244
|
+
violations.push({
|
|
245
|
+
arg: spec,
|
|
246
|
+
message: `${spec} matched ${matches.length} values, exceeding the policy maximum of ${c.count_max}`
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
if (c.sum_max !== undefined) {
|
|
250
|
+
let total = 0;
|
|
251
|
+
let numeric = true;
|
|
252
|
+
for (const match of matches) {
|
|
253
|
+
const n = typeof match.value === "number" ? match.value : Number(match.value);
|
|
254
|
+
if (!Number.isFinite(n)) {
|
|
255
|
+
numeric = false;
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
total += n;
|
|
259
|
+
}
|
|
260
|
+
if (!numeric) {
|
|
261
|
+
violations.push({ arg: spec, message: `${spec} must be numeric everywhere to be totalled by policy` });
|
|
262
|
+
}
|
|
263
|
+
else if (total > c.sum_max) {
|
|
264
|
+
const shownTotal = matches.some((m) => isRedacted(m.path)) ? "<redacted>" : String(total);
|
|
265
|
+
violations.push({
|
|
266
|
+
arg: spec,
|
|
267
|
+
message: `${spec} totals ${shownTotal} across ${matches.length} values, exceeding the policy maximum of ${c.sum_max}`
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return violations;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Arguments no constraint claims. This is what makes a bound a closed world
|
|
276
|
+
* rather than a note about one field: with `amount` bounded and nothing said
|
|
277
|
+
* about `force`, an open world passes `force: true` straight through.
|
|
278
|
+
*
|
|
279
|
+
* A constraint on a container claims everything beneath it, so bounding
|
|
280
|
+
* `items` with `maxItems` does not leave every element unclaimed.
|
|
281
|
+
*/
|
|
282
|
+
export function findUnknownArgs(args, constraints) {
|
|
283
|
+
const claimed = [];
|
|
284
|
+
for (const spec of Object.keys(constraints)) {
|
|
285
|
+
const parsed = parseArgPath(spec);
|
|
286
|
+
if (!parsed.ok)
|
|
287
|
+
continue;
|
|
288
|
+
for (const match of resolveArgPath(args, parsed.segments)) {
|
|
289
|
+
claimed.push([...match.path]);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
const unknown = [];
|
|
293
|
+
for (const leaf of leafPaths(args)) {
|
|
294
|
+
if (claimed.some((c) => isAtOrUnder(leaf.path, c)))
|
|
295
|
+
continue;
|
|
296
|
+
unknown.push(formatPath(leaf.path));
|
|
297
|
+
}
|
|
298
|
+
return unknown;
|
|
299
|
+
}
|
|
300
|
+
/** Collects the tenant-identifying names a policy cares about. */
|
|
301
|
+
export function tenantFieldNames(tenant) {
|
|
302
|
+
return [tenant.field, ...tenant.aliases];
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Pulls an agent-supplied tenant value out of an argument object, matching the
|
|
306
|
+
* canonical field name or any alias, case-insensitively.
|
|
307
|
+
*/
|
|
308
|
+
export function extractSuppliedTenant(args, tenant) {
|
|
309
|
+
if (!args)
|
|
310
|
+
return undefined;
|
|
311
|
+
const wanted = new Set(tenantFieldNames(tenant).map((f) => f.toLowerCase()));
|
|
312
|
+
for (const [key, value] of Object.entries(args)) {
|
|
313
|
+
if (!wanted.has(key.toLowerCase()))
|
|
314
|
+
continue;
|
|
315
|
+
if (value === undefined || value === null)
|
|
316
|
+
continue;
|
|
317
|
+
return String(value);
|
|
318
|
+
}
|
|
319
|
+
return undefined;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* The single decision point. Pure and synchronous by design: if enforcement
|
|
323
|
+
* ever needed to reach the network to decide, that would be a hole, not a
|
|
324
|
+
* feature. Generated servers and the gateway both route every call through
|
|
325
|
+
* this function, so the two cannot drift apart.
|
|
326
|
+
*/
|
|
327
|
+
export function decide(input) {
|
|
328
|
+
const { policy, tool } = input;
|
|
329
|
+
const args = input.args ?? {};
|
|
330
|
+
const effect = resolveEffect(input);
|
|
331
|
+
// 0. A tool switched off by an operator is refused before anything else is
|
|
332
|
+
// weighed, because nothing later can make it reachable again and a reason
|
|
333
|
+
// that names the switch is more useful than one that names a rule.
|
|
334
|
+
if (input.disabled) {
|
|
335
|
+
return {
|
|
336
|
+
kind: "deny",
|
|
337
|
+
effect,
|
|
338
|
+
ruleId: EXPOSURE_RULE,
|
|
339
|
+
reason: input.disabledReason ?? `tool "${tool}" is switched off`,
|
|
340
|
+
// The log gets who, when and why; the agent gets only that it is off.
|
|
341
|
+
agentReason: EXPOSURE_AGENT_NOTICE
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
// 1. Tenant integrity comes first. A call that tries to name a tenant other
|
|
345
|
+
// than the credential's own is refused whatever the rules say about it.
|
|
346
|
+
if (policy.tenant) {
|
|
347
|
+
const expected = input.tenant?.expected;
|
|
348
|
+
const supplied = input.tenant?.supplied ?? extractSuppliedTenant(args, policy.tenant);
|
|
349
|
+
if (supplied !== undefined && expected !== undefined && supplied !== expected) {
|
|
350
|
+
if (policy.tenant.on_mismatch === "deny") {
|
|
351
|
+
return {
|
|
352
|
+
kind: "deny",
|
|
353
|
+
effect,
|
|
354
|
+
ruleId: TENANT_RULE,
|
|
355
|
+
reason: `tenant scope violation: call named ${policy.tenant.field}=${JSON.stringify(supplied)} but the credential is bound to a different tenant`
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
if (policy.tenant.required && expected === undefined) {
|
|
360
|
+
return {
|
|
361
|
+
kind: "deny",
|
|
362
|
+
effect,
|
|
363
|
+
ruleId: "tenant.required",
|
|
364
|
+
reason: `tenant scoping is required but no value was resolved from ${policy.tenant.source.kind}`
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
// 2. Argument bounds, which are weighed before the classification is. A rule
|
|
369
|
+
// carrying bounds but no `effect` used to be skipped entirely for a tool
|
|
370
|
+
// the upstream never classified, because the unclassified return below
|
|
371
|
+
// came first - so the call was held with none of its bounds applied.
|
|
372
|
+
//
|
|
373
|
+
// A refusal from a bound is returned here because it is the most
|
|
374
|
+
// restrictive answer available and nothing below can make it less so. A
|
|
375
|
+
// *hold* from a bound is left for step 4, where it cannot soften an
|
|
376
|
+
// unclassified tool that policy says to deny.
|
|
377
|
+
const rule = firstMatch(policy.rules, tool);
|
|
378
|
+
const bounds = rule && !input.argsUnknown ? boundsVerdict(rule, args, effect, policy.audit.redact) : null;
|
|
379
|
+
if (bounds?.kind === "deny")
|
|
380
|
+
return bounds;
|
|
381
|
+
// 3. An unclassified tool is refused. This is what makes a newly added
|
|
382
|
+
// upstream tool safe by default rather than reachable by default.
|
|
383
|
+
if (effect === null) {
|
|
384
|
+
const kind = policy.defaults.on_unclassified === "approve" ? "approve" : "deny";
|
|
385
|
+
return {
|
|
386
|
+
kind,
|
|
387
|
+
effect,
|
|
388
|
+
ruleId: UNCLASSIFIED_RULE,
|
|
389
|
+
reason: `tool "${tool}" has no effect classification; policy says ${policy.defaults.on_unclassified} for unclassified tools`,
|
|
390
|
+
/*
|
|
391
|
+
* The record's wording is for a reviewer reading the log; this is for
|
|
392
|
+
* whoever has to act on the refusal, which is usually the caller or the
|
|
393
|
+
* person watching it. The old message named the rule and stopped there,
|
|
394
|
+
* so the most common refusal in the product was also the only one that
|
|
395
|
+
* described a dead end.
|
|
396
|
+
*
|
|
397
|
+
* Unlike a bound violation -- where the limit is deliberately withheld,
|
|
398
|
+
* because naming it makes a refusal an oracle a model can
|
|
399
|
+
* binary-search -- there is nothing here to leak. The tool is advertised
|
|
400
|
+
* in `tools/list`, the caller already knows its name, and "nobody has
|
|
401
|
+
* said what this does" is not a fact about the data behind it.
|
|
402
|
+
*/
|
|
403
|
+
agentReason: unclassifiedAgentReason(tool)
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
// 4. First matching rule wins, but a bound it set is honored ahead of its
|
|
407
|
+
// decision - a rule that allows transfers under 500 must not allow one
|
|
408
|
+
// over it, and a rule that holds them must hold it.
|
|
409
|
+
if (rule) {
|
|
410
|
+
const own = bounds ?? {
|
|
411
|
+
kind: rule.decision,
|
|
412
|
+
effect,
|
|
413
|
+
ruleId: rule.id,
|
|
414
|
+
reason: rule.reason ?? `rule "${rule.id}" (${rule.match}) says ${rule.decision} for ${effect} tools`
|
|
415
|
+
};
|
|
416
|
+
/*
|
|
417
|
+
* A ready-made rule (written from a rule pack, id `pack-...`) fills in
|
|
418
|
+
* what a person has not decided; it never loosens what they chose. Under
|
|
419
|
+
* a read-only posture a pack's "asks first" for a write is still looser
|
|
420
|
+
* than "blocked", so the stricter of the two wins. A person's own rule
|
|
421
|
+
* may loosen the posture: that is what writing one is for.
|
|
422
|
+
*/
|
|
423
|
+
if (isPackRule(rule.id)) {
|
|
424
|
+
const posture = postureDecision(policy, effect);
|
|
425
|
+
if (STRICTNESS[posture.kind] > STRICTNESS[own.kind]) {
|
|
426
|
+
return {
|
|
427
|
+
...posture,
|
|
428
|
+
reason: `${posture.reason}; the ready-made rule "${rule.id}" would say ${own.kind}, and a ready-made rule never loosens the posture`
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return own;
|
|
433
|
+
}
|
|
434
|
+
// 5. No rule matched: fall back to the posture.
|
|
435
|
+
return postureDecision(policy, effect);
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* What a rule's argument bounds say about this call, or null when they say
|
|
439
|
+
* nothing. `on_violation` chooses between refusing the call and holding it for
|
|
440
|
+
* a human; `unknown_args` chooses what happens to an argument no bound claims.
|
|
441
|
+
*
|
|
442
|
+
* The reason carries the exceeded bound and the offending value, because that
|
|
443
|
+
* is what the audit log and the reviewer need. The *agent* is told only that
|
|
444
|
+
* the arguments were out of bounds: the detailed string names the limit, which
|
|
445
|
+
* makes it an oracle a model can binary-search, and it echoes a value that
|
|
446
|
+
* `audit.redact` may have been configured to keep out of the record.
|
|
447
|
+
*/
|
|
448
|
+
function boundsVerdict(rule, args, effect, redactKeys) {
|
|
449
|
+
const problems = [];
|
|
450
|
+
const heldArgs = [];
|
|
451
|
+
// An out-of-bounds argument is governed by `on_violation`; an argument no
|
|
452
|
+
// bound claims by `unknown_args`. They are tracked apart because each only
|
|
453
|
+
// speaks for a problem that actually occurred: a rule left at the default
|
|
454
|
+
// `on_violation: deny` must not turn an unclaimed argument it was never
|
|
455
|
+
// asked about into a refusal.
|
|
456
|
+
const kinds = [];
|
|
457
|
+
let onlyOutsideFolder = true;
|
|
458
|
+
if (rule.args) {
|
|
459
|
+
const violations = checkArgConstraints(args, rule.args, redactKeys);
|
|
460
|
+
if (violations.length > 0) {
|
|
461
|
+
if (violations.some((v) => !v.outsideFolder))
|
|
462
|
+
onlyOutsideFolder = false;
|
|
463
|
+
problems.push(...violations.map((v) => v.message));
|
|
464
|
+
heldArgs.push(...violations.map((v) => v.arg));
|
|
465
|
+
kinds.push(rule.on_violation);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
if (rule.unknown_args !== "allow") {
|
|
469
|
+
const unknown = findUnknownArgs(args, rule.args ?? {});
|
|
470
|
+
if (unknown.length > 0) {
|
|
471
|
+
problems.push(`rule "${rule.id}" sets unknown_args: ${rule.unknown_args}, and ` +
|
|
472
|
+
`${unknown.map((u) => JSON.stringify(u)).join(", ")} ${unknown.length === 1 ? "is" : "are"} not bounded by it`);
|
|
473
|
+
heldArgs.push(...unknown);
|
|
474
|
+
kinds.push(rule.unknown_args);
|
|
475
|
+
onlyOutsideFolder = false;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
if (problems.length === 0)
|
|
479
|
+
return null;
|
|
480
|
+
// Both wrong at once: the stricter of the two applies.
|
|
481
|
+
const kind = kinds.includes("deny") ? "deny" : "approve";
|
|
482
|
+
return {
|
|
483
|
+
kind,
|
|
484
|
+
effect,
|
|
485
|
+
ruleId: rule.id,
|
|
486
|
+
reason: `argument constraints from rule "${rule.id}" not satisfied: ${problems.join("; ")}`,
|
|
487
|
+
agentReason: onlyOutsideFolder ? OUTSIDE_FOLDER_AGENT_NOTICE : BOUNDS_AGENT_NOTICE,
|
|
488
|
+
violations: problems,
|
|
489
|
+
heldArgs: [...new Set(heldArgs)]
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
function postureDecision(policy, effect) {
|
|
493
|
+
const base = { effect, ruleId: POSTURE_RULE };
|
|
494
|
+
switch (policy.defaults.mode) {
|
|
495
|
+
case "read-only":
|
|
496
|
+
return effect === "read"
|
|
497
|
+
? { ...base, kind: "allow", reason: "read-only posture allows reads" }
|
|
498
|
+
: {
|
|
499
|
+
...base,
|
|
500
|
+
kind: "deny",
|
|
501
|
+
reason: `read-only posture refuses ${effect} tools; add an explicit rule to permit this one`
|
|
502
|
+
};
|
|
503
|
+
case "approve-writes":
|
|
504
|
+
return effect === "read"
|
|
505
|
+
? { ...base, kind: "allow", reason: "approve-writes posture allows reads" }
|
|
506
|
+
: { ...base, kind: "approve", reason: `approve-writes posture requires approval for ${effect} tools` };
|
|
507
|
+
case "locked":
|
|
508
|
+
return {
|
|
509
|
+
...base,
|
|
510
|
+
kind: "deny",
|
|
511
|
+
reason: "locked posture refuses everything without an explicit allow rule"
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
/** True when a decision permits the call to reach the upstream. */
|
|
516
|
+
export function isPermitted(d) {
|
|
517
|
+
return d.kind === "allow";
|
|
518
|
+
}
|