Skip to content

fix(#754,#755,#756,#760): one shared guard for every injected memory file - #764

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/injection-guard
Sep 25, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/injection-guard

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #754.
Closes #755.
Closes #756.
Closes #760.

Problem

The #721 guard only checked the handoff file, and had gaps:

Injected memory files were also read through symlinks.

Fix

One decision, _remember_may_inject in scripts/lib-memory-context.sh, applied to every file the context loader injects and to the handoff (it replaces _remember_handoff_is_tracked):

Tests (red first)

tests/test_injection_guard_754_755_756.py, 15 tests, each red before and green after, with a positive control per case:

  • symlinked now.md, in-project and external: refused
  • tracked now.md / identity.md: refused
  • real git worktree add: tracked identity.md refused
  • repo-subdirectory launch: tracked handoff refused
  • PATH-shim git exiting 128 inside a repo: refused as unavailable. Control: the same shim in a non-repo still delivers.
  • every ls-files call carries a scoping pathspec, asserted from the spawn log

All 9 existing #721 tests pass unmodified. The spawn-budget tests (#660/#665) pass: session start makes no more git calls than before, and fewer when no repo is found. Full suite on macOS: 2629 passed, 59 skipped. Both changelog checks pass.

Platform

Observed: macOS, git 2.55. Reasoned: Linux, and Windows Git Bash (:(icase) has existed since git 1.8). The new module carries the blanket win32 skip, with its row added to docs/windows-skip-triage.md.

Not in scope: #761.

🤖 Generated with Claude Code

[AI-generated]

…d?" guard, and refuse symlinked memory files

Five bugs shared one root: files under `.remember/` that a cloned
repository controls could reach session context via
scripts/lib-memory-context.sh and scripts/session-start-hook.sh.

- #755: the #721 tracked-file refusal only matched the handoff by NAME.
  now.md, identity.md, recent.md and archive.md went through unrefused
  even when a repository committed them.
- #756: identity.md's own fallback path selection was still anchored on
  PROJECT_DIR instead of MEMORY_PROJECT_DIR -- the #747 bug at a second
  site, wrong from a linked git worktree.
- #754: the tracked-handoff check tested for `.git` in one fixed
  directory. Launching from a repository SUBDIRECTORY (so `.remember/`
  has no `.git` of its own) bypassed it.
- #760: the tracked check answered "untracked" whenever asking git
  failed for any reason (missing binary, corrupted `.git`, a broken
  shim on PATH), delivering the file on a git failure instead of
  refusing on an unverifiable answer.
- A private security finding: refuse symlinked memory files -- a legacy
  in-project store could commit an injected file as a symlink to a path
  outside the repository and have the target's bytes delivered as
  though they were the user's own memory.

Replaces the per-file, per-root checks with one shared decision,
_remember_may_inject (lib-memory-context.sh), used for every file the
context loader injects and for the handoff. Repository existence is
established on disk (_remember_repo_root_walk_into: a filesystem walk
for `.git`, no git spawn, worktree/subdirectory/submodule-safe by
construction) independently of whatever `git` itself reports, so a
git failure can be told apart from "genuinely no repository here" --
the three-state shape #760 asks for: allowed / refused / unavailable,
mirroring lib-case-divergence.sh's own ok/diverged/unavailable. Once a
repository is confirmed, one batched `ls-files` per distinct repository
root (not per file) answers tracked/not-tracked/unavailable.

Both the symlink and the tracked checks are scoped to legacy/in-project
storage only (REMEMBER_ROOT == MEMORY_PROJECT_DIR): external-storage
mode's own store can legitimately be a private git repository the
git_backup feature commits into on purpose, and an early draft of this
guard broke that delivery path before the full suite caught it (see
trap.d/754.injection-guard-must-exempt-external-storage-git-backup.md).

Every refusal is logged and named in the user-visible output.

Adds tests/test_injection_guard_754_755_756.py (12 cases: symlinked
now.md, tracked now.md/identity.md, identity.md from a real `git
worktree add`, tracked handoff from a repository subdirectory, a
git-failure-must-refuse case with a PATH-shimmed `git` that exits 128,
plus a positive control for each and one for external storage), red
before this change and green after. Updates docs/handoff.md's #721
description and docs/windows-skip-triage.md's triage table. Changelog
fragments for #754/#755/#756/#760; the private finding gets no fragment
here (maintainer adds one at publish time). #761 (a `.git` symlink
pointing at another repository) is out of scope for this branch.

Closes #754
Closes #755
Closes #756
Closes #760

Co-Authored-By: Max <noreply>
…-file ls-files pathspec

Review of 5e40a70 (fix/injection-guard) caught two gaps.

1. The symlink refusal was gated on _remember_in_project_store, same as
   the tracked check. That is wrong for the symlink half: #757's own
   first-run migration (bootstrap-dirs.sh's `mv "$_legacy_dir"
   "$REMEMBER_DIR"`) carries a legacy/in-project store's files -- symlink
   and all -- into the external store, so a repo-committed `now.md`
   symlink planted before migration would sit, after migration, in a
   directory this guard no longer checked at all. `[ -L ]` is a builtin
   (no spawn) and nothing in this plugin ever creates a symlink inside a
   memory store, so there is no legitimate case widening this check
   could break. Moved above the _remember_in_project_store gate; the
   git-tracked check stays gated (see that function's own comment on why
   external storage's git_backup repo must stay exempt from it).

2. `git -C root ls-files` with no pathspec lists the WHOLE repository on
   every session start -- on a large monorepo, an unbounded listing for a
   check that only needs an answer about a handful of memory files.
   `_remember_root_tracked_state_into` now takes the memory file's own
   directory, relative to the repository root, and scopes the call to it
   via an `:(icase)` pathspec (git 1.8+) -- confirmed against the git this
   host ships (2.55) and reasoned to hold on every supported CI runner.
   `:(icase)` keeps the differently-cased-directory case (`.Remember/`)
   correctly detected without a second, unscoped listing. Falls back to
   an unscoped call only in the one case where scoping is meaningless: the
   memory file sits directly at the repository root (the rare
   REMEMBER_ROOT/identity.md fallback).

Adds three tests to tests/test_injection_guard_754_755_756.py: a
symlinked now.md in external storage is refused (plus positive control),
and an ls-files spawn-argv assertion (via tests/spawn_counting.py's PATH
shim) that the call carries a scoping pathspec, with an unrelated
committed file in the fixture repo so the assertion is actually about
scoping rather than repo size. All three verified red before each fix and
green after (temporary reverts, not committed). Full suite: 2629 passed,
59 skipped.

Co-Authored-By: Max <noreply>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment