Repository navigation
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #375.
Mechanism
scripts/bootstrap-dirs.shbuilt itsEXITcleanup trap by string concatenation, interpolating the relocated merged-config path into a single-quoted span inside a string bash'strapre-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.shuses 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 %qthe 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%qalready 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, andtrapre-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 stringtrapre-parses at exit. The pre-existingsedreconstruction of a caller's own prior trap (chaining ontolib-memory-dir.sh's trap) is untouched: nothing in this codebase installs anEXITtrap beforebootstrap-dirs.shsourceslib-memory-dir.sh, so what's read back is always exactly onerm -f '$path'fromlib-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
EXITtrap 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%qfix 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-546thatscripts/lib-memory-dir.shis pinned code-line-for-code-line againstorigin/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 ownEXITtrap 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
%qround-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 %qbehavior: 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 onlydocs_targetsentry): 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
sedon 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.