Skip to content

fix(securefile,credstore): call Sync on temp file before close and rename#631

Merged
kevincodex1 merged 1 commit into
Gitlawb:mainfrom
euxaristia:fix/missing-fsync-durability
Jul 12, 2026
Merged

fix(securefile,credstore): call Sync on temp file before close and rename#631
kevincodex1 merged 1 commit into
Gitlawb:mainfrom
euxaristia:fix/missing-fsync-durability

Conversation

@euxaristia

Copy link
Copy Markdown
Contributor

Summary

Fixes a High-severity durability issue where credential and secret files could be corrupted or lost (0-byte file) if a system crash or power failure occurs shortly after a write.

Although a write-to-temp-then-rename pattern is used, neither securefile nor credstore called tmp.Sync() (fsync) to flush the page cache and directory metadata to physical disk before renaming.

This PR adds Sync() calls (and checks their errors) before closing and renaming.

Changes

  • In internal/securefile/securefile.go: Call tmp.Sync() in writeNewSecretFile.
  • In internal/credstore/credstore.go: Call tmp.Sync() in Store.write.

Test plan

  • go test -race ./internal/securefile/... ./internal/credstore/... — ok

…name

When writing credentials or generating user secrets, files are written using a write-to-temp-then-rename pattern. However, neither package called Sync (fsync) on the temporary file before closing and renaming. If a crash or power failure occurred shortly after write, this could result in truncated or 0-byte key/credential files on disk.

Add tmp.Sync() calls to writeNewSecretFile and Store.write to guarantee durability.
@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown

Warning

Review limit reached

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

Next review available in: 50 minutes

Your organization has reached its usage spending cap. Adjust your spending cap in the billing tab.

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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 858a07aa-c855-45d8-ba31-c183852ac5f5

📥 Commits

Reviewing files that changed from the base of the PR and between aa73a76 and c9db31d.

📒 Files selected for processing (2)
  • internal/credstore/credstore.go
  • internal/securefile/securefile.go
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@Vasanthdev2004 Vasanthdev2004 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Verified the fsync-before-rename idiom is applied on the right paths and in the right order in both internal/credstore/credstore.go (Store.write) and internal/securefile/securefile.go (writeNewSecretFile): tmp.Sync() is called while the file is still open, then Close, then Rename. Sync errors are checked and the temp file is closed on that path; the existing defer os.Remove(tmpPath) cleans up. os.File.Sync maps to FlushFileBuffers on Windows, so this is correct here too. go build ./..., go vet, and go test on both packages pass, and the PR merges cleanly into current main (its base is a few commits behind but no conflicts). It composes fine with #641, which touches disjoint stores. Approving.

@kevincodex1
kevincodex1 merged commit 212734a into Gitlawb:main Jul 12, 2026
7 checks passed
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.

3 participants