@hmharness/sandbox 0.23.25 → 0.23.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/broker.d.ts +104 -0
- package/dist/broker.js +121 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
package/dist/broker.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/sandbox - Capability Broker v1 (upgrade pack 01 P0-1 / MasterPrompt §3)
|
|
3
|
+
*
|
|
4
|
+
* The unified grant layer the pack demands: every capability access goes
|
|
5
|
+
* through ONE broker with deny-by-default semantics. A capability token
|
|
6
|
+
* carries the pack's eight fields (subject, class, scope, resource,
|
|
7
|
+
* operation, constraints, expiry, provenance); tokens ALWAYS expire; the
|
|
8
|
+
* audit trail records every decision.
|
|
9
|
+
*
|
|
10
|
+
* The execution ladder composes what already exists:
|
|
11
|
+
* low risk + read-only + no network -> worker (rlm-sandbox)
|
|
12
|
+
* medium / low+write -> process
|
|
13
|
+
* high / any network -> container (Docker provider)
|
|
14
|
+
* critical / high+long-running -> microvm
|
|
15
|
+
* via recommendedIsolation() plus the broker's own escalation rules -
|
|
16
|
+
* most importantly the RLM rule: the worker tier carries NO host
|
|
17
|
+
* capability by construction (network/secret/process/device classes are
|
|
18
|
+
* denied at worker tier regardless of tokens - the pack's "RLM 不得继承
|
|
19
|
+
* host capability" made mechanical).
|
|
20
|
+
*
|
|
21
|
+
* Layering: this module has zero cognitive imports; ledger mirroring is
|
|
22
|
+
* an injectable onDeny hook the composition layer wires.
|
|
23
|
+
*/
|
|
24
|
+
import { type IsolationLevel } from './microvm.js';
|
|
25
|
+
export type CapabilityClass = 'filesystem' | 'network' | 'process' | 'secret' | 'device';
|
|
26
|
+
export type CapabilityOperation = 'read' | 'write' | 'execute' | 'connect' | 'spawn' | 'grant';
|
|
27
|
+
/** The full ladder the pack demands (worker = the rlm-sandbox low tier). */
|
|
28
|
+
export type ExecutionTier = 'worker' | IsolationLevel;
|
|
29
|
+
export interface CapabilityToken {
|
|
30
|
+
readonly id: string;
|
|
31
|
+
/** who holds it: tool / agent / session id */
|
|
32
|
+
readonly subject: string;
|
|
33
|
+
readonly class: CapabilityClass;
|
|
34
|
+
/** e.g. 'workspace-root' | 'host' | 'loopback' | 'none' */
|
|
35
|
+
readonly scope: string;
|
|
36
|
+
readonly resource: string;
|
|
37
|
+
readonly operation: CapabilityOperation;
|
|
38
|
+
readonly constraints?: Record<string, string | number | boolean>;
|
|
39
|
+
/** epoch ms - every token expires, always */
|
|
40
|
+
readonly expiry: number;
|
|
41
|
+
/** who granted and why - the audit answer to 'qui bono' */
|
|
42
|
+
readonly provenance: string;
|
|
43
|
+
}
|
|
44
|
+
export interface CapabilityRequest {
|
|
45
|
+
subject: string;
|
|
46
|
+
class: CapabilityClass;
|
|
47
|
+
resource?: string;
|
|
48
|
+
operation?: CapabilityOperation;
|
|
49
|
+
}
|
|
50
|
+
export interface BrokerDecision {
|
|
51
|
+
allowed: boolean;
|
|
52
|
+
reason: string;
|
|
53
|
+
token?: CapabilityToken;
|
|
54
|
+
tier: ExecutionTier;
|
|
55
|
+
}
|
|
56
|
+
export interface GrantInput {
|
|
57
|
+
subject: string;
|
|
58
|
+
class: CapabilityClass;
|
|
59
|
+
scope: string;
|
|
60
|
+
resource: string;
|
|
61
|
+
operation: CapabilityOperation;
|
|
62
|
+
constraints?: Record<string, string | number | boolean>;
|
|
63
|
+
/** how long the token lives, ms (default 1h; cap 24h) */
|
|
64
|
+
ttlMs?: number;
|
|
65
|
+
provenance: string;
|
|
66
|
+
}
|
|
67
|
+
export interface AuditEntry {
|
|
68
|
+
at: number;
|
|
69
|
+
subject: string;
|
|
70
|
+
class: CapabilityClass;
|
|
71
|
+
allowed: boolean;
|
|
72
|
+
reason: string;
|
|
73
|
+
}
|
|
74
|
+
export declare class CapabilityBroker {
|
|
75
|
+
private readonly tokens;
|
|
76
|
+
private readonly audits;
|
|
77
|
+
private readonly auditLimit;
|
|
78
|
+
private seq;
|
|
79
|
+
/** injectable clock for deterministic tests */
|
|
80
|
+
now: () => number;
|
|
81
|
+
/** composition-layer hook (agent wires it to the Cognitive Ledger's capability.denied) */
|
|
82
|
+
onDeny?: (subject: string, cls: CapabilityClass, reason: string) => void;
|
|
83
|
+
grant(input: GrantInput): CapabilityToken;
|
|
84
|
+
/** Deny-by-default: no LIVE matching token = deny, with the reason stated. */
|
|
85
|
+
check(req: CapabilityRequest, risk: {
|
|
86
|
+
riskLevel: 'low' | 'medium' | 'high' | 'critical';
|
|
87
|
+
taskDurationMs?: number;
|
|
88
|
+
hasNetworkAccess?: boolean; /** the caller's execution context: rlm-worker = sandboxed eval, inherits NOTHING from the host */
|
|
89
|
+
executionContext?: 'rlm-worker' | 'host';
|
|
90
|
+
}): BrokerDecision;
|
|
91
|
+
/** The pack's ladder, composing recommendedIsolation plus broker rules. */
|
|
92
|
+
tierFor(req: CapabilityRequest, risk: {
|
|
93
|
+
riskLevel: 'low' | 'medium' | 'high' | 'critical';
|
|
94
|
+
taskDurationMs?: number;
|
|
95
|
+
hasNetworkAccess?: boolean;
|
|
96
|
+
}): ExecutionTier;
|
|
97
|
+
revoke(byIdOrSubject: string): number;
|
|
98
|
+
status(): {
|
|
99
|
+
active: number;
|
|
100
|
+
audits: number;
|
|
101
|
+
};
|
|
102
|
+
auditTrail(): readonly AuditEntry[];
|
|
103
|
+
private decide;
|
|
104
|
+
}
|
package/dist/broker.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/sandbox - Capability Broker v1 (upgrade pack 01 P0-1 / MasterPrompt §3)
|
|
3
|
+
*
|
|
4
|
+
* The unified grant layer the pack demands: every capability access goes
|
|
5
|
+
* through ONE broker with deny-by-default semantics. A capability token
|
|
6
|
+
* carries the pack's eight fields (subject, class, scope, resource,
|
|
7
|
+
* operation, constraints, expiry, provenance); tokens ALWAYS expire; the
|
|
8
|
+
* audit trail records every decision.
|
|
9
|
+
*
|
|
10
|
+
* The execution ladder composes what already exists:
|
|
11
|
+
* low risk + read-only + no network -> worker (rlm-sandbox)
|
|
12
|
+
* medium / low+write -> process
|
|
13
|
+
* high / any network -> container (Docker provider)
|
|
14
|
+
* critical / high+long-running -> microvm
|
|
15
|
+
* via recommendedIsolation() plus the broker's own escalation rules -
|
|
16
|
+
* most importantly the RLM rule: the worker tier carries NO host
|
|
17
|
+
* capability by construction (network/secret/process/device classes are
|
|
18
|
+
* denied at worker tier regardless of tokens - the pack's "RLM 不得继承
|
|
19
|
+
* host capability" made mechanical).
|
|
20
|
+
*
|
|
21
|
+
* Layering: this module has zero cognitive imports; ledger mirroring is
|
|
22
|
+
* an injectable onDeny hook the composition layer wires.
|
|
23
|
+
*/
|
|
24
|
+
import { recommendedIsolation } from "./microvm.js";
|
|
25
|
+
const MAX_TTL_MS = 24 * 60 * 60 * 1000;
|
|
26
|
+
const DEFAULT_TTL_MS = 60 * 60 * 1000;
|
|
27
|
+
/** classes the worker tier can never hold - the RLM rule, mechanical */
|
|
28
|
+
const WORKER_FORBIDDEN = ['network', 'secret', 'process', 'device'];
|
|
29
|
+
export class CapabilityBroker {
|
|
30
|
+
tokens = new Map();
|
|
31
|
+
audits = [];
|
|
32
|
+
auditLimit = 500;
|
|
33
|
+
seq = 0;
|
|
34
|
+
/** injectable clock for deterministic tests */
|
|
35
|
+
now = () => Date.now();
|
|
36
|
+
/** composition-layer hook (agent wires it to the Cognitive Ledger's capability.denied) */
|
|
37
|
+
onDeny;
|
|
38
|
+
grant(input) {
|
|
39
|
+
if (!input.provenance)
|
|
40
|
+
throw new Error('capability grant requires provenance - unattributable grants are non-auditable');
|
|
41
|
+
const ttl = Math.min(input.ttlMs ?? DEFAULT_TTL_MS, MAX_TTL_MS);
|
|
42
|
+
const token = Object.freeze({
|
|
43
|
+
id: `cap-${++this.seq}-${Math.random().toString(36).slice(2, 8)}`,
|
|
44
|
+
subject: input.subject,
|
|
45
|
+
class: input.class,
|
|
46
|
+
scope: input.scope,
|
|
47
|
+
resource: input.resource,
|
|
48
|
+
operation: input.operation,
|
|
49
|
+
...(input.constraints !== undefined ? { constraints: { ...input.constraints } } : {}),
|
|
50
|
+
expiry: this.now() + ttl,
|
|
51
|
+
provenance: input.provenance,
|
|
52
|
+
});
|
|
53
|
+
this.tokens.set(token.id, token);
|
|
54
|
+
return token;
|
|
55
|
+
}
|
|
56
|
+
/** Deny-by-default: no LIVE matching token = deny, with the reason stated. */
|
|
57
|
+
check(req, risk) {
|
|
58
|
+
const tier = this.tierFor(req, risk);
|
|
59
|
+
const op = req.operation ?? 'read';
|
|
60
|
+
const workerContext = tier === 'worker' || risk.executionContext === 'rlm-worker';
|
|
61
|
+
if (workerContext && WORKER_FORBIDDEN.includes(req.class)) {
|
|
62
|
+
return this.decide(req, false, `worker tier can never hold ${req.class} - the RLM no-host-capability rule`, tier);
|
|
63
|
+
}
|
|
64
|
+
// prune expired tokens on every check (expiry is enforced lazily but strictly)
|
|
65
|
+
const nowMs = this.now();
|
|
66
|
+
for (const [id, t] of this.tokens)
|
|
67
|
+
if (t.expiry <= nowMs)
|
|
68
|
+
this.tokens.delete(id);
|
|
69
|
+
const match = [...this.tokens.values()].find((t) => t.subject === req.subject
|
|
70
|
+
&& t.class === req.class
|
|
71
|
+
&& (req.resource === undefined || t.resource === req.resource || t.resource === '*')
|
|
72
|
+
&& t.operation === op);
|
|
73
|
+
if (!match) {
|
|
74
|
+
return this.decide(req, false, `no live ${req.class}/${op} grant for subject '${req.subject}' (deny-by-default)`, tier);
|
|
75
|
+
}
|
|
76
|
+
return this.decide(req, true, `grant ${match.id} (scope ${match.scope}, provenance: ${match.provenance})`, tier, match);
|
|
77
|
+
}
|
|
78
|
+
/** The pack's ladder, composing recommendedIsolation plus broker rules. */
|
|
79
|
+
tierFor(req, risk) {
|
|
80
|
+
const net = risk.hasNetworkAccess ?? req.class === 'network';
|
|
81
|
+
const recommended = recommendedIsolation({
|
|
82
|
+
riskLevel: risk.riskLevel,
|
|
83
|
+
taskDuration: risk.taskDurationMs ?? 0,
|
|
84
|
+
hasNetworkAccess: net,
|
|
85
|
+
});
|
|
86
|
+
if (recommended !== 'process')
|
|
87
|
+
return recommended;
|
|
88
|
+
// process tier refines to worker when everything is low and read-only
|
|
89
|
+
const lowAndReadOnly = risk.riskLevel === 'low' && !net && (req.operation ?? 'read') === 'read' && req.class === 'filesystem';
|
|
90
|
+
return lowAndReadOnly ? 'worker' : 'process';
|
|
91
|
+
}
|
|
92
|
+
revoke(byIdOrSubject) {
|
|
93
|
+
let n = 0;
|
|
94
|
+
for (const [id, t] of this.tokens) {
|
|
95
|
+
if (id === byIdOrSubject || t.subject === byIdOrSubject) {
|
|
96
|
+
this.tokens.delete(id);
|
|
97
|
+
n++;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return n;
|
|
101
|
+
}
|
|
102
|
+
status() {
|
|
103
|
+
return { active: this.tokens.size, audits: this.audits.length };
|
|
104
|
+
}
|
|
105
|
+
auditTrail() {
|
|
106
|
+
return this.audits.slice();
|
|
107
|
+
}
|
|
108
|
+
decide(req, allowed, reason, tier, token) {
|
|
109
|
+
const entry = { at: this.now(), subject: req.subject, class: req.class, allowed, reason };
|
|
110
|
+
this.audits.push(entry);
|
|
111
|
+
if (this.audits.length > this.auditLimit)
|
|
112
|
+
this.audits.splice(0, this.audits.length - this.auditLimit);
|
|
113
|
+
if (!allowed) {
|
|
114
|
+
try {
|
|
115
|
+
this.onDeny?.(req.subject, req.class, reason);
|
|
116
|
+
}
|
|
117
|
+
catch { /* a deny hook must never break the denial itself */ }
|
|
118
|
+
}
|
|
119
|
+
return { allowed, reason, tier, ...(token !== undefined ? { token } : {}) };
|
|
120
|
+
}
|
|
121
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hmharness/sandbox",
|
|
3
|
-
"version": "0.23.
|
|
3
|
+
"version": "0.23.27",
|
|
4
4
|
"description": "hmharness sandbox runtime (V2 blueprint M3): isolated workspaces with create/exec/read/write/snapshot/restore/diff/destroy, tiered permissions (READ_ONLY / WORKSPACE_WRITE / FULL_ACCESS), git-backed snapshots. Runtime owns agent turns; the sandbox owns workspace/process state (ADR-0001).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|