passport-bridge 0.26.0 → 0.28.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 +4 -4
- package/dist/blindKind.d.ts +15 -0
- package/dist/blindKind.js +5 -0
- package/dist/cliChanges.d.ts +12 -4
- package/dist/cliChanges.js +26 -15
- package/dist/cliGuardState.js +2 -2
- package/dist/cliHook.js +2 -2
- package/dist/cliHookBlind.d.ts +15 -0
- package/dist/cliHookBlind.js +65 -0
- package/dist/cliHookDecide.d.ts +8 -1
- package/dist/cliHookDecide.js +52 -4
- package/dist/cliHookLocal.d.ts +7 -16
- package/dist/cliHookLocal.js +75 -97
- package/dist/cliHookLocalCode.js +7 -1
- package/dist/cliHookLocalFs.d.ts +15 -0
- package/dist/cliHookLocalFs.js +77 -0
- package/dist/cliHookScripts.d.ts +2 -1
- package/dist/cliHookSessionEnd.d.ts +48 -0
- package/dist/cliHookSessionEnd.js +162 -0
- package/dist/cliOutput.js +2 -2
- package/dist/cliProjectSummary.d.ts +1 -1
- package/dist/cliProjectSummary.js +19 -8
- package/dist/cliScriptRead.d.ts +5 -4
- package/dist/cliScriptRead.js +3 -2
- package/dist/cliSeen.d.ts +24 -15
- package/dist/cliSeen.js +34 -16
- package/dist/cliSessions.js +20 -6
- package/dist/cliSessionsJson.d.ts +48 -0
- package/dist/cliSessionsJson.js +40 -10
- package/dist/cliSessionsRender.d.ts +4 -0
- package/dist/cliSessionsRender.js +42 -8
- package/dist/inlineCode.d.ts +35 -0
- package/dist/inlineCode.js +201 -0
- package/dist/inlineScan.d.ts +23 -0
- package/dist/inlineScan.js +389 -0
- package/dist/localCommands.d.ts +10 -3
- package/dist/localCommands.js +59 -37
- package/dist/localFiles.d.ts +38 -2
- package/dist/localFiles.js +453 -33
- package/dist/localGit.js +53 -17
- package/dist/localShared.d.ts +3 -0
- 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 +1 -1
package/dist/cliHookLocal.js
CHANGED
|
@@ -14,91 +14,19 @@
|
|
|
14
14
|
* leaves the line incomplete, which means no allow: the agent's own prompt
|
|
15
15
|
* applies. Asks and blocks still come from whatever parts were understood.
|
|
16
16
|
*/
|
|
17
|
-
import { closeSync, fstatSync, lstatSync, openSync, readFileSync, readSync, realpathSync, statSync } from "node:fs";
|
|
18
17
|
import { homedir, tmpdir } from "node:os";
|
|
19
18
|
import { posix } from "node:path";
|
|
20
19
|
import { classifyArgv, classifyNestedText } from "./cliHookClassify.js";
|
|
21
20
|
import { heredocBodies, projectCode } from "./cliHookLocalCode.js";
|
|
22
|
-
import {
|
|
21
|
+
import { blindKind, homeProgram, programOf } from "./cliHookBlind.js";
|
|
22
|
+
import { inlineCode } from "./inlineCode.js";
|
|
23
|
+
import { NODE_FS } from "./cliHookLocalFs.js";
|
|
23
24
|
import { scriptReading } from "./cliHookScripts.js";
|
|
24
|
-
import { INTERPRETERS,
|
|
25
|
+
import { INTERPRETERS, splitCommands } from "./cliHookParse.js";
|
|
25
26
|
import { VENDORS } from "./cliHookVendors.js";
|
|
26
27
|
import { databasePrefix } from "./envDatabase.js";
|
|
27
28
|
import { classifyLocalCommand } from "./localCommands.js";
|
|
28
|
-
|
|
29
|
-
export const NODE_FS = {
|
|
30
|
-
realpath(path) {
|
|
31
|
-
try {
|
|
32
|
-
return realpathSync.native(path);
|
|
33
|
-
}
|
|
34
|
-
catch {
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
},
|
|
38
|
-
readText(path) {
|
|
39
|
-
try {
|
|
40
|
-
if (statSync(path).size > MAX_TEXT)
|
|
41
|
-
return null;
|
|
42
|
-
return readFileSync(path, "utf8");
|
|
43
|
-
}
|
|
44
|
-
catch {
|
|
45
|
-
return null;
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
|
-
isDirectory(path) {
|
|
49
|
-
try {
|
|
50
|
-
return statSync(path).isDirectory();
|
|
51
|
-
}
|
|
52
|
-
catch {
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
isFile(path) {
|
|
57
|
-
try {
|
|
58
|
-
return lstatSync(path).isFile() || statSync(path).isFile();
|
|
59
|
-
}
|
|
60
|
-
catch {
|
|
61
|
-
return false;
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
readScript(path) {
|
|
65
|
-
let descriptor;
|
|
66
|
-
try {
|
|
67
|
-
descriptor = openSync(path, "r");
|
|
68
|
-
const stat = fstatSync(descriptor);
|
|
69
|
-
if (!stat.isFile() || stat.size > SCRIPT_MAX_BYTES)
|
|
70
|
-
return null;
|
|
71
|
-
const buffer = Buffer.alloc(stat.size);
|
|
72
|
-
let offset = 0;
|
|
73
|
-
while (offset < stat.size) {
|
|
74
|
-
const read = readSync(descriptor, buffer, offset, stat.size - offset, offset);
|
|
75
|
-
if (read <= 0)
|
|
76
|
-
break;
|
|
77
|
-
offset += read;
|
|
78
|
-
}
|
|
79
|
-
// Binary files aren't scripts.
|
|
80
|
-
if (buffer.subarray(0, offset).includes(0))
|
|
81
|
-
return null;
|
|
82
|
-
return { text: buffer.toString("utf8", 0, offset), mtimeMs: stat.mtimeMs, size: stat.size };
|
|
83
|
-
}
|
|
84
|
-
catch {
|
|
85
|
-
return null;
|
|
86
|
-
}
|
|
87
|
-
finally {
|
|
88
|
-
if (descriptor !== undefined)
|
|
89
|
-
closeSync(descriptor);
|
|
90
|
-
}
|
|
91
|
-
},
|
|
92
|
-
statFile(path) {
|
|
93
|
-
try {
|
|
94
|
-
const stat = statSync(path);
|
|
95
|
-
return stat.isFile() ? { mtimeMs: stat.mtimeMs, size: stat.size } : null;
|
|
96
|
-
}
|
|
97
|
-
catch {
|
|
98
|
-
return null;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
};
|
|
29
|
+
export { NODE_FS } from "./cliHookLocalFs.js";
|
|
102
30
|
/** Home-relative paths whose contents are keys, tokens, or an agent's own settings. */
|
|
103
31
|
const SENSITIVE_IN_HOME = [
|
|
104
32
|
".ssh",
|
|
@@ -375,16 +303,11 @@ export function localFinding(action) {
|
|
|
375
303
|
}
|
|
376
304
|
};
|
|
377
305
|
}
|
|
378
|
-
/** The program a part ran, by name. A path is never sent: a file run by path is "script". */
|
|
379
|
-
function programOf(argv) {
|
|
380
|
-
const normalized = normalizeArgv(argv);
|
|
381
|
-
return normalized.via.includes("absolute-path") ? "script" : (normalized.argv[0] ?? "");
|
|
382
|
-
}
|
|
383
306
|
function isVendor(program) {
|
|
384
307
|
return Object.hasOwn(VENDORS, program);
|
|
385
308
|
}
|
|
386
309
|
/** Interpreter flags whose code is on the line itself: a one-liner. */
|
|
387
|
-
const INLINE_CODE = /^(-e|-p|-c|--eval|--print)$/;
|
|
310
|
+
const INLINE_CODE = /^(-e|-E|-p|-c|--eval|--print)$/;
|
|
388
311
|
function contextAt(cwd, machine) {
|
|
389
312
|
const here = cwd ? placeOfPath(cwd, machine) : "unknown";
|
|
390
313
|
const branches = cwd && (here === "project" || here === "project-root") ? gitBranches(cwd, machine.fs) : null;
|
|
@@ -396,9 +319,21 @@ function contextAt(cwd, machine) {
|
|
|
396
319
|
},
|
|
397
320
|
isVendor,
|
|
398
321
|
currentBranch: branches?.current ?? null,
|
|
399
|
-
defaultBranch: branches?.defaultBranch ?? null
|
|
322
|
+
defaultBranch: branches?.defaultBranch ?? null,
|
|
323
|
+
sameRepository: (word) => {
|
|
324
|
+
const path = word ? resolveWord(word, cwd, machine) : cwd;
|
|
325
|
+
return path !== null && sharesRepository(path, machine);
|
|
326
|
+
}
|
|
400
327
|
};
|
|
401
328
|
}
|
|
329
|
+
/** `path` is in a checkout of the project's repository: the same common git folder. */
|
|
330
|
+
function sharesRepository(path, machine) {
|
|
331
|
+
if (!machine.projectRoot)
|
|
332
|
+
return false;
|
|
333
|
+
const there = gitLayout(path, machine.fs);
|
|
334
|
+
const ours = there && gitLayout(machine.projectRoot, machine.fs);
|
|
335
|
+
return Boolean(ours && canonical(there.commonDir, machine.fs) === canonical(ours.commonDir, machine.fs));
|
|
336
|
+
}
|
|
402
337
|
/** Redirections: writes are changes where they land; reads of keys are no decision. */
|
|
403
338
|
function redirections(shape, cwd, machine, walk) {
|
|
404
339
|
for (const redirect of shape.redirects) {
|
|
@@ -443,15 +378,39 @@ function nextDirectory(target, cwd, machine) {
|
|
|
443
378
|
return null;
|
|
444
379
|
return machine.fs.isDirectory(path) ? path : null;
|
|
445
380
|
}
|
|
446
|
-
|
|
381
|
+
/** Interpreter code on the line or in a heredoc (`inlineCode.ts`): true when it only reads. */
|
|
382
|
+
function inlineRead(shape, cwd, machine, heredocs, walk) {
|
|
383
|
+
const words = shape.words;
|
|
384
|
+
const key = posix.basename(words.find((word) => !/^\w+=/.test(word.text))?.text ?? "");
|
|
385
|
+
const body = shape.heredoc ? heredocs.bodies.get(key) : undefined;
|
|
386
|
+
const ctx = contextAt(cwd, machine);
|
|
387
|
+
const inline = inlineCode(words, body, {
|
|
388
|
+
ctx,
|
|
389
|
+
placePath: (path) => ctx.place({ text: path, ...(path.startsWith("~") ? { tilde: true } : {}) }),
|
|
390
|
+
shadowed: (module) => cwd !== null &&
|
|
391
|
+
(machine.fs.isFile(posix.join(cwd, `${module}.py`)) || machine.fs.isDirectory(posix.join(cwd, module))),
|
|
392
|
+
quotedHeredoc: heredocs.quoted
|
|
393
|
+
});
|
|
394
|
+
if (!inline || !("read" in inline))
|
|
395
|
+
return inline;
|
|
396
|
+
walk.findings.push(localFinding(inline.read));
|
|
397
|
+
if (body !== undefined)
|
|
398
|
+
heredocs.read.add(key);
|
|
399
|
+
return true;
|
|
400
|
+
}
|
|
401
|
+
function vendorFindings(shape, outcome, walk, at) {
|
|
402
|
+
const { depth, cwd } = at;
|
|
447
403
|
const argv = shape.words.map((word) => word.text);
|
|
404
|
+
const inline = outcome.kind === "vendor" ? null : inlineRead(shape, cwd, at.machine, at.heredocs, walk);
|
|
405
|
+
if (inline === true)
|
|
406
|
+
return;
|
|
448
407
|
// Where it runs, for folder links; database variables set in front of it.
|
|
449
408
|
const prefix = databasePrefix(argv);
|
|
450
|
-
const
|
|
409
|
+
const where = { cwd, ...(prefix ? { prefix } : {}) };
|
|
451
410
|
// Unrecognized here: keep only what the vendor map governs or can't see into.
|
|
452
411
|
const found = classifyArgv(argv, [], depth)
|
|
453
412
|
.filter((finding) => outcome.kind === "vendor" || (finding.risk !== "local" && finding.risk !== "http"))
|
|
454
|
-
.map((finding) => ({ ...finding, at }));
|
|
413
|
+
.map((finding) => ({ ...finding, at: where }));
|
|
455
414
|
const push = outcome.kind === "vendor" ? outcome.push : undefined;
|
|
456
415
|
const findings = push
|
|
457
416
|
? found.map((finding) => (finding.vendor === "git" ? { ...finding, pushEnvironment: push } : finding))
|
|
@@ -459,11 +418,14 @@ function vendorFindings(shape, outcome, walk, depth, cwd) {
|
|
|
459
418
|
walk.findings.push(...findings);
|
|
460
419
|
if (outcome.kind !== "vendor") {
|
|
461
420
|
walk.complete = false;
|
|
462
|
-
// Blind: nothing Passport knows ran here, or code
|
|
421
|
+
// Blind: nothing Passport knows ran here, or code it couldn't read.
|
|
463
422
|
const program = programOf(argv);
|
|
464
|
-
const
|
|
465
|
-
if (
|
|
466
|
-
|
|
423
|
+
const code = inline !== null || (INTERPRETERS.has(program) && (shape.heredoc || argv.some((word) => INLINE_CODE.test(word))));
|
|
424
|
+
if (code || findings.every((finding) => finding.risk === "unknown")) {
|
|
425
|
+
// A bare assignment that changes what runs (`PATH=…`) is the shell's own.
|
|
426
|
+
const name = program || "export";
|
|
427
|
+
walk.blind ??= { program: inline?.program ?? name, kind: code ? "inline" : blindKind(name, argv) };
|
|
428
|
+
}
|
|
467
429
|
return;
|
|
468
430
|
}
|
|
469
431
|
// A vendor part is understood when it is one confident, plain invocation.
|
|
@@ -517,11 +479,18 @@ export function classifyLine(command, machine, depth = 0) {
|
|
|
517
479
|
if (depth > 4)
|
|
518
480
|
return { findings: [], complete: false };
|
|
519
481
|
const { shapes, nested, flags } = splitCommands(command);
|
|
520
|
-
const
|
|
521
|
-
|
|
482
|
+
const bodies = heredocBodies(nested);
|
|
483
|
+
const heredocs = {
|
|
484
|
+
// Two bodies for one program can't be told apart: neither is read as code.
|
|
485
|
+
bodies: new Map([...bodies].filter(([key]) => nested.filter((sub) => heredocKey(sub) === key).length === 1)),
|
|
486
|
+
read: new Set(),
|
|
487
|
+
quoted: !/<<-?\s*[^\s'"<]/.test(command.replace(/<<</g, " "))
|
|
488
|
+
};
|
|
489
|
+
if (flags.braces || flags.processSubstitution || nested.some((sub) => sub.kind !== "heredoc"))
|
|
522
490
|
walk.complete = false;
|
|
523
491
|
const cwdAt = [machine.cwd];
|
|
524
492
|
let previous = null;
|
|
493
|
+
let heredocShapes = 0;
|
|
525
494
|
for (let index = 0; index < shapes.length; index++) {
|
|
526
495
|
const shape = shapes[index];
|
|
527
496
|
// Entering a subshell starts from the folder outside it; leaving one forgets its cd.
|
|
@@ -534,10 +503,11 @@ export function classifyLine(command, machine, depth = 0) {
|
|
|
534
503
|
const cwd = cwdAt[shape.depth] ?? null;
|
|
535
504
|
previous = shape;
|
|
536
505
|
if (shape.heredoc)
|
|
537
|
-
|
|
506
|
+
heredocShapes++;
|
|
538
507
|
redirections(shape, cwd, machine, walk);
|
|
539
508
|
if (!shape.words.length)
|
|
540
509
|
continue;
|
|
510
|
+
shape.words = homeProgram(shape.words);
|
|
541
511
|
const outcome = classifyLocalCommand(shape.words, contextAt(cwd, machine));
|
|
542
512
|
if (outcome.kind === "local") {
|
|
543
513
|
walk.findings.push(...outcome.actions.map(localFinding));
|
|
@@ -557,13 +527,21 @@ export function classifyLine(command, machine, depth = 0) {
|
|
|
557
527
|
cwdAt[shape.depth] = nextDirectory(outcome.target, cwd, machine);
|
|
558
528
|
continue;
|
|
559
529
|
}
|
|
560
|
-
if (outcome.kind === "unknown" && ownCode(shape, { cwd, depth, heredocs }, machine, walk))
|
|
530
|
+
if (outcome.kind === "unknown" && ownCode(shape, { cwd, depth, heredocs: bodies }, machine, walk))
|
|
561
531
|
continue;
|
|
562
|
-
vendorFindings(shape, outcome, walk, depth, cwd);
|
|
532
|
+
vendorFindings(shape, outcome, walk, { depth, cwd, machine, heredocs });
|
|
563
533
|
}
|
|
564
|
-
|
|
534
|
+
// A heredoc is hidden text unless inline code read it as a read.
|
|
535
|
+
const readBodies = nested.filter((sub) => sub.kind === "heredoc" && heredocs.read.has(heredocKey(sub)));
|
|
536
|
+
const heredocCount = nested.filter((sub) => sub.kind === "heredoc").length;
|
|
537
|
+
if ((flags.heredoc || heredocShapes) && (heredocCount > readBodies.length || heredocShapes > readBodies.length))
|
|
538
|
+
walk.complete = false;
|
|
539
|
+
nestedInto(nested.filter((sub) => !readBodies.includes(sub)), machine, depth, walk);
|
|
565
540
|
return walk;
|
|
566
541
|
}
|
|
542
|
+
function heredocKey(sub) {
|
|
543
|
+
return sub.kind === "heredoc" && sub.target ? posix.basename(sub.target) : "";
|
|
544
|
+
}
|
|
567
545
|
/** Substitutions and heredocs: their vendor findings, and a shell's local ones. */
|
|
568
546
|
function nestedInto(nested, machine, depth, walk) {
|
|
569
547
|
for (const sub of nested) {
|
package/dist/cliHookLocalCode.js
CHANGED
|
@@ -95,7 +95,7 @@ function shellLine(words) {
|
|
|
95
95
|
function scriptOperand(words) {
|
|
96
96
|
for (let index = 1; index < words.length; index++) {
|
|
97
97
|
const word = words[index];
|
|
98
|
-
if (INLINE.test(word.text))
|
|
98
|
+
if (INLINE.test(word.text) || bundledCode(words[0].text, word.text))
|
|
99
99
|
return null;
|
|
100
100
|
if (word.text === "run" && (words[0].text === "deno" || words[0].text === "bun"))
|
|
101
101
|
continue;
|
|
@@ -108,6 +108,12 @@ function scriptOperand(words) {
|
|
|
108
108
|
}
|
|
109
109
|
return null;
|
|
110
110
|
}
|
|
111
|
+
/** Code given in a flag bundle: `perl -ne`, `ruby -pe`, `python3 -Ic`. */
|
|
112
|
+
function bundledCode(program, flag) {
|
|
113
|
+
if (program === "perl" || program === "ruby")
|
|
114
|
+
return /^-[a-zA-Z0-9]*[eE]/.test(flag);
|
|
115
|
+
return program.startsWith("python") && /^-[a-zA-Z]*c/.test(flag);
|
|
116
|
+
}
|
|
111
117
|
function runScript(program) {
|
|
112
118
|
return {
|
|
113
119
|
vendor: program,
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** The hook's view of this computer's files (`cliHookLocal.ts`): injected so tests never touch them. */
|
|
2
|
+
import { type FileStat, type ScriptFile } from "./cliScriptRead.js";
|
|
3
|
+
/** What the hook can read from the machine. Injected so tests never touch it. */
|
|
4
|
+
export type LocalFs = {
|
|
5
|
+
/** The canonical path, or null when it doesn't exist. */
|
|
6
|
+
realpath(path: string): string | null;
|
|
7
|
+
/** A small text file, or null. */
|
|
8
|
+
readText(path: string): string | null;
|
|
9
|
+
isDirectory(path: string): boolean;
|
|
10
|
+
isFile(path: string): boolean;
|
|
11
|
+
/** A script up to 256 KB with its time and size (script reading), or null. */
|
|
12
|
+
readScript?(path: string): ScriptFile | null;
|
|
13
|
+
statFile?(path: string): FileStat | null;
|
|
14
|
+
};
|
|
15
|
+
export declare const NODE_FS: LocalFs;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/** The hook's view of this computer's files (`cliHookLocal.ts`): injected so tests never touch them. */
|
|
2
|
+
import { closeSync, fstatSync, lstatSync, openSync, readFileSync, readSync, realpathSync, statSync } from "node:fs";
|
|
3
|
+
import { SCRIPT_MAX_BYTES } from "./cliScriptRead.js";
|
|
4
|
+
const MAX_TEXT = 4096;
|
|
5
|
+
export const NODE_FS = {
|
|
6
|
+
realpath(path) {
|
|
7
|
+
try {
|
|
8
|
+
return realpathSync.native(path);
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
readText(path) {
|
|
15
|
+
try {
|
|
16
|
+
if (statSync(path).size > MAX_TEXT)
|
|
17
|
+
return null;
|
|
18
|
+
return readFileSync(path, "utf8");
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
isDirectory(path) {
|
|
25
|
+
try {
|
|
26
|
+
return statSync(path).isDirectory();
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
isFile(path) {
|
|
33
|
+
try {
|
|
34
|
+
return lstatSync(path).isFile() || statSync(path).isFile();
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
readScript(path) {
|
|
41
|
+
let descriptor;
|
|
42
|
+
try {
|
|
43
|
+
descriptor = openSync(path, "r");
|
|
44
|
+
const stat = fstatSync(descriptor);
|
|
45
|
+
if (!stat.isFile() || stat.size > SCRIPT_MAX_BYTES)
|
|
46
|
+
return null;
|
|
47
|
+
const buffer = Buffer.alloc(stat.size);
|
|
48
|
+
let offset = 0;
|
|
49
|
+
while (offset < stat.size) {
|
|
50
|
+
const read = readSync(descriptor, buffer, offset, stat.size - offset, offset);
|
|
51
|
+
if (read <= 0)
|
|
52
|
+
break;
|
|
53
|
+
offset += read;
|
|
54
|
+
}
|
|
55
|
+
// Binary files aren't scripts.
|
|
56
|
+
if (buffer.subarray(0, offset).includes(0))
|
|
57
|
+
return null;
|
|
58
|
+
return { text: buffer.toString("utf8", 0, offset), mtimeMs: stat.mtimeMs, size: stat.size };
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
if (descriptor !== undefined)
|
|
65
|
+
closeSync(descriptor);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
statFile(path) {
|
|
69
|
+
try {
|
|
70
|
+
const stat = statSync(path);
|
|
71
|
+
return stat.isFile() ? { mtimeMs: stat.mtimeMs, size: stat.size } : null;
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
};
|
package/dist/cliHookScripts.d.ts
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import type { CliFinding } from "./cliHookClassify.js";
|
|
9
9
|
import type { CodeScript } from "./cliHookLocalCode.js";
|
|
10
10
|
import type { CommandShape } from "./cliHookParse.js";
|
|
11
|
+
import type { Blind } from "./blindKind.js";
|
|
11
12
|
import { type ScriptFs, type ScriptReader, type ScriptSegment } from "./cliScriptRead.js";
|
|
12
13
|
import type { LocalAction, Place, ShellWord } from "./localCommands.js";
|
|
13
14
|
/** The parts of the classifier's machine script reading needs. */
|
|
@@ -24,7 +25,7 @@ export type ScriptMachine = {
|
|
|
24
25
|
type Walk = {
|
|
25
26
|
findings: CliFinding[];
|
|
26
27
|
complete: boolean;
|
|
27
|
-
blind?:
|
|
28
|
+
blind?: Blind;
|
|
28
29
|
};
|
|
29
30
|
/** The classifier's helpers, passed in. */
|
|
30
31
|
export type ScriptHost<M extends ScriptMachine> = {
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* End session, on this computer. When a person ends an agent's session in
|
|
3
|
+
* Passport, the server refuses everything that reaches it under that session.
|
|
4
|
+
* The guard decides most commands here, so before a command it would let run
|
|
5
|
+
* on its own (an allow, or nothing to decide; an ask is a person's call) it
|
|
6
|
+
* asks Passport whether the session was ended (`GET /api/my/cli/session`),
|
|
7
|
+
* and refuses with the server's own line when it was.
|
|
8
|
+
*
|
|
9
|
+
* It costs little: a session checked in the last 15 seconds isn't asked
|
|
10
|
+
* again, a session known to be ended is never asked again, and the question
|
|
11
|
+
* never waits more than 300 ms. On a timeout or any error the command
|
|
12
|
+
* proceeds as it would have: the guard is a guardrail, not a security
|
|
13
|
+
* boundary. A refusal from the decide fast path teaches it too.
|
|
14
|
+
*
|
|
15
|
+
* State lives in owner-only files named by a digest of the agent and session
|
|
16
|
+
* id, holding only when it was checked and, once ended, the server's line.
|
|
17
|
+
* Bounded in count and age. Never throws.
|
|
18
|
+
*/
|
|
19
|
+
export declare const CLI_SESSION_PATH = "/api/my/cli/session";
|
|
20
|
+
/** A session checked this recently isn't asked about again. */
|
|
21
|
+
export declare const SESSION_FRESH_MS = 15000;
|
|
22
|
+
/** The longest a command the guard would let run waits on the question. */
|
|
23
|
+
export declare const SESSION_CHECK_MS = 300;
|
|
24
|
+
/** The server's line, exactly: "Session ended by Maya at 14:05 UTC. Start a new session to continue." */
|
|
25
|
+
export declare const ENDED_LINE: RegExp;
|
|
26
|
+
type ApiCall = (path: string, init: RequestInit) => Promise<Response>;
|
|
27
|
+
export type SessionRef = {
|
|
28
|
+
runtime: string;
|
|
29
|
+
sessionId?: string;
|
|
30
|
+
} | null;
|
|
31
|
+
/** A refusal Passport sent for this session (decide): remember it, so the
|
|
32
|
+
* guard refuses the rest here without asking. */
|
|
33
|
+
export declare function rememberEnded(session: SessionRef, message: string | undefined, now?: number): void;
|
|
34
|
+
/** Validate what Passport said: the line when the session ended, "" when it
|
|
35
|
+
* runs, null for anything unexpected. */
|
|
36
|
+
export declare function parseSessionState(raw: unknown): string | null;
|
|
37
|
+
/**
|
|
38
|
+
* The line to refuse with when a person ended this session, else undefined.
|
|
39
|
+
* Known ended: no network. Checked in the last 15 s: no network. Otherwise one
|
|
40
|
+
* question, bounded by 300 ms (and the hook's own budget); no answer in time
|
|
41
|
+
* means the command proceeds, and the session is asked again after 15 s.
|
|
42
|
+
*/
|
|
43
|
+
export declare function endedSessionLine(session: SessionRef, openApi: () => Promise<ApiCall | null>, options: {
|
|
44
|
+
now: () => number;
|
|
45
|
+
budget: AbortSignal;
|
|
46
|
+
timeoutMs?: number;
|
|
47
|
+
}): Promise<string | undefined>;
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* End session, on this computer. When a person ends an agent's session in
|
|
3
|
+
* Passport, the server refuses everything that reaches it under that session.
|
|
4
|
+
* The guard decides most commands here, so before a command it would let run
|
|
5
|
+
* on its own (an allow, or nothing to decide; an ask is a person's call) it
|
|
6
|
+
* asks Passport whether the session was ended (`GET /api/my/cli/session`),
|
|
7
|
+
* and refuses with the server's own line when it was.
|
|
8
|
+
*
|
|
9
|
+
* It costs little: a session checked in the last 15 seconds isn't asked
|
|
10
|
+
* again, a session known to be ended is never asked again, and the question
|
|
11
|
+
* never waits more than 300 ms. On a timeout or any error the command
|
|
12
|
+
* proceeds as it would have: the guard is a guardrail, not a security
|
|
13
|
+
* boundary. A refusal from the decide fast path teaches it too.
|
|
14
|
+
*
|
|
15
|
+
* State lives in owner-only files named by a digest of the agent and session
|
|
16
|
+
* id, holding only when it was checked and, once ended, the server's line.
|
|
17
|
+
* Bounded in count and age. Never throws.
|
|
18
|
+
*/
|
|
19
|
+
import { createHash } from "node:crypto";
|
|
20
|
+
import { lstatSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync } from "node:fs";
|
|
21
|
+
import { basename, join } from "node:path";
|
|
22
|
+
import { readBoundedJson } from "./boundedResponse.js";
|
|
23
|
+
import { credentialDir, enforceOwner, ensurePrivateDirectory, writePrivateAtomic } from "./credentialStorage.js";
|
|
24
|
+
export const CLI_SESSION_PATH = "/api/my/cli/session";
|
|
25
|
+
/** A session checked this recently isn't asked about again. */
|
|
26
|
+
export const SESSION_FRESH_MS = 15_000;
|
|
27
|
+
/** The longest a command the guard would let run waits on the question. */
|
|
28
|
+
export const SESSION_CHECK_MS = 300;
|
|
29
|
+
const MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
|
|
30
|
+
const MAX_FILES = 500;
|
|
31
|
+
const MAX_RESPONSE_BYTES = 4 * 1024;
|
|
32
|
+
const DIRECTORY = "hook-sessions";
|
|
33
|
+
const SESSION = /^[A-Za-z0-9._:-]{1,128}$/;
|
|
34
|
+
const RUNTIMES = new Set(["claude-code", "codex", "copilot", "cursor", "other"]);
|
|
35
|
+
/** The server's line, exactly: "Session ended by Maya at 14:05 UTC. Start a new session to continue." */
|
|
36
|
+
export const ENDED_LINE = /^Session ended by [^\n]{1,80} at \d\d:\d\d UTC\. Start a new session to continue\.$/;
|
|
37
|
+
function directory() {
|
|
38
|
+
return join(credentialDir(), DIRECTORY);
|
|
39
|
+
}
|
|
40
|
+
function usable(session) {
|
|
41
|
+
return Boolean(session?.sessionId && SESSION.test(session.sessionId) && RUNTIMES.has(session.runtime));
|
|
42
|
+
}
|
|
43
|
+
function statePath(runtime, sessionId) {
|
|
44
|
+
const key = createHash("sha256").update(`${runtime}\u0000${sessionId}`).digest("hex").slice(0, 40);
|
|
45
|
+
return join(directory(), `${key}.json`);
|
|
46
|
+
}
|
|
47
|
+
function readState(runtime, sessionId) {
|
|
48
|
+
try {
|
|
49
|
+
const path = statePath(runtime, sessionId);
|
|
50
|
+
if (statSync(path).size > MAX_RESPONSE_BYTES)
|
|
51
|
+
return null;
|
|
52
|
+
const raw = JSON.parse(readFileSync(path, "utf8"));
|
|
53
|
+
if (typeof raw.checkedAt !== "number")
|
|
54
|
+
return null;
|
|
55
|
+
const ended = typeof raw.ended === "string" && ENDED_LINE.test(raw.ended) ? raw.ended : undefined;
|
|
56
|
+
return { checkedAt: raw.checkedAt, ...(ended ? { ended } : {}) };
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function writeState(runtime, sessionId, state, now) {
|
|
63
|
+
try {
|
|
64
|
+
ensurePrivateDirectory(true);
|
|
65
|
+
try {
|
|
66
|
+
mkdirSync(directory(), { mode: 0o700 });
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (error.code !== "EEXIST")
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
const stat = lstatSync(directory());
|
|
73
|
+
if (stat.isSymbolicLink() || !stat.isDirectory())
|
|
74
|
+
return;
|
|
75
|
+
enforceOwner(stat, "hook sessions directory");
|
|
76
|
+
const path = statePath(runtime, sessionId);
|
|
77
|
+
let names = readdirSync(directory()).filter((name) => /^[0-9a-f]{40}\.json$/.test(name));
|
|
78
|
+
if (names.length >= MAX_FILES && !names.includes(basename(path))) {
|
|
79
|
+
for (const name of names) {
|
|
80
|
+
const file = join(directory(), name);
|
|
81
|
+
if (now - statSync(file).mtimeMs > MAX_AGE_MS)
|
|
82
|
+
rmSync(file, { force: true });
|
|
83
|
+
}
|
|
84
|
+
names = readdirSync(directory()).filter((name) => /^[0-9a-f]{40}\.json$/.test(name));
|
|
85
|
+
if (names.length >= MAX_FILES)
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
writePrivateAtomic(path, ".hook-session", JSON.stringify(state), "hook session");
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
/* Asked again next time: harmless. */
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/** A refusal Passport sent for this session (decide): remember it, so the
|
|
95
|
+
* guard refuses the rest here without asking. */
|
|
96
|
+
export function rememberEnded(session, message, now = Date.now()) {
|
|
97
|
+
if (!usable(session) || !message || !ENDED_LINE.test(message))
|
|
98
|
+
return;
|
|
99
|
+
writeState(session.runtime, session.sessionId, { checkedAt: now, ended: message }, now);
|
|
100
|
+
}
|
|
101
|
+
/** Validate what Passport said: the line when the session ended, "" when it
|
|
102
|
+
* runs, null for anything unexpected. */
|
|
103
|
+
export function parseSessionState(raw) {
|
|
104
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
105
|
+
return null;
|
|
106
|
+
const body = raw;
|
|
107
|
+
if (body.ended === false)
|
|
108
|
+
return "";
|
|
109
|
+
return body.ended === true && typeof body.message === "string" && ENDED_LINE.test(body.message) ? body.message : null;
|
|
110
|
+
}
|
|
111
|
+
async function askPassport(openApi, session, signal) {
|
|
112
|
+
try {
|
|
113
|
+
const api = await openApi();
|
|
114
|
+
if (!api || signal.aborted)
|
|
115
|
+
return null;
|
|
116
|
+
const query = new URLSearchParams({ runtime: session.runtime, sessionId: session.sessionId });
|
|
117
|
+
const response = await api(`${CLI_SESSION_PATH}?${query.toString()}`, {
|
|
118
|
+
method: "GET",
|
|
119
|
+
headers: { accept: "application/json", "x-passport-surface": "cli" },
|
|
120
|
+
signal
|
|
121
|
+
});
|
|
122
|
+
if (!response.ok) {
|
|
123
|
+
await response.body?.cancel().catch(() => undefined);
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
return parseSessionState(await readBoundedJson(response, MAX_RESPONSE_BYTES));
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The line to refuse with when a person ended this session, else undefined.
|
|
134
|
+
* Known ended: no network. Checked in the last 15 s: no network. Otherwise one
|
|
135
|
+
* question, bounded by 300 ms (and the hook's own budget); no answer in time
|
|
136
|
+
* means the command proceeds, and the session is asked again after 15 s.
|
|
137
|
+
*/
|
|
138
|
+
export async function endedSessionLine(session, openApi, options) {
|
|
139
|
+
if (!usable(session))
|
|
140
|
+
return undefined;
|
|
141
|
+
const state = readState(session.runtime, session.sessionId);
|
|
142
|
+
if (state?.ended)
|
|
143
|
+
return state.ended;
|
|
144
|
+
const now = options.now();
|
|
145
|
+
if (state && now - state.checkedAt >= 0 && now - state.checkedAt < SESSION_FRESH_MS)
|
|
146
|
+
return undefined;
|
|
147
|
+
// A timer of our own (AbortSignal.timeout's doesn't keep the process up),
|
|
148
|
+
// and openApi may refresh a sign-in: the bound covers it too.
|
|
149
|
+
const bound = new AbortController();
|
|
150
|
+
const timer = setTimeout(() => bound.abort(), options.timeoutMs ?? SESSION_CHECK_MS);
|
|
151
|
+
const signal = AbortSignal.any([options.budget, bound.signal]);
|
|
152
|
+
const answer = await Promise.race([
|
|
153
|
+
askPassport(openApi, session, signal),
|
|
154
|
+
new Promise((resolve) => {
|
|
155
|
+
if (signal.aborted)
|
|
156
|
+
resolve(null);
|
|
157
|
+
signal.addEventListener("abort", () => resolve(null), { once: true });
|
|
158
|
+
})
|
|
159
|
+
]).finally(() => clearTimeout(timer));
|
|
160
|
+
writeState(session.runtime, session.sessionId, { checkedAt: options.now(), ...(answer ? { ended: answer } : {}) }, options.now());
|
|
161
|
+
return answer || undefined;
|
|
162
|
+
}
|
package/dist/cliOutput.js
CHANGED
|
@@ -151,10 +151,10 @@ function helpText(verb) {
|
|
|
151
151
|
env: "passport env [--json]\npassport env set <vendor> <resource> <production|staging|development>\nShows where commands in this folder land, read the way each vendor's own CLI reads it: Railway, Vercel, Supabase, Wrangler, Netlify, Firebase, Convex, Neon, Fly, Heroku, kubectl, AWS, gcloud, Terraform, Stripe, and database URLs. One row per app: the resource, how Passport knows (linked, a flag, the vendor's default), and its environment. Anything Passport can't place counts as Production. Read on this computer; nothing is sent.\n set place a resource for the whole workspace. Admins choose any environment; everyone else can only say Production.\nJSON: {environments:[{vendor,resource,how,kind,assumed,note?}]}",
|
|
152
152
|
sessions:
|
|
153
153
|
// copy-style-allow double-space: help columns line up
|
|
154
|
-
"passport sessions [--all] [--running] [--production-changes] [--project <name>] [--pr <number>] [--since <when>] [--status running|ended]\npassport sessions <session-id>\nYour agents' work the way the Sessions page shows it: work items (a pull request or branch, every session on it rolled up) by project, each with its agents and what happened: actions, active time, failures, confirmed Production changes, unplaced changes, asks and how they ended, and how much Passport couldn't check. Notable work comes first; routine work folds into one line per project.\n --all every work item, routine ones too (pass one of --all, --running, --production-changes)\n --running work items with a running session\n --production-changes changes to confirmed Production, by app and target (7 days unless --since)\n --project <name> one project (repository, else folder)\n --pr <number> one pull request\n --since <when> today, 24h, 7d, 30d, 90d, or a date (YYYY-MM-DD)\n --status <status> running or ended sessions only\n <session-id> one session: its outcome and recent actions with what they acted on\nJSON: {view,groups:[{key,kind,label,apps,running,production,unplaced,items:[{key,title,project,branch,pull,agents,sessions,sessionIds,startedAt,lastActivityAt,activeMs,running,waiting,shape,reasons,severity,link}],routine}],totals,complete,nextCursor}; --production-changes: {view,sinceTs,untilTs,total,unplaced,apps:[{appKey,appName,count,lastTs,targets}]}; <session-id>: {view,session,outcome,approvals,timeline,nextCursor}",
|
|
154
|
+
"passport sessions [--all] [--running] [--production-changes] [--project <name>] [--pr <number>] [--since <when>] [--status running|ended]\npassport sessions <session-id>\nYour agents' work the way the Sessions page shows it: work items (a pull request or branch, every session on it rolled up) by project, each with its agents and what happened: actions, active time, failures, confirmed Production changes, unplaced changes, asks and how they ended, and how much Passport couldn't check. Notable work comes first; routine work folds into one line per project.\n --all every work item, routine ones too (pass one of --all, --running, --production-changes)\n --running work items with a running session\n --production-changes changes to confirmed Production, by app and target (7 days unless --since)\n --project <name> one project (repository, else folder)\n --pr <number> one pull request\n --since <when> today, 24h, 7d, 30d, 90d, or a date (YYYY-MM-DD)\n --status <status> running or ended sessions only\n <session-id> one session: its outcome and recent actions with what they acted on\nJSON: {view,groups:[{key,kind,label,apps,running,production,unplaced,items:[{key,title,project,branch,pull,agents,sessions,sessionIds,startedAt,lastActivityAt,activeMs,running,waiting,shape,reasons,severity,link}],routine}],totals,work,window,complete,nextCursor}; --production-changes: {view,sinceTs,untilTs,total,unplaced,window,apps:[{appKey,appName,count,lastTs,targets}]}; <session-id>: {view,session,outcome,approvals,timeline,nextCursor}",
|
|
155
155
|
changes:
|
|
156
156
|
// copy-style-allow double-space: help columns line up
|
|
157
|
-
"passport changes [--env production] [--since <when>] [--until <date>] [--project <name>] [--pr <number>] [--app <key>] [--agent <id>]\nWhat your agents changed in Production, the list Sessions › Production changes shows: every change, delete, and irreversible action that ran on confirmed Production in the window, grouped by app, newest first. Each row has its time and command, then the agent, the person, the pull request (else branch), Failed or Approved, and whether Passport can undo it. Blocked and declined actions never reached Production and aren't listed; changes to targets nobody placed are one count.\n --env <env> production (the only one for now)\n --since <when> 1h, 6h, 24h (default),
|
|
157
|
+
"passport changes [--env production] [--since <when>] [--until <date>] [--project <name>] [--pr <number>] [--app <key>] [--agent <id>]\nWhat your agents changed in Production, the list Sessions › Production changes shows: every change, delete, and irreversible action that ran on confirmed Production in the window, grouped by app, newest first. Each row has its time and command, then the agent, the person, the pull request (else branch), Failed or Approved, and whether Passport can undo it. Blocked and declined actions never reached Production and aren't listed; changes to targets nobody placed are one count.\n --env <env> production (the only one for now)\n --since <when> 1h, 6h, 24h, 7d (default), 30d, today, or a date (YYYY-MM-DD)\n --until <date> through the end of a date (YYYY-MM-DD)\n --project <name> one project (repository, else folder)\n --pr <number> one pull request\n --app <key> one app, or several separated by commas\n --agent <id> one agent (claude-code, codex, cursor)\nJSON: {view,environment,sinceTs,untilTs,total,unplaced,complete,window,apps:[{vendor,appName,changes:[{id,ts,person,agent,agentName,appKey,vendor,appName,command,target,actionClass,result,approved,sessionId,project,branch,pull,undo}]}]}",
|
|
158
158
|
// copy-style-allow double-space: help columns line up
|
|
159
159
|
exec: "passport exec [--wait <sec>] [--ask-again] [--verbose] [--mask | --no-masking] -- <cli> [args…]\nRuns railway, gh, vercel, wrangler, supabase, stripe, aws, neonctl, or flyctl with a key Passport holds, after your Autopilot rules decide. The key reaches only that command's environment, short-lived where the vendor allows (GitHub App tokens, 15-minute AWS sessions, 15-minute Cloudflare tokens). The exit code passes through unchanged.\n Output: when stdout isn't a terminal (an agent's shell, a pipe, a log), any copy of the key the CLI prints (raw, base64, or URL-encoded) reads <hidden by Passport>, since that output is kept and read later. In a terminal the CLI gets the terminal itself (colors, prompts), unmasked.\n --mask hide the key in a terminal too (the CLI then writes to a pipe)\n --no-masking never hide it (in a shim, set PASSPORT_EXEC_NO_MASKING=1)\n --verbose say which key was issued (on stderr)\n Asks: waits for your approval in Passport when you're at the keyboard or with --wait; otherwise exits 4 with the approval id. Run passport wait <id>, then the command again.\n Blocked or declined: exit 5 with the reason. A decline answers the same command for ten minutes; --ask-again requests it again.\n No key in Passport, or it stopped working: exit 3.\n Offline: nothing runs (exit 1); exec never uses a key on this computer.\n Refuses shells, env, and subcommands that would print the key or run another program with it.",
|
|
160
160
|
// copy-style-allow double-space: help columns line up
|
|
@@ -5,5 +5,5 @@ export declare function projectOf(raw: unknown, link: string | null): ProjectJso
|
|
|
5
5
|
/** Where each target is placed: "acme-api main Production, deploys on merge". */
|
|
6
6
|
export declare function environmentCells(env: ProjectEnvironmentJson): [string, string];
|
|
7
7
|
/** The summary block: name and counts, Environments, Rules, and Production
|
|
8
|
-
* changes
|
|
8
|
+
* changes, all over the server's window, each section only when it has something. */
|
|
9
9
|
export declare function renderProject(project: ProjectJson, term: Term): string;
|