passport-bridge 0.13.2 → 0.14.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 +92 -17
- package/dist/cli.js +4 -3
- package/dist/cliArgs.d.ts +1 -1
- package/dist/cliArgs.js +21 -5
- package/dist/cliAutopilot.d.ts +60 -0
- package/dist/cliAutopilot.js +389 -0
- package/dist/cliAutopilotPolicy.d.ts +94 -0
- package/dist/cliAutopilotPolicy.js +193 -0
- package/dist/cliClient.d.ts +7 -0
- package/dist/cliClient.js +36 -19
- package/dist/cliHook.d.ts +20 -9
- package/dist/cliHook.js +125 -32
- package/dist/cliHookClassify.d.ts +3 -0
- package/dist/cliHookClassify.js +2 -1
- package/dist/cliHookDecide.d.ts +89 -0
- package/dist/cliHookDecide.js +167 -0
- package/dist/cliHookEnvironment.d.ts +55 -0
- package/dist/cliHookEnvironment.js +219 -0
- package/dist/cliHookGuard.d.ts +61 -12
- package/dist/cliHookGuard.js +211 -24
- package/dist/cliHookInstall.js +19 -4
- package/dist/cliHookMode.d.ts +31 -0
- package/dist/cliHookMode.js +50 -0
- package/dist/cliHookRedact.d.ts +16 -0
- package/dist/cliHookRedact.js +136 -0
- package/dist/cliInit.d.ts +4 -1
- package/dist/cliInit.js +10 -3
- package/dist/cliLogin.js +18 -2
- package/dist/cliOffline.js +9 -9
- package/dist/cliOutput.js +11 -5
- package/dist/cliPolicy.d.ts +11 -3
- package/dist/cliPolicy.js +5 -2
- package/dist/cliPolicyV2.d.ts +18 -0
- package/dist/cliPolicyV2.js +234 -0
- package/dist/cliSetupOrg.d.ts +15 -0
- package/dist/cliSetupOrg.js +241 -0
- package/dist/cliSupport.d.ts +4 -0
- package/dist/cliSupport.js +12 -9
- package/dist/completion.js +12 -3
- package/dist/doctor.js +3 -3
- package/dist/enrollment.d.ts +18 -0
- package/dist/enrollment.js +79 -0
- package/dist/hookPlugin.d.ts +7 -0
- package/dist/hookPlugin.js +54 -0
- package/dist/initAgents.d.ts +3 -1
- package/dist/initAgents.js +7 -3
- package/dist/initAutopilotStep.d.ts +24 -0
- package/dist/initAutopilotStep.js +257 -0
- package/dist/initLocalSteps.d.ts +3 -0
- package/dist/initLocalSteps.js +30 -5
- package/dist/initState.d.ts +24 -0
- package/dist/initUi.js +5 -0
- package/dist/passport.js +27 -3
- package/dist/pluginPin.d.ts +7 -0
- package/dist/pluginPin.js +7 -0
- package/dist/universalUrl.d.ts +28 -0
- package/dist/universalUrl.js +59 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
package/dist/cliClient.d.ts
CHANGED
|
@@ -33,6 +33,13 @@ type GatewayOptions = {
|
|
|
33
33
|
timeoutMs: number;
|
|
34
34
|
signal?: AbortSignal;
|
|
35
35
|
};
|
|
36
|
+
export type MemberApi = {
|
|
37
|
+
origin: string;
|
|
38
|
+
fetchApi: (path: string, init?: RequestInit) => Promise<Response>;
|
|
39
|
+
};
|
|
40
|
+
/** Same-origin REST calls without an MCP session, for commands that only
|
|
41
|
+
* need the member API (`passport wait`, `sessions`, `rules test`). */
|
|
42
|
+
export declare function memberApi(options: GatewayOptions): MemberApi;
|
|
36
43
|
/** One authenticated MCP session for one CLI invocation. */
|
|
37
44
|
export declare class CliGateway {
|
|
38
45
|
private readonly client;
|
package/dist/cliClient.js
CHANGED
|
@@ -38,6 +38,41 @@ function decorateFetch(base, headersToAdd) {
|
|
|
38
38
|
return base(input, { ...init, headers });
|
|
39
39
|
});
|
|
40
40
|
}
|
|
41
|
+
/** The credentialed fetch every CLI request uses: an agent key as a bearer, or
|
|
42
|
+
* a profile's rotating refresh token exchanged (and rotations persisted)
|
|
43
|
+
* under the profile lock. */
|
|
44
|
+
function authedFetchFor(options) {
|
|
45
|
+
if (options.auth.kind === "agent-key") {
|
|
46
|
+
return decorateFetch(fetch, {
|
|
47
|
+
authorization: `Bearer ${options.auth.agentKey}`,
|
|
48
|
+
"x-passport-surface": "cli",
|
|
49
|
+
...(options.caller ? { "x-passport-caller": options.caller } : {})
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
const { endpoint, profile } = options.auth;
|
|
53
|
+
const manager = new TokenManager(endpoint, options.auth.refreshToken, (next, previous) => persistCurrentProfileRotation(profile, previous, next, endpoint), {
|
|
54
|
+
reload: () => recoverProfileRotation(profile)?.refreshToken,
|
|
55
|
+
lock: (fn) => withProfileLock(profile, fn)
|
|
56
|
+
}, options.timeoutMs, options.signal);
|
|
57
|
+
return decorateFetch(manager.authedFetch, {
|
|
58
|
+
"x-passport-surface": "cli",
|
|
59
|
+
...(options.caller ? { "x-passport-caller": options.caller } : {})
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/** Same-origin REST calls without an MCP session, for commands that only
|
|
63
|
+
* need the member API (`passport wait`, `sessions`, `rules test`). */
|
|
64
|
+
export function memberApi(options) {
|
|
65
|
+
const origin = new URL(options.auth.endpoint).origin;
|
|
66
|
+
const authed = authedFetchFor(options);
|
|
67
|
+
return {
|
|
68
|
+
origin,
|
|
69
|
+
fetchApi: (path, init) => {
|
|
70
|
+
if (!path.startsWith("/") || path.startsWith("//"))
|
|
71
|
+
throw new Error("Passport API path must be relative.");
|
|
72
|
+
return authed(`${origin}${path}`, init);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
41
76
|
/** One authenticated MCP session for one CLI invocation. */
|
|
42
77
|
export class CliGateway {
|
|
43
78
|
client = new Client({ name: "passport-cli", version: VERSION });
|
|
@@ -51,25 +86,7 @@ export class CliGateway {
|
|
|
51
86
|
this.timeoutMs = options.timeoutMs;
|
|
52
87
|
this.signal = options.signal;
|
|
53
88
|
this.origin = new URL(options.auth.endpoint).origin;
|
|
54
|
-
|
|
55
|
-
if (options.auth.kind === "agent-key") {
|
|
56
|
-
authedFetch = decorateFetch(fetch, {
|
|
57
|
-
authorization: `Bearer ${options.auth.agentKey}`,
|
|
58
|
-
"x-passport-surface": "cli",
|
|
59
|
-
...(options.caller ? { "x-passport-caller": options.caller } : {})
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
const { endpoint, profile } = options.auth;
|
|
64
|
-
const manager = new TokenManager(endpoint, options.auth.refreshToken, (next, previous) => persistCurrentProfileRotation(profile, previous, next, endpoint), {
|
|
65
|
-
reload: () => recoverProfileRotation(profile)?.refreshToken,
|
|
66
|
-
lock: (fn) => withProfileLock(profile, fn)
|
|
67
|
-
}, options.timeoutMs, options.signal);
|
|
68
|
-
authedFetch = decorateFetch(manager.authedFetch, {
|
|
69
|
-
"x-passport-surface": "cli",
|
|
70
|
-
...(options.caller ? { "x-passport-caller": options.caller } : {})
|
|
71
|
-
});
|
|
72
|
-
}
|
|
89
|
+
const authedFetch = authedFetchFor(options);
|
|
73
90
|
this.apiFetch = authedFetch;
|
|
74
91
|
this.transport = new StreamableHTTPClientTransport(new URL(options.auth.endpoint), {
|
|
75
92
|
fetch: boundedMcpFetch(authedFetch)
|
package/dist/cliHook.d.ts
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
*/
|
|
17
17
|
import { type ParsedArgs } from "./cliArgs.js";
|
|
18
18
|
import { type CliFinding } from "./cliHookClassify.js";
|
|
19
|
-
import { type GuardDecision } from "./cliHookGuard.js";
|
|
19
|
+
import { type DecidedFinding, type GuardDecision } from "./cliHookGuard.js";
|
|
20
20
|
import { type CommandResult, type Env, type Runtime } from "./cliSupport.js";
|
|
21
21
|
/** Claude Code, Codex, and Cursor fail open on a slow hook, so the budget is ours. */
|
|
22
22
|
export declare const HOOK_BUDGET_MS = 1500;
|
|
@@ -29,22 +29,31 @@ export type CliHookReport = {
|
|
|
29
29
|
cwdName?: string;
|
|
30
30
|
exitCode?: number;
|
|
31
31
|
durationMs?: number;
|
|
32
|
-
findings:
|
|
33
|
-
vendor: string;
|
|
34
|
-
verb: string;
|
|
35
|
-
risk: string;
|
|
36
|
-
}>;
|
|
32
|
+
findings: ReportedFinding[];
|
|
37
33
|
opaque: boolean;
|
|
38
34
|
opaqueKinds?: string[];
|
|
39
|
-
/** What the guard answered before the command ran.
|
|
40
|
-
|
|
35
|
+
/** What the guard answered before the command ran. "allow" only with a v2
|
|
36
|
+
* (Autopilot) policy; the server works out the cause and class itself. */
|
|
37
|
+
decision?: "allow" | "ask" | "deny";
|
|
41
38
|
/** With decision "ask": true when the command then ran (one row, "asked
|
|
42
39
|
* first, then ran"), false when no run followed (a declined prompt).
|
|
43
40
|
* Absent when the agent gives no tool-use id to match on (Cursor). */
|
|
44
41
|
ran?: boolean;
|
|
45
42
|
};
|
|
43
|
+
/** One governed command as reported: never its arguments. v2 adds the
|
|
44
|
+
* environment: a short name the flags gave, or a kind. */
|
|
45
|
+
export type ReportedFinding = {
|
|
46
|
+
vendor: string;
|
|
47
|
+
verb: string;
|
|
48
|
+
risk: string;
|
|
49
|
+
environment?: string;
|
|
50
|
+
};
|
|
46
51
|
export type HookInput = {
|
|
47
52
|
session_id?: unknown;
|
|
53
|
+
/** Claude Code and Codex: prompt-free modes mean nobody is asked. */
|
|
54
|
+
permission_mode?: unknown;
|
|
55
|
+
/** Cursor: background and cloud agents. */
|
|
56
|
+
is_background_agent?: unknown;
|
|
48
57
|
cwd?: unknown;
|
|
49
58
|
hook_event_name?: unknown;
|
|
50
59
|
tool_name?: unknown;
|
|
@@ -93,11 +102,13 @@ export declare function outcomeOf(response: unknown): {
|
|
|
93
102
|
exitCode?: number;
|
|
94
103
|
durationMs?: number;
|
|
95
104
|
};
|
|
96
|
-
export declare function buildReport(input: HookInput, findings: CliFinding[], env: Env, declared?: string): CliHookReport | null;
|
|
105
|
+
export declare function buildReport(input: HookInput, findings: CliFinding[], env: Env, declared?: string, decided?: DecidedFinding[]): CliHookReport | null;
|
|
97
106
|
/** What the guard answered, and for which findings. The audit row that
|
|
98
107
|
* follows a command that ran carries its outcome and any opaque parts. */
|
|
99
108
|
export declare function buildDecisionReport(input: HookInput, decision: GuardDecision, env: Env, declared?: string): CliHookReport | null;
|
|
100
109
|
/** Normalise one stdin event into the command and when it runs. */
|
|
101
110
|
export declare function shellEvent(input: HookInput): ShellEvent | null;
|
|
111
|
+
/** The report an older server accepts: no Autopilot fields, ask or deny only. */
|
|
112
|
+
export declare function legacyReport(report: CliHookReport): CliHookReport | null;
|
|
102
113
|
export declare function hook(args: ParsedArgs, runtime: Runtime, deps?: HookDependencies): Promise<CommandResult>;
|
|
103
114
|
export {};
|
package/dist/cliHook.js
CHANGED
|
@@ -16,7 +16,10 @@
|
|
|
16
16
|
*/
|
|
17
17
|
import { value } from "./cliArgs.js";
|
|
18
18
|
import { classifyCommand, summarize } from "./cliHookClassify.js";
|
|
19
|
-
import {
|
|
19
|
+
import { planGuard } from "./cliHookDecide.js";
|
|
20
|
+
import { decideGuard } from "./cliHookGuard.js";
|
|
21
|
+
import { detectMode } from "./cliHookMode.js";
|
|
22
|
+
import { hookViaPlugin } from "./hookPlugin.js";
|
|
20
23
|
import { INTERPRETERS } from "./cliHookParse.js";
|
|
21
24
|
import { hasExpired, holdAsk, takeAsk, takeExpired } from "./cliHookPending.js";
|
|
22
25
|
import { claimStaleRefresh, readCliPolicy, refreshCliPolicy } from "./cliPolicy.js";
|
|
@@ -140,18 +143,30 @@ function context(input, env, declared) {
|
|
|
140
143
|
...(cwdName ? { cwdName } : {})
|
|
141
144
|
};
|
|
142
145
|
}
|
|
143
|
-
function reportable(findings) {
|
|
146
|
+
function reportable(findings, decided = []) {
|
|
144
147
|
return findings
|
|
145
148
|
.filter((finding) => Boolean(finding.vendor))
|
|
146
149
|
.filter((finding) => VENDOR_NAME.test(finding.vendor))
|
|
147
150
|
.slice(0, MAX_FINDINGS)
|
|
148
|
-
.map((finding) =>
|
|
151
|
+
.map((finding) => {
|
|
152
|
+
const entry = decided.find((candidate) => candidate.finding === finding);
|
|
153
|
+
return {
|
|
154
|
+
vendor: finding.vendor,
|
|
155
|
+
verb: safeVerb(finding.verb),
|
|
156
|
+
risk: finding.risk,
|
|
157
|
+
...(entry?.reported ? { environment: entry.reported } : {})
|
|
158
|
+
};
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/** A v2 server records the guard's allow; everything else it decides itself. */
|
|
162
|
+
function autopilotDecision(decision) {
|
|
163
|
+
return decision?.cause ? { decision: decision.verdict } : {};
|
|
149
164
|
}
|
|
150
|
-
export function buildReport(input, findings, env, declared) {
|
|
165
|
+
export function buildReport(input, findings, env, declared, decided = []) {
|
|
151
166
|
const summary = summarize(findings);
|
|
152
167
|
if (!summary.governed.length && !summary.opaque)
|
|
153
168
|
return null;
|
|
154
|
-
const governed = reportable(summary.governed);
|
|
169
|
+
const governed = reportable(summary.governed, decided);
|
|
155
170
|
if (!governed.length && !summary.opaque)
|
|
156
171
|
return null;
|
|
157
172
|
const kinds = [...new Set(summary.unknown.map(opaqueKind))].slice(0, MAX_OPAQUE_KINDS);
|
|
@@ -166,7 +181,7 @@ export function buildReport(input, findings, env, declared) {
|
|
|
166
181
|
/** What the guard answered, and for which findings. The audit row that
|
|
167
182
|
* follows a command that ran carries its outcome and any opaque parts. */
|
|
168
183
|
export function buildDecisionReport(input, decision, env, declared) {
|
|
169
|
-
const findings = reportable(decision.findings);
|
|
184
|
+
const findings = reportable(decision.findings, decision.decided);
|
|
170
185
|
if (!findings.length)
|
|
171
186
|
return null;
|
|
172
187
|
return { ...context(input, env, declared), findings, opaque: false, decision: decision.verdict };
|
|
@@ -278,14 +293,28 @@ async function profileApi(profile, signal, fetcher) {
|
|
|
278
293
|
const manager = new TokenManager(saved.endpoint, saved.refreshToken, undefined, undefined, HOOK_BUDGET_MS, signal, fetcher);
|
|
279
294
|
return (path, init) => manager.authedFetch(`${origin}${path}`, init);
|
|
280
295
|
}
|
|
296
|
+
/** The report an older server accepts: no Autopilot fields, ask or deny only. */
|
|
297
|
+
export function legacyReport(report) {
|
|
298
|
+
if (report.decision === "allow")
|
|
299
|
+
return null;
|
|
300
|
+
return { ...report, findings: report.findings.map(({ vendor, verb, risk }) => ({ vendor, verb, risk })) };
|
|
301
|
+
}
|
|
302
|
+
function hasAutopilotFields(report) {
|
|
303
|
+
return report.decision === "allow" || report.findings.some((finding) => finding.environment !== undefined);
|
|
304
|
+
}
|
|
281
305
|
async function postWith(api, report, signal) {
|
|
282
|
-
const
|
|
306
|
+
const post = (body) => api(CLI_ACTIVITY_PATH, {
|
|
283
307
|
method: "POST",
|
|
284
308
|
headers: { "content-type": "application/json", "x-passport-surface": "cli" },
|
|
285
|
-
body: JSON.stringify(
|
|
309
|
+
body: JSON.stringify(body),
|
|
286
310
|
signal
|
|
287
311
|
});
|
|
312
|
+
const response = await post(report);
|
|
288
313
|
await response.body?.cancel().catch(() => undefined);
|
|
314
|
+
// A server that refuses the Autopilot fields still gets the row it knows.
|
|
315
|
+
const legacy = response.status === 400 && hasAutopilotFields(report) ? legacyReport(report) : null;
|
|
316
|
+
if (legacy)
|
|
317
|
+
await (await post(legacy)).body?.cancel().catch(() => undefined);
|
|
289
318
|
}
|
|
290
319
|
/** Deliver reports with the same credential `passport discover` uses. Agent
|
|
291
320
|
* keys are deliberately unsupported: a hook reports a person's own shell.
|
|
@@ -310,10 +339,16 @@ async function reportSender(args, runtime, signal, deps) {
|
|
|
310
339
|
function expiredAsks(now) {
|
|
311
340
|
if (!hasExpired(now))
|
|
312
341
|
return [];
|
|
313
|
-
return takeExpired(now)
|
|
342
|
+
return takeExpired(now)
|
|
343
|
+
.map((parked) => parked.report)
|
|
344
|
+
.filter((report) => report.decision === "ask")
|
|
345
|
+
.map((report) => ({ ...report, ran: false }));
|
|
314
346
|
}
|
|
315
347
|
/** Only hold an ask when the audit hook will be there to claim it. */
|
|
316
348
|
async function claudeAuditRegistered(runtime) {
|
|
349
|
+
// The Passport plugin ships the audit hook alongside this guard.
|
|
350
|
+
if (hookViaPlugin(runtime.env))
|
|
351
|
+
return true;
|
|
317
352
|
try {
|
|
318
353
|
const { registeredEntries } = await import("./cliHookInstall.js");
|
|
319
354
|
return registeredEntries("claude-code", runtime.env).audit !== null;
|
|
@@ -350,12 +385,22 @@ async function runAudit(args, runtime, deps) {
|
|
|
350
385
|
return;
|
|
351
386
|
if (event.cursorImported && (await cursorHasOwnEntry(runtime, "audit")))
|
|
352
387
|
return;
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
388
|
+
const findings = classifyCommand(event.command);
|
|
389
|
+
const profile = localProfile(args, runtime.env);
|
|
390
|
+
const policy = profile ? readCliPolicy(profile) : null;
|
|
391
|
+
// v2: the same decision the guard made, so the row says why it ran.
|
|
392
|
+
const decision = policy?.version === 2 ? decideGuard(findings, policy, event.command) : null;
|
|
393
|
+
let report = buildReport(input, findings, runtime.env, value(args, "client"), decision?.decided);
|
|
394
|
+
if (report && policy?.version === 2) {
|
|
395
|
+
report = { ...report, ...autopilotDecision(decision) };
|
|
396
|
+
}
|
|
397
|
+
// The guard parked what it decided before this exact command: send one row
|
|
398
|
+
// for both, "asked first, then ran" (or the server's own allow).
|
|
356
399
|
const toolUseId = text(input.tool_use_id, 128);
|
|
357
|
-
|
|
358
|
-
|
|
400
|
+
const parked = report && toolUseId && !event.cursorImported ? takeAsk(report.runtime, report.sessionId, toolUseId) : null;
|
|
401
|
+
if (report && parked) {
|
|
402
|
+
const held = parked.report;
|
|
403
|
+
report = held.decision === "allow" ? { ...report, decision: "allow" } : { ...report, decision: "ask", ran: true };
|
|
359
404
|
}
|
|
360
405
|
const now = (deps.now ?? Date.now)();
|
|
361
406
|
if (!report && !hasExpired(now))
|
|
@@ -375,9 +420,48 @@ function outputClient(event, runtime) {
|
|
|
375
420
|
return "claude-code";
|
|
376
421
|
return runtime === "codex" ? "codex" : "claude-code";
|
|
377
422
|
}
|
|
423
|
+
/** The one report the guard sends now, or null when the audit hook will (an
|
|
424
|
+
* allow, or an ask parked until the command runs). */
|
|
425
|
+
async function guardReport(base, plan, context) {
|
|
426
|
+
if (plan.server && base)
|
|
427
|
+
return serverDecidedReport(base, plan.server, context.now);
|
|
428
|
+
const report = base && context.v2 && base.decision === "allow" ? null : base;
|
|
429
|
+
if (!report)
|
|
430
|
+
return null;
|
|
431
|
+
// Claude Code gives the same tool-use id before and after a command, so an
|
|
432
|
+
// ask is parked until the audit hook says it ran, and sent as one row.
|
|
433
|
+
const held = report.decision === "ask" &&
|
|
434
|
+
report.runtime === "claude-code" &&
|
|
435
|
+
report.toolUseId !== undefined &&
|
|
436
|
+
(await claudeAuditRegistered(context.runtime)) &&
|
|
437
|
+
holdAsk(report.runtime, report.sessionId, report.toolUseId, report, context.now);
|
|
438
|
+
return held ? null : report;
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* What the server decided for an unattended ask, as one row: an allow (approved
|
|
442
|
+
* earlier) is parked for the run's own row; a waiting request is "asked"; a
|
|
443
|
+
* declined one is "asked, not run"; a block is a block.
|
|
444
|
+
*/
|
|
445
|
+
function serverDecidedReport(base, server, now) {
|
|
446
|
+
if (server.decision === "none")
|
|
447
|
+
return null;
|
|
448
|
+
if (server.decision === "allow") {
|
|
449
|
+
if (base.toolUseId)
|
|
450
|
+
holdAsk(base.runtime, base.sessionId, base.toolUseId, { ...base, decision: "allow" }, now);
|
|
451
|
+
return null;
|
|
452
|
+
}
|
|
453
|
+
if (server.decision === "ask")
|
|
454
|
+
return { ...base, decision: "ask" };
|
|
455
|
+
return server.message?.startsWith("Declined by")
|
|
456
|
+
? { ...base, decision: "ask", ran: false }
|
|
457
|
+
: { ...base, decision: "deny" };
|
|
458
|
+
}
|
|
459
|
+
/** Leave room after the server call to print and exit inside the budget. */
|
|
460
|
+
const PRINT_RESERVE_MS = 200;
|
|
378
461
|
async function runGuard(args, runtime, deps) {
|
|
379
462
|
if (runtime.env.PASSPORT_HOOK_DISABLED === "1")
|
|
380
463
|
return;
|
|
464
|
+
const startedAt = (deps.now ?? Date.now)();
|
|
381
465
|
const budget = budgetSignal(runtime);
|
|
382
466
|
const input = await readInput(runtime, budget);
|
|
383
467
|
if (!input || budget.aborted)
|
|
@@ -385,35 +469,44 @@ async function runGuard(args, runtime, deps) {
|
|
|
385
469
|
const event = shellEvent(input);
|
|
386
470
|
if (!event || event.phase !== "pre")
|
|
387
471
|
return;
|
|
388
|
-
// Decide from local data
|
|
472
|
+
// Decide from local data first. Only an unattended ask may ask the server.
|
|
389
473
|
const profile = localProfile(args, runtime.env);
|
|
390
474
|
const policy = profile ? readCliPolicy(profile) : null;
|
|
391
|
-
const decision = decideGuard(classifyCommand(event.command), policy);
|
|
475
|
+
const decision = decideGuard(classifyCommand(event.command), policy, event.command);
|
|
392
476
|
const declared = value(args, "client");
|
|
393
|
-
const
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
477
|
+
const client = outputClient(event, detectRuntime(input, runtime.env, declared));
|
|
478
|
+
const mode = detectMode(input, runtime.env).mode;
|
|
479
|
+
const canReport = Boolean(profile) && !runtime.env.PASSPORT_AGENT_KEY && !runtime.env.PASSPORT_ENDPOINT;
|
|
480
|
+
const base = decision && !event.cursorImported ? buildDecisionReport(input, decision, runtime.env, declared) : null;
|
|
481
|
+
const plan = await planGuard(decision, policy, {
|
|
482
|
+
client,
|
|
483
|
+
mode,
|
|
484
|
+
canAsk: Boolean(profile) && canReport && !event.cursorImported,
|
|
485
|
+
openApi: () => profileApi(profile, budget, deps.fetch ?? fetch).catch(() => null),
|
|
486
|
+
report: base,
|
|
487
|
+
command: event.command,
|
|
488
|
+
budget,
|
|
489
|
+
deadline: startedAt + HOOK_BUDGET_MS - PRINT_RESERVE_MS,
|
|
490
|
+
now: deps.now ?? Date.now
|
|
491
|
+
});
|
|
492
|
+
if (plan.output)
|
|
493
|
+
runtime.stdout.write(`${JSON.stringify(plan.output)}\n`);
|
|
398
494
|
// Leftover budget: one Activity report and, at most hourly, a policy refresh.
|
|
399
495
|
// Cursor does not enforce "ask" on hooks it imports, so an imported copy
|
|
400
496
|
// records nothing; Passport's own Cursor entry does.
|
|
401
|
-
if (!profile ||
|
|
497
|
+
if (!profile || !canReport)
|
|
402
498
|
return;
|
|
403
499
|
const now = (deps.now ?? Date.now)();
|
|
404
|
-
const report =
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
(await claudeAuditRegistered(runtime)) &&
|
|
411
|
-
holdAsk(report.runtime, report.sessionId, report.toolUseId, report, now);
|
|
412
|
-
const reports = [...(report && !held ? [report] : []), ...expiredAsks(now)];
|
|
500
|
+
const report = await guardReport(base && plan.output ? base : null, plan, {
|
|
501
|
+
v2: policy?.version === 2,
|
|
502
|
+
runtime,
|
|
503
|
+
now
|
|
504
|
+
});
|
|
505
|
+
const reports = [...(report ? [report] : []), ...expiredAsks(now)];
|
|
413
506
|
const refresh = claimStaleRefresh(profile, policy, now);
|
|
414
507
|
if (!reports.length && !refresh)
|
|
415
508
|
return;
|
|
416
|
-
const api = await profileApi(profile, budget, deps.fetch ?? fetch);
|
|
509
|
+
const api = plan.api ?? (await profileApi(profile, budget, deps.fetch ?? fetch));
|
|
417
510
|
if (!api)
|
|
418
511
|
return;
|
|
419
512
|
const send = deps.send
|
|
@@ -24,6 +24,9 @@ export type CliFinding = {
|
|
|
24
24
|
/** `--help` or `--dry-run`: the invocation describes the action instead of
|
|
25
25
|
* taking it, so the guard has no opinion. Local only; never reported. */
|
|
26
26
|
inert?: true;
|
|
27
|
+
/** The words after the program name. Local only; never reported, and read
|
|
28
|
+
* only to find the environment a command acts on. */
|
|
29
|
+
args?: string[];
|
|
27
30
|
};
|
|
28
31
|
export type CliSummary = {
|
|
29
32
|
governed: CliFinding[];
|
package/dist/cliHookClassify.js
CHANGED
|
@@ -208,7 +208,8 @@ function pushKey(flags, positional) {
|
|
|
208
208
|
}
|
|
209
209
|
function classifyVendor(vendor, args, via, envInjected) {
|
|
210
210
|
const found = classifyVendorVerb(vendor, args, via, envInjected);
|
|
211
|
-
|
|
211
|
+
const withArgs = { ...found, args };
|
|
212
|
+
return isInert(args.filter((arg) => arg.startsWith("-"))) ? { ...withArgs, inert: true } : withArgs;
|
|
212
213
|
}
|
|
213
214
|
function classifyVendorVerb(vendor, args, via, envInjected) {
|
|
214
215
|
const spec = VENDORS[vendor];
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unattended asks: when nobody is at the keyboard to answer the agent's own
|
|
3
|
+
* prompt, the guard asks Passport instead (`POST /api/my/cli/decide`). The
|
|
4
|
+
* server knows what this machine can't: grants already given in this session,
|
|
5
|
+
* every rule, and the workspace's environments. It answers allow (approved
|
|
6
|
+
* earlier), block, or ask with an approval request a person answers on the web
|
|
7
|
+
* or their phone; the command is declined for now and the agent is told how
|
|
8
|
+
* to wait.
|
|
9
|
+
*
|
|
10
|
+
* It all happens inside the hook's budget. If the server can't be reached in
|
|
11
|
+
* time, the guard falls back to its local decision and says so in one line.
|
|
12
|
+
*
|
|
13
|
+
* The request carries vendors, coarse verbs, risk classes, the environment
|
|
14
|
+
* names the flags gave, the session and tool-use ids, the folder name, and a
|
|
15
|
+
* command summary with secrets removed on this machine (`cliHookRedact.ts`).
|
|
16
|
+
* Never the raw command line.
|
|
17
|
+
*/
|
|
18
|
+
import type { ActionClass, Cause } from "./cliAutopilotPolicy.js";
|
|
19
|
+
import { type EnvKind } from "./cliHookEnvironment.js";
|
|
20
|
+
import { type GuardClient, type GuardDecision } from "./cliHookGuard.js";
|
|
21
|
+
import type { HookMode } from "./cliHookMode.js";
|
|
22
|
+
import type { CachedCliPolicy } from "./cliPolicy.js";
|
|
23
|
+
export declare const CLI_DECIDE_PATH = "/api/my/cli/decide";
|
|
24
|
+
export type CliDecideRequest = {
|
|
25
|
+
runtime: string;
|
|
26
|
+
sessionId?: string;
|
|
27
|
+
toolUseId?: string;
|
|
28
|
+
folder?: string;
|
|
29
|
+
interactive: boolean;
|
|
30
|
+
findings: Array<{
|
|
31
|
+
vendor: string;
|
|
32
|
+
verb: string;
|
|
33
|
+
risk: string;
|
|
34
|
+
environment?: string;
|
|
35
|
+
}>;
|
|
36
|
+
commandSummary?: string;
|
|
37
|
+
};
|
|
38
|
+
export type ServerDecision = {
|
|
39
|
+
decision: "allow" | "ask" | "block" | "none";
|
|
40
|
+
cause?: Cause;
|
|
41
|
+
explanation?: string;
|
|
42
|
+
environment?: EnvKind;
|
|
43
|
+
actionClass?: ActionClass;
|
|
44
|
+
approvalId?: string;
|
|
45
|
+
/** What to tell the agent, e.g. "Waiting for approval from Ramish. …" */
|
|
46
|
+
message?: string;
|
|
47
|
+
};
|
|
48
|
+
type ApiCall = (path: string, init: RequestInit) => Promise<Response>;
|
|
49
|
+
/** Validate what the server said. Anything unexpected is no answer at all. */
|
|
50
|
+
export declare function parseServerDecision(raw: unknown): ServerDecision | null;
|
|
51
|
+
export declare function decideRequest(decision: GuardDecision, context: {
|
|
52
|
+
runtime: string;
|
|
53
|
+
sessionId?: string;
|
|
54
|
+
toolUseId?: string;
|
|
55
|
+
folder?: string;
|
|
56
|
+
}, command: string): CliDecideRequest;
|
|
57
|
+
/** Ask the server, or null when it couldn't answer in time. Never throws. */
|
|
58
|
+
export declare function decideOnServer(api: ApiCall, request: CliDecideRequest, signal: AbortSignal): Promise<ServerDecision | null>;
|
|
59
|
+
export type GuardPlan = {
|
|
60
|
+
output: Record<string, unknown> | null;
|
|
61
|
+
server: ServerDecision | null;
|
|
62
|
+
offline: boolean;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Attended, or no v2 policy: the local decision, printed at once. Unattended
|
|
66
|
+
* ask: the server decides within the budget, and an approval request replaces
|
|
67
|
+
* the prompt nobody would answer; a server that can't answer in time leaves
|
|
68
|
+
* the local decision with one line saying so.
|
|
69
|
+
*/
|
|
70
|
+
export declare function planGuard(decision: GuardDecision | null, policy: CachedCliPolicy | null, context: {
|
|
71
|
+
client: GuardClient;
|
|
72
|
+
mode: HookMode;
|
|
73
|
+
/** Signed in with a member profile that may report. */
|
|
74
|
+
canAsk: boolean;
|
|
75
|
+
openApi: () => Promise<ApiCall | null>;
|
|
76
|
+
report: {
|
|
77
|
+
runtime: string;
|
|
78
|
+
sessionId?: string;
|
|
79
|
+
toolUseId?: string;
|
|
80
|
+
cwdName?: string;
|
|
81
|
+
} | null;
|
|
82
|
+
command: string;
|
|
83
|
+
budget: AbortSignal;
|
|
84
|
+
deadline: number;
|
|
85
|
+
now: () => number;
|
|
86
|
+
}): Promise<GuardPlan & {
|
|
87
|
+
api?: ApiCall;
|
|
88
|
+
}>;
|
|
89
|
+
export {};
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unattended asks: when nobody is at the keyboard to answer the agent's own
|
|
3
|
+
* prompt, the guard asks Passport instead (`POST /api/my/cli/decide`). The
|
|
4
|
+
* server knows what this machine can't: grants already given in this session,
|
|
5
|
+
* every rule, and the workspace's environments. It answers allow (approved
|
|
6
|
+
* earlier), block, or ask with an approval request a person answers on the web
|
|
7
|
+
* or their phone; the command is declined for now and the agent is told how
|
|
8
|
+
* to wait.
|
|
9
|
+
*
|
|
10
|
+
* It all happens inside the hook's budget. If the server can't be reached in
|
|
11
|
+
* time, the guard falls back to its local decision and says so in one line.
|
|
12
|
+
*
|
|
13
|
+
* The request carries vendors, coarse verbs, risk classes, the environment
|
|
14
|
+
* names the flags gave, the session and tool-use ids, the folder name, and a
|
|
15
|
+
* command summary with secrets removed on this machine (`cliHookRedact.ts`).
|
|
16
|
+
* Never the raw command line.
|
|
17
|
+
*/
|
|
18
|
+
import { readBoundedJson } from "./boundedResponse.js";
|
|
19
|
+
import { isEnvKind } from "./cliHookEnvironment.js";
|
|
20
|
+
import { OFFLINE_NOTE, guardOutput, waitingMessage } from "./cliHookGuard.js";
|
|
21
|
+
import { redactCommand } from "./cliHookRedact.js";
|
|
22
|
+
export const CLI_DECIDE_PATH = "/api/my/cli/decide";
|
|
23
|
+
const MAX_RESPONSE_BYTES = 16 * 1024;
|
|
24
|
+
const MAX_FINDINGS = 8;
|
|
25
|
+
const ID = /^[A-Za-z0-9_-]{1,80}$/;
|
|
26
|
+
const CAUSE_KINDS = new Set(["block", "grant", "rule", "floor", "preset", "default"]);
|
|
27
|
+
const CLASSES = new Set(["read", "change", "delete", "irreversible"]);
|
|
28
|
+
function line(raw, max) {
|
|
29
|
+
if (typeof raw !== "string")
|
|
30
|
+
return undefined;
|
|
31
|
+
const cleaned = raw
|
|
32
|
+
.replace(/[\u0000-\u001f\u007f-\u009f]/g, " ")
|
|
33
|
+
.replace(/\s+/g, " ")
|
|
34
|
+
.trim();
|
|
35
|
+
return cleaned ? cleaned.slice(0, max) : undefined;
|
|
36
|
+
}
|
|
37
|
+
/** Validate what the server said. Anything unexpected is no answer at all. */
|
|
38
|
+
export function parseServerDecision(raw) {
|
|
39
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
40
|
+
return null;
|
|
41
|
+
const body = raw;
|
|
42
|
+
const decision = body.decision;
|
|
43
|
+
if (decision !== "allow" && decision !== "ask" && decision !== "block" && decision !== "none")
|
|
44
|
+
return null;
|
|
45
|
+
const causeBody = body.cause && typeof body.cause === "object" ? body.cause : null;
|
|
46
|
+
const label = line(causeBody?.label, 160);
|
|
47
|
+
const cause = causeBody && typeof causeBody.kind === "string" && CAUSE_KINDS.has(causeBody.kind) && label
|
|
48
|
+
? {
|
|
49
|
+
kind: causeBody.kind,
|
|
50
|
+
label,
|
|
51
|
+
...(typeof causeBody.id === "string" && ID.test(causeBody.id) ? { id: causeBody.id } : {})
|
|
52
|
+
}
|
|
53
|
+
: undefined;
|
|
54
|
+
const approvalId = typeof body.approvalId === "string" && ID.test(body.approvalId) ? body.approvalId : undefined;
|
|
55
|
+
const explanation = line(body.explanation, 240);
|
|
56
|
+
const message = line(body.message, 400);
|
|
57
|
+
return {
|
|
58
|
+
decision,
|
|
59
|
+
...(cause ? { cause } : {}),
|
|
60
|
+
...(explanation ? { explanation } : {}),
|
|
61
|
+
...(isEnvKind(body.environment) ? { environment: body.environment } : {}),
|
|
62
|
+
...(typeof body.actionClass === "string" && CLASSES.has(body.actionClass)
|
|
63
|
+
? { actionClass: body.actionClass }
|
|
64
|
+
: {}),
|
|
65
|
+
...(approvalId ? { approvalId } : {}),
|
|
66
|
+
...(message ? { message } : {})
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
const VERB_TOKEN = /^-{0,2}[A-Za-z0-9][A-Za-z0-9._:+-]{0,39}$/;
|
|
70
|
+
/** The shape of the verb, never its arguments (the same rule as `safeVerb`). */
|
|
71
|
+
function safeVerbOf(raw) {
|
|
72
|
+
const tokens = (raw ?? "")
|
|
73
|
+
.split(/\s+/)
|
|
74
|
+
.filter((token) => VERB_TOKEN.test(token))
|
|
75
|
+
.slice(0, 4);
|
|
76
|
+
return tokens.join(" ").slice(0, 80).trim() || "invoke";
|
|
77
|
+
}
|
|
78
|
+
export function decideRequest(decision, context, command) {
|
|
79
|
+
const findings = (decision.decided ?? [])
|
|
80
|
+
.filter((entry) => entry.finding.vendor)
|
|
81
|
+
.slice(0, MAX_FINDINGS)
|
|
82
|
+
.map((entry) => ({
|
|
83
|
+
vendor: entry.finding.vendor,
|
|
84
|
+
verb: safeVerbOf(entry.finding.verbKey ?? entry.finding.verb),
|
|
85
|
+
risk: entry.finding.risk,
|
|
86
|
+
...(entry.reported ? { environment: entry.reported } : {})
|
|
87
|
+
}));
|
|
88
|
+
const summary = redactCommand(command);
|
|
89
|
+
return {
|
|
90
|
+
runtime: context.runtime,
|
|
91
|
+
...(context.sessionId ? { sessionId: context.sessionId } : {}),
|
|
92
|
+
...(context.toolUseId ? { toolUseId: context.toolUseId } : {}),
|
|
93
|
+
...(context.folder ? { folder: context.folder } : {}),
|
|
94
|
+
interactive: false,
|
|
95
|
+
findings,
|
|
96
|
+
...(summary ? { commandSummary: summary } : {})
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/** Ask the server, or null when it couldn't answer in time. Never throws. */
|
|
100
|
+
export async function decideOnServer(api, request, signal) {
|
|
101
|
+
try {
|
|
102
|
+
const response = await api(CLI_DECIDE_PATH, {
|
|
103
|
+
method: "POST",
|
|
104
|
+
headers: { "content-type": "application/json", accept: "application/json", "x-passport-surface": "cli" },
|
|
105
|
+
body: JSON.stringify(request),
|
|
106
|
+
signal
|
|
107
|
+
});
|
|
108
|
+
if (!response.ok) {
|
|
109
|
+
await response.body?.cancel().catch(() => undefined);
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
return parseServerDecision(await readBoundedJson(response, MAX_RESPONSE_BYTES));
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/** Turn the server's answer into what the agent reads. */
|
|
119
|
+
function serverOutput(client, decision, server) {
|
|
120
|
+
if (server.decision === "none")
|
|
121
|
+
return null;
|
|
122
|
+
const decided = {
|
|
123
|
+
...decision,
|
|
124
|
+
verdict: server.decision === "block" ? "deny" : server.decision,
|
|
125
|
+
...(server.cause ? { cause: server.cause } : {}),
|
|
126
|
+
...(server.environment ? { environment: server.environment } : {}),
|
|
127
|
+
...(server.actionClass ? { actionClass: server.actionClass } : {})
|
|
128
|
+
};
|
|
129
|
+
const options = server.decision === "ask" && server.approvalId
|
|
130
|
+
? { waiting: server.message ?? waitingMessage(server.approvalId) }
|
|
131
|
+
: server.decision === "block" && server.message
|
|
132
|
+
? { waiting: server.message }
|
|
133
|
+
: {};
|
|
134
|
+
return guardOutput(client, decided, options);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Attended, or no v2 policy: the local decision, printed at once. Unattended
|
|
138
|
+
* ask: the server decides within the budget, and an approval request replaces
|
|
139
|
+
* the prompt nobody would answer; a server that can't answer in time leaves
|
|
140
|
+
* the local decision with one line saying so.
|
|
141
|
+
*/
|
|
142
|
+
export async function planGuard(decision, policy, context) {
|
|
143
|
+
const local = { output: decision ? guardOutput(context.client, decision) : null, server: null, offline: false };
|
|
144
|
+
if (!decision || decision.verdict !== "ask" || context.mode !== "unattended" || policy?.version !== 2)
|
|
145
|
+
return local;
|
|
146
|
+
if (!context.canAsk)
|
|
147
|
+
return local;
|
|
148
|
+
const api = await context.openApi();
|
|
149
|
+
const left = context.deadline - context.now();
|
|
150
|
+
const server = api && left > 50
|
|
151
|
+
? await decideOnServer(api, decideRequest(decision, {
|
|
152
|
+
runtime: context.report?.runtime ?? "other",
|
|
153
|
+
...(context.report?.sessionId ? { sessionId: context.report.sessionId } : {}),
|
|
154
|
+
...(context.report?.toolUseId ? { toolUseId: context.report.toolUseId } : {}),
|
|
155
|
+
...(context.report?.cwdName ? { folder: context.report.cwdName } : {})
|
|
156
|
+
}, context.command), AbortSignal.any([context.budget, AbortSignal.timeout(left)]))
|
|
157
|
+
: null;
|
|
158
|
+
if (!server) {
|
|
159
|
+
return {
|
|
160
|
+
output: guardOutput(context.client, decision, { note: OFFLINE_NOTE }),
|
|
161
|
+
server: null,
|
|
162
|
+
offline: true,
|
|
163
|
+
...(api ? { api } : {})
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
return { output: serverOutput(context.client, decision, server), server, offline: false, ...(api ? { api } : {}) };
|
|
167
|
+
}
|