fix(fs): refuse credential writes through symlinks#6
Conversation
Ported from upstream anomalyco#36360.
|
Warning Review limit reached
Next review available in: 45 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughFilesystem writes now check whether the destination is a symbolic link before writing. Symlink targets produce errors, missing paths retain existing creation behavior, and a test confirms the protected target remains unchanged. ChangesSymlink write protection
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ 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 |
|
@coderabbitai full review |
✅ Action performedFull review finished. Your plan includes PR reviews subject to rate limits. More reviews will be available in 35 minutes. |
|
@coderabbitai full review |
✅ Action performedFull review finished. |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/opencode/src/util/filesystem.ts (1)
62-93: 🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift
write()still has a symlink-race bypass
lstat(p)only checks the final path, so a symlinked parent can redirect both the preflight andwriteFile(). TheENOENTmkdir-and-retry path adds another race window. Use an atomic no-follow write flow rooted in a trusted directory, and add a regression for a case likelinkDir/credential.json.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/opencode/src/util/filesystem.ts` around lines 62 - 93, The write flow in write and assertNotSymlink remains vulnerable when a parent directory is symlinked or changes between validation and writing. Replace the final-path lstat plus mkdir-and-retry logic with an atomic no-follow write strategy rooted in a trusted directory, ensuring paths such as linkDir/credential.json cannot redirect writes; add a regression test covering that case.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/core/src/fs-util.ts`:
- Around line 113-135: Replace the lstat-then-path-based write in the writeJson
flow with a descriptor-based no-follow operation: open the target from a trusted
non-symlinked parent, reject symlinked targets or parents, and write and chmod
through the opened descriptor without resolving the pathname again. Update the
relevant filesystem helper APIs as needed, and add coverage for a
symlinked-parent path to verify the write is refused.
---
Outside diff comments:
In `@packages/opencode/src/util/filesystem.ts`:
- Around line 62-93: The write flow in write and assertNotSymlink remains
vulnerable when a parent directory is symlinked or changes between validation
and writing. Replace the final-path lstat plus mkdir-and-retry logic with an
atomic no-follow write strategy rooted in a trusted directory, ensuring paths
such as linkDir/credential.json cannot redirect writes; add a regression test
covering that case.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: bb86f349-ab85-4d0a-b271-306b4cd4345c
📒 Files selected for processing (3)
packages/core/src/fs-util.tspackages/opencode/src/util/filesystem.tspackages/opencode/test/util/filesystem.test.ts
Addresses CodeRabbit TOCTOU finding on the anomalyco#36360 port: the leaf symlink check now happens at open time via O_NOFOLLOW (lstat retained as the Windows fallback). Symlinked parent directories remain allowed by design for dotfile-manager setups.
|
Addressed the Critical TOCTOU finding in the latest commit:
|
|
@coderabbitai review |
✅ Action performedReview finished.
|
Port of upstream anomalyco/opencode#36360.
FSUtil.writeJsonandFilesystem.writenow lstat the destination and refuse to write when it is an existing symlink, preventing credential files (auth.json etc.) from being redirected to attacker-chosen paths.Verified:
bun test test/util/filesystem.test.ts(62 pass) from packages/opencode;bun typecheckclean in packages/core and packages/opencode. Applied cleanly.Summary by CodeRabbit