passport-bridge 0.11.2 → 0.13.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.
Files changed (58) hide show
  1. package/README.md +76 -10
  2. package/dist/cli.js +24 -0
  3. package/dist/cliArgs.d.ts +1 -1
  4. package/dist/cliArgs.js +14 -2
  5. package/dist/cliHook.d.ts +54 -11
  6. package/dist/cliHook.js +317 -75
  7. package/dist/cliHookClassify.d.ts +6 -0
  8. package/dist/cliHookClassify.js +23 -1
  9. package/dist/cliHookGuard.d.ts +39 -0
  10. package/dist/cliHookGuard.js +127 -0
  11. package/dist/cliHookInstall.d.ts +54 -21
  12. package/dist/cliHookInstall.js +397 -119
  13. package/dist/cliHookPending.d.ts +31 -0
  14. package/dist/cliHookPending.js +141 -0
  15. package/dist/cliInit.d.ts +74 -0
  16. package/dist/cliInit.js +477 -0
  17. package/dist/cliLogin.d.ts +15 -1
  18. package/dist/cliLogin.js +219 -41
  19. package/dist/cliOutput.js +10 -4
  20. package/dist/cliPolicy.d.ts +63 -0
  21. package/dist/cliPolicy.js +216 -0
  22. package/dist/cliScan.d.ts +21 -0
  23. package/dist/cliScan.js +220 -0
  24. package/dist/cliSetup.js +2 -0
  25. package/dist/cliSupport.d.ts +2 -0
  26. package/dist/completion.js +8 -3
  27. package/dist/discover.d.ts +9 -0
  28. package/dist/discover.js +18 -11
  29. package/dist/initAgents.d.ts +83 -0
  30. package/dist/initAgents.js +503 -0
  31. package/dist/initAppsStep.d.ts +22 -0
  32. package/dist/initAppsStep.js +346 -0
  33. package/dist/initInstall.d.ts +58 -0
  34. package/dist/initInstall.js +372 -0
  35. package/dist/initLocalSteps.d.ts +7 -0
  36. package/dist/initLocalSteps.js +313 -0
  37. package/dist/initState.d.ts +112 -0
  38. package/dist/initState.js +80 -0
  39. package/dist/initUi.d.ts +73 -0
  40. package/dist/initUi.js +273 -0
  41. package/dist/nodeVersion.d.ts +13 -0
  42. package/dist/nodeVersion.js +42 -0
  43. package/dist/passport.d.ts +1 -0
  44. package/dist/passport.js +21 -1
  45. package/dist/scan.d.ts +3 -0
  46. package/dist/scan.js +274 -0
  47. package/dist/scanCore.d.ts +139 -0
  48. package/dist/scanCore.js +386 -0
  49. package/dist/scanParse.d.ts +36 -0
  50. package/dist/scanParse.js +275 -0
  51. package/dist/scanRules.d.ts +246 -0
  52. package/dist/scanRules.js +402 -0
  53. package/dist/scanStores.d.ts +3 -0
  54. package/dist/scanStores.js +221 -0
  55. package/dist/version.d.ts +1 -1
  56. package/dist/version.js +1 -1
  57. package/npm-shrinkwrap.json +8 -8
  58. package/package.json +3 -3
@@ -0,0 +1,127 @@
1
+ /**
2
+ * The guard's decision and the one line each agent shows for it.
3
+ *
4
+ * Passport can only add friction here, never remove it: the guard answers
5
+ * "ask" or "deny", or says nothing at all so the agent's own permission rules
6
+ * apply. It never answers "allow", whatever the policy says, because an allow
7
+ * from a hook skips the agent's prompt, and a guard that misreads a command must
8
+ * never be the reason something ran unasked.
9
+ */
10
+ import { VENDORS } from "./cliHookVendors.js";
11
+ const RANK = { ask: 1, deny: 2 };
12
+ const LABEL_TOKEN = /^-{0,2}[A-Za-z0-9][A-Za-z0-9._:+-]{0,39}$/;
13
+ /** `fly` and `flyctl` share one map; the server may key either spelling. */
14
+ function vendorKeys(vendor) {
15
+ const spec = Object.hasOwn(VENDORS, vendor) ? VENDORS[vendor] : undefined;
16
+ const aliases = spec ? Object.keys(VENDORS).filter((key) => VENDORS[key] === spec && key !== vendor) : [];
17
+ return [vendor, ...aliases];
18
+ }
19
+ function policyDecision(policy, vendor, verb) {
20
+ for (const key of vendorKeys(vendor)) {
21
+ const verbs = Object.hasOwn(policy.vendors, key) ? policy.vendors[key] : undefined;
22
+ if (verbs && Object.hasOwn(verbs, verb))
23
+ return verbs[verb];
24
+ }
25
+ return undefined;
26
+ }
27
+ /** The words a person needs to recognise the command, never its arguments. */
28
+ export function guardLabel(finding) {
29
+ const verb = (finding.verbKey ?? finding.verb ?? "")
30
+ .split(/\s+/)
31
+ .filter((token) => LABEL_TOKEN.test(token))
32
+ .slice(0, 4)
33
+ .join(" ");
34
+ return `${finding.vendor ?? "command"}${verb ? ` ${verb}` : ""}`.slice(0, 80);
35
+ }
36
+ /** A string literal in interpreter code that merely names a CLI is not a call. */
37
+ function speculative(finding) {
38
+ return finding.note?.includes("spawn call not confirmed") === true;
39
+ }
40
+ function verdictOf(finding, policy) {
41
+ if (!finding.vendor || !finding.verb || finding.inert || speculative(finding))
42
+ return null;
43
+ if (finding.risk === "local" || finding.risk === "unknown")
44
+ return null;
45
+ const explicit = policy ? policyDecision(policy, finding.vendor, finding.verbKey ?? finding.verb) : undefined;
46
+ // Built-in default when no policy was ever synced: ask before destructive.
47
+ const decision = explicit ?? (finding.risk === "destructive" ? (policy?.default.destructive ?? "ask") : undefined);
48
+ if (decision === "never")
49
+ return "deny";
50
+ if (decision === "ask")
51
+ return "ask";
52
+ return null;
53
+ }
54
+ /** The strictest verdict across every command in the line wins. */
55
+ export function decideGuard(findings, policy) {
56
+ const decided = [];
57
+ for (const finding of findings) {
58
+ const verdict = verdictOf(finding, policy);
59
+ if (verdict)
60
+ decided.push({ finding, verdict });
61
+ }
62
+ if (!decided.length)
63
+ return null;
64
+ const ordered = decided
65
+ .map((entry, index) => ({ ...entry, index }))
66
+ .sort((a, b) => RANK[b.verdict] - RANK[a.verdict] || a.index - b.index);
67
+ const top = ordered[0];
68
+ return {
69
+ verdict: top.verdict,
70
+ label: guardLabel(top.finding),
71
+ findings: ordered.map((entry) => entry.finding),
72
+ destructive: top.finding.risk === "destructive"
73
+ };
74
+ }
75
+ export function askMessage(decision) {
76
+ return decision.destructive
77
+ ? `Passport: ${decision.label} is destructive. Run it only if you meant to.`
78
+ : `Passport: your safety settings ask before ${decision.label}. Run it only if you meant to.`;
79
+ }
80
+ export function denyMessage(decision) {
81
+ return `Passport: your safety settings block ${decision.label}. Change this in Passport Settings → Safety.`;
82
+ }
83
+ export const DENY_AGENT_MESSAGE = "Blocked by Passport safety settings. Ask the user to run this command themselves.";
84
+ /**
85
+ * The exact stdout each agent reads, or null for nothing at all.
86
+ *
87
+ * - Claude Code shows an ask reason in its permission prompt; a deny reason
88
+ * goes to the model, so the person gets the deny line as a system message.
89
+ * - Codex parses "ask" but does not support it yet, so the guard stays silent
90
+ * and Codex's own approval policy applies. Deny is supported.
91
+ * - Cursor's beforeShellExecution takes a flat, snake_case response.
92
+ */
93
+ export function guardOutput(client, decision) {
94
+ if (client === "cursor") {
95
+ return decision.verdict === "deny"
96
+ ? { permission: "deny", user_message: denyMessage(decision), agent_message: DENY_AGENT_MESSAGE }
97
+ : { permission: "ask", user_message: askMessage(decision) };
98
+ }
99
+ if (client === "codex") {
100
+ if (decision.verdict !== "deny")
101
+ return null;
102
+ return {
103
+ hookSpecificOutput: {
104
+ hookEventName: "PreToolUse",
105
+ permissionDecision: "deny",
106
+ // Codex has no separate line for the person, so the model relays both.
107
+ permissionDecisionReason: `${denyMessage(decision)} Ask the user to run it themselves.`
108
+ }
109
+ };
110
+ }
111
+ return decision.verdict === "deny"
112
+ ? {
113
+ systemMessage: denyMessage(decision),
114
+ hookSpecificOutput: {
115
+ hookEventName: "PreToolUse",
116
+ permissionDecision: "deny",
117
+ permissionDecisionReason: DENY_AGENT_MESSAGE
118
+ }
119
+ }
120
+ : {
121
+ hookSpecificOutput: {
122
+ hookEventName: "PreToolUse",
123
+ permissionDecision: "ask",
124
+ permissionDecisionReason: askMessage(decision)
125
+ }
126
+ };
127
+ }
@@ -1,34 +1,67 @@
1
1
  /**
2
- * Registering and removing the audit-only `passport hook` in an agent's own
3
- * settings. Every change is previewed unless `--apply` is supplied, the write
4
- * is atomic, and only Passport's own entry is ever added or taken away.
2
+ * Registering and removing Passport's shell hooks in an agent's own settings.
3
+ *
4
+ * Two entries per agent: the audit hook after each shell command, and, with
5
+ * `--guard`, the guard before it. Every change is previewed unless `--apply` is
6
+ * supplied, the write is atomic, and only Passport's own entries are ever added
7
+ * or taken away. Other hooks and settings are left as they were.
8
+ *
9
+ * | Agent | File | Before (guard) | After (audit) |
10
+ * | ----------- | ------------------------ | ---------------------- | --------------------- |
11
+ * | Claude Code | ~/.claude/settings.json | PreToolUse (Bash) | PostToolUse (Bash) |
12
+ * | Codex | ~/.codex/hooks.json | PreToolUse (Bash) | PostToolUse (Bash) |
13
+ * | Cursor | ~/.cursor/hooks.json | beforeShellExecution | afterShellExecution |
5
14
  */
6
15
  import { type ParsedArgs } from "./cliArgs.js";
7
16
  import { type CommandResult, type Env, type Runtime } from "./cliSupport.js";
8
- export declare const HOOK_CLIENTS: readonly ["claude-code", "codex"];
17
+ export declare const HOOK_CLIENTS: readonly ["claude-code", "codex", "cursor"];
9
18
  export type HookClient = (typeof HOOK_CLIENTS)[number];
10
- type HookEntry = {
11
- type?: string;
12
- command?: string;
13
- timeout?: number;
14
- };
15
- type HookGroup = {
16
- matcher?: string;
17
- hooks?: HookEntry[];
18
- };
19
+ export declare function claudeSettingsPath(env: Env): string;
20
+ export declare function codexHooksPath(env: Env): string;
21
+ export declare function cursorHooksPath(env: Env): string;
19
22
  type Settings = Record<string, unknown> & {
20
- hooks?: Record<string, HookGroup[]>;
23
+ hooks?: Record<string, unknown>;
21
24
  };
22
- /** Recognise Passport's own entry however it was spelled: `passport hook`, or
23
- * an absolute path to the installed CLI followed by the same verb. */
25
+ type Kind = "audit" | "guard";
26
+ /** Passport's audit entry, with or without `--client`. */
24
27
  export declare function isPassportHookCommand(command: unknown): boolean;
25
- /** `passport hook` when the CLI is on PATH, otherwise this exact process. */
26
- export declare function hookCommand(env: Env, exists?: (path: string) => boolean): string;
27
- export declare function claudeSettingsPath(env: Env): string;
28
+ /** Passport's guard entry: `passport hook guard`, spelled the same ways. */
29
+ export declare function isPassportGuardCommand(command: unknown): boolean;
30
+ /** The registered audit command. Without a client it stays the historical
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): string;
33
+ /** A shell-safe `node <entry>` prefix for a hook command. */
34
+ export declare function nodeInvocation(node: string, entry: string): string;
35
+ /** The Passport audit command registered in Claude Code settings, if any. */
28
36
  export declare function registeredHookCommand(settings: Settings): string | null;
29
- /** Add Passport's entry to the Bash matcher without touching anything else. */
37
+ /** Add Passport's audit entry to Claude Code's Bash matcher, touching nothing else. */
30
38
  export declare function withPassportHook(settings: Settings, command: string): Settings;
31
- /** Remove only Passport's entry; a group we emptied goes with it. */
39
+ /** Remove only Passport's audit entries; a group we emptied goes with it. */
32
40
  export declare function withoutPassportHook(settings: Settings): Settings;
41
+ /** Which Passport entries an agent's settings hold. Never throws. */
42
+ export declare function registeredEntries(client: HookClient, env: Env): {
43
+ audit: string | null;
44
+ guard: string | null;
45
+ };
46
+ export declare function displayPath(path: string): string;
47
+ export type Change = {
48
+ action: "add" | "update" | "remove";
49
+ event: string;
50
+ command: string;
51
+ };
52
+ /** What `passport init` would change to turn on audit + guard for one agent. */
53
+ export type GuardInstallPlan = {
54
+ client: HookClient;
55
+ name: string;
56
+ path: string;
57
+ /** The settings file already exists, so the change edits a file Passport didn't create. */
58
+ existed: boolean;
59
+ changes: Change[];
60
+ };
61
+ /** Plan audit + guard for one agent, pointing at `invocation`. Throws a
62
+ * one-line CliFailure when the settings file is unreadable or malformed. */
63
+ export declare function planGuardInstall(client: HookClient, env: Env, invocation: string): GuardInstallPlan;
64
+ /** Apply a plan. The file is read again, so an edit made since planning stays. */
65
+ export declare function applyGuardInstall(plan: GuardInstallPlan, env: Env, invocation: string): void;
33
66
  export declare function hookRegistration(action: "install" | "uninstall" | "status", client: string | undefined, args: ParsedArgs, runtime: Runtime): CommandResult;
34
67
  export {};