pi-ext bugabingas pi extensions
owned by bugabinga
README Source History Refs Compare Notes Search refactor(git-safe): drop --no-verify rejection 13a3cf54e1ca86564a31e07fee43333a49c9c0ab
Oliver Jan Krylow <oliver@bugabinga.net > · 25 Sep 2026
SSH signature · not checked
Browse files Compare with HEAD
jj shells out to git push with --no-verify by design, so the wrapper
blocked a legitimate VCS workflow. The Git wrapper now only enforces
noninteractive editor settings and delegates everything else.
Changed files (7) extensions/git-safe/CHANGELOG.md Modified 7 7 - Prevent agent-issued Git commands from opening interactive editors
8 - - Reject hook-skipping `--no-verify` options through normal Git, aliases, shell indirection, nested Bash, `xargs`, child-process PATH lookup, and literal bypasses
9 8 - Launch through Git Bash even when callers disable MSYS argument conversion, and hide launcher internals when Git cannot start
10 9 - Document explicit `git.exe` child-process and unrestricted-Bash residual bypasses
11 10 - Keep safe-clone behavior unchanged
extensions/git-safe/README.md Modified 40 40 A generated Git executable wrapper receives parsed arguments from normal Bash calls, `command git`, `env git`, `xargs git`, nested Bash, and unqualified child-process PATH lookup.
41 41 The first Bash call creates it with fixed Node and Git paths; later calls and reloads reuse it.
42 - It rejects hook-skipping `--no-verify` options, including accepted abbreviations, short `commit -n`, and non-shell alias chains.
43 42 It resets the editor environment immediately before invoking real Git and delegates ordinary commands unchanged.
44 - Bash `git()` and `git.exe()` functions route direct calls to that wrapper, while narrow preflight checks cover simple literal bypasses.
43 + Bash `git()` and `git.exe()` functions route direct calls to that wrapper.
45 44 Windows paths are converted for Git Bash, and common `git.exe` shell forms route through the wrapper.
46 45 The layers are defense in depth, not a security boundary; unrestricted Bash can still address or construct alternate executable paths.
47 46 On Windows, a non-Bash child process that explicitly launches `git.exe` can bypass the script wrapper because the extension does not ship a native PE launcher.
extensions/git-safe/__tests__/git-policy.test.ts Modified 10 10 import { tmpdir }
from "node:os" ;
11 11 import path
from "node:path" ;
12 12 import { afterEach, describe, expect, it }
from "vitest" ;
15 - blockedGitCommandReason,
16 - createGitPolicyRuntime,
17 - }
from "../git-policy" ;
13 + import { applyGitPolicy, createGitPolicyRuntime }
from "../git-policy" ;
19 15 const testDirs
: string []
= [];
25 21 describe (
"git-safe agent Git policy" , ()
=> {
26 - it (
"blocks literal command-git no-verify bypasses" , ()
=> {
27 - for (
const command
of [
28 - "command git commit --no-verify" ,
29 - "command -p git push --no-verify" ,
30 - " command -- git rebase --no-verify" ,
32 - expect (
blockedGitCommandReason (command)).
toContain (
"blocked --no-verify" );
36 - it (
"does not classify ordinary Git or quoted documentation as command bypasses" , ()
=> {
37 - for (
const command
of [
38 - "git commit --no-verify" ,
39 - "printf '%s' 'command git commit --no-verify'" ,
40 - "command git status" ,
41 - "command git commit -m 'Document --no-verify usage'" ,
42 - "command git merge --no-verify-signatures topic" ,
44 - expect (
blockedGitCommandReason (command)).
toBeUndefined ();
48 22 it (
"generates and reuses a wrapper with fixed executables" ,
async ()
=> {
49 23 const bin
= mkdtempSync (path.
join (
tmpdir (),
"pi-git-policy-bin-" ));
50 24 testDirs.
push (bin);
138 112 it (
"does not trust an agent-supplied policy marker" , ()
=> {
139 - const command
= "# pi-git-safe-policy-v1\ngit commit --no-verify" ;
113 + const command
= "# pi-git-safe-policy-v1\ngit status" ;
140 114 const wrapped
= applyGitPolicy (command, {
141 115 wrapperDir:
"/tmp/wrapper" ,
142 116 realGit:
"/usr/bin/git" ,
extensions/git-safe/__tests__/harness.test.ts Modified 106 - "if [[ \"$*\" == '--list-cmds=builtins' ]]; then printf 'status\\n'; exit 0; fi" ,
107 - "if [[ \"$*\" == *'config --get alias.'* ]]; then exit 1; fi" ,
108 106 "printf 'FAKE_GIT\\n'" ,
109 107 "printf 'ARGS:'" ,
110 108 "printf '<%s>' \"$@\"" ,
126 124 "if [[ \"$1\" == '--version' ]]; then printf 'rtk 0.42.4\\n'; exit 0; fi" ,
127 - "if [[ \"$1\" == 'rewrite' && \"$2\" == 'git commit --no-verify' ]]; then printf 'rtk git commit --no-verify\\n'; exit 0; fi" ,
128 125 "if [[ \"$1\" == 'rewrite' && \"$2\" == 'git status' ]]; then printf 'rtk git status\\n'; exit 0; fi" ,
129 126 'if [[ "$1" == \'git\' ]]; then shift; exec git "$@"; fi' ,
158 155 describe (
"git-safe pi-test-harness" , ()
=> {
159 156 let t
: TestSession | undefined ;
160 157 let root
: string | undefined ;
161 - let fakeGit
: string | undefined ;
163 159 afterEach (()
=> {
165 161 if (
root )
rmSync (
root , { recursive:
true , force:
true });
167 163 root = undefined ;
168 - fakeGit
= undefined ;
171 166 async function startWithFakeGit (useRtk
= false )
: Promise <
void > {
172 167 root = mkdtempSync (path.
join (
tmpdir (),
"pi-git-safe-test-" ));
173 168 const installed
= installFakeGit (
root );
174 169 if (useRtk)
installFakeRtk (installed.bin);
175 - fakeGit
= installed.executable;
176 170 t
= await createTestSession ({
178 172 env: { PATH:
`${installed.bin}${path.delimiter}${ process . env .PATH}` },
212 - async function expectRejected (command
: string )
: Promise <
void > {
206 + async function expectGitOutput (command
: string )
: Promise <
void > {
213 207 const result
= await execute (command);
214 - expect (result.output, command).
toContain (
"git-safe blocked --no-verify" );
215 - expect (result.output, command).not.
toContain (
"FAKE_GIT" );
208 + expect (result.blocked, command).
toBeUndefined ();
209 + expect (result.output, command).
toContain (
"FAKE_GIT" );
218 212 it (
"loads in real Pi extension runtime" ,
async ()
=> {
345 339 expect (result.output).
toContain (
"EDITOR=<true> SEQUENCE=<true> MERGE=<no>" );
348 - it (
"rejects no-verify after shell expansion and indirect PATH lookup" ,
async ()
=> {
349 - await startWithFakeGit ();
351 - for (
const command
of [
352 - 'flag=--no-verify; git commit "$flag"' ,
353 - "git am --no-v patch.mbox" ,
354 - "git am -n patch.mbox" ,
355 - 'command git commit --no-"verify"' ,
356 - "command -p git -C . commit --no-verify" ,
357 - "env git push --no-verify" ,
358 - "env --uns PATH git commit --no-verify" ,
359 - "bash -p -c 'git rebase --no-verify'" ,
360 - "bash -c 'command git commit --no-verify'" ,
361 - "unset -f git; git commit --no-verify" ,
362 - "printf '%s\\0' commit --no-verify | xargs -0 git" ,
363 - "printf '%s\\0' commit --no-verify | xargs -0 git.exe" ,
364 - "printf '%s\\0' commit --no-verify | xargs -0 --max-a 2 git.exe" ,
366 - "git commit --no-verif" ,
367 - "git commit --untracked-files --no-verify" ,
368 - "git push --signed --no-verify" ,
369 - "git rebase --rebase-merges --no-verify main" ,
370 - "git pull --no-verify" ,
372 - await expectRejected (command);
374 - if (
process .
platform === "win32" )
375 - await expectRejected (
376 - "MSYS2_ARG_CONV_EXCL='*' cmd.exe /d /c 'git commit --no-verify'" ,
380 - it (
"rejects temporary aliases that create no-verify inside Git" ,
async ()
=> {
381 - await startWithFakeGit ();
383 - for (
const command
of [
384 - "git -c 'alias.nv=commit --no-verify' nv" ,
385 - `git -c "alias.nv=commit -m '' --no-verify" nv` ,
386 - "git -c 'alias.nv=--no-pager commit --no-verify' nv" ,
387 - "git -c 'alias.x=-c alias.y=commit y --no-verify' x" ,
388 - "git -calias.nv='commit --no-verify' nv" ,
389 - "git -c 'alias.nv=commit -n' nv" ,
390 - "git -c 'alias.nv=commit \"--no-verify\"' nv" ,
391 - "git -c alias.ci=commit ci --no-verify" ,
392 - "NV='commit --no-verify' git --config-env=alias.nv=NV nv" ,
393 - "GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=alias.nv GIT_CONFIG_VALUE_0='commit --no-verify' git nv" ,
395 - await expectRejected (command);
398 - const chain
= Array .
from (
400 - (_, index)
=> `-c alias.a${index}=a${index + 1}` ,
402 - await expectRejected (
`git ${chain} -c 'alias.a12=commit --no-verify' a0` );
405 342 it (
"restores editor protection at the executable boundary" ,
async ()
=> {
406 343 await startWithFakeGit ();
430 367 it (
"does not trust an agent-supplied policy marker" ,
async ()
=> {
431 368 await startWithFakeGit ();
433 - await expectRejected (
434 - "# pi-git-safe-policy-v1\\ngit commit --no-verify" .
replace (
"\\n" ,
"\n" ),
370 + await expectGitOutput (
371 + "# pi-git-safe-policy-v1\\ngit status" .
replace (
"\\n" ,
"\n" ),
450 387 expect (result.output, command).
toContain (
"FAKE_GIT" );
451 388 expect (result.output, command).not.
toContain (
"OVERRIDE" );
453 - await expectRejected (
454 - `PI_GIT_SAFE_NODE=${ JSON . stringify (alternateNode)} PI_GIT_SAFE_REAL_GIT=${ JSON . stringify ( process . execPath )} git commit --no-verify` ,
458 - it (
"permits no-verify when Git treats it as data" ,
async ()
=> {
459 - await startWithFakeGit ();
462 - "git add -- --no-verify" ,
463 - "git grep -e --no-verify" ,
464 - "git commit -m --no-verify" ,
465 - "git commit -am --no-verify" ,
467 - "git commit -t/dev/null" ,
468 - "git commit --mess --no-verify" ,
469 - "git commit -m safe --end-of-options --no-verify" ,
470 - "git commit -Sn -m signed" ,
471 - "git merge --into-name --no-verify topic" ,
472 - "git merge -F --no-verify topic" ,
473 - "git am --resolvemsg --no-verify patch.mbox" ,
474 - "git rebase --trailer --no-verify trunk" ,
475 - "git pull --no-rebase -o --no-verify" ,
476 - "git -c 'alias.nv=commit --no-verify' status" ,
477 - "git -c 'alias.note=!printf %s --no-verify' note" ,
478 - "command git merge --no-verify-signatures topic" ,
479 - "command git commit -m 'Document --no-verify usage'" ,
480 - 'env -i PATH="$PATH" git status' ,
481 - "GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=alias.nv GIT_CONFIG_VALUE_0='commit --no-verify' git -c alias.nv=status nv" ,
482 - "GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=alias.commit GIT_CONFIG_VALUE_0='rebase --no-verify' git commit -m safe" ,
484 - if (
process .
platform !== "win32" ) {
486 - "GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=alias.status GIT_CONFIG_VALUE_0='commit --no-verify' git status" ,
489 - for (
const command
of commands) {
490 - const result
= await execute (command);
491 - expect (result.blocked, command).
toBeUndefined ();
492 - if (
process .
platform !== "win32" || ! command.
includes (
"git -c " )) {
493 - expect (result.output, command).
toContain (
"FAKE_GIT" );
498 - it (
"blocks literal command-git bypasses before execution" ,
async ()
=> {
499 - await startWithFakeGit ();
501 - const result
= await execute (
"command git commit --no-verify" );
503 - expect (result.blocked).
toMatchObject ({ block:
true });
504 - expect (result.blocked?.reason).
toContain (
"git-safe blocked --no-verify" );
507 - it (
"rejects literal absolute Git paths" ,
async ()
=> {
508 - await startWithFakeGit ();
509 - if (
! fakeGit)
throw new Error (
"missing fake Git path" );
511 - for (
const command
of [
"commit" ,
"pull" ]) {
512 - const result
= await execute (
513 - `${ JSON . stringify (fakeGit)} ${command} --no-verify` ,
515 - expect (result.blocked).
toMatchObject ({ block:
true });
519 392 it (
"keeps policy active after RTK rewrites Git" ,
async ()
=> {
520 393 await startWithFakeGit (
true );
522 - await expectRejected (
"git commit --no-verify" );
523 395 const ordinary
= await execute (
"git status" );
524 396 expect (ordinary.output).
toContain (
"FAKE_GIT" );
525 397 expect (ordinary.output).
toContain (
"ARGS:<status>" );
extensions/git-safe/git-policy.ts Modified 13 13 const POLICY_MARKER
= "# pi-git-safe-policy-v1" ;
14 14 const COMMAND_PREFIX_END
= "# pi-command-prefix-end" ;
17 - "git-safe blocked --no-verify. Fix the hook failure or ask the user for help." ;
19 - const DIRECT_COMMAND_GIT_NO_VERIFY_RE
= 20 - / ^ \s * command\s + (?:(?:-p | --)\s + ) * git(?:\.exe) ? \s + (?:am | commit | merge | pull | push | rebase)\s + --no-verify(?:\s |$ )/ u ;
22 16 export interface GitPolicyRuntime {
23 17 wrapperDir
: string ;
148 142 return drive
? `/${drive[1]. toLowerCase ()}/${drive[2]}` : normalized;
151 - export function blockedGitCommandReason (
153 - runtime
?: GitPolicyRuntime ,
154 - )
: string | undefined {
155 - if (DIRECT_COMMAND_GIT_NO_VERIFY_RE.
test (command))
return BLOCK_REASON;
156 - if (
! runtime)
return undefined ;
157 - const trimmed
= command.
trimStart ();
158 - const literalPaths
= [
160 - bashPath (runtime.realGit),
161 - shellQuote (runtime.realGit),
162 - shellQuote (
bashPath (runtime.realGit)),
163 - JSON .
stringify (runtime.realGit),
165 - for (
const path
of literalPaths) {
166 - if (
! trimmed.
startsWith (
`${path} ` ))
continue ;
167 - const args
= trimmed.
slice (path.
length ).
trimStart ();
169 - / ^ (?:am | commit | merge | pull | push | rebase)\s + --no-verify(?:\s |$ )/ u .
test (args)
171 - return BLOCK_REASON;
176 145 export function applyGitPolicy (
178 147 runtime
: GitPolicyRuntime ,
extensions/git-safe/git-wrapper.cjs Modified 5 5 const { spawnSync }
= require (
"node:child_process" );
7 7 const realGit
= "__PI_GIT_SAFE_REAL_GIT__" ;
8 - const protectedCommands
= new Set ([
16 9 const globalValueOptions
= new Set ([
24 - const commandValueOptions
= {
48 - "--pathspec-from-file" ,
65 - "--strategy-option" ,
73 - "--negotiation-tip" ,
75 - "--shallow-exclude" ,
78 - "--strategy-option" ,
81 - push:
new Set ([
"-o" ,
"--exec" ,
"--push-option" ,
"--receive-pack" ,
"--repo" ]),
90 - "--strategy-option" ,
95 - const shortValueOptions
= {
96 - am:
new Set ([
"C" ,
"p" ]),
97 - commit:
new Set ([
"C" ,
"F" ,
"c" ,
"m" ,
"t" ]),
98 - merge:
new Set ([
"F" ,
"m" ,
"s" ,
"X" ]),
99 - pull:
new Set ([
"o" ,
"s" ,
"X" ]),
100 - push:
new Set ([
"o" ]),
101 - rebase:
new Set ([
"s" ,
"X" ,
"x" ]),
103 - const shortAttachedValueOptions
= {
105 - commit:
new Set ([
"S" ,
"u" ]),
112 18 function optionName (argument) {
113 19 const equals
= argument.indexOf(
"=" );
118 24 return ! argument.includes(
"=" )
&& options.has(optionName(argument));
121 - function consumesCommandValue (argument, options) {
122 - if (argument.includes(
"=" ))
return false ;
123 - if (options.has(argument))
return true ;
124 - if (
! argument.startsWith(
"--" ))
return false ;
125 - return [
... options].filter((option)
=> option.startsWith(argument)).length
=== 1 ;
128 27 function commandPosition (args) {
129 28 for (
let index
= 0 ; index
< args.length; index
++ ) {
130 29 const argument
= args[index];
138 - function addAlias (aliases, config) {
139 - const match
= config.match(
/ ^ alias\.([ ^ =] + )=(. * ) $ / isu );
140 - if (match) aliases.set(match[
1 ].toLowerCase(), match[
2 ]);
143 - function addArgumentAliases (aliases, args, commandIndex) {
144 - for (
let index
= 0 ; index
< commandIndex; index
++ ) {
145 - const argument
= args[index];
146 - if (argument
=== "-c" ) {
147 - addAlias(aliases, args[
++ index]
?? "" );
150 - if (argument.startsWith(
"-c" )
&& argument.length
> 2 ) {
151 - addAlias(aliases, argument.slice(
2 ));
154 - const configEnv
= argument.startsWith(
"--config-env=" )
155 - ? argument.slice(
"--config-env=" .length)
156 - : argument
=== "--config-env" 157 - ? (args[
++ index]
?? "" )
159 - if (configEnv
!== undefined ) {
160 - const equals
= configEnv.indexOf(
"=" );
162 - const name
= configEnv.slice(
0 , equals);
163 - const variable
= configEnv.slice(equals
+ 1 );
164 - addAlias(aliases,
` ${name}
= ${
process .
env [variable]
?? "" }
` );
170 - function temporaryAliases (args, commandIndex) {
171 - const aliases
= new Map ();
172 - const count
= Number .
parseInt (
process .
env .GIT_CONFIG_COUNT
?? "0" ,
10 );
173 - for (
let index
= 0 ; index
< count; index
++ ) {
174 - const key
= process .
env [
`GIT_CONFIG_KEY_ ${index}
` ]
?? "" ;
175 - const value
= process .
env [
`GIT_CONFIG_VALUE_ ${index}
` ]
?? "" ;
176 - addAlias(aliases,
` ${key}
= ${value}
` );
178 - addArgumentAliases(aliases, args, commandIndex);
182 - function splitAlias (value) {
186 - let escaped
= false ;
187 - let started
= false ;
188 - for (
const character
of value.trim()) {
195 - if (character
=== "\\" && quote
!== "'" ) {
199 - if ((character
=== "'" || character
=== '"' )
&& ! quote) {
204 - if (character
=== quote) {
208 - if (
/\s/ u .test(character)
&& ! quote) {
209 - if (started) words.push(word);
221 - if (started) words.push(word);
225 - function configuredAlias (globalArgs, command) {
226 - const result
= spawnSync(
228 - [
... globalArgs,
"config" ,
"--get" ,
`alias. ${command}
` ],
234 - return result.status
=== 0 ? result.stdout.trim()
: undefined ;
237 - function isBuiltinCommand (command) {
238 - const result
= spawnSync(realGit, [
"--list-cmds=builtins" ], {
242 - return result.status
=== 0 && result.stdout.split(
/\s + / u ).includes(command);
245 - function resolveCommand (args) {
246 - const position
= commandPosition(args);
247 - if (position
< 0 || position
>= args.length)
return undefined ;
248 - const globalArgs
= args.slice(
0 , position);
249 - const aliases
= temporaryAliases(args, position);
250 - const seen
= new Set ();
251 - let command
= args[position];
252 - let commandArgs
= args.slice(position
+ 1 );
253 - while (
! protectedCommands.has(command)) {
254 - const key
= command.toLowerCase();
255 - if (seen.has(key))
break ;
257 - const alias
= aliases.get(key)
?? configuredAlias(globalArgs, command);
258 - if (
! alias
|| alias.startsWith(
"!" )
|| isBuiltinCommand(command))
break ;
259 - const expansion
= [
... splitAlias(alias),
... commandArgs];
260 - const expandedPosition
= commandPosition(expansion);
261 - if (expandedPosition
< 0 || expandedPosition
>= expansion.length)
break ;
262 - addArgumentAliases(aliases, expansion, expandedPosition);
263 - globalArgs.push(
... expansion.slice(
0 , expandedPosition));
264 - command
= expansion[expandedPosition];
265 - commandArgs
= expansion.slice(expandedPosition
+ 1 );
267 - return { command, args: commandArgs };
270 - function isNoVerifyLongOption (command, argument) {
271 - const minimum
= command
=== "am" ? "--no-v" : "--no-veri" ;
272 - return argument.length
>= minimum.length
&& "--no-verify" .startsWith(argument);
275 - function inspectShortOptions (command, argument) {
276 - if (
! / ^ -[ ^ -]/ u .test(argument))
return { blocked:
false , consumesNext:
false };
277 - const options
= argument.slice(
1 );
278 - const values
= shortValueOptions[command];
279 - const attachedValues
= shortAttachedValueOptions[command];
280 - for (
let index
= 0 ; index
< options.length; index
++ ) {
281 - const option
= options[index];
282 - if ((command
=== "am" || command
=== "commit" )
&& option
=== "n" ) {
283 - return { blocked:
true , consumesNext:
false };
285 - if (values.has(option)) {
288 - consumesNext: index
=== options.length
- 1 ,
291 - if (attachedValues.has(option)) {
292 - return { blocked:
false , consumesNext:
false };
295 - return { blocked:
false , consumesNext:
false };
298 - function hasNoVerifyOption (args) {
299 - const resolved
= resolveCommand(args);
300 - if (
! resolved
|| ! protectedCommands.has(resolved.command))
return false ;
301 - const valueOptions
= commandValueOptions[resolved.command];
302 - for (
let index
= 0 ; index
< resolved.args.length; index
++ ) {
303 - const argument
= resolved.args[index];
304 - if (argument
=== "--" || argument
=== "--end-of-options" )
return false ;
305 - if (isNoVerifyLongOption(resolved.command, argument))
return true ;
306 - const short
= inspectShortOptions(resolved.command, argument);
307 - if (short.blocked)
return true ;
308 - if (short.consumesNext
|| consumesCommandValue(argument, valueOptions)) index
++ ;
313 37 const args
= process .
argv .slice(
2 );
314 - if (hasNoVerifyOption(args)) {
316 - "git-safe blocked --no-verify. Fix the hook failure or ask the user for help." ,
39 + const commandIndex
= commandPosition(args);
321 40 const editorQuery
= 322 - args[commandPosition(args)]
=== "var" && 323 - (args[commandPosition(args)
+ 1 ]
=== "GIT_EDITOR" || 324 - args[commandPosition(args)
+ 1 ]
=== "GIT_SEQUENCE_EDITOR" );
41 + args[commandIndex]
=== "var" && 42 + (args[commandIndex
+ 1 ]
=== "GIT_EDITOR" || 43 + args[commandIndex
+ 1 ]
=== "GIT_SEQUENCE_EDITOR" );
325 45 const env
= {
... process .
env };
326 46 if (
! editorQuery) {
327 47 env.GIT_EDITOR
= "true" ;
328 48 env.GIT_SEQUENCE_EDITOR
= "true" ;
329 49 env.GIT_MERGE_AUTOEDIT
= "no" ;
331 52 const result
= spawnSync(realGit, args, { stdio:
"inherit" , env });
332 53 if (result.error) {
333 54 console .
error (result.error.message);
extensions/git-safe/index.ts Modified 29 29 import type { SafeCloneResult }
from "./clone.js" ;
32 - blockedGitCommandReason,
33 32 createGitPolicyRuntime,
34 33 type GitPolicyRuntime,
35 34 }
from "./git-policy.js" ;
134 133 if (
! isToolCallEventType (
"bash" ,
event ))
return ;
135 134 const runtime
= await ensureGitPolicy (ctx);
136 135 if (
! runtime)
return ;
137 - const reason
= blockedGitCommandReason (
event .
input .command, runtime);
138 - if (reason)
return { block:
true , reason };
139 136 event .
input .command
= applyGitPolicy (
event .
input .command, runtime);