Skip to content

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

Closed

Description

@fdaviddpt

Found by the round-two release audit of v0.21.0..HEAD, holding the 0.22.0 tag. Not yet in any released version — 1c95c91 is on main and no tag contains it.

Symptom

A user whose project path contains an apostrophe (~/Bob's Project) gets the merged-config temp file left behind at every hook exit. That is the leak #362 was filed about and #371 was merged to fix, reintroduced by the fix itself for that slice of users — while the changelog says the issue is closed.

Location

scripts/bootstrap-dirs.sh:179-186:

_remember_existing_trap=$(trap -p EXIT 2>/dev/null | sed "s/trap -- '//;s/' EXIT//;s/'\\\\''/'/g")
if [ -n "$_remember_existing_trap" ]; then
    trap "${_remember_existing_trap}; rm -f '${_remember_relocated_cfg}'" EXIT
else
    trap "rm -f '${_remember_relocated_cfg}'" EXIT
fi

Mechanism

_remember_relocated_cfg is $REMEMBER_DIR/tmp/remember-config-$$.json. In legacy mode REMEMBER_DIR is $PROJECT_DIR/.remember — the raw project path, not slugified. It is interpolated into a single-quoted span inside a string that trap stores and the shell re-parses at exit. An apostrophe in the path terminates that span early, and the remainder of the path is then re-parsed as shell source rather than as a filename.

The benign consequence is the failed rm above. The general consequence is that any character sequence a project path can contain is evaluated as shell at hook exit — so a directory name is an input to the shell's parser. The audit confirmed a marker file created this way and removed it; the working construction is deliberately not reproduced here.

Why neither commit is wrong alone, which is the part worth keeping. scripts/lib-memory-dir.sh:336-343 has used this identical string-built-trap idiom for a long time and is safe, because its path lives under $SYS_TMPDIR — a location no user names. 1c95c91 (#371) preserved the idiom and repointed the path at $REMEMBER_DIR, which is user-controlled. The idiom was safe; the relocation was safe; the composition is not. No per-PR review could see it, because only one of the two halves is in any one diff.

What would settle it

Create a project directory whose name contains an apostrophe, run the hook chain (resolve-paths.sh → detect-tools.sh → bootstrap-dirs.sh), and list .remember/tmp/ after exit. The file surviving is the defect. Pair it with the same run under a path containing no apostrophe, which must clean up — otherwise the assertion passes for the wrong reason.

Shape of a fix

Do not build a trap body by concatenating an interpolated path. printf %q on _remember_relocated_cfg is the small local fix. Removing the string-built trap entirely — a cleanup function referencing a variable, rather than a string carrying its value — removes the class rather than this instance, and is worth weighing against the re-quoting sed on line 179 that exists to chain a pre-existing trap.

The test gap underneath it

tests/test_stale_config_sweep_362.py uses proj as its project name throughout. A single case with a quoted path would have caught this. Whatever fix lands needs a project-path fixture that is not a plain identifier, and it needs its own positive control.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions