fix(securefile,credstore): call Sync on temp file before close and rename#631
Conversation
…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.
|
Warning Review limit reached
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 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: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✨ 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 |
Vasanthdev2004
left a comment
There was a problem hiding this comment.
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.
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
securefilenorcredstorecalledtmp.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
internal/securefile/securefile.go: Calltmp.Sync()inwriteNewSecretFile.internal/credstore/credstore.go: Calltmp.Sync()inStore.write.Test plan
go test -race ./internal/securefile/... ./internal/credstore/...— ok