Repository navigation
fix(storage): atomic saves keep file modes and sync the credential bytes - #2458
Conversation
Four writers had drifted from ADE's own atomic-save helper: - writeGlobalState hand-rolled a temp+rename that replaced an existing ade-state.json's permission bits with the umask default. - The credential store's atomic writer renamed a 0600 temp without syncing it, so a crash right after the rename could leave an empty credential file and sign the machine out. - writeFileAtomic skipped the post-open chmod when the caller passed an explicit mode, so a narrowing umask could still strip bits. - secureWriteAtomicWithinRoot, used by every Files-tab and lane-diff save, replaced the target inode with a temp at the umask default: editing a 0755 script dropped its executable bit and a 0600 .env became readable. Use the shared writer for global state, fsync the credential temp and its directory, always apply the mode to the open descriptor, and carry the target's existing bits onto the user-file temp before any bytes land. The new mode tests are POSIX-only and skipped on this Windows host. Co-Authored-By: Claude Code <noreply@anthropic.com>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 22 minutes. View limit details
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. |
There was a problem hiding this comment.
Devin Review found 2 potential issues.
2 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)
| // One atomic writer for the whole app: it keeps the replaced file's | ||
| // permission bits (this hand-rolled save widened a 0o600 state file under | ||
| // the process umask) and syncs the temp file and its directory. | ||
| writeFileAtomic(filePath, serialized, { fsync: true }); |
There was a problem hiding this comment.
🔴 Settings replaced after failed disk flush
When the temp file’s disk flush fails, writeGlobalState can still replace the last good settings file. bestEffortFsync suppresses writeback errors, so the rename can publish incomplete settings.
Learn more
Desktop settings, recent projects, and pending update records share one JSON file. Desktop startup reads it as a unit. The previous writer abandoned the temporary file when flushing its contents failed. The shared atomic writer now proceeds to replacement after its flush helper catches the error. If a real writeback error leaves incomplete bytes, readGlobalState falls back to empty state on the next launch.
Example: A recent-project update fits in the filesystem cache, but delayed allocation returns ENOSPC on flush. The temporary file can still be renamed over the valid state, losing previously stored settings along with the attempted update.
Recommended fix: Use a strict regular-file flush for global state and propagate its failure before replacing the destination. Directory flush can remain best effort. Exercise this path with an injected temp-file fsyncSync failure and verify that the old JSON survives.
Was this helpful? React with 👍 or 👎 to provide feedback.
| fs.fsyncSync(fd); | ||
| fs.closeSync(fd); | ||
| fd = null; | ||
| fs.renameSync(tempPath, filePath); |
There was a problem hiding this comment.
🔴 Non-atomic overwrite of desktop settings
When Windows blocks replacement but allows writes, writeGlobalState can overwrite the live settings file in place. replaceViaRename falls back to a direct copy, so an interrupted copy can destroy the previous settings.
Learn more
The desktop keeps preferences, recent projects, and update recovery data in one JSON file. The previous save used only a rename to publish new bytes, so a refused rename left the previous bytes untouched. The shared replacement helper first retries the rename, then tries a staged rename, and finally copies directly to the destination for certain repeated rename errors. The last operation is not atomic and does not flush the copied destination file. readGlobalState treats malformed JSON as empty state.
Example: While the user changes a preference, a Windows reader holds the settings file open with write sharing but without delete sharing. Both renames fail, so the save copies directly onto the settings file. If that copy is interrupted, the next launch can find incomplete JSON instead of the previous settings.
Recommended fix: Give global-state writes a rename-only replacement policy. Keep transient rename retries, but never let this caller fall back to copying over the live file. Verify that a persistent rename refusal leaves the original bytes untouched.
Was this helpful? React with 👍 or 👎 to provide feedback.
| // One atomic writer for the whole app: it keeps the replaced file's | ||
| // permission bits (this hand-rolled save widened a 0o600 state file under | ||
| // the process umask) and syncs the temp file and its directory. | ||
| writeFileAtomic(filePath, serialized, { fsync: true }); |
There was a problem hiding this comment.
writeGlobalState now reaches a fallback that copies directly over the saved file when both rename attempts are refused. On Windows, another program holding the file open can trigger this path. Readers can see incomplete JSON, and a failed copy can destroy the previous state. The old writer left the saved file untouched when rename failed. Keep this caller on a rename-only path, even if that means skipping the update.
Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/desktop/src/main/services/state/globalState.ts
Line: 226
Comment:
**Saved state can be truncated**
`writeGlobalState` now reaches a fallback that copies directly over the saved file when both rename attempts are refused. On Windows, another program holding the file open can trigger this path. Readers can see incomplete JSON, and a failed copy can destroy the previous state. The old writer left the saved file untouched when rename failed. Keep this caller on a rename-only path, even if that means skipping the update.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 2d57067. Configure here.
| } catch { | ||
| // Filesystems without POSIX bits keep the open's mode. | ||
| } | ||
| } |
There was a problem hiding this comment.
Temp created before mode is applied
Medium Severity
writeFileByDescriptor still opens the sibling temp with 0o666 (umask-filtered, typically 0644) and only then fchmods the preserved mode. A local observer can open that temp in the create-to-chmod window and keep the fd after the mode is narrowed, so a 0600 .env is readable while its bytes land. writeFileAtomic already passes the intended mode into open so the file is never born wider.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 2d57067. Configure here.
Review follow-ups: - writeFileByDescriptor passes the kept mode to open as well, so a 0600 user-file temp is never briefly readable between create and fchmod. - The umask test skips in Vitest worker threads, where process.umask()'s setter throws (ERR_WORKER_UNSUPPORTED_OPERATION) on Linux CI. - globalState's leftover-temp assertion follows the shared writer's `.tmp-<pid>-<hex>` shape instead of a `.tmp` suffix. Co-Authored-By: Claude Code <noreply@anthropic.com>


Why
Four atomic writers had drifted from ADE's shared helper:
writeGlobalStatereplacedade-state.jsonthrough a temp at the umask default, so an existing 0600 file became group/world-readable.writeFileAtomicskipped its post-open chmod when the caller passed a mode, so a narrowing umask could still strip bits.secureWriteAtomicWithinRoot(Files tab and lane-diff saves) replaced the target inode at the umask default: editing a 0755 script dropped its executable bit and a 0600 .env became readable.Same defect class as pingdotgg/t3code#24310 and issue #14967, verified against ADE's writers.
What changed
writeFileAtomic(..., { fsync: true }).Evidence
Not checked
The POSIX mode assertions were not run on this Windows host. Full CI matrix not run locally.
🤖 Generated with Claude Code
The PR should not merge until the previously reported saved-state truncation risk is fixed.
Fix with agent prompt
Summary
The PR keeps file permissions during atomic saves and adds sync calls to credential writes.
No new actionable issues were found in the follow-up changes.
Reviews (2) · Last reviewed commit: "fix(storage): keep the temp private at c..." · Reviewed by Greptile