Skip to content

fix(hooks): refuse symlinked backup destinations - #4186

Closed
Ha1baraA11 wants to merge 1 commit into
rtk-ai:developfrom
Ha1baraA11:fix/backup-no-follow
Closed

Ha1baraA11 wants to merge 1 commit into
rtk-ai:developfrom
Ha1baraA11:fix/backup-no-follow

Conversation

@Ha1baraA11

@Ha1baraA11 Ha1baraA11 commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Test plan

  • cargo fmt --all && cargo clippy --all-targets && cargo test
  • Manual testing: rtk <command> output inspected
  • cargo fmt --all -- --check
  • cargo clippy --all-targets -- -D warnings
  • cargo test hooks::init::tests::test_backup_and_atomic_write -- --nocapture (3 passed)
  • cargo test --all-targets (3,735 passed; the two pre-existing guard integration failures remain unchanged)
  • cargo test --test guard_integration_test -- --skip git_diff_dash_dash_stat_pathspec_after_double_dash_is_not_stat_flag --skip git_log_malformed_digit_run_propagates_real_git_error (20 passed)
  • Unix regression invokes the backup writer against a dangling destination symlink: the open fails, the original remains unchanged, and the outside target is not created.
  • Manual reproduction with a cloned repository carrying a dangling .codex/hooks.json.bak symlink: init refused the outside-resolving path, the symlink remained unchanged, and the outside file was not modified.

Important: All PRs must target the develop branch (not master).
See CONTRIBUTING.md for details.

@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added bug Something isn't working hooks security labels Sep 21, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes a symlink-planting vulnerability where backup writes for hook JSON files (e.g. .codex/hooks.json.bak) could follow a symlink at the destination and overwrite or exfiltrate files outside the project. Introduces a shared copy_backup_without_following_destination helper that opens the destination with O_NOFOLLOW (Unix) or FILE_FLAG_OPEN_REPARSE_POINT (Windows) so the check-then-write path can't be raced, and replaces all fs::copy backup call sites with it.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #4157


Analyzed automatically by wshm · This is an automated analysis, not a human review.

@KuSh

KuSh commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator

Thanks for this. The same fix is part of #4269 (commit "fix(init): back up without following a symlink at the .bak"), which routes every init backup through one helper on the split src/hooks/init/ modules, where this PR's change to init.rs no longer applies.

#4269 writes the backup to a temp file renamed over the .bak, so a planted symlink or hard link there is replaced rather than written through, with no platform-specific flag. Your approach is credited in its description. Closing in favour of #4269.

@KuSh KuSh closed this Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working hooks security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fs::copy onto a .bak path follows a symlink there, so a cloned repo can redirect rtk init writes outside the project

2 participants