Skip to content

fix(fs): refuse credential writes through symlinks#6

Merged
CreatorGhost merged 2 commits into
devfrom
symlink-credential-writes
Jul 11, 2026
Merged

fix(fs): refuse credential writes through symlinks#6
CreatorGhost merged 2 commits into
devfrom
symlink-credential-writes

Conversation

@CreatorGhost

@CreatorGhost CreatorGhost commented Jul 11, 2026

Copy link
Copy Markdown
Owner

Port of upstream anomalyco/opencode#36360.

FSUtil.writeJson and Filesystem.write now 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 typecheck clean in packages/core and packages/opencode. Applied cleanly.

Summary by CodeRabbit

  • Bug Fixes
    • Prevented file writes through symbolic links, protecting the linked target from unintended changes.
    • Preserved normal write behavior for new files and non-symbolic-link paths.
    • Added validation to ensure failed symbolic-link checks are reported clearly.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@CreatorGhost, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 45 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6314b041-45cc-4949-a9b0-1fa312db9565

📥 Commits

Reviewing files that changed from the base of the PR and between b55c0d2 and c92962e.

📒 Files selected for processing (3)
  • packages/core/src/fs-util.ts
  • packages/opencode/src/util/filesystem.ts
  • packages/opencode/test/util/filesystem.test.ts
📝 Walkthrough

Walkthrough

Filesystem 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.

Changes

Symlink write protection

Layer / File(s) Summary
Add symlink guards
packages/core/src/fs-util.ts, packages/opencode/src/util/filesystem.ts
writeJson and Filesystem.write use lstat to reject symbolic-link targets while preserving missing-path handling and error wrapping.
Validate symlink refusal
packages/opencode/test/util/filesystem.test.ts
The test verifies writes through symlinks fail and do not modify the target file.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the change and verification, but it misses required template sections like issue, type of change, and checklist details. Add the missing template sections, including the issue link, type of change, and checklist items, and format the description to match the repository template.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the main security-related change.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch symlink-credential-writes

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@CreatorGhost

Copy link
Copy Markdown
Owner Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
✅ Action performed

Full review finished.


Your plan includes PR reviews subject to rate limits. More reviews will be available in 35 minutes.

@CreatorGhost

Copy link
Copy Markdown
Owner Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
✅ Action performed

Full review finished.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 and writeFile(). The ENOENT mkdir-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 like linkDir/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

📥 Commits

Reviewing files that changed from the base of the PR and between 67e2bd4 and b55c0d2.

📒 Files selected for processing (3)
  • packages/core/src/fs-util.ts
  • packages/opencode/src/util/filesystem.ts
  • packages/opencode/test/util/filesystem.test.ts

Comment thread packages/core/src/fs-util.ts Outdated
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.
@CreatorGhost

Copy link
Copy Markdown
Owner Author

Addressed the Critical TOCTOU finding in the latest commit:

  • Fixed (leaf race): writes now go through a descriptor opened with O_NOFOLLOW (ELOOP/EMLINK mapped to the same symlink-refusal error), so the check is bound to the open itself instead of a separate lstat. The lstat pre-check remains as the Windows fallback where O_NOFOLLOW is unavailable.
  • Intentionally not changed (symlinked parents): rejecting symlinked parent directories would break legitimate dotfile-manager setups that symlink ~/.config/data dirs. An attacker who can swap a parent directory component already controls the directory tree; the realistic planted-symlink attack on the credential file itself is closed race-free. Added a test documenting that symlinked parents remain supported.

@CreatorGhost

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@CreatorGhost CreatorGhost merged commit b1f46a6 into dev Jul 11, 2026
5 of 7 checks passed
@CreatorGhost CreatorGhost deleted the symlink-credential-writes branch July 11, 2026 22:36
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