fix: reject symlinked sub-agent files#209
Conversation
|
Warning Review limit reached
More reviews will be available in 28 minutes and 41 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 38b14bbfc4
ℹ️ 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".
| assert_safe_destination(file)?; | ||
| fs::remove_file(file)?; | ||
| } | ||
| if let Err(e) = fs::rename(&temp, file) { |
There was a problem hiding this comment.
Preserve existing sub-agent file permissions
When updating an existing sub-agent file with restrictive permissions (for example 0600 in a user's global agent directory), this path unlinks it and renames in a newly-created temp file, so the saved file gets the process's default create mode (0666 & umask) instead of the original mode. That regresses confidentiality compared with the previous fs::write behavior, which truncated the existing inode and kept its permissions.
Useful? React with 👍 / 👎.
| if ensure_no_symlink_components(path).is_err() || !is_regular_file(path) { | ||
| return None; |
There was a problem hiding this comment.
Avoid path-based reads after symlink checks
When the sub-agent directory is writable by another process, these metadata checks can pass for a regular *.md file and the path can then be swapped to a symlink before the later fs::read_to_string, so loading can still follow the symlink and read an arbitrary target. Since this change is meant to close symlink-backed reads from untrusted project agent directories, the file needs to be opened in a no-follow way (or validated via the opened handle) instead of relying on a separate path check.
Useful? React with 👍 / 👎.
|
|
||
| if existed { | ||
| assert_safe_destination(file)?; | ||
| fs::remove_file(file)?; |
There was a problem hiding this comment.
Keep the old file until replacement succeeds
When updating an existing sub-agent, this removes the only committed file before the replacement is in place; if the process is killed here or the subsequent rename fails (for example because the path is concurrently recreated as a directory), the user loses the previous agent definition and is left with only a hidden temp file. Keep the old regular file until the replacement operation has succeeded (or restore it on failure) so a failed save doesn't delete working config.
Useful? React with 👍 / 👎.
Motivation
*.mdsub-agent files could read or overwrite arbitrary filesystem targets.Description
crates/agents/src/sub_agents.rsso parsing now refuses files with any symlink component and requires the final path be a regular non-symlink file (ensure_no_symlink_components,is_regular_file).ensure_safe_sub_agent_dir).fs::writewith a safe write path that validates the destination and writes via a temporary,create_newfile thenrenameto avoid following symlinks and avoid TOCTOU when possible (assert_safe_destination,write_sub_agent_file,unique_temp_path).Testing
cargo fmt --package aghub-agents --checkwhich succeeded.git diff --checkwhich reported no whitespace/errors related to the change.cargo test -p aghub-agents sub_agents(and offline variant) but test execution was blocked by missing cached crates / network access to crates.io, so the new tests were added but could not be executed in this environment.Codex Task