Skip to content

Canonicalize paths across all humanize hooks and scripts#93

Merged
SihaoLiu merged 2 commits intodevfrom
use-realpath4everything
Apr 17, 2026
Merged

Canonicalize paths across all humanize hooks and scripts#93
SihaoLiu merged 2 commits intodevfrom
use-realpath4everything

Conversation

@SihaoLiu
Copy link
Copy Markdown
Contributor

Summary

  • Introduce a shared resolve_project_root helper in hooks/lib/project-root.sh and route every hook and script through it. The resolver prefers CLAUDE_PROJECT_DIR, falls back to git rev-parse --show-toplevel, and refuses the legacy pwd fallback (which drifts with cd during a session and silently caused state lookups to miss the active loop directory).
  • Canonicalize the resolved path through realpath so symlinked ancestors (e.g. /var/... vs /private/var/... on macOS, or a symlinked repo checkout on Linux) do not diverge between setup time and hook time.
  • Mirror the canonicalization on every path-comparison site that consumes PROJECT_ROOT: loop-read-validator.sh, loop-write-validator.sh, and is_cancel_authorized in hooks/lib/loop-common.sh. is_cancel_authorized now canonicalizes both the loop dir and the parsed mv source/destination, then re-lowercases so the comparison stays symmetric on case-insensitive filesystems.
  • Guard the merged-config load in loop-common.sh so sourcing humanize.sh from .bashrc/.zshrc in a non-repo directory no longer emits Error: Usage: load_merged_config <plugin_root> <project_root> to stderr on every shell startup.

Test plan

  • bash tests/run-all-tests.sh -> 1723 passed / 0 failed
  • New regression test HELPER TEST 8 in tests/test-cancel-signal-file.sh covers a symlinked-prefix cancel command against a canonical loop dir
  • New stop-gate fixture in tests/test-stop-gate.sh stages hooks/lib/project-root.sh alongside the mock hook so the wrapper resolves a project root
  • Manual: source scripts/humanize.sh from $HOME (non-repo, no CLAUDE_PROJECT_DIR) and confirm no usage error is printed

is_cancel_authorized previously compared the user-provided mv source
and destination (lowercased command text) against a lowercase-only
prefix derived from active_loop_dir. Now that resolve_project_root
canonicalizes via realpath, a user command that uses the non-canonical
form of the prefix (e.g. a symlinked ancestor) would fail authorization
even when every other check passes. Canonicalize both the loop dir and
the parsed src/dest through canonicalize_path, then re-lowercase, so
the comparison is symmetric across symlinked prefixes. Extend the
symlink-rejection check to use the canonical loop dir so the on-disk
probe targets the real path.

Also guard the merged-config load in loop-common.sh: when the library
is sourced from .bashrc/.zshrc in a non-repo directory, project root
resolution yields an empty string and load_merged_config would emit
its usage error to stderr on every shell startup. Skip the call when
the resolved root is empty and leave the cached config empty.

Add a regression test that points a symlinked prefix at the real loop
dir and confirms the helper still authorizes the cancel mv.
@SihaoLiu SihaoLiu merged commit 0e2c6a0 into dev Apr 17, 2026
9 checks passed
@SihaoLiu
Copy link
Copy Markdown
Contributor Author

@codex review this PR

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0e2c6a0afa

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread hooks/lib/loop-common.sh
Comment on lines +1215 to +1219
dest_canonical="$(canonicalize_path "$dest")"
dest_canonical="${dest_canonical:-$dest}"
dest_canonical=$(echo "$dest_canonical" | tr '[:upper:]' '[:lower:]')
local expected_dest="${loop_dir_lower}cancel-state.md"
if [[ "$dest" != "$expected_dest" ]]; then
if [[ "$dest_canonical" != "$expected_dest" ]]; then
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Reject symlink aliases for cancel destination

Canonicalizing dest with realpath before comparison allows a destination symlink outside the loop (for example, /tmp/link -> <loop>/cancel-state.md) to pass authorization as if it were the real cancel-state.md. In that case mv <loop>/state.md /tmp/link is allowed, but mv replaces the symlink path itself, so state.md is removed without creating <loop>/cancel-state.md, corrupting loop state and bypassing the intended path restriction.

Useful? React with 👍 / 👎.

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.

1 participant