Skip to content

fix: quote bootstrap-dirs.sh's EXIT-trap path so an apostrophe cannot terminate it early (#375) - #376

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/375
Aug 27, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/375

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Fixes #375.

Mechanism

scripts/bootstrap-dirs.sh built its EXIT cleanup trap by string concatenation, interpolating the relocated merged-config path into a single-quoted span inside a string bash's trap re-parses at exit. In legacy mode that path is rooted at $REMEMBER_DIR, the raw, non-slugified project directory -- user-controlled. An apostrophe in the project path (e.g. ~/Bob's Project) terminated the quoted span early, so the remainder of the path was re-parsed as shell source at exit rather than as the filename it names. Benign symptom: the temp file survives (reintroducing #362's leak for this slice of users, even though the changelog said #362 was closed). General consequence: a directory name became an input to the shell's parser at every hook exit.

scripts/lib-memory-dir.sh uses the identical trap-building idiom on a $SYS_TMPDIR-rooted path, which is not user-controlled and was never broken -- confirmed unaffected and left untouched (see below).

The chosen fix, and why not the other one

Two shapes were on the table in the brief: (1) printf %q the interpolated value, small and local; (2) replace the whole string-built trap with a cleanup function that reads a variable at exit, removing the class rather than patching the instance.

Took (1). _remember_relocated_cfg_q=$(printf %q "$_remember_relocated_cfg"), embedded UNQUOTED in both trap strings, since %q already produces a token the same shell can re-parse safely as exactly one word regardless of embedded quote characters. Verified by hand (and independently by a spawned reviewer) across apostrophe, $, backtick, double-quote, an embedded space, an embedded backslash, and an embedded literal newline (bash 3.2 emits $'...' ANSI-C quoting for the newline case, and trap re-parses that correctly too).

What still relies on the string-built trap, and why that's acceptable: the whole mechanism is still trap "<chained>; rm -f <new>" EXIT -- a string trap re-parses at exit. The pre-existing sed reconstruction of a caller's own prior trap (chaining onto lib-memory-dir.sh's trap) is untouched: nothing in this codebase installs an EXIT trap before bootstrap-dirs.sh sources lib-memory-dir.sh, so what's read back is always exactly one rm -f '$path' from lib-memory-dir.sh's own $SYS_TMPDIR-rooted (non-user-controlled) path -- that half was never the vulnerable half. It composes safely after the newly %q-quoted fragment with no change of its own.

Did not take (2). This is the release-critical path for 0.22.0 and the issue asked for a deliberate, argued choice rather than defaulting to the smaller one. Rewriting the trap as a function would still have to touch the same reconstruction/chaining machinery this fix needed to leave working (a caller's own prior EXIT trap still has to be preserved somehow), doubling the diff and the surface a full-suite rerun has to re-certify, for a class of defect the %q fix already closes completely: arbitrary-content-in-a-re-parsed-string. Worth reconsidering outside the release window if another composition defect turns up in this area, but not here.

lib-memory-dir.sh -- deliberately not touched

Confirmed via tests/test_case_divergence_298.py:529-546 that scripts/lib-memory-dir.sh is pinned code-line-for-code-line against origin/main. Its copy of the same idiom operates on a $SYS_TMPDIR-rooted path that no user names, so it is not broken today, and this fix does not touch it.

Tests

tests/test_trap_quoting_375.py, TDD: red first (apostrophe case: returncode=2, file survives) against pre-fix code, green after. Parametrized over a plain-identifier positive control plus apostrophe/$/backtick/double-quote (double-quote skipped on win32, illegal there). Runs the real hook chain as a subprocess so bash's own EXIT trap genuinely fires. Only the apostrophe case is a true pre-fix regression -- the trap's vulnerable span is single-quoted, so $, backtick and double-quote are inert inside it and pass before the fix too; the docstring was corrected in a follow-up commit (self-review finding, below) to say so rather than overclaiming all four were pre-fix failures.

Self-review

Spawned reviewer (Explore) found zero issues after independently reverting to pre-fix code and re-running the new test to confirm the red/green claim, and independently verifying %q round-trips a literal newline. Spawned auditor (oss:auditor) found one finding, fixed in this branch: tests/test_trap_quoting_375.py's docstring claimed every metacharacter case failed pre-fix, when only the apostrophe case actually does -- corrected to say which three cases are fix-side regression coverage rather than bug reproductions. misreports-finding-docstring-overclaim -- fixed, not filed, since it was a one-file, one-paragraph correction in the same commit series.

Cross-platform

printf %q behavior: observed on macOS bash 3.2 (this machine and the reviewer's). Reasoned, not observed, for Linux bash 5.x and Windows Git Bash -- CI (ubuntu-latest, windows-latest, macos-latest) is positioned to catch a divergence there but was not manually invoked as part of this session.

Docs

README.md (the repo's only docs_targets entry): read against this change -- no-change-needed. Its existing description of the temp-file cleanup mechanism (trap + sweep, around the #362 section) remains accurate; the fix restores the trap's own correctness rather than changing the behavior README already documents.

Changelog fragment: changelog.d/375.fixed.md, checked with .oss/assemble_changelog.py --check (passed, 7 fragments all parse).

Below-bar: a docstring-adjacent gap is worth noting for whoever reads the trap-building idiom next -- the sed on line ~200 is only documented as depending on "nothing in this codebase installs an EXIT trap before bootstrap-dirs.sh sources lib-memory-dir.sh" in a code comment. If a third caller of this trap-chaining pattern is ever added, that comment's assumption needs re-checking by hand; no test currently pins it. Not filed -- no reachable caller today, and the comment already states the assumption for the next person who adds one.

…oject directory cannot terminate it early (#375)

bootstrap-dirs.sh built its EXIT trap by string concatenation, embedding
the relocated merged-config path inside a single-quoted span in a string
that `trap` re-parses at exit. In legacy mode that path is rooted at the
raw, non-slugified project directory, so an apostrophe in the project
path terminated the span early and left the temp file behind -- silently
reintroducing #362's leak for exactly this slice of users. `printf %q`
now turns the interpolated path into a form the same shell can safely
re-parse as one word, so it is embedded unquoted below (it already
carries its own quoting).

lib-memory-dir.sh uses the identical idiom on a $SYS_TMPDIR-rooted path,
which is not user-controlled and is unaffected -- left alone, since it
is byte-pinned against origin/main by tests/test_case_divergence_298.py
to protect the hot path bootstrap-dirs.sh's own migration-guard comment
(lines 116-127) explains.

Co-Authored-By: Max <noreply>
The reviewer (self-review round) measured that only the apostrophe case
actually fails against pre-fix bootstrap-dirs.sh -- the dollar-sign,
backtick and double-quote cases pass before the fix too, because the
vulnerable trap wraps the interpolated path in a SINGLE-quoted span and
those three characters are inert inside single quotes. The docstring
claimed "every metacharacter case here leaves the relocated ... behind"
before the fix, which overstated what the matrix actually pins.
Corrected to say what is true: apostrophe is the regression case, the
other three are regression coverage for the fix (printf %q) rather than
reproductions of the bug.

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt merged commit 691dd76 into main Aug 27, 2026
13 checks passed
@fdaviddpt
fdaviddpt deleted the fix/375 branch August 27, 2026 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bootstrap-dirs.sh builds its EXIT trap by string concatenation around a user-controlled path, so an apostrophe in a project path defeats cleanup

1 participant