Skip to content

Wrapper differential audit: byte-exact argv forwarding, message-safe POSIX-path guard, differential test suite - #6

Merged
caomengxuan666 merged 4 commits into
masterfrom
fix/wrapper-differential-audit
Oct 12, 2026
Merged

caomengxuan666 merged 4 commits into
masterfrom
fix/wrapper-differential-audit

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Wrapper differential audit — pre-release fixes (issues #3, #4, #5)

Full-stack differential audit of the niu-git wrapper, driven by #3 ("wrapper
runs command substitution on commit -m message text"). Same command through
【wrapper opt/niugit/git.exe】 and 【real mingw64/bin/git.exe】 in twin repo
copies; byte-compare stdout/stderr/exit code + repo end state (HEAD, commit
message bytes, reflog, index/status).

Issue #3 verdict: the wrapper does NOT evaluate message text

Reproduced the report through every layer (niubash sh/niu -c, usr/bin
multicall link, launcher, real git) with single-quoted backtick/${}/$()
messages: every layer is byte-transparent, stored commits byte-match. The
reported symptoms (first commit FAILS + backticks executed) reproduce exactly
with double-quoting at the call site — e.g. git commit -m "note \uname -r`
and ${(kv)}"— which is standard POSIX shell semantics under any shell (niu, Git Bash, GNU bash): the backticks run and${(kv)}raises "bad substitution". Root cause is the composing agent's quoting;-F ` "working" is
consistent (bypasses shell quoting). Detail: comment on #3.

Wrapper-side defects the audit DID find (fixed here):

#4 — launcher re-quoted argv unconditionally (fixed: commit 1)

quote_append() wrapped every argument in literal "...". Args containing "
lost the quotes (she said "hi" → she said hi in a committed message, rc=0);
args ending in \ corrupted (ends with backslash \ → ends with backslash ";
git add 'D:\dir\' failed rc=128); when the mangled arg wasn't last, the
unbalanced quote swallowed following args (commit aborted). Now: standard
Windows argv quoting (quote only when empty/contains space-tab-quote; escape
per MSVCRT rules) + content-sized buffer instead of fixed 32 KiB.

#5 — POSIX-path fallback rewrote message args (fixed: commit 2)

Patch 0002 converted every argv element: commit -m /c stored c:/,
tag -a -m /c stored annotation c:/ — silent, and machine-state-dependent
(-m /d survived on machines that happen to have D:\d). Now guarded: values
following -m/--message are never converted (attached forms -m<x> /
--message=<x> can't match the shape check). Kept: -F values are real
file paths and still convert (commit -F /d/msg.txt works), and path-position
conversion (git add /d/..., git -C /d/...) is untouched.

build fix (commit 3)

busybox-style patch(1) (winuxcmd) mishandles patch -d <dir> -p1 (does not
strip a/ b/; dies on first hunk) which made build.py unusable on such
hosts; also git apply silently skips paths when run from a subdirectory
(prefix filter, still exits 0). apply_patches now falls back to
git apply -p1 --directory=<rel-src> run from the repo root, idempotent.

tests (commit 4)

scripts/test-launcher.py: differential wrapper-vs-real suite + absolute
message-byte assertions (catches bugs shared by both stacks). 21/21 pass
with this branch's build; 15/21 with the installed 2026-10 stack (the six
failures are exactly the #4/#5 regressions). The wider 49-case audit harness
(URLs ssh://git@/scp-like/file://, config values with CJK paths/equals,
GIT_DIR/GIT_WORK_TREE/GIT_SSH_COMMAND passthrough, exit codes 128/1, bare
invocation/help -a/add -p/hash-object --stdin, CJK filenames, 8KB messages)
is 49/49 PASS with the fixed stack, with the POSIX-path feature cases
(add/-F/-C conversion) all green.

Verification

  • Launcher: compiled with MSVC (/W4 /O2 /MT, same flags as CMakeLists).
  • Patch 0002: validated with git apply --check against pristine
    v2.55.0.windows.2 source, then a full local build/git.exe build used for
    the test runs above.

What needs upstream/other-repo cooperation

Deployment path (for the release owner)

master merge → tag (e.g. v2.55.0.3+) → CI/build.py artifact zip → wpm
index wpm/niugit.json rehash → wpm install niugit updates the installed
tree (opt/niugit/git.exe + mingw64/). The installed tree was NOT touched
during this audit; all verification ran against builds under the audit clone
and NTFS-junction shadow layouts in testbed/ (untracked, local only).

Fixes #4, fixes #5; informs #3.

quote_append() wrapped every argument in literal double quotes, so any
argument containing a quote was mangled by the child CRT parse (embedded
quotes silently stripped from commit -m payloads) and any argument ending
in a backslash reinterpreted the closing quote (backslash became a quote;
when the argument was not last, the unbalanced quote swallowed the next
arguments, aborting e.g. "commit -m X --allow-empty" outright). Trailing
backslash path arguments ("git add D:\dir\") failed hard.

Emit standard Windows command-line quoting instead: quote only when the
argument is empty or contains space/tab/quote; inside quotes double the
backslashes preceding a quote, emit an embedded quote as backslash-quote,
and double trailing backslashes before the closing quote. Size the
command-line buffer from actual content and fail cleanly instead of
silently truncating at a fixed 32 KiB.

Differential suite (49 cases): 3 message-corruption failures before,
0 after. Fixes #4.
Patch 0002 (POSIX-shaped path argument fallback) rewrote every argv
element from argv[1] on, with no notion of which position the argument
occupies. Message payloads are not path arguments, but a message that is
a single token shaped like /x and whose converted Windows path exists got
silently rewritten:

    git commit -q --allow-empty -m /c         # stored: c:/          (was /c)
    git commit -q --allow-empty -m /c/Windows # stored: c:/Windows   (was /c/Windows)
    git tag -a -m /c v1                       # annotation: c:/      (was /c)

Whether a message was rewritten depended on machine state (it fires only
when the converted path exists; e.g. "-m /d" survived on machines that
happen to have a D:\d directory). That breaks git's literal-bytes
contract for commit messages - the same contract issue #3 is about.

Guard the conversion: never rewrite the token following -m or --message
(attached forms -m<val> / --message=<val> are single tokens starting with
"-" and can never match the POSIX-path shape check). -F/--file values are
genuine file paths and keep benefiting from the exists-checked
conversion, so "git commit -F /d/msg.txt" keeps working.

Verified by building git with the updated patch: differential suite
21/21, including the previously-rewritten message and tag-annotation
cases, while add/-F/-C POSIX-path conversion cases still pass.

Fixes #5.
…prefixes

The busybox-style patch(1) shipped in some Windows environments (observed
with the winuxcmd coreutils) mishandles `patch -d <dir> -p1`: it does not
strip the a/ b/ prefixes and dies on the first hunk with

    patch: cannot open file '<dir>/b/<file>': No such file or directory

which made scripts/build.py unusable on such hosts. Fall back to
`git apply -p1 --directory=<src>` for these unified diffs; git is always
available in this project's dev/CI environments.

git apply must run from the repo root with a relative --directory:
- --directory rejects absolute paths ("invalid path"), and
- from a subdirectory git sets a prefix and silently skips every patch
  path outside it ("Skipped patch '<path>'") while still exiting 0.
Idempotency is preserved: a re-run surfaces "already exists", which is
treated like patch -N's "Skipping patch".
scripts/test-launcher.py runs the same logical command through the
launcher (git.exe) and the real mingw64/bin/git.exe in twin repo copies
and compares stdout/stderr/exit code plus repo end state (HEAD, commit
message bytes, reflog, status) byte-for-byte. Message payloads are
additionally asserted against their intended literal bytes, so a bug
shared by both stacks (e.g. rewriting "commit -m /c" into "c:/") still
fails the suite.

Covered: the issue #3 message family (backticks, ${}, $(), zsh-style
${(kv)}), the issue #4 quoting family (embedded quotes, trailing
backslash, shell snippets with quoted values), the issue #5 message-as-
path family (/c, /d, /d/repo, /D:/repo, tag annotations), CJK/emoji/
multiline/8KB/leading-dash messages, empty messages, and
--amend. Identity and dates are frozen so oids are deterministic; target
selection via NIUGIT_TEST_ROOT (default: dist/ after a build, installed
product as smoke fallback).

Current status against a build with both fixes: 21/21 pass; against the
2026-10 installed stack: 15/21 (the six issue #4/#5 regressions fail as
intended).

Also document the suite and the two fidelity fixes in README.md.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant