@jjanczur/tyran 0.1.51 → 0.1.53

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.
@@ -63,9 +63,9 @@
63
63
  */
64
64
  import { spawn } from 'node:child_process';
65
65
  import { realpathSync, statSync } from 'node:fs';
66
- import { mkdtemp, readFile, rm, stat } from 'node:fs/promises';
66
+ import { mkdir, mkdtemp, readFile, rm, stat, writeFile } from 'node:fs/promises';
67
67
  import { homedir, tmpdir } from 'node:os';
68
- import { basename, dirname, join, resolve as resolvePath } from 'node:path';
68
+ import { basename, dirname, join, relative, resolve as resolvePath, sep } from 'node:path';
69
69
  import { fileURLToPath } from 'node:url';
70
70
 
71
71
  import { PASS, field, main, runGate } from './hook-io.mjs';
@@ -331,6 +331,14 @@ export function splitSegments(command) {
331
331
  // what every consumer sees for a reason unrelated to splitting.
332
332
  current += ch;
333
333
  } else if (SEPARATORS.includes(ch)) {
334
+ // A redirection's file descriptor (`2>/dev/null`, `2>&1`, `0<in`) is a
335
+ // whole word of digits directly in front of `<` or `>`, and the shell
336
+ // never passes it to the command. Left in place it was the LAST WORD of
337
+ // the segment: `git push origin :gone 2>/dev/null` read `2` as a
338
+ // refspec, so the deletion was scanned as a wide push, and `git push
339
+ // 2>&1` read `2` as the remote. Only a whole word is a descriptor, which
340
+ // is the shell's own rule: `v2>x` passes `v2` and redirects stdout.
341
+ if (ch === '<' || ch === '>') current = current.replace(/(^|\s)\d+$/, '$1');
334
342
  out.push(current);
335
343
  current = '';
336
344
  } else {
@@ -353,6 +361,318 @@ export function tokensOf(segment) {
353
361
  .filter((w) => w !== '');
354
362
  }
355
363
 
364
+ /**
365
+ * Words of one segment as the SHELL splits them, for the subcommand slot.
366
+ *
367
+ * `tokensOf` splits on whitespace and only then drops quote characters, so
368
+ * `git -c user.name="Jacek Janczura" commit` read `Janczura` as the verb and
369
+ * `GIT_AUTHOR_NAME='Jacek Janczura' git commit` read `Janczura` as the
370
+ * program. Measured with two real findings staged: both, and the single-quoted
371
+ * `-c` spelling, PASSED UNSCANNED while `-c user.name=JJ` was refused. A
372
+ * quoted value with a space is how every agent sets a commit identity.
373
+ *
374
+ * So this reading honours single quotes, double quotes (with the backslash
375
+ * escapes the shell allows there), `$'...'` and unquoted backslashes. It is an
376
+ * ADDITIONAL reading, never a replacement: `planCommand` keeps `tokensOf` and
377
+ * asks both, so a spelling either one recognises is scanned. That is the
378
+ * direction this gate wants to be wrong in, and it is why the measured reason
379
+ * `splitSegments` stays quote-blind (see SEPARATORS) still holds.
380
+ *
381
+ * Nothing is expanded: `$VAR`, globs and `~` stay literal and still fail
382
+ * `isLiteralPath`. `closed` is false when a quote is still open at the end of
383
+ * the segment, which is what a quote-blind split through a quoted value looks
384
+ * like; `quotedSeparatorHidesVerb` is what reads that shape, and a caller that
385
+ * narrows anything from these words must not do it from an open quote.
386
+ */
387
+ export function shellWords(segment) {
388
+ const text = String(segment);
389
+ const words = [];
390
+ let word = '';
391
+ let inWord = false;
392
+ let quote = null;
393
+ const open = (kind) => {
394
+ quote = kind;
395
+ };
396
+ for (let k = 0; k < text.length; k++) {
397
+ const ch = text[k];
398
+ if (quote === "'") {
399
+ if (ch === "'") quote = null;
400
+ else word += ch;
401
+ } else if (quote === "$'") {
402
+ if (ch === '\\' && k + 1 < text.length) word += text[++k];
403
+ else if (ch === "'") quote = null;
404
+ else word += ch;
405
+ } else if (quote === '"') {
406
+ if (ch === '\\' && k + 1 < text.length && '$`"\\\n'.includes(text[k + 1])) word += text[++k];
407
+ else if (ch === '"') quote = null;
408
+ else word += ch;
409
+ } else if (/\s/.test(ch)) {
410
+ if (inWord) words.push(word);
411
+ word = '';
412
+ inWord = false;
413
+ } else {
414
+ inWord = true;
415
+ if (ch === '\\') {
416
+ if (k + 1 < text.length) word += text[++k];
417
+ } else if (ch === "'" || ch === '"') {
418
+ open(ch);
419
+ } else if (ch === '$' && (text[k + 1] === "'" || text[k + 1] === '"')) {
420
+ open(text[k + 1] === "'" ? "$'" : '"');
421
+ k++;
422
+ } else {
423
+ word += ch;
424
+ }
425
+ }
426
+ }
427
+ if (inWord) words.push(word);
428
+ return { words, closed: quote === null };
429
+ }
430
+
431
+ /** `NAME=value` in the program slot: an assignment for the command after it. */
432
+ const ASSIGNMENT_RE = /^[A-Za-z_][A-Za-z0-9_]*=/;
433
+
434
+ /**
435
+ * Where the git program sits in a `shellWords` reading, and the environment
436
+ * assigned in front of it.
437
+ *
438
+ * Assignments and transparent prefixes are peeled in a LOOP, because each may
439
+ * follow the other: `env X=1 git commit` was a bypass with no quote at all,
440
+ * since the old reading peeled assignments once, then prefixes once, and read
441
+ * `X=1` as the program and `git` as the verb. A prefix's own options are
442
+ * peeled with it; `env -S STRING` splits STRING into the command it runs.
443
+ *
444
+ * When the program slot still is not git, the FIRST git word of the segment is
445
+ * taken instead. `timeout 60 git commit`, `sudo -u u git commit` and any
446
+ * wrapper this list does not know all run git, and the cost of being wrong the
447
+ * other way (`echo git commit`, unquoted) is a scan, not a pass.
448
+ */
449
+ export function gitInvocation(words) {
450
+ const all = [...words];
451
+ const env = {};
452
+ let k = 0;
453
+ for (;;) {
454
+ if (k < all.length && ASSIGNMENT_RE.test(all[k])) {
455
+ const at = all[k].indexOf('=');
456
+ env[all[k].slice(0, at)] = all[k].slice(at + 1);
457
+ k++;
458
+ continue;
459
+ }
460
+ if (k < all.length && TRANSPARENT_PREFIXES.has(basename(all[k]))) {
461
+ const prefix = basename(all[k]);
462
+ k++;
463
+ while (k < all.length && all[k].startsWith('-')) {
464
+ const flag = all[k];
465
+ k++;
466
+ if (flag === '--') break;
467
+ if (prefix !== 'env') continue;
468
+ let split = null;
469
+ if (flag === '-S' || flag === '--split-string') split = all[k++] ?? '';
470
+ else if (flag.startsWith('--split-string=')) split = flag.slice('--split-string='.length);
471
+ else if (flag.startsWith('-S')) split = flag.slice(2);
472
+ if (split !== null) all.splice(k, 0, ...shellWords(split).words);
473
+ }
474
+ continue;
475
+ }
476
+ break;
477
+ }
478
+ const index = k < all.length && isGitProgram(all[k]) ? k : all.findIndex((w, i) => i >= k && isGitProgram(w));
479
+ if (index === -1) return null;
480
+ return { index, words: all, tokens: all.slice(index), env };
481
+ }
482
+
483
+ /** `git commit` options that take no value and leave the pathspec readable. */
484
+ const COMMIT_BARE_FLAGS = new Set([
485
+ '-q', '--quiet', '-v', '--verbose', '-s', '--signoff', '--no-signoff', '-e', '--edit', '--no-edit',
486
+ '--amend', '--no-post-rewrite', '--allow-empty', '--allow-empty-message', '--dry-run', '--short',
487
+ '--branch', '--porcelain', '--long', '-z', '--null', '--reset-author', '--status', '--no-status',
488
+ '-S', '--gpg-sign', '--no-gpg-sign', '--verify', '-u', '--untracked-files',
489
+ ]);
490
+
491
+ /** `git commit` options whose value is the NEXT word unless it is attached. */
492
+ const COMMIT_VALUE_FLAGS = new Set([
493
+ '-m', '--message', '-F', '--file', '-C', '--reuse-message', '-c', '--reedit-message', '--author',
494
+ '--date', '--fixup', '--squash', '-t', '--template', '--cleanup', '--trailer',
495
+ ]);
496
+
497
+ /**
498
+ * The pathspec of a `git commit`, read from the words after the verb, or null
499
+ * when they cannot be read with certainty.
500
+ *
501
+ * A commit with a pathspec commits the WORKING-TREE content of the named
502
+ * paths and nothing else that is staged (`--only`, git's default), or the
503
+ * index plus those paths (`--include`). Scanning the whole index instead
504
+ * refused the commit for an unrelated staged key, and missed a key in an
505
+ * unstaged edit of a named path (issue #105, point 3).
506
+ *
507
+ * Narrowing a scan is the direction in which a misreading publishes a key, so
508
+ * this answers null for anything it does not positively understand: an
509
+ * unknown flag (it may take a value and shift every position after it), a
510
+ * short cluster, `-a` (git refuses it with paths), `--pathspec-from-file`, a
511
+ * path the shell would expand, a glob or pathspec magic (git's to expand, not
512
+ * this parser's). `{ paths: [] }` means "no pathspec": the commit is read as
513
+ * before.
514
+ */
515
+ export function commitPathspec(args) {
516
+ let only = false;
517
+ let include = false;
518
+ const paths = [];
519
+ for (let k = 0; k < args.length; k++) {
520
+ const t = args[k];
521
+ if (t === '--') {
522
+ paths.push(...args.slice(k + 1));
523
+ break;
524
+ }
525
+ if (!t.startsWith('-') || t === '-') {
526
+ paths.push(t);
527
+ continue;
528
+ }
529
+ if (COMMIT_VALUE_FLAGS.has(t)) {
530
+ if (k + 1 >= args.length) return null;
531
+ k++;
532
+ } else if (t.startsWith('--') && t.includes('=')) {
533
+ const name = t.slice(0, t.indexOf('='));
534
+ if (!COMMIT_VALUE_FLAGS.has(name) && name !== '--gpg-sign' && name !== '--untracked-files') return null;
535
+ } else if (/^-[mFCct]./.test(t) || /^-[Su]./.test(t)) {
536
+ // a short option with its value attached: `-mfix`, `-Skeyid`, `-uno`
537
+ } else if (t === '-o' || t === '--only') {
538
+ only = true;
539
+ } else if (t === '-i' || t === '--include') {
540
+ include = true;
541
+ } else if (!COMMIT_BARE_FLAGS.has(t)) {
542
+ return null;
543
+ }
544
+ }
545
+ if (paths.length === 0) return { paths: [], mode: null };
546
+ if (only && include) return null;
547
+ for (const p of paths) {
548
+ // `#` starts a comment: `git commit -m x # note` commits the whole index.
549
+ if (p === '' || p.startsWith(':') || p.startsWith('#') || [...EXPANSION_CHARS].some((c) => p.includes(c))) {
550
+ return null;
551
+ }
552
+ }
553
+ return { paths, mode: include ? 'include' : 'only' };
554
+ }
555
+
556
+ /** The git verbs whose arguments decide what this gate scans. */
557
+ const PUBLISHING_VERBS = Object.freeze(['commit', 'push', 'add']);
558
+
559
+ /**
560
+ * True when a command separator sits inside a quoted value IN FRONT OF the
561
+ * verb of a `git commit`, `git push` or `git add`, within one command as the
562
+ * shell runs it.
563
+ *
564
+ * That is the shape `splitSegments`, which is quote-blind on purpose, cannot
565
+ * read: it cuts the command inside the value, and the pieces land in
566
+ * different segments. Three spellings defeated a check made one segment at a
567
+ * time, each measured to pass a staged key unscanned:
568
+ *
569
+ * - `GIT_AUTHOR_NAME="A; B" git commit`: the quote hiding `git` opened in
570
+ * the previous segment;
571
+ * - `GIT_AUTHOR_NAME="A; cd /tmp" git commit`: the second segment starts
572
+ * with `cd`, which the walk handles and skips;
573
+ * - `GIT_AUTHOR_NAME="A; git -C <decoy> commit" git commit`: a commit IS
574
+ * found, in a decoy repository, and stood in for the real one.
575
+ *
576
+ * So this is decided by POSITION, in one linear pass over the whole line: the
577
+ * line is split where the shell splits it, and a segment is marked when a
578
+ * separator inside a quote comes at or before the verb's word. A separator in
579
+ * a commit MESSAGE comes after the verb (`git commit -m "fix; x"`) and does
580
+ * not mark it. A command substitution, `"$(...)"` or backticks, is parsed by
581
+ * the shell as commands, and `splitSegments` already reads those as segments
582
+ * of their own, so a separator inside one is not counted.
583
+ */
584
+ export function quotedSeparatorHidesVerb(command) {
585
+ const text = stripHeredocBodies(command).replace(/\\\r?\n/g, ' ');
586
+ const modes = [];
587
+ let words = [];
588
+ let word = '';
589
+ let inWord = false;
590
+ let sepWord = -1;
591
+ let hidden = false;
592
+ const endWord = () => {
593
+ if (inWord) words.push(word);
594
+ word = '';
595
+ inWord = false;
596
+ };
597
+ const endSegment = () => {
598
+ endWord();
599
+ if (sepWord !== -1 && !hidden) {
600
+ const inv = gitInvocation(words);
601
+ const op = inv === null ? null : gitOperation(inv.tokens);
602
+ if (op !== null && PUBLISHING_VERBS.includes(op.name) && inv.index + op.index >= sepWord) hidden = true;
603
+ }
604
+ words = [];
605
+ sepWord = -1;
606
+ };
607
+ const literal = (ch) => {
608
+ const inSubstitution = modes.includes('$(') || modes.includes('`');
609
+ if (!inSubstitution && SEPARATORS.includes(ch) && sepWord === -1) sepWord = words.length;
610
+ word += ch;
611
+ };
612
+ for (let k = 0; k < text.length; k++) {
613
+ const ch = text[k];
614
+ const mode = modes[modes.length - 1];
615
+ if (mode === "'") {
616
+ if (ch === "'") modes.pop();
617
+ else literal(ch);
618
+ continue;
619
+ }
620
+ if (mode === "$'") {
621
+ if (ch === '\\' && k + 1 < text.length) word += text[++k];
622
+ else if (ch === "'") modes.pop();
623
+ else literal(ch);
624
+ continue;
625
+ }
626
+ if (mode === '"') {
627
+ if (ch === '\\' && k + 1 < text.length && '$`"\\\n'.includes(text[k + 1])) word += text[++k];
628
+ else if (ch === '"') modes.pop();
629
+ else if (ch === '$' && text[k + 1] === '(') {
630
+ modes.push('$(');
631
+ word += '$(';
632
+ k++;
633
+ } else if (ch === '`') {
634
+ modes.push('`');
635
+ word += ch;
636
+ } else literal(ch);
637
+ continue;
638
+ }
639
+ // Unquoted, or inside a command substitution, which parses like unquoted.
640
+ if (ch === '\\') {
641
+ inWord = true;
642
+ if (k + 1 < text.length) word += text[++k];
643
+ } else if ((mode === '$(' && ch === ')') || (mode === '`' && ch === '`')) {
644
+ modes.pop();
645
+ word += ch;
646
+ } else if (ch === "'" || ch === '"') {
647
+ modes.push(ch);
648
+ inWord = true;
649
+ } else if (ch === '$' && (text[k + 1] === "'" || text[k + 1] === '(')) {
650
+ modes.push(text[k + 1] === "'" ? "$'" : '$(');
651
+ if (text[k + 1] === '(') word += '$(';
652
+ inWord = true;
653
+ k++;
654
+ } else if (ch === '`') {
655
+ modes.push('`');
656
+ inWord = true;
657
+ word += ch;
658
+ } else if (mode !== undefined) {
659
+ // Inside a substitution: its words and separators belong to the inner
660
+ // command, which `splitSegments` reads as segments of its own.
661
+ inWord = true;
662
+ word += ch;
663
+ } else if (/\s/.test(ch)) {
664
+ endWord();
665
+ } else if (SEPARATORS.includes(ch)) {
666
+ endSegment();
667
+ } else {
668
+ inWord = true;
669
+ word += ch;
670
+ }
671
+ }
672
+ endSegment();
673
+ return hidden;
674
+ }
675
+
356
676
  export function isGitProgram(token) {
357
677
  const base = basename(token).toLowerCase();
358
678
  return base === 'git' || base === 'git.exe';
@@ -556,6 +876,14 @@ const UNMODELLABLE_REMEDY =
556
876
  'a shell. Write the path literally, run the command from that directory, or split it into ' +
557
877
  'separate tool calls so each one is unambiguous.';
558
878
 
879
+ /**
880
+ * `--force-with-lease=<ref>:<full object name>`. Group 1 is the flag and ref,
881
+ * group 2 the expected value. A ref cannot contain `:`, so the split is exact;
882
+ * only the FULL 40 (sha-1) or 64 (sha-256) lowercase hex is taken, because an
883
+ * abbreviation is not worth a second rule and stays scanned.
884
+ */
885
+ const LEASE_EXPECT = /^(--force-with-lease=[^:\s]+:)([0-9a-f]{40}|[0-9a-f]{64})$/;
886
+
559
887
  function flagValue(token) {
560
888
  for (const flag of ['--git-dir=', '--work-tree=']) {
561
889
  if (token.startsWith(flag)) return token.slice(flag.length);
@@ -577,6 +905,8 @@ export function planCommand(command, startDir) {
577
905
  const triggers = [];
578
906
  const unmodellable = [];
579
907
  const extraFiles = [];
908
+ /** `--force-with-lease=<ref>:<object name>` spellings, with the repo they target. */
909
+ const leaseExpects = [];
580
910
  /** dir -> what has to be scanned there */
581
911
  const targets = new Map();
582
912
 
@@ -597,6 +927,8 @@ export function planCommand(command, startDir) {
597
927
  const stack = [];
598
928
  /** Set when a git alias makes some subcommand of this line unreadable. */
599
929
  let aliased = false;
930
+ /** Set when a separator inside a quoted value hides which subcommand runs. */
931
+ let unreadableVerb = false;
600
932
  /** Set once a `git add` is seen: a later commit in the line will include it. */
601
933
  let pendingAdd = null;
602
934
 
@@ -680,23 +1012,23 @@ export function planCommand(command, startDir) {
680
1012
 
681
1013
  // ---- which repository this segment would touch --------------------------
682
1014
  const hasGit = raw.some(isGitProgram);
683
- let dir = cur;
684
- if (hasGit) {
1015
+ const repoDir = (dashC, flagWords, gitDir, workTree) => {
1016
+ let at = cur;
685
1017
  // `-C` may repeat, each relative to the previous one (git's own rule).
686
- for (let k = 0; k < tokens.length; k++) {
687
- if (tokens[k] !== '-C') continue;
688
- const arg = tokens[k + 1];
1018
+ for (let k = 0; k < dashC.length; k++) {
1019
+ if (dashC[k] !== '-C') continue;
1020
+ const arg = dashC[k + 1];
689
1021
  if (arg === undefined || !isLiteralPath(arg)) {
690
1022
  unmodellable.push({ what: '`git -C` names a directory that needs shell expansion' });
691
1023
  } else {
692
- dir = resolvePath(dir, arg);
1024
+ at = resolvePath(at, arg);
693
1025
  }
694
1026
  }
695
- const named = [envWorkTree, envGitDir, ...raw.map(flagValue)].filter((v) => v !== null && v !== undefined);
1027
+ const named = [workTree, gitDir, ...flagWords.map(flagValue)].filter((v) => v !== null && v !== undefined);
696
1028
  // Resolved against the directory this segment runs in, NOT against each
697
1029
  // other: `--git-dir=r/.git --work-tree=r` names ONE repository twice,
698
1030
  // and folding the second onto the first produced `r/r`.
699
- const base = dir;
1031
+ const base = at;
700
1032
  for (const value of named) {
701
1033
  if (!isLiteralPath(value)) {
702
1034
  unmodellable.push({ what: 'a `--git-dir`/`--work-tree` value needs shell expansion' });
@@ -706,9 +1038,11 @@ export function planCommand(command, startDir) {
706
1038
  // `--git-dir=repo/.git` names the repository through its metadata
707
1039
  // directory. Review used exactly this spelling to walk past a rule
708
1040
  // that only understood `-C`.
709
- dir = basename(abs) === '.git' ? dirname(abs) : abs;
1041
+ at = basename(abs) === '.git' ? dirname(abs) : abs;
710
1042
  }
711
- }
1043
+ return at;
1044
+ };
1045
+ const dir = hasGit ? repoDir(tokens, raw, envGitDir, envWorkTree) : cur;
712
1046
 
713
1047
  // Asked of the SUBCOMMAND SLOT, never of the token bag.
714
1048
  //
@@ -729,15 +1063,50 @@ export function planCommand(command, startDir) {
729
1063
  // `git remote add` and `git worktree add` had the same shape against
730
1064
  // `gitAdd` — and `worktree add` is the command rule 7 tells every parallel
731
1065
  // agent to run.
732
- const op = hasGit ? gitOperation(tokens) : null;
733
- const gitCommit = op !== null && op.name === 'commit';
1066
+ //
1067
+ // Two READINGS of the slot are asked, and a verb either one finds counts.
1068
+ // `tokensOf` (whitespace, quotes dropped) is the one every rule below was
1069
+ // measured on, and a verb it recognises is handled exactly as before.
1070
+ // `shellWords` (quotes honoured) is what the shell actually runs, and it
1071
+ // is the one that sees `git -c user.name="A B" commit`: see `shellWords`
1072
+ // for the measured bypass. Each reading carries its OWN tokens and
1073
+ // directory, because after a quoted value the old tokens are misaligned:
1074
+ // `git -C "a b" commit` would scan `a`.
1075
+ const parsed = shellWords(segment);
1076
+ const quoted = gitInvocation(parsed.words);
1077
+ const quotedOp = quoted === null ? null : gitOperation(quoted.tokens);
1078
+ const readings = [{ op: hasGit ? gitOperation(tokens) : null, tokens, dir: () => dir }];
1079
+ if (quotedOp !== null) {
1080
+ readings.push({
1081
+ op: quotedOp,
1082
+ tokens: quoted.tokens,
1083
+ dir: () => {
1084
+ // A wrapper that changes directory before git runs (`env -C`,
1085
+ // `sudo -D`) is not modelled; the old reading never saw it either.
1086
+ const before = quoted.words.slice(0, quoted.index);
1087
+ if (before.some((w) => w === '-C' || w === '-D' || w === '--chdir' || w.startsWith('--chdir='))) {
1088
+ unmodellable.push({ what: 'a wrapper changes directory before `git` runs' });
1089
+ }
1090
+ return repoDir(quoted.tokens, quoted.tokens, quoted.env.GIT_DIR ?? null, quoted.env.GIT_WORK_TREE ?? null);
1091
+ },
1092
+ });
1093
+ }
1094
+ const reading = (verb) => {
1095
+ const found = readings.find((r) => r.op !== null && r.op.name === verb);
1096
+ return found === undefined ? null : { op: found.op, tokens: found.tokens, dir: found.dir() };
1097
+ };
1098
+ const commitAt = reading('commit');
734
1099
  // NOT narrower than the token bag where it counts: `git subtree push`
735
1100
  // publishes for real and its verb is the second word, so the resolver
736
1101
  // returns that word rather than stopping at `subtree`. Dropping it would
737
1102
  // have traded one false positive for a false NEGATIVE on a real push,
738
1103
  // which is the wrong direction for this gate.
739
- const gitPush = op !== null && op.name === 'push';
740
- const gitAdd = op !== null && op.name === 'add';
1104
+ const pushAt = reading('push');
1105
+ const addAt = reading('add');
1106
+ const gitCommit = commitAt !== null;
1107
+ const gitPush = pushAt !== null;
1108
+ const gitAdd = addAt !== null;
1109
+
741
1110
  const hasGh = raw.some(isGhProgram);
742
1111
  const ghPublishes =
743
1112
  hasGh &&
@@ -745,7 +1114,7 @@ export function planCommand(command, startDir) {
745
1114
  (words.has('pr') || words.has('repo') || words.has('release') || words.has('gist'));
746
1115
 
747
1116
  if (gitAdd) {
748
- const after = tokens.slice(op.index + 1);
1117
+ const after = addAt.tokens.slice(addAt.op.index + 1);
749
1118
  const all = after.some((t) => t === '-A' || t === '--all' || t === '.' || t === '-u');
750
1119
  const named = [];
751
1120
  let widen = false;
@@ -764,22 +1133,61 @@ export function planCommand(command, startDir) {
764
1133
  }
765
1134
  // Naming files one by one is the commonest form an agent uses, and it
766
1135
  // was the form round 2 left open: only `-A`/`.` folded anything in.
767
- pendingAdd = { dir, all: all || widen, paths: named };
1136
+ // Every `git add` in the line, not the last one: `git add a && git add b
1137
+ // && git commit` kept only `b`. The paths are absolute, resolved where
1138
+ // that add ran, so a later `cd` cannot change which file they name.
1139
+ pendingAdd = {
1140
+ all: (pendingAdd?.all ?? false) || all || widen,
1141
+ paths: [...new Set([...(pendingAdd?.paths ?? []), ...named.map((t) => resolvePath(addAt.dir, t))])],
1142
+ };
768
1143
  }
769
1144
 
770
1145
  if (gitCommit) {
771
- const commitAll = tokens.some((t) => t === '--all' || (isShortCluster(t) && t.includes('a')));
772
- note(dir, {
1146
+ const commitAll = commitAt.tokens.some((t) => t === '--all' || (isShortCluster(t) && t.includes('a')));
1147
+ // Any `git add` earlier in the line, not only one from the same
1148
+ // directory: `cd sub && git add x && cd .. && git commit` stages `x` in
1149
+ // the same repository from another directory, and the directories here
1150
+ // are as the shell sees them, not repositories. Folding in an add from
1151
+ // another repository scans more, never less.
1152
+ const sameRepo = pendingAdd !== null;
1153
+ // The pathspec is read only from the quote-aware reading, and only when
1154
+ // its quotes are closed: the whitespace reading turns `-m "add b.txt"`
1155
+ // into a pathspec `b.txt`, and narrowing on that would scan the wrong
1156
+ // file (see `commitPathspec`).
1157
+ const spec =
1158
+ quotedOp !== null && quotedOp.name === 'commit' && parsed.closed
1159
+ ? commitPathspec(quoted.tokens.slice(quotedOp.index + 1))
1160
+ : null;
1161
+ // One target per repository, and a line can commit twice. The second
1162
+ // commit's plan replaced the first's: its pathspec narrowed the first
1163
+ // commit's scan, and a missing `-a` dropped the first one's working
1164
+ // tree. Two commits are never narrowed, and every flag only widens.
1165
+ const prior = targets.get(commitAt.dir);
1166
+ const priorCommit = prior !== undefined && prior.scanCommit === true ? prior : null;
1167
+ const narrowed = spec !== null && spec.paths.length > 0 && priorCommit === null ? spec : null;
1168
+ note(commitAt.dir, {
1169
+ commitPaths: narrowed === null ? null : narrowed.paths,
1170
+ commitMode: narrowed === null ? null : narrowed.mode,
1171
+ // Where the pathspec is relative to: the directory the segment ran
1172
+ // in, not the repository root.
1173
+ commitFrom: commitAt.dir,
773
1174
  scanCommit: true,
774
1175
  // `git commit -a` stages tracked modifications as it runs, and
775
1176
  // `git add X && git commit` stages them in an earlier segment of the
776
1177
  // very same command line. In both cases the index at the moment this
777
1178
  // gate runs is NOT what the commit will contain, so the working tree
778
1179
  // is folded in. Round 1 declared this a limitation; closing it is
779
- // cheaper than describing it.
780
- includeWorktree: commitAll || (pendingAdd !== null && pendingAdd.dir === dir),
781
- includeUntracked: pendingAdd !== null && pendingAdd.dir === dir && pendingAdd.all,
782
- includePaths: pendingAdd !== null && pendingAdd.dir === dir ? pendingAdd.paths : [],
1180
+ // cheaper than describing it. A second commit in the line also folds
1181
+ // it in: the first one's pathspec, if it had one, committed from it.
1182
+ // So does a pathspec this commit carries but is not narrowed on.
1183
+ includeWorktree:
1184
+ commitAll ||
1185
+ sameRepo ||
1186
+ (spec !== null && spec.paths.length > 0 && narrowed === null) ||
1187
+ priorCommit?.includeWorktree === true ||
1188
+ (priorCommit?.commitPaths ?? null) !== null,
1189
+ includeUntracked: (sameRepo && pendingAdd.all) || priorCommit?.includeUntracked === true,
1190
+ includePaths: [...new Set([...(priorCommit?.includePaths ?? []), ...(sameRepo ? pendingAdd.paths : [])])],
783
1191
  });
784
1192
  triggers.push('a git commit (everything the commit would add is scanned)');
785
1193
  }
@@ -789,8 +1197,8 @@ export function planCommand(command, startDir) {
789
1197
  // lives on a private `upstream` be published to a public `origin`
790
1198
  // without ever being scanned. Review raised it as a hypothesis; it
791
1199
  // reproduced on the first attempt.
792
- const after = tokens
793
- .slice(op.index + 1)
1200
+ const after = pushAt.tokens
1201
+ .slice(pushAt.op.index + 1)
794
1202
  .filter((t) => !t.startsWith('-'))
795
1203
  .map((t) => (t.startsWith('+') ? t.slice(1) : t));
796
1204
  const remote = after[0] ?? null;
@@ -816,7 +1224,7 @@ export function planCommand(command, startDir) {
816
1224
  // would have been the third spelling of one rule in this repository
817
1225
  // (ADR-21), and the one that decides whether a push reaches production is
818
1226
  // not the place to start diverging.
819
- const pushArgv = tokens.slice(op.index + 1);
1227
+ const pushArgv = pushAt.tokens.slice(pushAt.op.index + 1);
820
1228
  const deletesOnly = readable && isDeletionOnly(pushArgv, after.slice(1));
821
1229
  // One line can push the same repository more than once. The scan is the
822
1230
  // UNION of what every segment publishes: letting the last segment's refs
@@ -824,25 +1232,58 @@ export function planCommand(command, startDir) {
824
1232
  // main` publish `leaky` unscanned, and a deletion, which adds nothing,
825
1233
  // must not replace a real push either. `pushRefs: []` is the wide range
826
1234
  // and stays wide.
827
- const prior = targets.get(dir);
1235
+ const prior = targets.get(pushAt.dir);
828
1236
  const priorPush = prior !== undefined && prior.scanPush === true ? prior : null;
1237
+ //
1238
+ // The REMOTES are unioned too, for the same reason. Which commits are
1239
+ // already public is a question about one remote (see `pushRange`), and
1240
+ // keeping only the last one let `git push upstream leaky && git push
1241
+ // origin leaky` scan `leaky --not --remotes=origin`: empty, because
1242
+ // `leaky` was on origin already, while upstream had never seen it.
1243
+ // `buildPayload` lists the range once per remote and scans the union.
829
1244
  let scan;
830
1245
  if (deletesOnly) {
831
- scan = priorPush !== null ? {} : { pushRemote: remote, pushRefs: [], pushDeletesOnly: true };
1246
+ scan =
1247
+ priorPush !== null ? {} : { pushRemote: remote, pushRemotes: [remote], pushRefs: [], pushDeletesOnly: true };
832
1248
  } else if (priorPush === null || priorPush.pushDeletesOnly === true) {
833
- scan = { pushRemote: remote, pushRefs: refs, pushDeletesOnly: false };
1249
+ scan = { pushRemote: remote, pushRemotes: [remote], pushRefs: refs, pushDeletesOnly: false };
834
1250
  } else {
835
1251
  // A prior `gh` publish can leave `pushRefs` unset, which is wide too.
836
1252
  const priorRefs = priorPush.pushRefs ?? [];
837
1253
  const wide = priorRefs.length === 0 || refs.length === 0;
838
- scan = { pushRemote: remote, pushRefs: wide ? [] : [...new Set([...priorRefs, ...refs])] };
1254
+ // A prior `gh pr create` names no remote (null). Beside a push that
1255
+ // names one it is not a second remote: counted as one, a repository
1256
+ // with two remotes refused `gh pr create && git push origin x`, which
1257
+ // `pushRange` cannot answer for an unnamed remote.
1258
+ const remotes = [...new Set([...(priorPush.pushRemotes ?? [priorPush.pushRemote]), remote])];
1259
+ const named = remotes.filter((r) => r !== null);
1260
+ scan = {
1261
+ pushRemote: remote,
1262
+ pushRemotes: named.length > 0 ? named : remotes,
1263
+ pushRefs: wide ? [] : [...new Set([...priorRefs, ...refs])],
1264
+ };
839
1265
  }
840
- note(dir, { scanPush: true, ...scan, pushArgv, pushReadable: readable });
1266
+ // Every push's argv, in order, deletions included. The policy gate
1267
+ // judges each one: keeping only the last let `git push origin HEAD:main
1268
+ // && git push origin feature/x` reach `main` under P1 while the gate
1269
+ // read `feature/x`. `pushArgv` is the last of them, for readers that
1270
+ // only ever see one push.
1271
+ const pushArgvs = [...(prior?.pushArgvs ?? []), pushArgv];
1272
+ note(pushAt.dir, { scanPush: true, ...scan, pushArgv, pushArgvs, pushReadable: readable });
841
1273
  triggers.push(
842
1274
  deletesOnly
843
1275
  ? 'a git push that only deletes remote refs (it publishes no objects; the command line is scanned)'
844
1276
  : 'a git push (every commit not already on the target remote is scanned)',
845
1277
  );
1278
+ // The expected value of an explicit lease is a git object name, which
1279
+ // gitleaks reads as a high-entropy `generic-api-key` whenever the ref in
1280
+ // front of it carries a keyword (`secret`, `token`, `api`...). Recorded
1281
+ // here, with the directory THIS segment targets, so `decide` can check it
1282
+ // against that repository before it hides anything from the scan.
1283
+ for (const t of pushArgv) {
1284
+ const lease = LEASE_EXPECT.exec(t);
1285
+ if (lease !== null) leaseExpects.push({ dir: pushAt.dir, spelled: t, sha: lease[2] });
1286
+ }
846
1287
  }
847
1288
  if (ghPublishes) {
848
1289
  // `gh pr create` publishes ONE branch — the PR's head — and adds no git
@@ -997,7 +1438,7 @@ export function planCommand(command, startDir) {
997
1438
  remedy: 'run the command without -c core.hooksPath=... (a bare `git config core.hooksPath` query is fine)',
998
1439
  });
999
1440
  }
1000
- if (gitCommit && tokens.some((t) => isShortCluster(t) && t.includes('n'))) {
1441
+ if (gitCommit && commitAt.tokens.some((t) => isShortCluster(t) && t.includes('n'))) {
1001
1442
  denials.push({
1002
1443
  code: DENIAL_CODES.NO_VERIFY,
1003
1444
  detail: "`-n` on git commit is --no-verify, which disables git's own hooks",
@@ -1005,7 +1446,7 @@ export function planCommand(command, startDir) {
1005
1446
  });
1006
1447
  }
1007
1448
  if (gitPush) {
1008
- for (const token of tokens) {
1449
+ for (const token of pushAt.tokens) {
1009
1450
  const forced =
1010
1451
  token.startsWith('--force') ||
1011
1452
  (isShortCluster(token) && token.includes('f')) ||
@@ -1049,16 +1490,31 @@ export function planCommand(command, startDir) {
1049
1490
  }
1050
1491
  }
1051
1492
 
1493
+ // FAIL CLOSED where a separator inside a quoted value hides the verb.
1494
+ // `splitSegments` is quote-blind on purpose, so `git -c user.name="A; B"
1495
+ // commit` arrives as `git -c user.name="A` (git, no verb) and ` B" commit`
1496
+ // (verb, no git), and no reading of either segment sees a commit. Asked of
1497
+ // the whole line, once, so no `continue` in the walk above can skip it.
1498
+ if (quotedSeparatorHidesVerb(command)) {
1499
+ unreadableVerb = true;
1500
+ unmodellable.push({
1501
+ what: 'a quoted value in front of the `git` subcommand holds a command separator, so which subcommand runs cannot be read',
1502
+ });
1503
+ }
1504
+
1052
1505
  // `aliased` counts as "something might be published here": without it the
1053
1506
  // unmodellable entry above is discarded by the filter below, which is
1054
- // exactly how the hole stayed open.
1055
- const needsScan = targets.size > 0 || extraFiles.length > 0 || aliased;
1507
+ // exactly how the hole stayed open. `unreadableVerb` is the same hole,
1508
+ // opened by a quoted separator instead of an alias.
1509
+ const needsScan = targets.size > 0 || extraFiles.length > 0 || aliased || unreadableVerb;
1056
1510
  return {
1057
1511
  aliased,
1512
+ unreadableVerb,
1058
1513
  denials,
1059
1514
  triggers: [...new Set(triggers)],
1060
1515
  targets: [...targets.values()],
1061
1516
  extraFiles,
1517
+ leaseExpects,
1062
1518
  // An unmodellable construct only matters if something is being published.
1063
1519
  // `cd "$D" && npm test` has no scan to misdirect.
1064
1520
  unmodellable: needsScan ? unmodellable : [],
@@ -1531,27 +1987,64 @@ export async function buildPayload(target, extraFiles, { runner, budget }) {
1531
1987
  };
1532
1988
 
1533
1989
  if (target !== null && target.scanCommit) {
1534
- const staged = parseRawDiff(
1535
- await git(['-C', target.dir, 'diff', '--cached', '--raw', '-z', '--no-renames', '--diff-filter=ACMR'], {
1536
- runner, timeoutMs: budget(GIT_BUDGET_MS), what: 'reading the staged index',
1537
- }),
1538
- );
1539
- assertComplete(0, staged.malformed, 'reading the staged index');
1540
- const wanted = [...staged.entries];
1541
- if (target.includeWorktree) {
1542
- const unstaged = parseRawDiff(
1543
- await git(['-C', target.dir, 'diff', '--raw', '-z', '--no-renames', '--diff-filter=ACMR'], {
1544
- runner, timeoutMs: budget(GIT_BUDGET_MS), what: 'reading unstaged modifications',
1545
- }),
1546
- );
1547
- assertComplete(0, unstaged.malformed, 'reading unstaged modifications');
1548
- wanted.push(...unstaged.entries);
1990
+ const rawDiff = async (args, what) => {
1991
+ const parsed = parseRawDiff(await git(args, { runner, timeoutMs: budget(GIT_BUDGET_MS), what }));
1992
+ assertComplete(0, parsed.malformed, what);
1993
+ return parsed.entries;
1994
+ };
1995
+ // `--no-relative`: with `diff.relative` set, a diff run from a
1996
+ // subdirectory prints paths relative to it, and every one would then be
1997
+ // read from the wrong place below.
1998
+ const DIFF = ['--raw', '-z', '--no-renames', '--no-relative', '--diff-filter=ACMR'];
1999
+ // A pathspec is relative to the directory the commit ran in, so these
2000
+ // diffs run THERE; their output paths are relative to the root either way.
2001
+ const from = target.commitFrom ?? target.dir;
2002
+ const paths = Array.isArray(target.commitPaths) && target.commitPaths.length > 0 ? target.commitPaths : null;
2003
+ // `--only` (git's default with a pathspec) commits the working-tree
2004
+ // content of the named paths over HEAD, and nothing else that is staged:
2005
+ // the diff from HEAD to the working tree, for those paths, is exactly
2006
+ // what it adds. With no HEAD yet there is nothing to diff from, and the
2007
+ // commit is scanned as a whole instead (see below).
2008
+ //
2009
+ // Narrowed only on PROOF that the pathspec names what git will commit:
2010
+ // every path must match a tracked file. Words that merely parse as paths
2011
+ // (`git commit -m fix the bug`) match nothing, git refuses that commit,
2012
+ // and the scan stays whole.
2013
+ const ok = (r) => r.spawned === true && r.timedOut !== true && r.code === 0;
2014
+ let hasHead = false;
2015
+ if (paths !== null && target.commitMode === 'only') {
2016
+ hasHead =
2017
+ ok(await runner('git', ['-C', target.dir, 'rev-parse', '--verify', '--quiet', 'HEAD^{commit}'], {
2018
+ timeoutMs: budget(GIT_BUDGET_MS),
2019
+ })) &&
2020
+ ok(await runner('git', ['-C', from, 'ls-files', '--error-unmatch', '--', ...paths], {
2021
+ timeoutMs: budget(GIT_BUDGET_MS),
2022
+ }));
2023
+ }
2024
+ const wanted = [];
2025
+ if (hasHead) {
2026
+ wanted.push(...(await rawDiff(['-C', from, 'diff', 'HEAD', ...DIFF, '--', ...paths], 'reading the paths this commit names')));
2027
+ } else {
2028
+ wanted.push(...(await rawDiff(['-C', target.dir, 'diff', '--cached', ...DIFF], 'reading the staged index')));
2029
+ if (paths !== null && target.commitMode === 'include' && !target.includeWorktree) {
2030
+ // `--include`: the index, plus the named paths as they are on disk.
2031
+ wanted.push(...(await rawDiff(['-C', from, 'diff', ...DIFF, '--', ...paths], 'reading the paths this commit names')));
2032
+ } else if (target.includeWorktree || paths !== null) {
2033
+ // A pathspec this gate could not narrow on (no HEAD) still commits
2034
+ // the named paths' working-tree content, so the working tree is folded
2035
+ // in: wider than the commit, never narrower.
2036
+ wanted.push(...(await rawDiff(['-C', target.dir, 'diff', ...DIFF], 'reading unstaged modifications')));
2037
+ }
1549
2038
  }
1550
2039
  // Files named on a `git add <path>` earlier in the same command line. They
1551
2040
  // are not in the index yet and may not be modifications of a tracked file
1552
- // at all, so neither diff above can see them.
1553
- for (const rel of target.includePaths ?? []) {
1554
- add(rel, await readFileBounded(join(target.dir, rel)));
2041
+ // at all, so neither diff above can see them. They are read where that
2042
+ // `git add` ran: joining them to the repository ROOT read `sub/new.pem`
2043
+ // as `new.pem` when the add ran in `sub/`, found nothing there, and the
2044
+ // commit published it unscanned.
2045
+ for (const path of target.includePaths ?? []) {
2046
+ const abs = resolvePath(target.dir, path);
2047
+ add(repoLabel(target.dir, abs), await readFileBounded(abs));
1555
2048
  }
1556
2049
  if (target.includeUntracked) {
1557
2050
  const listed = await git(['-C', target.dir, 'ls-files', '-z', '--others', '--exclude-standard'], {
@@ -1573,17 +2066,25 @@ export async function buildPayload(target, extraFiles, { runner, budget }) {
1573
2066
  // so there is no range to list. Its command line is still scanned by
1574
2067
  // `decide`, like every other command this gate sees.
1575
2068
  if (target !== null && target.scanPush && target.pushDeletesOnly !== true) {
1576
- const range = await pushRange(target.dir, target.pushRemote, target.pushRefs, {
1577
- runner, timeoutMs: budget(GIT_BUDGET_MS),
1578
- });
1579
- const listed = await git(['-C', target.dir, 'rev-list', '--objects', ...range], {
1580
- runner, timeoutMs: budget(GIT_BUDGET_MS), what: 'listing the objects this push would publish',
1581
- });
2069
+ // One range per remote the line pushes to (see `planCommand`): what is
2070
+ // already public on one remote is not on another. `addBlobs` reads each
2071
+ // object once, so the union costs nothing for what the ranges share.
2072
+ const remotes = Array.isArray(target.pushRemotes) && target.pushRemotes.length > 0
2073
+ ? target.pushRemotes
2074
+ : [target.pushRemote];
1582
2075
  const objects = [];
1583
- for (const line of String(listed).split('\n')) {
1584
- const space = line.indexOf(' ');
1585
- if (space === -1) continue; // a commit, which carries no path
1586
- objects.push({ sha: line.slice(0, space), path: line.slice(space + 1) });
2076
+ for (const remote of remotes) {
2077
+ const range = await pushRange(target.dir, remote, target.pushRefs, {
2078
+ runner, timeoutMs: budget(GIT_BUDGET_MS),
2079
+ });
2080
+ const listed = await git(['-C', target.dir, 'rev-list', '--objects', ...range], {
2081
+ runner, timeoutMs: budget(GIT_BUDGET_MS), what: 'listing the objects this push would publish',
2082
+ });
2083
+ for (const line of String(listed).split('\n')) {
2084
+ const space = line.indexOf(' ');
2085
+ if (space === -1) continue; // a commit, which carries no path
2086
+ objects.push({ sha: line.slice(0, space), path: line.slice(space + 1) });
2087
+ }
1587
2088
  }
1588
2089
  await addBlobs(target.dir, objects, add, { runner, budget });
1589
2090
  }
@@ -1594,6 +2095,22 @@ export async function buildPayload(target, extraFiles, { runner, budget }) {
1594
2095
  add(file.shown, await readFileBounded(file.path));
1595
2096
  }
1596
2097
 
2098
+ const { bytes, map } = assemble(chunks);
2099
+ if (bytes.length > MAX_PAYLOAD_BYTES) {
2100
+ throw new ScanFailure(
2101
+ `this would publish ${bytes.length} bytes, past the ${MAX_PAYLOAD_BYTES} this gate can scan inside its budget`,
2102
+ 'commit or push in smaller steps. The gate refuses rather than scanning a prefix: a partial ' +
2103
+ 'scan that reports nothing looks exactly like a clean one.',
2104
+ );
2105
+ }
2106
+ return { bytes, map, chunks, files: chunks.length };
2107
+ }
2108
+
2109
+ /**
2110
+ * Chunks as ONE byte stream for `gitleaks stdin`, with a line map back to
2111
+ * their labels.
2112
+ */
2113
+ export function assemble(chunks) {
1597
2114
  const parts = [];
1598
2115
  const map = [];
1599
2116
  let line = 1;
@@ -1609,15 +2126,211 @@ export async function buildPayload(target, extraFiles, { runner, budget }) {
1609
2126
  parts.push(chunk.body, Buffer.from('\n\n'));
1610
2127
  line += newlines + 2;
1611
2128
  }
1612
- const bytes = Buffer.concat(parts);
1613
- if (bytes.length > MAX_PAYLOAD_BYTES) {
2129
+ return { bytes: Buffer.concat(parts), map };
2130
+ }
2131
+
2132
+ /**
2133
+ * The repository-relative label of a file named on the command line, or the
2134
+ * path as given when it is not inside the repository. Both sides are
2135
+ * canonicalized, because the shell's spelling of a directory and git's
2136
+ * `--show-toplevel` disagree wherever a symlink sits on the way (`/var` and
2137
+ * `/private/var` on macOS).
2138
+ */
2139
+ function repoLabel(top, abs) {
2140
+ const canonical = (p) => {
2141
+ try {
2142
+ return realpathSync(p);
2143
+ } catch {
2144
+ return p;
2145
+ }
2146
+ };
2147
+ const rel = relative(canonical(top), join(canonical(dirname(abs)), basename(abs)));
2148
+ return rel.split(sep).join('/');
2149
+ }
2150
+
2151
+ // --------------------------------------------------------- scanning by path
2152
+
2153
+ /**
2154
+ * A chunk label as a path the gate may write into a scan tree, or null.
2155
+ *
2156
+ * Only a plain relative path qualifies: no `..`, no empty or `.` segment, no
2157
+ * absolute path, nothing under `.git/`, and no file whose name gitleaks reads
2158
+ * as its OWN configuration when it finds one in the tree it scans
2159
+ * (`.gitleaks.toml`, `.gitleaksignore`, measured: a `.gitleaksignore` at the
2160
+ * root of the tree suppressed a finding even with `--gitleaks-ignore-path`
2161
+ * pointing elsewhere). A label that does not qualify is scanned the old way.
2162
+ */
2163
+ function treePath(label) {
2164
+ if (typeof label !== 'string' || label === '' || label.length > 4096) return null;
2165
+ if (label.startsWith('/') || label.includes('\\') || label.includes('\u0000')) return null;
2166
+ const parts = label.split('/');
2167
+ if (parts.some((p) => p === '' || p === '.' || p === '..' || Buffer.byteLength(p, 'utf8') > 255)) return null;
2168
+ if (parts[0] === '.git' || basename(label).startsWith('.gitleaks')) return null;
2169
+ return label;
2170
+ }
2171
+
2172
+ /** How many trees one payload may be split into before the rest goes to stdin. */
2173
+ export const MAX_PATH_ROUNDS = 4;
2174
+
2175
+ /**
2176
+ * Split chunks into ROUNDS that can each be written as one tree, and the
2177
+ * REST, which cannot be written as a tree at all.
2178
+ *
2179
+ * A push range carries several versions of one path, and a disk may fold case
2180
+ * or Unicode normalization, so two labels can name one file; and a path can
2181
+ * be a file in one commit and a directory in another. Each round holds every
2182
+ * path at most once, under that folding, and never a file where another
2183
+ * path needs a directory. A chunk that fits no round within `maxRounds`, and
2184
+ * a chunk whose label is no plain relative path (`treePath`), goes to `rest`
2185
+ * and is scanned exactly as before this existed.
2186
+ */
2187
+ export function pathScanRounds(chunks, maxRounds = MAX_PATH_ROUNDS) {
2188
+ const rounds = [];
2189
+ const rest = [];
2190
+ for (const chunk of chunks) {
2191
+ const path = treePath(chunk.label);
2192
+ if (path === null) {
2193
+ rest.push(chunk);
2194
+ continue;
2195
+ }
2196
+ const key = path.normalize('NFC').toLowerCase();
2197
+ const segments = key.split('/');
2198
+ const parents = segments.slice(0, -1).map((_, i) => segments.slice(0, i + 1).join('/'));
2199
+ const round = rounds.find(
2200
+ (r) => !r.files.has(key) && !r.dirs.has(key) && !parents.some((d) => r.files.has(d)),
2201
+ );
2202
+ if (round !== undefined) {
2203
+ round.chunks.push(chunk);
2204
+ round.files.add(key);
2205
+ for (const d of parents) round.dirs.add(d);
2206
+ } else if (rounds.length < maxRounds) {
2207
+ rounds.push({ chunks: [chunk], files: new Set([key]), dirs: new Set(parents) });
2208
+ } else {
2209
+ rest.push(chunk);
2210
+ }
2211
+ }
2212
+ return { rounds: rounds.map((r) => r.chunks), rest };
2213
+ }
2214
+
2215
+ /** One trace line of `gitleaks dir`: a file it read, or one it skipped and why. */
2216
+ const TRACE_LINE = /\b(?:TRC|DBG) (scanning path|skipping file: ([^=]*?)) path=(.*)$/;
2217
+
2218
+ /** A `path=` value from that trace, unquoted the way gitleaks quotes one with a space. */
2219
+ function tracePath(value) {
2220
+ const v = value.trim();
2221
+ if (!v.startsWith('"')) return v.replace(/^\.\//, '');
2222
+ try {
2223
+ return JSON.parse(v).replace(/^\.\//, '');
2224
+ } catch {
2225
+ return null;
2226
+ }
2227
+ }
2228
+
2229
+ /**
2230
+ * Scan chunks as FILES, under their own paths, so a tracked `.gitleaks.toml`
2231
+ * can allowlist a path (issue #105, point 1). Returns the findings, or null
2232
+ * when the scan's coverage could not be accounted for, and the caller scans
2233
+ * the same chunks over stdin instead.
2234
+ *
2235
+ * `gitleaks stdin` sees one nameless buffer, so every finding had `File: ""`
2236
+ * and a `paths` allowlist could never match: a fixture under `__tests__/` was
2237
+ * refused whatever the config said. `gitleaks dir` reads real paths and
2238
+ * applies the config's allowlists, and it SKIPS a file a path allowlist
2239
+ * matches, reading none of its bytes, so the stdin coverage rule ("read
2240
+ * exactly the bytes sent") cannot hold. The rule that replaces it is the same
2241
+ * arithmetic, per file: the scanner's trace must name EVERY file written,
2242
+ * either as read or as skipped by the allowlist and for no other reason, and
2243
+ * the bytes it reports must equal the sizes of the files it says it read.
2244
+ * Anything else (a file the trace does not name, another skip reason, a path
2245
+ * that will not parse, a byte count that disagrees) is not trusted, and those
2246
+ * chunks are scanned the old way: failing to use a path costs a false
2247
+ * refusal, never a missed key.
2248
+ *
2249
+ * The tree is written by this gate into an empty directory, so no file the
2250
+ * command did not determine can be in it, and nothing the scanner reads as
2251
+ * configuration (`treePath`). The config itself is passed explicitly; the
2252
+ * ignore path is always explicit too, so the default (`.`, the tree) is never
2253
+ * consulted.
2254
+ */
2255
+ export async function scanTree(chunks, { runner, budget, workDir, suppressionArgs = [] }) {
2256
+ const timeoutMs = budget(CHILD_BUDGET_MS);
2257
+ if (timeoutMs <= 0) {
1614
2258
  throw new ScanFailure(
1615
- `this would publish ${bytes.length} bytes, past the ${MAX_PAYLOAD_BYTES} this gate can scan inside its budget`,
1616
- 'commit or push in smaller steps. The gate refuses rather than scanning a prefix: a partial ' +
1617
- 'scan that reports nothing looks exactly like a clean one.',
2259
+ 'the gate ran out of its own budget before it could start the scan',
2260
+ 'raise DEADLINE_MS together with the timeout in hooks.json',
1618
2261
  );
1619
2262
  }
1620
- return { bytes, map, files: chunks.length };
2263
+ const tree = await mkdtemp(join(workDir, 'tree-'));
2264
+ const sizes = new Map();
2265
+ try {
2266
+ for (const chunk of chunks) {
2267
+ const abs = join(tree, ...chunk.label.split('/'));
2268
+ await mkdir(dirname(abs), { recursive: true });
2269
+ // `wx`: a path that already exists is a collision `pathScanRounds`
2270
+ // should have prevented, and it must fail rather than overwrite.
2271
+ await writeFile(abs, chunk.body, { flag: 'wx' });
2272
+ sizes.set(chunk.label, chunk.body.length);
2273
+ }
2274
+ } catch {
2275
+ // A name this disk will not hold (too long, a folding `pathScanRounds`
2276
+ // did not foresee): those files are scanned over stdin instead.
2277
+ return null;
2278
+ }
2279
+ const ignoreArgs = [];
2280
+ if (!suppressionArgs.includes('--gitleaks-ignore-path')) {
2281
+ const empty = join(workDir, 'no-ignore');
2282
+ await writeFile(empty, '');
2283
+ ignoreArgs.push('--gitleaks-ignore-path', empty);
2284
+ }
2285
+ const reportPath = join(workDir, `report-${Math.random().toString(36).slice(2)}.json`);
2286
+ const args = [
2287
+ 'dir', '.',
2288
+ '--report-format', 'json',
2289
+ '--report-path', reportPath,
2290
+ '--no-banner',
2291
+ '--no-color',
2292
+ '--redact',
2293
+ '--log-level', 'trace',
2294
+ ...ignoreArgs,
2295
+ ...suppressionArgs,
2296
+ ];
2297
+ const result = await runner(GITLEAKS_BIN(), args, { cwd: tree, timeoutMs, env: scannerEnv() });
2298
+ if (!result.spawned) {
2299
+ if (result.error?.code === 'ENOENT') {
2300
+ throw new ScanFailure('gitleaks is not installed (or not on PATH)', INSTALL_INSTRUCTIONS);
2301
+ }
2302
+ throw new ScanFailure(`gitleaks could not be started (${result.error?.code ?? 'unknown error'})`, INSTALL_INSTRUCTIONS);
2303
+ }
2304
+ if (result.timedOut) {
2305
+ throw new ScanFailure(
2306
+ `the scan did not finish within ${timeoutMs} ms and was killed`,
2307
+ 'commit or push in smaller steps, or run gitleaks by hand',
2308
+ );
2309
+ }
2310
+ const read = new Set();
2311
+ const skipped = new Set();
2312
+ let unaccounted = false;
2313
+ for (const line of String(result.stderr).split('\n')) {
2314
+ const m = TRACE_LINE.exec(line);
2315
+ if (m === null) continue;
2316
+ const path = tracePath(m[3]);
2317
+ if (path === null || !sizes.has(path)) unaccounted = true;
2318
+ else if (m[1] === 'scanning path') read.add(path);
2319
+ else if (m[2].trim() === 'global allowlist') skipped.add(path);
2320
+ else unaccounted = true;
2321
+ }
2322
+ const readBytes = [...read].reduce((n, p) => n + sizes.get(p), 0);
2323
+ const covered =
2324
+ !unaccounted &&
2325
+ [...sizes.keys()].every((p) => read.has(p) !== skipped.has(p)) &&
2326
+ parseScannedBytes(result.stderr) === readBytes;
2327
+ if (!covered) return null;
2328
+ return (await readReport(reportPath, result)).map((raw) => ({
2329
+ ...safeFinding(raw),
2330
+ line: null,
2331
+ label: typeof raw?.File === 'string' ? raw.File.replace(/^\.\//, '') : null,
2332
+ fileLine: Number.isInteger(raw?.StartLine) ? raw.StartLine : null,
2333
+ }));
1621
2334
  }
1622
2335
 
1623
2336
  // ------------------------------------------------------------------ scanner
@@ -1896,7 +2609,7 @@ export function locate(map, line) {
1896
2609
 
1897
2610
  export function renderFindings(findings, map) {
1898
2611
  const shown = findings.slice(0, MAX_FINDINGS_SHOWN).map((f) => {
1899
- const at = locate(map, f.line);
2612
+ const at = f.label !== undefined ? { label: f.label, line: f.fileLine } : locate(map, f.line);
1900
2613
  const where = at.label === null ? 'somewhere in the scanned payload' : JSON.stringify(shortPath(at.label));
1901
2614
  const line = at.line === null ? '' : `:${at.line}`;
1902
2615
  return ` - ${where}${line} — rule \`${safeRuleName(f.rule)}\``;
@@ -1907,6 +2620,40 @@ export function renderFindings(findings, map) {
1907
2620
 
1908
2621
  // ----------------------------------------------------------------- the gate
1909
2622
 
2623
+ /**
2624
+ * The command text with the expected value of an explicit lease hidden from
2625
+ * the scanner, and nothing else changed.
2626
+ *
2627
+ * `git push --force-with-lease=pv3/api-fix:<40 hex> origin ...` was refused as
2628
+ * `generic-api-key`: the ref carries a keyword and the object name's entropy is
2629
+ * above the rule's floor (measured, 1908 of 2000 recent shas). The value is
2630
+ * hidden ONLY when it names an object that exists in the repository the
2631
+ * segment targets: a hash cannot be chosen to carry a payload, while a 40-hex
2632
+ * that is no object is exactly what a pasted key looks like and stays scanned.
2633
+ * Same length, all zeros, so offsets and the byte count do not move. Any
2634
+ * failure to ask (no budget, git missing, timeout, not a repo) leaves the text
2635
+ * as it was: the question is only ever allowed to NARROW a refusal on proof.
2636
+ */
2637
+ async function maskLeaseExpects(command, leaseExpects, { runner, budget }) {
2638
+ let text = command;
2639
+ for (const { dir, spelled, sha } of leaseExpects) {
2640
+ const timeoutMs = budget(GIT_BUDGET_MS);
2641
+ if (timeoutMs <= 0) break;
2642
+ try {
2643
+ // `--no-lazy-fetch`: in a partial clone, asking about a missing object
2644
+ // makes git FETCH it from the promisor remote, sending the very value
2645
+ // under suspicion off the machine (measured: `want <hex>` on the wire).
2646
+ // A git too old to know the flag exits 129, which leaves the text scanned.
2647
+ const r = await runner('git', ['--no-lazy-fetch', '-C', dir, 'cat-file', '-e', `${sha}^{object}`], { timeoutMs });
2648
+ if (r.spawned !== true || r.timedOut === true || r.code !== 0) continue;
2649
+ } catch {
2650
+ continue;
2651
+ }
2652
+ text = text.split(spelled).join(spelled.slice(0, spelled.length - sha.length) + '0'.repeat(sha.length));
2653
+ }
2654
+ return text;
2655
+ }
2656
+
1910
2657
  export async function decide({ input, cwd, runner = runChild, startedAt = Date.now() } = {}) {
1911
2658
  const budget = makeBudget(startedAt);
1912
2659
  const toolName = field(input, 'tool_name');
@@ -1998,29 +2745,52 @@ export async function decide({ input, cwd, runner = runChild, startedAt = Date.n
1998
2745
  suppressed.push(...supp.found);
1999
2746
  const payload = await buildPayload(resolved, index === 0 ? plan.extraFiles : [], { runner, budget });
2000
2747
  totalBytes += payload.bytes.length;
2001
- if (payload.bytes.length > 0) {
2748
+ // With a TRACKED config, every chunk that can be a file in a tree is
2749
+ // scanned under its own path, so the config's path allowlists apply
2750
+ // (`scanTree`). Without one nothing changes: gitleaks' default config
2751
+ // allowlists paths too (lock files, `vendor/`...), and applying that
2752
+ // here would pass what this gate refused before.
2753
+ const byPath = supp.found.some((f) => f.kind === 'config');
2754
+ const { rounds, rest } = byPath ? pathScanRounds(payload.chunks) : { rounds: [], rest: payload.chunks };
2755
+ const viaStdin = [...rest];
2756
+ let viaTree = 0;
2757
+ for (const round of rounds) {
2758
+ const found = await scanTree(round, { runner, budget, workDir, suppressionArgs: supp.args });
2759
+ if (found === null) {
2760
+ viaStdin.push(...round);
2761
+ } else {
2762
+ findings.push(...found);
2763
+ viaTree += round.length;
2764
+ }
2765
+ }
2766
+ const stream = assemble(viaStdin);
2767
+ if (stream.bytes.length > 0) {
2002
2768
  // The map and the FINDINGS have to move by the SAME amount, and by the
2003
2769
  // offset as it stood BEFORE this payload was added. Capturing it after
2004
2770
  // the increment shifted every finding by a whole payload and produced
2005
2771
  // "somewhere in the scanned payload" — a refusal that names nothing.
2006
2772
  const base = lineOffset;
2007
- for (const entry of payload.map) {
2773
+ for (const entry of stream.map) {
2008
2774
  map.push({ ...entry, from: entry.from + base, to: entry.to + base });
2009
2775
  }
2010
- const found = await scanBytes(payload.bytes, { runner, budget, workDir, suppressionArgs: supp.args });
2776
+ const found = await scanBytes(stream.bytes, { runner, budget, workDir, suppressionArgs: supp.args });
2011
2777
  findings.push(...found.map((f) => ({ ...f, line: f.line === null ? null : f.line + base })));
2012
- lineOffset += newlineCount(payload.bytes) + 2;
2778
+ lineOffset += newlineCount(stream.bytes) + 2;
2013
2779
  }
2014
2780
  scanned.push(
2015
2781
  resolved === null
2016
2782
  ? `${payload.files} uploaded file(s)`
2017
- : `${payload.files} object(s), ${payload.bytes.length} bytes, from ${shortPath(resolved.dir)}`,
2783
+ : `${payload.files} object(s), ${payload.bytes.length} bytes, from ${shortPath(resolved.dir)}` +
2784
+ (viaTree > 0 ? ` (${viaTree} of them by path, under the tracked ${SUPPRESSION_FILES.config})` : ''),
2018
2785
  );
2019
2786
  }
2020
2787
 
2021
2788
  // The command line itself, through the same rule set. `git commit -m
2022
2789
  // "<key>"` puts a secret in history without it ever being a staged file.
2023
- const commandBytes = Buffer.from(command, 'utf8');
2790
+ const commandBytes = Buffer.from(
2791
+ await maskLeaseExpects(command, plan.leaseExpects, { runner, budget }),
2792
+ 'utf8',
2793
+ );
2024
2794
  const commandFindings = await scanBytes(commandBytes, { runner, budget, workDir });
2025
2795
  for (const f of commandFindings) findings.push({ ...f, line: null, inCommand: true });
2026
2796
  scanned.push('the command line itself');