passport-bridge 0.17.0 → 0.19.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 +23 -20
- package/dist/approvalLines.d.ts +17 -0
- package/dist/approvalLines.js +40 -0
- package/dist/approvalNote.d.ts +20 -0
- package/dist/approvalNote.js +33 -0
- package/dist/cli.js +5 -5
- package/dist/cliArgs.d.ts +5 -0
- package/dist/cliArgs.js +12 -5
- package/dist/cliGuardHealth.d.ts +1 -1
- package/dist/cliGuardHealth.js +1 -1
- package/dist/cliHook.js +31 -10
- package/dist/cliHookDecide.d.ts +14 -0
- package/dist/cliHookDecide.js +61 -3
- package/dist/cliHookDeliver.d.ts +8 -1
- package/dist/cliHookDeliver.js +3 -3
- package/dist/cliHookHold.d.ts +91 -0
- package/dist/cliHookHold.js +168 -0
- package/dist/cliHookInstall.d.ts +8 -1
- package/dist/cliHookInstall.js +58 -12
- package/dist/cliHookKey.d.ts +25 -0
- package/dist/cliHookKey.js +160 -0
- package/dist/cliHookRedact.d.ts +13 -0
- package/dist/cliHookRedact.js +60 -0
- package/dist/cliInit.js +6 -1
- package/dist/cliMain.js +96 -38
- package/dist/cliOutput.js +11 -11
- package/dist/cliPolicyRefresh.d.ts +1 -1
- package/dist/cliPolicyRefresh.js +1 -1
- package/dist/cliScan.d.ts +17 -2
- package/dist/cliScan.js +29 -8
- package/dist/cliSetupLock.js +1 -1
- package/dist/cliSetupOrg.js +18 -6
- package/dist/cliWait.d.ts +7 -8
- package/dist/cliWait.js +50 -19
- package/dist/completion.js +14 -10
- package/dist/doctor.js +14 -0
- package/dist/enrollment.js +1 -1
- package/dist/holdNotice.d.ts +23 -0
- package/dist/holdNotice.js +73 -0
- package/dist/initAgents.d.ts +12 -3
- package/dist/initAgents.js +40 -9
- package/dist/initAutopilotStep.d.ts +2 -4
- package/dist/initAutopilotStep.js +6 -5
- package/dist/initLocalSteps.d.ts +0 -18
- package/dist/initLocalSteps.js +2 -78
- package/dist/initOutro.js +0 -2
- package/dist/initPlan.js +10 -14
- package/dist/initState.d.ts +0 -2
- package/dist/initState.js +0 -10
- package/dist/launcherFastPath.js +11 -4
- package/dist/lockedHooks.d.ts +1 -1
- package/dist/lockedHooks.js +3 -3
- package/dist/loginFlow.js +1 -1
- package/dist/managedHooks.js +1 -1
- package/dist/pluginPin.d.ts +1 -1
- package/dist/pluginPin.js +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +3 -2
package/dist/cliHookDecide.js
CHANGED
|
@@ -20,6 +20,7 @@ import { readBoundedJson } from "./boundedResponse.js";
|
|
|
20
20
|
import { isEnvKind } from "./cliHookEnvironment.js";
|
|
21
21
|
import { askMessage, guardOutput, waitingMessage } from "./cliHookGuard.js";
|
|
22
22
|
import { OFFLINE_CACHE_MAX_MS, OFFLINE_DEFAULT, OFFLINE_NOTES, offlineDecision } from "./cliGuardState.js";
|
|
23
|
+
import { commandKey } from "./cliHookKey.js";
|
|
23
24
|
import { redactCommand } from "./cliHookRedact.js";
|
|
24
25
|
export const CLI_DECIDE_PATH = "/api/my/cli/decide";
|
|
25
26
|
const MAX_RESPONSE_BYTES = 16 * 1024;
|
|
@@ -56,6 +57,10 @@ export function parseServerDecision(raw) {
|
|
|
56
57
|
const approvalId = typeof body.approvalId === "string" && ID.test(body.approvalId) ? body.approvalId : undefined;
|
|
57
58
|
const explanation = line(body.explanation, 240);
|
|
58
59
|
const message = line(body.message, 400);
|
|
60
|
+
const waitingFor = line(body.waitingFor, 80);
|
|
61
|
+
const expiresAt = typeof body.expiresAt === "number" && Number.isSafeInteger(body.expiresAt) && body.expiresAt > 0
|
|
62
|
+
? body.expiresAt
|
|
63
|
+
: undefined;
|
|
59
64
|
return {
|
|
60
65
|
decision,
|
|
61
66
|
...(cause ? { cause } : {}),
|
|
@@ -65,7 +70,9 @@ export function parseServerDecision(raw) {
|
|
|
65
70
|
? { actionClass: body.actionClass }
|
|
66
71
|
: {}),
|
|
67
72
|
...(approvalId ? { approvalId } : {}),
|
|
68
|
-
...(message ? { message } : {})
|
|
73
|
+
...(message ? { message } : {}),
|
|
74
|
+
...(expiresAt !== undefined ? { expiresAt } : {}),
|
|
75
|
+
...(waitingFor ? { waitingFor } : {})
|
|
69
76
|
};
|
|
70
77
|
}
|
|
71
78
|
const VERB_TOKEN = /^-{0,2}[A-Za-z0-9][A-Za-z0-9._:+-]{0,39}$/;
|
|
@@ -91,6 +98,7 @@ export function decideRequest(decision, context, command, local = false) {
|
|
|
91
98
|
...entry.placement
|
|
92
99
|
}));
|
|
93
100
|
const summary = redactCommand(command);
|
|
101
|
+
const key = commandKey(command);
|
|
94
102
|
return {
|
|
95
103
|
runtime: context.runtime,
|
|
96
104
|
...(context.sessionId ? { sessionId: context.sessionId } : {}),
|
|
@@ -98,7 +106,8 @@ export function decideRequest(decision, context, command, local = false) {
|
|
|
98
106
|
...(context.folder ? { folder: context.folder } : {}),
|
|
99
107
|
interactive: false,
|
|
100
108
|
findings,
|
|
101
|
-
...(summary ? { commandSummary: summary } : {})
|
|
109
|
+
...(summary ? { commandSummary: summary } : {}),
|
|
110
|
+
...(key ? { commandKey: key } : {})
|
|
102
111
|
};
|
|
103
112
|
}
|
|
104
113
|
/** Ask the server, or null when it couldn't answer in time. Never throws. */
|
|
@@ -257,5 +266,54 @@ export async function planGuard(decision, policy, context) {
|
|
|
257
266
|
...(api ? { api } : {})
|
|
258
267
|
};
|
|
259
268
|
}
|
|
260
|
-
return
|
|
269
|
+
return api
|
|
270
|
+
? heldPlan(decision, server, request, api, context)
|
|
271
|
+
: { output: serverOutput(context.client, decision, server), server, offline: false };
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* The server's answer as a plan. A request a person can answer: hold for them (Claude Code and Cursor run
|
|
275
|
+
* the original command when they approve in time); otherwise, or when the
|
|
276
|
+
* hold ends undecided, say how to wait. Every path but a fresh allow denies.
|
|
277
|
+
*/
|
|
278
|
+
async function heldPlan(decision, server, request, api, context) {
|
|
279
|
+
const waiting = server.decision === "ask" && server.approvalId && server.waitingFor ? server : null;
|
|
280
|
+
if (!waiting)
|
|
281
|
+
return { output: serverOutput(context.client, decision, server), server, offline: false, api };
|
|
282
|
+
const hold = await import("./cliHookHold.js");
|
|
283
|
+
const pending = (who, expiresAt, approvalId) => guardOutput(context.client, { ...decision, verdict: "deny" }, {
|
|
284
|
+
waiting: hold.pendingMessage(approvalId ?? waiting.approvalId, who ?? waiting.waitingFor, expiresAt ?? waiting.expiresAt, context.now())
|
|
285
|
+
});
|
|
286
|
+
// Only Claude Code holds: Codex can't run an approved command from a hook,
|
|
287
|
+
// and Cursor's behaviour past a hook's timeout isn't documented.
|
|
288
|
+
const seconds = context.client === "claude-code" && context.hold ? await context.hold().catch(() => 0) : 0;
|
|
289
|
+
if (seconds <= 0)
|
|
290
|
+
return { output: pending(), server, offline: false, api };
|
|
291
|
+
const began = context.now();
|
|
292
|
+
const outcome = await hold.holdForDecision(api, waiting.approvalId, (signal) => decideOnServer(api, request, signal), {
|
|
293
|
+
seconds,
|
|
294
|
+
signal: context.holdSignal ?? new AbortController().signal,
|
|
295
|
+
now: context.now
|
|
296
|
+
});
|
|
297
|
+
const heldMs = Math.max(0, context.now() - began);
|
|
298
|
+
const plan = { offline: false, api, held: outcome.kind, heldMs };
|
|
299
|
+
if (outcome.kind === "approved") {
|
|
300
|
+
return { ...plan, output: serverOutput(context.client, decision, outcome.server), server: outcome.server };
|
|
301
|
+
}
|
|
302
|
+
if (outcome.kind === "declined") {
|
|
303
|
+
return outcome.server
|
|
304
|
+
? { ...plan, output: serverOutput(context.client, decision, outcome.server), server: outcome.server }
|
|
305
|
+
: {
|
|
306
|
+
...plan,
|
|
307
|
+
output: guardOutput(context.client, { ...decision, verdict: "deny" }, { waiting: outcome.message }),
|
|
308
|
+
server
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
if (outcome.kind === "expired") {
|
|
312
|
+
return {
|
|
313
|
+
...plan,
|
|
314
|
+
output: guardOutput(context.client, { ...decision, verdict: "deny" }, { waiting: hold.EXPIRED_MESSAGE }),
|
|
315
|
+
server
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
return { ...plan, output: pending(outcome.waitingFor, outcome.expiresAt, outcome.approvalId), server };
|
|
261
319
|
}
|
package/dist/cliHookDeliver.d.ts
CHANGED
|
@@ -89,6 +89,13 @@ export type HookDependencies = {
|
|
|
89
89
|
/** Network seam for the default delivery and the policy refresh. */
|
|
90
90
|
fetch?: typeof fetch;
|
|
91
91
|
now?: () => number;
|
|
92
|
+
/** The hook's budget clocks. By default the overall budget is a
|
|
93
|
+
* `HOOK_BUDGET_MS` timer and script reading runs on `performance.now`; a test
|
|
94
|
+
* that compares decisions supplies its own so host load cannot change one. */
|
|
95
|
+
budget?: {
|
|
96
|
+
signal: AbortSignal;
|
|
97
|
+
scriptClock: () => number;
|
|
98
|
+
};
|
|
92
99
|
/** How the guard's rewrite starts `passport`; found on PATH by default. */
|
|
93
100
|
invocation?: string;
|
|
94
101
|
};
|
|
@@ -121,7 +128,7 @@ type Send = (report: CliHookReport) => Promise<void>;
|
|
|
121
128
|
export declare function reportSender(args: ParsedArgs, runtime: Runtime, signal: AbortSignal, deps: HookDependencies): Promise<Send | null>;
|
|
122
129
|
/** Parked asks nobody claimed in time: the prompt was declined, or nothing ran. */
|
|
123
130
|
export declare function expiredAsks(now: number): CliHookReport[];
|
|
124
|
-
export declare function budgetSignal(runtime: Runtime): AbortSignal;
|
|
131
|
+
export declare function budgetSignal(runtime: Runtime, deps?: HookDependencies): AbortSignal;
|
|
125
132
|
/** Never wait on the network after a command: queue the report, and a
|
|
126
133
|
* detached flush sends it. */
|
|
127
134
|
export declare function queueAfterCommand(report: CliHookReport | null, args: ParsedArgs, runtime: Runtime, runtimeName: string, now: number): Promise<void>;
|
package/dist/cliHookDeliver.js
CHANGED
|
@@ -107,8 +107,8 @@ export function expiredAsks(now) {
|
|
|
107
107
|
.filter((report) => report.decision === "ask")
|
|
108
108
|
.map((report) => ({ ...report, ran: false }));
|
|
109
109
|
}
|
|
110
|
-
export function budgetSignal(runtime) {
|
|
111
|
-
return AbortSignal.any([runtime.signal, AbortSignal.timeout(HOOK_BUDGET_MS)]);
|
|
110
|
+
export function budgetSignal(runtime, deps = {}) {
|
|
111
|
+
return AbortSignal.any([runtime.signal, deps.budget?.signal ?? AbortSignal.timeout(HOOK_BUDGET_MS)]);
|
|
112
112
|
}
|
|
113
113
|
/** Never wait on the network after a command: queue the report, and a
|
|
114
114
|
* detached flush sends it. */
|
|
@@ -211,7 +211,7 @@ export async function runFlush(args, runtime, deps) {
|
|
|
211
211
|
policy,
|
|
212
212
|
local: policy?.version === 2 && policy.autopilot.localCommands === true,
|
|
213
213
|
runtimeName,
|
|
214
|
-
budget: budgetSignal(runtime),
|
|
214
|
+
budget: budgetSignal(runtime, deps),
|
|
215
215
|
runtime,
|
|
216
216
|
args,
|
|
217
217
|
deps
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The hold: after Passport opens an approval request for an unattended ask,
|
|
3
|
+
* the guard waits a little while for a person to answer on their phone. When
|
|
4
|
+
* they approve in time, the agent's original command runs with no retry.
|
|
5
|
+
*
|
|
6
|
+
* Fail closed, always. The only way out that runs the command is a fresh
|
|
7
|
+
* `allow` from `POST /api/my/cli/decide`, asked again with the same request
|
|
8
|
+
* after the approval says allowed: that call consumes the approval once and
|
|
9
|
+
* re-checks every rule on fresh state. A timeout, an error, an abort, an
|
|
10
|
+
* answer that doesn't parse, or a slow second decide leaves the command
|
|
11
|
+
* declined with the pending line, and the approval waits for `passport wait`.
|
|
12
|
+
*
|
|
13
|
+
* The hold must end before the agent kills the hook: Claude Code runs the
|
|
14
|
+
* command anyway when a hook outlives its `timeout`. So its length travels in
|
|
15
|
+
* the installed command (`passport hook guard --hold 25`), written in the same
|
|
16
|
+
* entry as `timeout: hold + 15`. An entry without `--hold` doesn't hold.
|
|
17
|
+
* `PASSPORT_HOOK_HOLD` may shorten the hold, never lengthen it.
|
|
18
|
+
*/
|
|
19
|
+
/** The longest hold an install writes; with its margin, under a minute. */
|
|
20
|
+
export declare const HOLD_MAX_SECONDS = 45;
|
|
21
|
+
/** What `hook install` and `init` write for agents that can run a held command. */
|
|
22
|
+
export declare const HOLD_DEFAULT_SECONDS = 25;
|
|
23
|
+
/** The hook's `timeout` is the hold plus this, so the agent never kills it first. */
|
|
24
|
+
export declare const HOLD_TIMEOUT_MARGIN_SECONDS = 15;
|
|
25
|
+
/** Kept at the end of the hold for the second decide and printing. */
|
|
26
|
+
export declare const HOLD_SETTLE_MS = 3000;
|
|
27
|
+
type ApiCall = (path: string, init: RequestInit) => Promise<Response>;
|
|
28
|
+
/** The hold an installed guard command carries (`--hold <s>` anywhere), or 0. */
|
|
29
|
+
export declare function holdOfCommand(command: string): number;
|
|
30
|
+
/**
|
|
31
|
+
* The hold this run may use, checked against the timeout the agent will
|
|
32
|
+
* actually enforce: the guard's entry in Claude Code's settings. A hand-edited
|
|
33
|
+
* shorter timeout shortens the hold; an entry this run can't find (another
|
|
34
|
+
* settings file, a plugin, managed settings) means no hold, so a hold can
|
|
35
|
+
* never outlive the hook. Only Claude Code holds.
|
|
36
|
+
*/
|
|
37
|
+
export declare function cappedHold(seconds: number, entry: {
|
|
38
|
+
command: string;
|
|
39
|
+
timeout?: unknown;
|
|
40
|
+
} | null): number;
|
|
41
|
+
/** Seconds as a hold, or null when it isn't a whole number in range. */
|
|
42
|
+
export declare function parseHold(raw: string | undefined): number | null;
|
|
43
|
+
/**
|
|
44
|
+
* This run's hold: what the installed command says (`--hold`), shortened by
|
|
45
|
+
* `PASSPORT_HOOK_HOLD` when that is smaller. No flag, or a bad one, is no hold.
|
|
46
|
+
*/
|
|
47
|
+
export declare function holdSeconds(flag: string | undefined, env: Record<string, string | undefined>): number;
|
|
48
|
+
/** The parts of a decide answer the hold reads. */
|
|
49
|
+
export type DecideAnswer = {
|
|
50
|
+
decision: string;
|
|
51
|
+
message?: string;
|
|
52
|
+
approvalId?: string;
|
|
53
|
+
waitingFor?: string;
|
|
54
|
+
expiresAt?: number;
|
|
55
|
+
};
|
|
56
|
+
export type HoldOutcome<D extends DecideAnswer = DecideAnswer> =
|
|
57
|
+
/** Approved, and the second decide allowed it: the command runs now. */
|
|
58
|
+
{
|
|
59
|
+
kind: "approved";
|
|
60
|
+
server: D;
|
|
61
|
+
}
|
|
62
|
+
/** Declined, or blocked on the second decide: the line says by whom and why. */
|
|
63
|
+
| {
|
|
64
|
+
kind: "declined";
|
|
65
|
+
message: string;
|
|
66
|
+
server?: D;
|
|
67
|
+
} | {
|
|
68
|
+
kind: "expired";
|
|
69
|
+
}
|
|
70
|
+
/** Still waiting when the hold ended, or anything went wrong. */
|
|
71
|
+
| {
|
|
72
|
+
kind: "pending";
|
|
73
|
+
waitingFor?: string;
|
|
74
|
+
expiresAt?: number;
|
|
75
|
+
/** A new request the second decide opened (the first was used elsewhere). */
|
|
76
|
+
approvalId?: string;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Hold for a decision on one approval, within `seconds`. Never throws, and
|
|
80
|
+
* never answers "approved" without a fresh allow from the server.
|
|
81
|
+
*/
|
|
82
|
+
export declare function holdForDecision<D extends DecideAnswer>(api: ApiCall, approvalId: string,
|
|
83
|
+
/** Decide again with the original request (`decideOnServer`), or null. */
|
|
84
|
+
decideAgain: (signal: AbortSignal) => Promise<D | null>, options: {
|
|
85
|
+
seconds: number;
|
|
86
|
+
signal: AbortSignal;
|
|
87
|
+
now: () => number;
|
|
88
|
+
/** For tests: how the guard sleeps between gentle polls. */
|
|
89
|
+
sleep?: (ms: number, signal: AbortSignal) => Promise<void>;
|
|
90
|
+
}): Promise<HoldOutcome<D>>;
|
|
91
|
+
export { EXPIRED_MESSAGE, expiryTime, pendingMessage } from "./approvalLines.js";
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The hold: after Passport opens an approval request for an unattended ask,
|
|
3
|
+
* the guard waits a little while for a person to answer on their phone. When
|
|
4
|
+
* they approve in time, the agent's original command runs with no retry.
|
|
5
|
+
*
|
|
6
|
+
* Fail closed, always. The only way out that runs the command is a fresh
|
|
7
|
+
* `allow` from `POST /api/my/cli/decide`, asked again with the same request
|
|
8
|
+
* after the approval says allowed: that call consumes the approval once and
|
|
9
|
+
* re-checks every rule on fresh state. A timeout, an error, an abort, an
|
|
10
|
+
* answer that doesn't parse, or a slow second decide leaves the command
|
|
11
|
+
* declined with the pending line, and the approval waits for `passport wait`.
|
|
12
|
+
*
|
|
13
|
+
* The hold must end before the agent kills the hook: Claude Code runs the
|
|
14
|
+
* command anyway when a hook outlives its `timeout`. So its length travels in
|
|
15
|
+
* the installed command (`passport hook guard --hold 25`), written in the same
|
|
16
|
+
* entry as `timeout: hold + 15`. An entry without `--hold` doesn't hold.
|
|
17
|
+
* `PASSPORT_HOOK_HOLD` may shorten the hold, never lengthen it.
|
|
18
|
+
*/
|
|
19
|
+
import { readBoundedJson } from "./boundedResponse.js";
|
|
20
|
+
import { declinedFrom } from "./approvalLines.js";
|
|
21
|
+
/** The longest hold an install writes; with its margin, under a minute. */
|
|
22
|
+
export const HOLD_MAX_SECONDS = 45;
|
|
23
|
+
/** What `hook install` and `init` write for agents that can run a held command. */
|
|
24
|
+
export const HOLD_DEFAULT_SECONDS = 25;
|
|
25
|
+
/** The hook's `timeout` is the hold plus this, so the agent never kills it first. */
|
|
26
|
+
export const HOLD_TIMEOUT_MARGIN_SECONDS = 15;
|
|
27
|
+
/** Kept at the end of the hold for the second decide and printing. */
|
|
28
|
+
export const HOLD_SETTLE_MS = 3_000;
|
|
29
|
+
const WAIT_POLL_SECONDS = 30;
|
|
30
|
+
const MAX_RESPONSE_BYTES = 16 * 1024;
|
|
31
|
+
/** The hold an installed guard command carries (`--hold <s>` anywhere), or 0. */
|
|
32
|
+
export function holdOfCommand(command) {
|
|
33
|
+
const found = /(?:^|\s)--hold(?:\s+|=)(\d{1,2})(?=\s|$)/.exec(command);
|
|
34
|
+
return found ? (parseHold(found[1]) ?? 0) : 0;
|
|
35
|
+
}
|
|
36
|
+
/** Claude Code's own default when an entry sets no timeout. */
|
|
37
|
+
const CLAUDE_DEFAULT_TIMEOUT_SECONDS = 60;
|
|
38
|
+
/** Kept under the configured timeout for the guard's own work around the hold. */
|
|
39
|
+
const HOLD_RUNTIME_RESERVE_SECONDS = 8;
|
|
40
|
+
/**
|
|
41
|
+
* The hold this run may use, checked against the timeout the agent will
|
|
42
|
+
* actually enforce: the guard's entry in Claude Code's settings. A hand-edited
|
|
43
|
+
* shorter timeout shortens the hold; an entry this run can't find (another
|
|
44
|
+
* settings file, a plugin, managed settings) means no hold, so a hold can
|
|
45
|
+
* never outlive the hook. Only Claude Code holds.
|
|
46
|
+
*/
|
|
47
|
+
export function cappedHold(seconds, entry) {
|
|
48
|
+
if (seconds <= 0 || !entry || holdOfCommand(entry.command) <= 0)
|
|
49
|
+
return 0;
|
|
50
|
+
const timeout = typeof entry.timeout === "number" && Number.isFinite(entry.timeout)
|
|
51
|
+
? entry.timeout
|
|
52
|
+
: entry.timeout === undefined
|
|
53
|
+
? CLAUDE_DEFAULT_TIMEOUT_SECONDS
|
|
54
|
+
: 0;
|
|
55
|
+
return Math.max(0, Math.min(seconds, Math.floor(timeout - HOLD_RUNTIME_RESERVE_SECONDS)));
|
|
56
|
+
}
|
|
57
|
+
/** Seconds as a hold, or null when it isn't a whole number in range. */
|
|
58
|
+
export function parseHold(raw) {
|
|
59
|
+
if (raw === undefined || !/^\d{1,2}$/.test(raw.trim()))
|
|
60
|
+
return null;
|
|
61
|
+
const seconds = Number(raw.trim());
|
|
62
|
+
return seconds <= HOLD_MAX_SECONDS ? seconds : null;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* This run's hold: what the installed command says (`--hold`), shortened by
|
|
66
|
+
* `PASSPORT_HOOK_HOLD` when that is smaller. No flag, or a bad one, is no hold.
|
|
67
|
+
*/
|
|
68
|
+
export function holdSeconds(flag, env) {
|
|
69
|
+
const installed = parseHold(flag);
|
|
70
|
+
if (installed === null)
|
|
71
|
+
return 0;
|
|
72
|
+
const shorter = parseHold(env.PASSPORT_HOOK_HOLD);
|
|
73
|
+
return shorter === null ? installed : Math.min(installed, shorter);
|
|
74
|
+
}
|
|
75
|
+
function line(raw, max) {
|
|
76
|
+
if (typeof raw !== "string")
|
|
77
|
+
return undefined;
|
|
78
|
+
const cleaned = raw
|
|
79
|
+
.replace(/[\u0000-\u001f\u007f-\u009f]/g, " ")
|
|
80
|
+
.replace(/\s+/g, " ")
|
|
81
|
+
.trim();
|
|
82
|
+
return cleaned ? cleaned.slice(0, max) : undefined;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Hold for a decision on one approval, within `seconds`. Never throws, and
|
|
86
|
+
* never answers "approved" without a fresh allow from the server.
|
|
87
|
+
*/
|
|
88
|
+
export async function holdForDecision(api, approvalId,
|
|
89
|
+
/** Decide again with the original request (`decideOnServer`), or null. */
|
|
90
|
+
decideAgain, options) {
|
|
91
|
+
const { now } = options;
|
|
92
|
+
const end = now() + options.seconds * 1000;
|
|
93
|
+
const pollUntil = end - HOLD_SETTLE_MS;
|
|
94
|
+
const sleep = options.sleep ?? defaultSleep;
|
|
95
|
+
let pending = { kind: "pending" };
|
|
96
|
+
try {
|
|
97
|
+
while (!options.signal.aborted) {
|
|
98
|
+
const left = Math.floor((pollUntil - now()) / 1000);
|
|
99
|
+
if (left < 1)
|
|
100
|
+
return pending;
|
|
101
|
+
const poll = Math.min(WAIT_POLL_SECONDS, left);
|
|
102
|
+
const response = await api(`/api/my/approvals/${encodeURIComponent(approvalId)}/wait?timeout=${poll}`, {
|
|
103
|
+
method: "GET",
|
|
104
|
+
headers: { accept: "application/json", "x-passport-surface": "cli" },
|
|
105
|
+
signal: AbortSignal.any([options.signal, AbortSignal.timeout(Math.max(1, pollUntil - now()))])
|
|
106
|
+
});
|
|
107
|
+
if (!response.ok) {
|
|
108
|
+
await response.body?.cancel().catch(() => undefined);
|
|
109
|
+
return pending;
|
|
110
|
+
}
|
|
111
|
+
const body = await readBoundedJson(response, MAX_RESPONSE_BYTES);
|
|
112
|
+
if (body.status === "pending") {
|
|
113
|
+
pending = {
|
|
114
|
+
kind: "pending",
|
|
115
|
+
...(line(body.waitingFor, 80) ? { waitingFor: line(body.waitingFor, 80) } : {}),
|
|
116
|
+
...(typeof body.expiresAt === "number" && Number.isFinite(body.expiresAt)
|
|
117
|
+
? { expiresAt: body.expiresAt }
|
|
118
|
+
: {})
|
|
119
|
+
};
|
|
120
|
+
// A server that answers at once instead of holding is polled gently.
|
|
121
|
+
if (response.headers.get("x-passport-long-poll") === "0")
|
|
122
|
+
await sleep(1_000, options.signal);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (body.status === "denied")
|
|
126
|
+
return { kind: "declined", message: declinedFrom(body.message) };
|
|
127
|
+
if (body.status === "expired")
|
|
128
|
+
return { kind: "expired" };
|
|
129
|
+
if (body.status !== "allowed")
|
|
130
|
+
return pending;
|
|
131
|
+
// Approved: ask again, so the approval is consumed once and every rule
|
|
132
|
+
// is checked on fresh state. Only that allow runs the command.
|
|
133
|
+
const leftMs = end - now() - 250;
|
|
134
|
+
if (leftMs < 100)
|
|
135
|
+
return pending;
|
|
136
|
+
const server = await decideAgain(AbortSignal.any([options.signal, AbortSignal.timeout(leftMs)]));
|
|
137
|
+
if (server?.decision === "allow")
|
|
138
|
+
return { kind: "approved", server };
|
|
139
|
+
// A new request instead (the approval was used by another run): name it.
|
|
140
|
+
if (server?.decision === "ask" && server.approvalId) {
|
|
141
|
+
return {
|
|
142
|
+
kind: "pending",
|
|
143
|
+
approvalId: server.approvalId,
|
|
144
|
+
...(server.waitingFor ? { waitingFor: server.waitingFor } : {}),
|
|
145
|
+
...(server.expiresAt !== undefined ? { expiresAt: server.expiresAt } : {})
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
if (server?.decision === "block") {
|
|
149
|
+
return { kind: "declined", message: server.message ?? "Blocked by Passport.", server };
|
|
150
|
+
}
|
|
151
|
+
return pending;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
/* Fail closed: still pending. */
|
|
156
|
+
}
|
|
157
|
+
return pending;
|
|
158
|
+
}
|
|
159
|
+
function defaultSleep(ms, signal) {
|
|
160
|
+
return new Promise((resolve) => {
|
|
161
|
+
const timer = setTimeout(resolve, ms);
|
|
162
|
+
signal.addEventListener("abort", () => {
|
|
163
|
+
clearTimeout(timer);
|
|
164
|
+
resolve();
|
|
165
|
+
}, { once: true });
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
export { EXPIRED_MESSAGE, expiryTime, pendingMessage } from "./approvalLines.js";
|
package/dist/cliHookInstall.d.ts
CHANGED
|
@@ -29,7 +29,9 @@ export declare function isPassportHookCommand(command: unknown): boolean;
|
|
|
29
29
|
export declare function isPassportGuardCommand(command: unknown): boolean;
|
|
30
30
|
/** The registered audit command. Without a client it stays the historical
|
|
31
31
|
* `passport hook`, so older registrations still read as current. */
|
|
32
|
-
export declare function hookCommand(env: Env, exists?: (path: string) => boolean, client?: HookClient, kind?: Kind, invocation?: string
|
|
32
|
+
export declare function hookCommand(env: Env, exists?: (path: string) => boolean, client?: HookClient, kind?: Kind, invocation?: string,
|
|
33
|
+
/** The guard's hold, in seconds (`cliHookHold.ts`); 0 writes no flag. */
|
|
34
|
+
hold?: number): string;
|
|
33
35
|
/** A shell-safe `node <entry>` prefix for a hook command. */
|
|
34
36
|
export declare function nodeInvocation(node: string, entry: string): string;
|
|
35
37
|
/** What hooks run for a stable install: its version-independent launcher, so
|
|
@@ -89,5 +91,10 @@ export declare function agentHookStatus(client: HookClient, env: Env): {
|
|
|
89
91
|
path: string;
|
|
90
92
|
agentPresent: boolean;
|
|
91
93
|
};
|
|
94
|
+
/** One agent's guard entry, its command and timeout; null when off or unreadable. Never throws. */
|
|
95
|
+
export declare function guardEntryOf(client: HookClient, env: Env): {
|
|
96
|
+
command: string;
|
|
97
|
+
timeout?: unknown;
|
|
98
|
+
} | null;
|
|
92
99
|
export declare function hookRegistration(action: "install" | "uninstall" | "status", client: string | undefined, args: ParsedArgs, runtime: Runtime): CommandResult;
|
|
93
100
|
export {};
|
package/dist/cliHookInstall.js
CHANGED
|
@@ -16,7 +16,8 @@ import { randomBytes } from "node:crypto";
|
|
|
16
16
|
import { accessSync, closeSync, constants, existsSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, realpathSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
17
17
|
import { homedir } from "node:os";
|
|
18
18
|
import { dirname, join } from "node:path";
|
|
19
|
-
import { bool } from "./cliArgs.js";
|
|
19
|
+
import { bool, value } from "./cliArgs.js";
|
|
20
|
+
import { HOLD_DEFAULT_SECONDS, HOLD_MAX_SECONDS, HOLD_TIMEOUT_MARGIN_SECONDS, holdOfCommand, parseHold } from "./cliHookHold.js";
|
|
20
21
|
import { coveredByPlugin, pluginInstalled } from "./hookPlugin.js";
|
|
21
22
|
import { syncNative } from "./nativeSync.js";
|
|
22
23
|
import { credentialDir } from "./credentialStorage.js";
|
|
@@ -62,8 +63,8 @@ const CLIENTS = {
|
|
|
62
63
|
path: cursorHooksPath
|
|
63
64
|
}
|
|
64
65
|
};
|
|
65
|
-
const PASSPORT_COMMAND = /passport[^\s"']*["']?\s+hook(\s+guard)?(?:\s+--client(?:\s+|=)[a-z-]{1,24})?\s*$/i;
|
|
66
|
-
const VERB_TAIL = /^\s+hook(\s+guard)?(?:\s+--client(?:\s+|=)[a-z-]{1,24})?\s*$/i;
|
|
66
|
+
const PASSPORT_COMMAND = /passport[^\s"']*["']?\s+hook(\s+guard)?(?:\s+--client(?:\s+|=)[a-z-]{1,24})?(?:\s+--hold(?:\s+|=)\d{1,2})?\s*$/i;
|
|
67
|
+
const VERB_TAIL = /^\s+hook(\s+guard)?(?:\s+--client(?:\s+|=)[a-z-]{1,24})?(?:\s+--hold(?:\s+|=)\d{1,2})?\s*$/i;
|
|
67
68
|
/** "audit", "guard", or null. Recognises `passport hook`, an absolute path to
|
|
68
69
|
* the installed CLI followed by the same verb, and the exact `node <entry>`
|
|
69
70
|
* form this process registers when `passport` isn't on PATH, whatever
|
|
@@ -133,9 +134,21 @@ function passportInvocation(env, exists) {
|
|
|
133
134
|
}
|
|
134
135
|
/** The registered audit command. Without a client it stays the historical
|
|
135
136
|
* `passport hook`, so older registrations still read as current. */
|
|
136
|
-
export function hookCommand(env, exists = executable, client, kind = "audit", invocation
|
|
137
|
+
export function hookCommand(env, exists = executable, client, kind = "audit", invocation,
|
|
138
|
+
/** The guard's hold, in seconds (`cliHookHold.ts`); 0 writes no flag. */
|
|
139
|
+
hold = 0) {
|
|
137
140
|
const verb = kind === "guard" ? "hook guard" : "hook";
|
|
138
|
-
|
|
141
|
+
const held = kind === "guard" && hold > 0 ? ` --hold ${hold}` : "";
|
|
142
|
+
return `${invocation ?? passportInvocation(env, exists)} ${verb}${client ? ` --client ${client}` : ""}${held}`;
|
|
143
|
+
}
|
|
144
|
+
/** Agents whose hook can answer allow, so a held command can run when approved. */
|
|
145
|
+
function holdsFor(spec, hold) {
|
|
146
|
+
return spec.id === "claude-code" ? hold : 0; // Codex can't run it; Cursor's timeout behaviour is undocumented.
|
|
147
|
+
}
|
|
148
|
+
/** The entry's timeout: long enough that the agent never kills a hold first. */
|
|
149
|
+
function timeoutFor(command) {
|
|
150
|
+
const hold = holdOfCommand(command);
|
|
151
|
+
return hold > 0 ? hold + HOLD_TIMEOUT_MARGIN_SECONDS : HOOK_TIMEOUT_SECONDS;
|
|
139
152
|
}
|
|
140
153
|
/** A shell-safe `node <entry>` prefix for a hook command. */
|
|
141
154
|
export function nodeInvocation(node, entry) {
|
|
@@ -238,6 +251,14 @@ function registeredAt(spec, settings, slot) {
|
|
|
238
251
|
const found = commandsIn(spec, settings, slot.event).find(isOurs(slot.kind));
|
|
239
252
|
return typeof found === "string" ? found : null;
|
|
240
253
|
}
|
|
254
|
+
/** Passport's entry on one event, with its timeout. */
|
|
255
|
+
function entryAt(spec, settings, event, kind) {
|
|
256
|
+
const list = listOf(settings, event);
|
|
257
|
+
const entries = spec.format === "cursor"
|
|
258
|
+
? list
|
|
259
|
+
: list.flatMap((group) => (Array.isArray(group?.hooks) ? group.hooks : []));
|
|
260
|
+
return entries.find((entry) => isOurs(kind)(entry?.command)) ?? null;
|
|
261
|
+
}
|
|
241
262
|
/** Whether a kind is on: its primary event holds Passport's entry. */
|
|
242
263
|
function registered(spec, settings, kind) {
|
|
243
264
|
return registeredAt(spec, settings, slotsOf(spec, kind)[0]);
|
|
@@ -254,12 +275,12 @@ function withEntry(spec, settings, slot, command) {
|
|
|
254
275
|
if (spec.format === "cursor") {
|
|
255
276
|
const at = list.findIndex((entry) => ours(entry?.command));
|
|
256
277
|
const kept = list.filter((entry) => !ours(entry?.command));
|
|
257
|
-
const fresh = { command, timeout:
|
|
278
|
+
const fresh = { command, timeout: timeoutFor(command) };
|
|
258
279
|
hooks[event] = at === -1 ? [...kept, fresh] : [...kept.slice(0, at), fresh, ...kept.slice(at)];
|
|
259
280
|
const version = typeof settings.version === "number" ? settings.version : 1;
|
|
260
281
|
return { ...settings, version, hooks };
|
|
261
282
|
}
|
|
262
|
-
const entry = { type: "command", command, timeout:
|
|
283
|
+
const entry = { type: "command", command, timeout: timeoutFor(command) };
|
|
263
284
|
const groups = list;
|
|
264
285
|
// An older Passport entry (another version's path) is replaced where it stands.
|
|
265
286
|
const owner = groups.findIndex((group) => group && Array.isArray(group.hooks) && group.hooks.some((candidate) => ours(candidate?.command)));
|
|
@@ -352,17 +373,23 @@ function previewResult(spec, action, path, changes, guard, verbose = false) {
|
|
|
352
373
|
].join("\n")
|
|
353
374
|
};
|
|
354
375
|
}
|
|
355
|
-
function wantedEntries(spec, env, guard, invocation) {
|
|
376
|
+
function wantedEntries(spec, env, guard, invocation, hold = HOLD_DEFAULT_SECONDS) {
|
|
356
377
|
// Claude Code is the historical default and is recognised from its input, so
|
|
357
378
|
// its commands stay unqualified and existing registrations stay current.
|
|
358
379
|
const named = spec.id === "claude-code" ? undefined : spec.id;
|
|
359
|
-
return (guard ? ["guard", "audit"] : ["audit"]).flatMap((kind) => slotsOf(spec, kind).map((slot) => ({
|
|
380
|
+
return (guard ? ["guard", "audit"] : ["audit"]).flatMap((kind) => slotsOf(spec, kind).map((slot) => ({
|
|
381
|
+
...slot,
|
|
382
|
+
command: hookCommand(env, undefined, named, kind, invocation, holdsFor(spec, hold))
|
|
383
|
+
})));
|
|
360
384
|
}
|
|
361
385
|
function pendingChanges(spec, settings, wanted) {
|
|
362
386
|
const changes = [];
|
|
363
387
|
for (const entry of wanted) {
|
|
364
388
|
const current = registeredAt(spec, settings, entry);
|
|
365
|
-
|
|
389
|
+
// A timeout shorter than the entry needs (hand-edited) is fixed too.
|
|
390
|
+
const timeout = entryAt(spec, settings, entry.event, entry.kind)?.timeout;
|
|
391
|
+
const short = typeof timeout === "number" && timeout < timeoutFor(entry.command);
|
|
392
|
+
if (current === entry.command && !short)
|
|
366
393
|
continue;
|
|
367
394
|
changes.push({ action: current ? "update" : "add", event: entry.event, command: entry.command });
|
|
368
395
|
}
|
|
@@ -402,8 +429,14 @@ function installClient(spec, args, runtime) {
|
|
|
402
429
|
};
|
|
403
430
|
}
|
|
404
431
|
const { settings } = readSettings(path);
|
|
405
|
-
|
|
406
|
-
const
|
|
432
|
+
// A guard already on is kept current too (its hold), with or without --guard.
|
|
433
|
+
const guard = bool(args, "guard") || registered(spec, settings, "guard") !== null;
|
|
434
|
+
const holdFlag = value(args, "hold");
|
|
435
|
+
const hold = holdFlag === undefined ? HOLD_DEFAULT_SECONDS : parseHold(holdFlag);
|
|
436
|
+
if (hold === null) {
|
|
437
|
+
return failure(`--hold takes whole seconds from 0 to ${HOLD_MAX_SECONDS}.`, 6, "usage");
|
|
438
|
+
}
|
|
439
|
+
const wanted = wantedEntries(spec, runtime.env, guard, undefined, hold);
|
|
407
440
|
const changes = pendingChanges(spec, settings, wanted);
|
|
408
441
|
const guarded = guard || registered(spec, settings, "guard") !== null;
|
|
409
442
|
if (!changes.length) {
|
|
@@ -515,6 +548,19 @@ function clientStatus(spec, env) {
|
|
|
515
548
|
agentPresent: existsSync(dirname(path))
|
|
516
549
|
};
|
|
517
550
|
}
|
|
551
|
+
/** One agent's guard entry, its command and timeout; null when off or unreadable. Never throws. */
|
|
552
|
+
export function guardEntryOf(client, env) {
|
|
553
|
+
try {
|
|
554
|
+
const spec = CLIENTS[client];
|
|
555
|
+
if (pluginInstalled(client, env))
|
|
556
|
+
return null;
|
|
557
|
+
const found = entryAt(spec, readSettings(spec.path(env)).settings, spec.pre, "guard");
|
|
558
|
+
return found && typeof found.command === "string" ? { command: found.command, timeout: found.timeout } : null;
|
|
559
|
+
}
|
|
560
|
+
catch {
|
|
561
|
+
return null;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
518
564
|
export function hookRegistration(action, client, args, runtime) {
|
|
519
565
|
if (action === "status") {
|
|
520
566
|
// `hook status` lives in cliHookStatus.ts (cliHook.ts sends it there).
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The command key an approval binds to: the governed part of a command line,
|
|
3
|
+
* normalized so an agent's equivalent retry finds the same approval.
|
|
4
|
+
*
|
|
5
|
+
* Quoting is read by the shell parser, so `'release/old'` and `release/old`
|
|
6
|
+
* are one word. Dropped, because they never change what the command does: a
|
|
7
|
+
* pipe into a read-only filter whose every flag is on that filter's harmless
|
|
8
|
+
* list (`| tail -5`, `| grep -v hint`), `2>&1`, output sent to /dev/null, and
|
|
9
|
+
* a trailing `|| true`. Everything else stays: `cd`, flag order, every
|
|
10
|
+
* argument, env assignments, and any filter with a flag that could write a
|
|
11
|
+
* file or run a program (`| sort -o out`, `| rg --pre ./prog`).
|
|
12
|
+
*
|
|
13
|
+
* Secrets go first: every word and redirect target is redacted on its own,
|
|
14
|
+
* exactly as the summary redacts (`cliHookRedact.ts`), before anything is
|
|
15
|
+
* quoted or joined. Then a fail-closed check: if any piece the summary would
|
|
16
|
+
* hide still shows in the key, the key is the redacted summary instead. A
|
|
17
|
+
* line the parser can't read flatly (substitutions, a heredoc) keys on its
|
|
18
|
+
* redacted summary too. A key longer than the server takes is sent as a
|
|
19
|
+
* SHA-256 of the whole key, so nothing past a cut is lost. The server hashes
|
|
20
|
+
* the key again; it is never shown or stored as text.
|
|
21
|
+
*/
|
|
22
|
+
/** What the server accepts as a key; anything longer is hashed here. */
|
|
23
|
+
export declare const COMMAND_KEY_MAX = 400;
|
|
24
|
+
/** The normalized, redacted key, or undefined for an empty line. Never throws. */
|
|
25
|
+
export declare function commandKey(command: string): string | undefined;
|