@agent-corpus/policy-manager 0.4.0 → 0.6.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 +95 -10
- package/dist/adapters.d.ts +14 -2
- package/dist/adapters.js +86 -7
- package/dist/audit.js +16 -2
- package/dist/cli.js +27 -3
- package/dist/config.js +11 -3
- package/dist/created.d.ts +44 -0
- package/dist/created.js +236 -0
- package/dist/evidence.d.ts +53 -1
- package/dist/evidence.js +201 -6
- package/dist/flow.d.ts +2 -1
- package/dist/flow.js +856 -76
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/permissive.d.ts +11 -3
- package/dist/permissive.js +648 -28
- package/dist/policy.d.ts +5 -1
- package/dist/policy.js +132 -30
- package/dist/principle.d.ts +8 -0
- package/dist/principle.js +8 -0
- package/dist/profile.d.ts +8 -0
- package/dist/profile.js +26 -0
- package/dist/recoverable.d.ts +41 -0
- package/dist/recoverable.js +391 -0
- package/dist/redact.d.ts +3 -0
- package/dist/redact.js +140 -0
- package/dist/replay.d.ts +2 -0
- package/dist/replay.js +9 -1
- package/dist/review.d.ts +55 -0
- package/dist/review.js +231 -0
- package/dist/script.d.ts +35 -1
- package/dist/script.js +215 -17
- package/dist/service.d.ts +1 -0
- package/dist/service.js +3 -3
- package/dist/session.d.ts +17 -2
- package/dist/session.js +156 -20
- package/dist/setup.js +3 -1
- package/dist/types.d.ts +21 -3
- package/dist/types.js +12 -1
- package/package.json +2 -2
package/dist/review.js
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { existsSync, lstatSync, readdirSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { AuditLog } from "@agent-corpus/audit-log";
|
|
5
|
+
const BUCKETS = [
|
|
6
|
+
["0.00-0.50", 0, 0.5],
|
|
7
|
+
["0.50-0.80", 0.5, 0.8],
|
|
8
|
+
["0.80-0.90", 0.8, 0.9],
|
|
9
|
+
["0.90-0.95", 0.9, 0.95],
|
|
10
|
+
["0.95-0.99", 0.95, 0.99],
|
|
11
|
+
["0.99-1.00", 0.99, 1.0001],
|
|
12
|
+
];
|
|
13
|
+
export function sessionDirectories(extra = []) {
|
|
14
|
+
const root = tmpdir();
|
|
15
|
+
const found = existsSync(root)
|
|
16
|
+
? readdirSync(root)
|
|
17
|
+
.filter((name) => /^acp-[A-Za-z0-9]+-[a-f0-9]{24}$/.test(name))
|
|
18
|
+
.map((name) => join(root, name))
|
|
19
|
+
: [];
|
|
20
|
+
return [...found, ...extra].filter((path) => {
|
|
21
|
+
try {
|
|
22
|
+
return lstatSync(path).isDirectory();
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
const text = (value) => typeof value === "string" ? value : undefined;
|
|
30
|
+
const num = (value) => typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
31
|
+
export async function reviewClassifications(directories, since) {
|
|
32
|
+
const requests = [];
|
|
33
|
+
let sessions = 0;
|
|
34
|
+
for (const directory of directories) {
|
|
35
|
+
const records = [];
|
|
36
|
+
try {
|
|
37
|
+
let after;
|
|
38
|
+
for (;;) {
|
|
39
|
+
const page = await new AuditLog(directory).query({
|
|
40
|
+
...(since ? { since } : {}),
|
|
41
|
+
...(after ? { after } : {}),
|
|
42
|
+
limit: 10000,
|
|
43
|
+
});
|
|
44
|
+
records.push(...page.records);
|
|
45
|
+
if (!page.nextCursor)
|
|
46
|
+
break;
|
|
47
|
+
after = page.nextCursor;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
continue; // an unreadable or foreign directory is skipped, never trusted
|
|
52
|
+
}
|
|
53
|
+
if (!records.length)
|
|
54
|
+
continue;
|
|
55
|
+
sessions++;
|
|
56
|
+
const byRequest = new Map();
|
|
57
|
+
const entry = (record) => {
|
|
58
|
+
if (!record.requestId)
|
|
59
|
+
return undefined;
|
|
60
|
+
let value = byRequest.get(record.requestId);
|
|
61
|
+
if (!value) {
|
|
62
|
+
value = {
|
|
63
|
+
session: record.sessionId,
|
|
64
|
+
requestHash: record.requestId,
|
|
65
|
+
time: record.timestamp,
|
|
66
|
+
attempts: [],
|
|
67
|
+
outcome: "unresolved",
|
|
68
|
+
};
|
|
69
|
+
byRequest.set(record.requestId, value);
|
|
70
|
+
}
|
|
71
|
+
return value;
|
|
72
|
+
};
|
|
73
|
+
for (const record of records) {
|
|
74
|
+
const a = record.attributes;
|
|
75
|
+
// Only requests that entered the classification flow are reviewed.
|
|
76
|
+
if (record.eventName !== "classification-requested" && !byRequest.has(record.requestId ?? ""))
|
|
77
|
+
continue;
|
|
78
|
+
if (record.eventName === "classification-requested") {
|
|
79
|
+
const value = entry(record);
|
|
80
|
+
if (!value)
|
|
81
|
+
continue;
|
|
82
|
+
value.time = record.timestamp;
|
|
83
|
+
const command = text(a.redactedCommand);
|
|
84
|
+
if (command)
|
|
85
|
+
value.command = command;
|
|
86
|
+
const reason = text(a.reason);
|
|
87
|
+
if (reason)
|
|
88
|
+
value.reason = reason;
|
|
89
|
+
if (!requests.includes(value))
|
|
90
|
+
requests.push(value);
|
|
91
|
+
}
|
|
92
|
+
else if (record.eventName === "classification") {
|
|
93
|
+
const value = entry(record);
|
|
94
|
+
if (!value)
|
|
95
|
+
continue;
|
|
96
|
+
const attempt = { time: record.timestamp, outcome: "invalid" };
|
|
97
|
+
const category = text(a.category);
|
|
98
|
+
if (category)
|
|
99
|
+
attempt.category = category;
|
|
100
|
+
const confidence = num(a.confidence);
|
|
101
|
+
if (confidence !== undefined)
|
|
102
|
+
attempt.confidence = confidence;
|
|
103
|
+
if (typeof a.recoverable === "boolean")
|
|
104
|
+
attempt.recoverable = a.recoverable;
|
|
105
|
+
const rationale = text(a.rationale);
|
|
106
|
+
if (rationale)
|
|
107
|
+
attempt.rationale = rationale;
|
|
108
|
+
value.attempts.push(attempt);
|
|
109
|
+
}
|
|
110
|
+
else if (record.eventName === "decision") {
|
|
111
|
+
const value = entry(record);
|
|
112
|
+
if (!value)
|
|
113
|
+
continue;
|
|
114
|
+
const latest = value.attempts.at(-1);
|
|
115
|
+
if (a.classification === "approved" || a.classification === "denied") {
|
|
116
|
+
// The decision that follows a classification settles that attempt; a retry's decision
|
|
117
|
+
// repeats the same outcome and changes nothing.
|
|
118
|
+
if (latest?.outcome === "invalid")
|
|
119
|
+
latest.outcome = a.classification;
|
|
120
|
+
value.outcome = a.classification;
|
|
121
|
+
}
|
|
122
|
+
else if (a.classification === "fallback")
|
|
123
|
+
value.outcome = "fallback";
|
|
124
|
+
}
|
|
125
|
+
else if (record.eventName === "classification-fallback") {
|
|
126
|
+
const value = entry(record);
|
|
127
|
+
if (!value)
|
|
128
|
+
continue;
|
|
129
|
+
value.outcome = "fallback";
|
|
130
|
+
const reason = text(a.fallbackReason);
|
|
131
|
+
if (reason)
|
|
132
|
+
value.fallbackReason = reason;
|
|
133
|
+
}
|
|
134
|
+
else if (record.eventName === "execution-result") {
|
|
135
|
+
const value = entry(record);
|
|
136
|
+
if (!value)
|
|
137
|
+
continue;
|
|
138
|
+
value.execution = {
|
|
139
|
+
...(text(a.status) ? { status: text(a.status) } : {}),
|
|
140
|
+
...(num(a.exitCode) !== undefined ? { exitCode: num(a.exitCode) } : {}),
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
else if (record.eventName === "human-response") {
|
|
144
|
+
const value = entry(record);
|
|
145
|
+
if (value)
|
|
146
|
+
value.humanResponse = text(a.response) ?? "";
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const approved = requests.filter((r) => r.outcome === "approved");
|
|
151
|
+
const denied = requests.filter((r) => r.outcome === "denied");
|
|
152
|
+
const fallback = requests.filter((r) => r.outcome === "fallback");
|
|
153
|
+
const fallbackReasons = {};
|
|
154
|
+
for (const r of fallback) {
|
|
155
|
+
const key = r.fallbackReason ?? "unavailable";
|
|
156
|
+
fallbackReasons[key] = (fallbackReasons[key] ?? 0) + 1;
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
sessions,
|
|
160
|
+
requested: requests.length,
|
|
161
|
+
approved: approved.length,
|
|
162
|
+
denied: denied.length,
|
|
163
|
+
fallback: fallback.length,
|
|
164
|
+
fallbackReasons,
|
|
165
|
+
unresolved: requests.length - approved.length - denied.length - fallback.length,
|
|
166
|
+
fallbackRate: requests.length ? fallback.length / requests.length : 0,
|
|
167
|
+
histogram: BUCKETS.map(([range, low, high]) => ({
|
|
168
|
+
range,
|
|
169
|
+
count: requests
|
|
170
|
+
.flatMap((r) => r.attempts)
|
|
171
|
+
.filter((t) => t.confidence !== undefined && t.confidence >= low && t.confidence < high)
|
|
172
|
+
.length,
|
|
173
|
+
})),
|
|
174
|
+
aiApproved: approved,
|
|
175
|
+
aiDenied: denied,
|
|
176
|
+
approvedButFailed: approved.filter((r) => r.execution &&
|
|
177
|
+
(r.execution.status === "failed" ||
|
|
178
|
+
(r.execution.exitCode !== undefined && r.execution.exitCode !== 0))),
|
|
179
|
+
humanDenied: requests.filter((r) => r.humanResponse === "denied"),
|
|
180
|
+
reclassified: requests.filter((r) => r.attempts.length > 1),
|
|
181
|
+
deniedThenApproved: requests.filter((r) => {
|
|
182
|
+
const first = r.attempts.findIndex((t) => t.outcome === "denied");
|
|
183
|
+
return first >= 0 && r.attempts.slice(first + 1).some((t) => t.outcome === "approved");
|
|
184
|
+
}),
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
function row(r) {
|
|
188
|
+
const outcome = r.execution
|
|
189
|
+
? ` -> ${r.execution.status ?? "ran"}${r.execution.exitCode !== undefined ? ` (exit ${r.execution.exitCode})` : ""}`
|
|
190
|
+
: "";
|
|
191
|
+
const attempts = r.attempts
|
|
192
|
+
.map((t, i) => ` ${r.attempts.length > 1 ? `attempt ${i + 1}: ` : ""}${t.outcome} ${t.category ?? "?"} ${t.confidence ?? "?"} recoverable=${t.recoverable ?? "?"}${t.rationale ? ` why: ${t.rationale}` : ""}`)
|
|
193
|
+
.join("\n");
|
|
194
|
+
return ` ${r.time} ${r.command ?? r.requestHash}${outcome}${attempts ? `\n${attempts}` : ""}`;
|
|
195
|
+
}
|
|
196
|
+
export function formatReview(review) {
|
|
197
|
+
const pct = (n) => `${((100 * n) / Math.max(review.requested, 1)).toFixed(1)}%`;
|
|
198
|
+
const lines = [
|
|
199
|
+
`Sessions scanned: ${review.sessions}`,
|
|
200
|
+
`Ambiguous requests sent to classification: ${review.requested}`,
|
|
201
|
+
` AI approved: ${review.approved} (${pct(review.approved)})`,
|
|
202
|
+
` AI denied: ${review.denied} (${pct(review.denied)})`,
|
|
203
|
+
` Fell back to human: ${review.fallback} (${pct(review.fallback)})${Object.entries(review.fallbackReasons)
|
|
204
|
+
.map(([k, v]) => ` ${k}=${v}`)
|
|
205
|
+
.join("")}`,
|
|
206
|
+
` Never resolved: ${review.unresolved} (${pct(review.unresolved)}; the model abandoned the command)`,
|
|
207
|
+
"",
|
|
208
|
+
"Confidence histogram (classifier self-report):",
|
|
209
|
+
...review.histogram.map((b) => ` ${b.range} ${b.count}`),
|
|
210
|
+
"",
|
|
211
|
+
`Requests classified more than once (${review.reclassified.length}):`,
|
|
212
|
+
...review.reclassified.map(row),
|
|
213
|
+
"",
|
|
214
|
+
`AI-denied, then approved on a later classification (${review.deniedThenApproved.length}):`,
|
|
215
|
+
...review.deniedThenApproved.map(row),
|
|
216
|
+
"",
|
|
217
|
+
`AI-approved commands that failed (${review.approvedButFailed.length}):`,
|
|
218
|
+
...review.approvedButFailed.map(row),
|
|
219
|
+
"",
|
|
220
|
+
`Commands a person later denied (${review.humanDenied.length}):`,
|
|
221
|
+
...review.humanDenied.map(row),
|
|
222
|
+
"",
|
|
223
|
+
`All AI-approved commands (${review.aiApproved.length}):`,
|
|
224
|
+
...review.aiApproved.map(row),
|
|
225
|
+
"",
|
|
226
|
+
`AI-denied commands (${review.aiDenied.length}):`,
|
|
227
|
+
...review.aiDenied.map(row),
|
|
228
|
+
];
|
|
229
|
+
return lines.join("\n");
|
|
230
|
+
}
|
|
231
|
+
//# sourceMappingURL=review.js.map
|
package/dist/script.d.ts
CHANGED
|
@@ -1,8 +1,29 @@
|
|
|
1
1
|
/** Stands for the unknown output of an expansion inside a word. */
|
|
2
2
|
export declare const SUBSTITUTION = "\uE000";
|
|
3
|
+
/** `${X:-w}`, `${X:+w}`, `${#X}`, `${X#p}`/`${X##p}`/`${X%p}`/`${X%%p}` and `${X:o:l}`. */
|
|
4
|
+
export type ParamOp = {
|
|
5
|
+
kind: "default";
|
|
6
|
+
word: string;
|
|
7
|
+
} | {
|
|
8
|
+
kind: "alternate";
|
|
9
|
+
word: string;
|
|
10
|
+
} | {
|
|
11
|
+
kind: "length";
|
|
12
|
+
} | {
|
|
13
|
+
kind: "strip";
|
|
14
|
+
suffix: boolean;
|
|
15
|
+
longest: boolean;
|
|
16
|
+
pattern: string;
|
|
17
|
+
} | {
|
|
18
|
+
kind: "slice";
|
|
19
|
+
offset: number;
|
|
20
|
+
length?: number;
|
|
21
|
+
};
|
|
3
22
|
export interface VarRef {
|
|
23
|
+
/** A variable name, or a positional parameter (`1`..`9`, `@`, `*`). */
|
|
4
24
|
name: string;
|
|
5
25
|
quoted: boolean;
|
|
26
|
+
op?: ParamOp;
|
|
6
27
|
}
|
|
7
28
|
export interface UnknownPart {
|
|
8
29
|
unknown: true;
|
|
@@ -57,6 +78,8 @@ export interface ForNode {
|
|
|
57
78
|
}
|
|
58
79
|
export interface WhileNode {
|
|
59
80
|
kind: "while";
|
|
81
|
+
/** `until`: the body runs while the condition fails. */
|
|
82
|
+
until?: boolean;
|
|
60
83
|
cond: ListNode;
|
|
61
84
|
body: ListNode;
|
|
62
85
|
redirects: Redir[];
|
|
@@ -79,7 +102,13 @@ export interface CaseNode {
|
|
|
79
102
|
}>;
|
|
80
103
|
redirects: Redir[];
|
|
81
104
|
}
|
|
82
|
-
|
|
105
|
+
/** `name() { ...; }` or `name() ( ... )`: defines the function, runs nothing. */
|
|
106
|
+
export interface FunctionNode {
|
|
107
|
+
kind: "function";
|
|
108
|
+
name: string;
|
|
109
|
+
body: Group | Subshell;
|
|
110
|
+
}
|
|
111
|
+
export type Command = Simple | Subshell | Group | ForNode | WhileNode | IfNode | CaseNode | FunctionNode;
|
|
83
112
|
export interface PipelineNode {
|
|
84
113
|
stages: Command[];
|
|
85
114
|
negated: boolean;
|
|
@@ -97,5 +126,10 @@ export interface ListNode {
|
|
|
97
126
|
background: boolean;
|
|
98
127
|
}>;
|
|
99
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* Applies a parameter operation to a value that is set and not empty, which is the only kind of
|
|
131
|
+
* value the evaluator knows.
|
|
132
|
+
*/
|
|
133
|
+
export declare function applyParamOp(value: string, op: ParamOp | undefined): string;
|
|
100
134
|
export declare function parseScript(command: string): ListNode | undefined;
|
|
101
135
|
//# sourceMappingURL=script.d.ts.map
|
package/dist/script.js
CHANGED
|
@@ -163,6 +163,134 @@ function closingParen(command, start) {
|
|
|
163
163
|
}
|
|
164
164
|
return undefined;
|
|
165
165
|
}
|
|
166
|
+
/** No leading `=`: zsh's EQUALS option expands `=cmd` to a command path. */
|
|
167
|
+
const PARAM_WORD = /^(?:[A-Za-z0-9_@%+:,./-][A-Za-z0-9_@%+=:,./-]*)?$/;
|
|
168
|
+
const PARAM_PATTERN = /^[A-Za-z0-9_@%+=:,./*?-]+$/;
|
|
169
|
+
/** The name and operation inside `${...}`, or undefined for a form that is not modelled. */
|
|
170
|
+
function parameter(body) {
|
|
171
|
+
const NAME_OR_POSITION = "([A-Za-z_][A-Za-z0-9_]*|[1-9][0-9]*)";
|
|
172
|
+
let m = new RegExp(`^${NAME_OR_POSITION}$`).exec(body);
|
|
173
|
+
if (m)
|
|
174
|
+
return { name: m[1] };
|
|
175
|
+
m = new RegExp(`^#${NAME_OR_POSITION}$`).exec(body);
|
|
176
|
+
if (m)
|
|
177
|
+
return { name: m[1], op: { kind: "length" } };
|
|
178
|
+
m = new RegExp(`^${NAME_OR_POSITION}(:?)([-+])(.*)$`).exec(body);
|
|
179
|
+
if (m && PARAM_WORD.test(m[4]))
|
|
180
|
+
return {
|
|
181
|
+
name: m[1],
|
|
182
|
+
op: { kind: m[3] === "-" ? "default" : "alternate", word: m[4] },
|
|
183
|
+
};
|
|
184
|
+
m = new RegExp(`^${NAME_OR_POSITION}(##?|%%?)(.*)$`).exec(body);
|
|
185
|
+
if (m && PARAM_PATTERN.test(m[3])) {
|
|
186
|
+
const op = m[2];
|
|
187
|
+
return {
|
|
188
|
+
name: m[1],
|
|
189
|
+
op: {
|
|
190
|
+
kind: "strip",
|
|
191
|
+
suffix: op.startsWith("%"),
|
|
192
|
+
longest: op.length === 2,
|
|
193
|
+
pattern: m[3],
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
m = new RegExp(`^${NAME_OR_POSITION}:([0-9]+)(?::([0-9]+))?$`).exec(body);
|
|
198
|
+
if (m)
|
|
199
|
+
return {
|
|
200
|
+
name: m[1],
|
|
201
|
+
op: {
|
|
202
|
+
kind: "slice",
|
|
203
|
+
offset: Number(m[2]),
|
|
204
|
+
...(m[3] === undefined ? {} : { length: Number(m[3]) }),
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
return undefined;
|
|
208
|
+
}
|
|
209
|
+
function globRegex(pattern) {
|
|
210
|
+
const source = [...pattern]
|
|
211
|
+
.map((c) => (c === "*" ? ".*" : c === "?" ? "." : c.replace(/[.+^${}()|[\]\\/-]/g, "\\$&")))
|
|
212
|
+
.join("");
|
|
213
|
+
return new RegExp(`^${source}$`, "s");
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Applies a parameter operation to a value that is set and not empty, which is the only kind of
|
|
217
|
+
* value the evaluator knows.
|
|
218
|
+
*/
|
|
219
|
+
export function applyParamOp(value, op) {
|
|
220
|
+
if (!op || op.kind === "default")
|
|
221
|
+
return value;
|
|
222
|
+
if (op.kind === "alternate")
|
|
223
|
+
return op.word;
|
|
224
|
+
if (op.kind === "length")
|
|
225
|
+
return String([...value].length);
|
|
226
|
+
if (op.kind === "slice")
|
|
227
|
+
return value.slice(op.offset, op.length === undefined ? undefined : op.offset + op.length);
|
|
228
|
+
const regex = globRegex(op.pattern);
|
|
229
|
+
const lengths = [...Array(value.length + 1).keys()];
|
|
230
|
+
if (op.longest)
|
|
231
|
+
lengths.reverse();
|
|
232
|
+
for (const n of lengths) {
|
|
233
|
+
const piece = op.suffix ? value.slice(value.length - n) : value.slice(0, n);
|
|
234
|
+
if (regex.test(piece))
|
|
235
|
+
return op.suffix ? value.slice(0, value.length - n) : value.slice(n);
|
|
236
|
+
}
|
|
237
|
+
return value;
|
|
238
|
+
}
|
|
239
|
+
const ANSI_ESCAPES = {
|
|
240
|
+
a: "\x07",
|
|
241
|
+
b: "\b",
|
|
242
|
+
e: "\x1b",
|
|
243
|
+
E: "\x1b",
|
|
244
|
+
f: "\f",
|
|
245
|
+
n: "\n",
|
|
246
|
+
r: "\r",
|
|
247
|
+
t: "\t",
|
|
248
|
+
v: "\v",
|
|
249
|
+
"\\": "\\",
|
|
250
|
+
'"': '"',
|
|
251
|
+
"?": "?",
|
|
252
|
+
};
|
|
253
|
+
/**
|
|
254
|
+
* `$'...'` starting at `command[at] === "$"`. `\'` is refused so that the plain single-quote scan
|
|
255
|
+
* used to find the end of a substitution agrees with the shell; so are NUL, `\c`, `\u` and `\U`.
|
|
256
|
+
*/
|
|
257
|
+
function ansiString(command, at) {
|
|
258
|
+
let text = "";
|
|
259
|
+
for (let i = at + 2; i < command.length; i++) {
|
|
260
|
+
const c = command[i];
|
|
261
|
+
if (c === "'") {
|
|
262
|
+
if (text.includes(SUBSTITUTION) || text.includes("\0"))
|
|
263
|
+
return undefined;
|
|
264
|
+
return { kind: "text", text, next: i + 1, quoted: true };
|
|
265
|
+
}
|
|
266
|
+
if (c !== "\\") {
|
|
267
|
+
text += c;
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
const e = command[i + 1];
|
|
271
|
+
if (e === undefined || e === "'" || e === "c" || e === "u" || e === "U")
|
|
272
|
+
return undefined;
|
|
273
|
+
const known = ANSI_ESCAPES[e];
|
|
274
|
+
if (known !== undefined) {
|
|
275
|
+
text += known;
|
|
276
|
+
i++;
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
const hex = e === "x" ? /^[0-9A-Fa-f]{1,2}/.exec(command.slice(i + 2)) : null;
|
|
280
|
+
const octal = /^[0-7]{1,3}/.exec(command.slice(i + 1));
|
|
281
|
+
if (hex) {
|
|
282
|
+
text += String.fromCharCode(Number.parseInt(hex[0], 16));
|
|
283
|
+
i += 1 + hex[0].length;
|
|
284
|
+
}
|
|
285
|
+
else if (octal) {
|
|
286
|
+
text += String.fromCharCode(Number.parseInt(octal[0], 8) & 0xff);
|
|
287
|
+
i += octal[0].length;
|
|
288
|
+
}
|
|
289
|
+
else
|
|
290
|
+
text += c; // an unknown escape keeps its backslash; the next character follows
|
|
291
|
+
}
|
|
292
|
+
return undefined;
|
|
293
|
+
}
|
|
166
294
|
/**
|
|
167
295
|
* Reads the expansion that starts at `command[at] === "$"`, or undefined for one that is not
|
|
168
296
|
* understood. Only HOME has a value known before anything runs; `$?`, `$#`, `$$` and PIPESTATUS
|
|
@@ -191,18 +319,34 @@ function expansion(command, at, quoted = false) {
|
|
|
191
319
|
return undefined;
|
|
192
320
|
return { kind: "unknown", next: end + 1, inner: command.slice(at + 2, end) };
|
|
193
321
|
}
|
|
322
|
+
if (rest[0] === "'" && !quoted)
|
|
323
|
+
return ansiString(command, at);
|
|
194
324
|
let name;
|
|
325
|
+
let op;
|
|
195
326
|
let next;
|
|
196
327
|
if (rest[0] === "{") {
|
|
197
328
|
const close = rest.indexOf("}");
|
|
198
|
-
|
|
329
|
+
if (close <= 0)
|
|
330
|
+
return undefined;
|
|
331
|
+
const body = rest.slice(1, close);
|
|
199
332
|
next = at + close + 2;
|
|
200
|
-
if (
|
|
333
|
+
if (UNKNOWN_PARAMETER.test(body))
|
|
334
|
+
return { kind: "unknown", next };
|
|
335
|
+
const parsed = parameter(body);
|
|
336
|
+
if (!parsed)
|
|
201
337
|
return undefined;
|
|
338
|
+
({ name, op } = parsed);
|
|
339
|
+
}
|
|
340
|
+
else if (rest[0] && /[0-9]/.test(rest[0]) && /[0-9]/.test(rest[1] ?? "")) {
|
|
341
|
+
return undefined; // `$10` is `$1` then `0` in bash and sh, the tenth parameter in zsh
|
|
202
342
|
}
|
|
203
|
-
else if (rest[0] && "?#$".includes(rest[0])) {
|
|
343
|
+
else if (rest[0] && "?#$0".includes(rest[0])) {
|
|
204
344
|
return { kind: "unknown", next: at + 2 };
|
|
205
345
|
}
|
|
346
|
+
else if (rest[0] && /[1-9@*]/.test(rest[0])) {
|
|
347
|
+
name = rest[0];
|
|
348
|
+
next = at + 2;
|
|
349
|
+
}
|
|
206
350
|
else {
|
|
207
351
|
name = /^[A-Za-z_][A-Za-z0-9_]*/.exec(rest)?.[0];
|
|
208
352
|
if (!name)
|
|
@@ -213,9 +357,9 @@ function expansion(command, at, quoted = false) {
|
|
|
213
357
|
return { kind: "unknown", next };
|
|
214
358
|
if (name === "HOME") {
|
|
215
359
|
const text = home();
|
|
216
|
-
return text === undefined ? undefined : { kind: "text", text, next };
|
|
360
|
+
return text === undefined ? undefined : { kind: "text", text: applyParamOp(text, op), next };
|
|
217
361
|
}
|
|
218
|
-
return { kind: "var", name, next };
|
|
362
|
+
return op ? { kind: "var", name, next, op } : { kind: "var", name, next };
|
|
219
363
|
}
|
|
220
364
|
function tokenize(src) {
|
|
221
365
|
const tokens = [];
|
|
@@ -227,7 +371,8 @@ function tokenize(src) {
|
|
|
227
371
|
let quotedAny = false;
|
|
228
372
|
let headPlain = true;
|
|
229
373
|
let assignName;
|
|
230
|
-
let
|
|
374
|
+
let braceOpen = false;
|
|
375
|
+
let braceExpansion = false;
|
|
231
376
|
let globChar = false;
|
|
232
377
|
let quote = "";
|
|
233
378
|
let pendingRedirect;
|
|
@@ -245,8 +390,8 @@ function tokenize(src) {
|
|
|
245
390
|
if (!active)
|
|
246
391
|
return;
|
|
247
392
|
flush();
|
|
248
|
-
|
|
249
|
-
if (
|
|
393
|
+
// `{a,b}` and `{1..3}` expand to several words; `{}`, `@{u}` and a lone `{` are literal.
|
|
394
|
+
if (braceExpansion)
|
|
250
395
|
reject();
|
|
251
396
|
const word = { parts, subs, quoted: quotedAny, assign: assignName, glob: globChar };
|
|
252
397
|
parts = [];
|
|
@@ -255,7 +400,8 @@ function tokenize(src) {
|
|
|
255
400
|
quotedAny = false;
|
|
256
401
|
headPlain = true;
|
|
257
402
|
assignName = undefined;
|
|
258
|
-
|
|
403
|
+
braceOpen = false;
|
|
404
|
+
braceExpansion = false;
|
|
259
405
|
globChar = false;
|
|
260
406
|
if (pendingRedirect) {
|
|
261
407
|
tokens.push({ t: "redir", redir: { ...pendingRedirect, target: word } });
|
|
@@ -277,11 +423,14 @@ function tokenize(src) {
|
|
|
277
423
|
subs.push(inner);
|
|
278
424
|
};
|
|
279
425
|
const appendExpansion = (e, quoted) => {
|
|
280
|
-
if (e.kind === "text")
|
|
426
|
+
if (e.kind === "text") {
|
|
281
427
|
lit += e.text;
|
|
428
|
+
if (e.quoted)
|
|
429
|
+
quotedAny = true;
|
|
430
|
+
}
|
|
282
431
|
else if (e.kind === "var") {
|
|
283
432
|
flush();
|
|
284
|
-
parts.push({ name: e.name, quoted });
|
|
433
|
+
parts.push(e.op ? { name: e.name, quoted, op: e.op } : { name: e.name, quoted });
|
|
285
434
|
}
|
|
286
435
|
else
|
|
287
436
|
appendUnknown(quoted, e.inner);
|
|
@@ -382,9 +531,12 @@ function tokenize(src) {
|
|
|
382
531
|
i++;
|
|
383
532
|
continue;
|
|
384
533
|
}
|
|
534
|
+
// History expansion is off in the non-interactive shells that run these commands, but `!(`
|
|
535
|
+
// is an extended glob and `!!`/`!$` are worth no risk; `!=` is a comparison operator.
|
|
385
536
|
if (char === "!" &&
|
|
386
|
-
!(active === false && (src[i + 1] === undefined || /\s/.test(src[i + 1])))
|
|
387
|
-
|
|
537
|
+
!(active === false && (src[i + 1] === undefined || /\s/.test(src[i + 1]))) &&
|
|
538
|
+
src[i + 1] !== "=") {
|
|
539
|
+
reject();
|
|
388
540
|
}
|
|
389
541
|
if (char === " " || char === "\t") {
|
|
390
542
|
endWord();
|
|
@@ -453,7 +605,14 @@ function tokenize(src) {
|
|
|
453
605
|
continue;
|
|
454
606
|
}
|
|
455
607
|
if (char === "(") {
|
|
456
|
-
|
|
608
|
+
// `name()` starts a function definition; the parser checks the rest.
|
|
609
|
+
const definition = active &&
|
|
610
|
+
src[i + 1] === ")" &&
|
|
611
|
+
parts.length === 0 &&
|
|
612
|
+
!quotedAny &&
|
|
613
|
+
assignName === undefined &&
|
|
614
|
+
NAME.test(lit);
|
|
615
|
+
if ((active && !definition) || src[i + 1] === "(")
|
|
457
616
|
reject();
|
|
458
617
|
operator("(");
|
|
459
618
|
continue;
|
|
@@ -468,8 +627,10 @@ function tokenize(src) {
|
|
|
468
627
|
if (parts.length === 0 && /^[0-9]{2,}$/.test(lit))
|
|
469
628
|
reject();
|
|
470
629
|
}
|
|
471
|
-
if (char === "{"
|
|
472
|
-
|
|
630
|
+
if (char === "{")
|
|
631
|
+
braceOpen = true;
|
|
632
|
+
else if (braceOpen && (char === "," || (char === "." && src[i + 1] === ".")))
|
|
633
|
+
braceExpansion = true;
|
|
473
634
|
if (char === "*" || char === "?" || char === "[")
|
|
474
635
|
globChar = true;
|
|
475
636
|
if (char === ">" || char === "<") {
|
|
@@ -561,6 +722,19 @@ function tokenize(src) {
|
|
|
561
722
|
}
|
|
562
723
|
if (assignName === undefined && !/[A-Za-z0-9_]/.test(char))
|
|
563
724
|
headPlain = false;
|
|
725
|
+
// An assignment value starting with `~` or `~/` is the home directory.
|
|
726
|
+
if (char === "~" &&
|
|
727
|
+
assignName !== undefined &&
|
|
728
|
+
parts.length === 0 &&
|
|
729
|
+
lit === "" &&
|
|
730
|
+
!quotedAny &&
|
|
731
|
+
(src[i + 1] === undefined || /[/\s;&|)]/.test(src[i + 1]))) {
|
|
732
|
+
const path = homedir();
|
|
733
|
+
if (/[\s*?[\]'"$`~]/.test(path))
|
|
734
|
+
reject();
|
|
735
|
+
lit += path;
|
|
736
|
+
continue;
|
|
737
|
+
}
|
|
564
738
|
lit += char;
|
|
565
739
|
active = true;
|
|
566
740
|
}
|
|
@@ -733,6 +907,24 @@ class Parser {
|
|
|
733
907
|
return { kind: "subshell", body, redirects: this.redirects() };
|
|
734
908
|
}
|
|
735
909
|
const word = this.keyword(tok);
|
|
910
|
+
const open = this.tokens[this.pos + 1];
|
|
911
|
+
const close = this.tokens[this.pos + 2];
|
|
912
|
+
if (word === undefined &&
|
|
913
|
+
tok?.t === "word" &&
|
|
914
|
+
open?.t === "op" &&
|
|
915
|
+
open.op === "(" &&
|
|
916
|
+
close?.t === "op" &&
|
|
917
|
+
close.op === ")") {
|
|
918
|
+
const name = plainWord(tok.w);
|
|
919
|
+
if (name === undefined || !NAME.test(name) || depth >= MAX_DEPTH)
|
|
920
|
+
reject();
|
|
921
|
+
this.pos += 3;
|
|
922
|
+
this.skipNewlines();
|
|
923
|
+
const body = this.command(depth + 1);
|
|
924
|
+
if ((body.kind !== "group" && body.kind !== "subshell") || body.redirects.length)
|
|
925
|
+
reject();
|
|
926
|
+
return { kind: "function", name: name, body: body };
|
|
927
|
+
}
|
|
736
928
|
if (word === undefined)
|
|
737
929
|
return this.simple();
|
|
738
930
|
if (depth >= MAX_DEPTH)
|
|
@@ -774,7 +966,13 @@ class Parser {
|
|
|
774
966
|
this.expectKeyword("do");
|
|
775
967
|
const body = this.list(new Set(["done"]), depth + 1);
|
|
776
968
|
this.expectKeyword("done");
|
|
777
|
-
return {
|
|
969
|
+
return {
|
|
970
|
+
kind: "while",
|
|
971
|
+
cond,
|
|
972
|
+
body,
|
|
973
|
+
redirects: this.redirects(),
|
|
974
|
+
...(word === "until" ? { until: true } : {}),
|
|
975
|
+
};
|
|
778
976
|
}
|
|
779
977
|
case "if": {
|
|
780
978
|
const clauses = [];
|
package/dist/service.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ interface Message {
|
|
|
5
5
|
request?: Request;
|
|
6
6
|
classification?: Classification;
|
|
7
7
|
report?: OutcomeReport;
|
|
8
|
+
phase?: string;
|
|
8
9
|
}
|
|
9
10
|
export declare function serve(harness: string, sessionId: string): Promise<void>;
|
|
10
11
|
export declare function send(harness: string, sessionId: string, message: Omit<Message, "token">): Promise<unknown>;
|
package/dist/service.js
CHANGED
|
@@ -3,7 +3,7 @@ import { chmodSync, existsSync } from "node:fs";
|
|
|
3
3
|
import { createConnection, createServer } from "node:net";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { audit, sessionDirectory, sessionDirectoryPath, sessionToken } from "./audit.js";
|
|
6
|
-
import { hash } from "./policy.js";
|
|
6
|
+
import { hash, requestHash } from "./policy.js";
|
|
7
7
|
import { loadProfile, strictProfile } from "./profile.js";
|
|
8
8
|
import { PolicySession } from "./session.js";
|
|
9
9
|
function endpoint(directory) {
|
|
@@ -44,7 +44,7 @@ export async function serve(harness, sessionId) {
|
|
|
44
44
|
event: "request-envelope",
|
|
45
45
|
payloadHash: hash(data.slice(0, end)),
|
|
46
46
|
...(message.request
|
|
47
|
-
? { requestHash:
|
|
47
|
+
? { requestHash: requestHash(message.request) }
|
|
48
48
|
: message.classification
|
|
49
49
|
? { requestHash: message.classification.requestHash }
|
|
50
50
|
: message.report
|
|
@@ -64,7 +64,7 @@ export async function serve(harness, sessionId) {
|
|
|
64
64
|
server.close();
|
|
65
65
|
}
|
|
66
66
|
else if (message.action === "review" && message.request)
|
|
67
|
-
result = session.review(message.request);
|
|
67
|
+
result = session.review(message.request, undefined, message.phase);
|
|
68
68
|
else if (message.action === "classify" && message.classification)
|
|
69
69
|
result = session.classify(message.classification);
|
|
70
70
|
else if (message.action === "record" && message.report) {
|