fix(cli): prevent symlink attacks during instruction writes - #145
fix(cli): prevent symlink attacks during instruction writes#145tachyon-beep wants to merge 1 commit into
Conversation
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
There was a problem hiding this comment.
Pull request overview
This PR hardens loomweave-cli’s instruction installer against symlink-based arbitrary overwrite attacks by replacing a predictable, PID-based temp file write with a securely-created temporary file that cannot be pre-symlinked by an attacker, while keeping the same-directory atomic rename behavior and permission preservation.
Changes:
- Replace deterministic sibling temp-path +
fs::writewithtempfile::NamedTempFilecreated viatempfile_in(parent), followed bywrite_allandpersist. - Preserve Unix permission-copy behavior via
preserve_modebefore the final atomic persist/rename. - Add a Unix regression test that plants the legacy predictable temp-path symlink and asserts the “victim” file is not overwritten.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| crates/loomweave-cli/src/instructions.rs | Uses NamedTempFile for safe atomic writes and adds a regression test for the predictable-temp symlink attack. |
| crates/loomweave-cli/Cargo.toml | Promotes tempfile to a runtime dependency to support the hardened write path. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Superseded by the rebase onto |
Motivation
fs::write, which could follow a pre-planted symlink and allow attacker-influenced arbitrary overwrites duringatomic_write.Description
fs::writepattern withtempfile::Builder::tempfile_in(parent)and an exclusiveNamedTempFile, thenwrite_allandpersistto atomically install the file in the target directory.preserve_mode, and keep the write/rename as a same-filesystem atomic operation.tempfileto a runtime dependency and adduse std::io::Write;forwrite_all.atomic_write_does_not_follow_predictable_temp_symlinkthat plants the legacy predictable temp symlink and verifies the victim is not overwritten while the intended target is updated.Testing
cargo fmt --check, which succeeded.cargo test -p loomweave-cli instructions::tests::atomic_write, and the targeted tests passed (atomic_write_refuses_empty_content,atomic_write_preserves_mode,atomic_write_does_not_follow_predictable_temp_symlink).cargo clippy -p loomweave-cli --all-targets -- -D warnings, which completed without warnings.git diff --checkandgit status --shortafter committing the fix.Codex Task