You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit da387a7
Browse filesBrowse the repository at this point in the historyBrowse files
fix(atomic): bind replacement modes to descriptors (#91)
replaceFileAtomic() and replaceFileAtomicSync() reached the requested mode with
a chmod on the destination path after the rename, so a local user able to write
the parent directory could swap in a symlink and redirect the mode change to an
unrelated file.
The exclusive temp descriptor is now held open across write, mode application,
optional fsync and close, before the rename. The copy fallback applies modes
through its pinned destination descriptor. No post-publication destination chmod
remains.
The async path needs no interface change: it applies the mode through the
FileHandle returned by the already-required open. The sync path adds an optional
fchmodSync, since openSync returns a bare descriptor; adapters that request a
mode without providing it fail before mutating anything.
Reported by @yetval with a complete reproduction.
Copy file name to clipboardExpand all lines: CHANGELOG.md
+8Lines changed: 8 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,14 @@
11
11
12
12
- Suffix Windows reserved basenames with `_` in `sanitizeUntrustedFileName()` while preserving case and extensions on every platform, including dollar names and superscript COM/LPT variants; thanks @SebTardif (#67).
13
13
14
+
### Security and Correctness
15
+
16
+
- Apply `replaceFileAtomic()` and `replaceFileAtomicSync()` modes through pinned temp-file descriptors before rename, and through pinned copy-fallback descriptors, so a post-rename symlink swap cannot redirect `chmod` to an unrelated file while exact modes remain independent of umask; thanks @yetval for reporting this (#86).
17
+
18
+
### Compatibility
19
+
20
+
- Add an optional `fchmodSync` operation to the injectable synchronous atomic-replacement filesystem type. Async adapters need no new member because their required `open()` returns a mode-capable `FileHandle`; custom sync adapters that pass `mode` or `preserveExistingMode` now fail before mutation when `fchmodSync` is absent, while adapters that request neither option remain compatible.
`replaceFileAtomic()` writes a sibling temp file, optionally fsyncs it, and renames it over the destination. Mode preservation, pinned-destination hardlink rejection, rename retry / copy fallback on `EPERM`, bounded original-content restoration after a torn fallback, parent-directory fsync, and a `beforeRename` hook for backup or observer flows are all opt-in. `movePathWithCopyFallback()` stages cross-device moves before commit and removes only the copied source entries, so concurrent source additions or replacements are preserved.
279
+
`replaceFileAtomic()` writes a sibling temp file, applies its exact mode through the still-open descriptor, optionally fsyncs it, and renames it over the destination. It never follows the published destination path to set file permissions. Mode preservation, pinned-destination hardlink rejection, rename retry / copy fallback on `EPERM`, bounded original-content restoration after a torn fallback, parent-directory fsync, and a `beforeRename` hook for backup or observer flows are all opt-in. `movePathWithCopyFallback()` stages cross-device moves before commit and removes only the copied source entries, so concurrent source additions or replacements are preserved.
`replaceFileAtomicSync()` covers the synchronous case with the same options shape. Both accept an injectable `fileSystem` for tests.
293
+
`replaceFileAtomicSync()` covers the synchronous case with the same options shape. Both accept an injectable `fileSystem` for tests. Async adapters use `chmod()` on the `FileHandle` returned by their required `open()` operation; custom sync adapters using `mode` or `preserveExistingMode` provide the optional descriptor-bound `fchmodSync` operation.
Copy file name to clipboardExpand all lines: docs/atomic.md
+13-2Lines changed: 13 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ import {
14
14
15
15
## `replaceFileAtomic` / `replaceFileAtomicSync`
16
16
17
-
Write `content` to a sibling temp file in the destination directory, optionally `fsync`the temp file, optionally `fsync` the parent directory after rename, then atomically rename over the destination.
17
+
Write `content` to a sibling temp file in the destination directory, apply the final mode through its still-open descriptor, optionally `fsync`that descriptor, optionally `fsync` the parent directory after rename, then atomically rename over the destination. No permission change follows the caller-supplied destination path after publication.
18
18
19
19
Async replacements to the same destination are serialized inside the current process, so two overlapping `replaceFileAtomic()` calls do not interleave their temp-write/rename phases. Use a sidecar lock when multiple processes may write the same target.
20
20
@@ -121,7 +121,7 @@ Use it when callers must see a whole staged tree at the target path. For single-
121
121
## `writeTextAtomic`
122
122
123
123
Atomic UTF-8 text write with the same secure defaults as `writeJson`: sibling
124
-
temp file, temp fsync, rename, parent fsync, and final chmod best-effort.
124
+
temp file, descriptor-bound mode setting and fsync, rename, and parent fsync.
125
125
It delegates to `replaceFileAtomic()` with a smaller call shape. Use it when
126
126
you do not need replacement hooks such as `beforeRename`, `preserveExistingMode`,
127
127
or custom copy-fallback policy.
@@ -212,6 +212,17 @@ await replaceFileAtomic({
212
212
});
213
213
```
214
214
215
+
The synchronous injectable interface adds one optional descriptor-mode operation:
216
+
217
+
```ts
218
+
typeReplaceFileAtomicSyncFileSystem= {
219
+
// other required operations omitted
220
+
fchmodSync?:typeofimport("node:fs").fchmodSync;
221
+
};
222
+
```
223
+
224
+
The async interface already requires `open()`, whose `FileHandle` supplies `chmod()`, so injecting `node:fs` or another conforming adapter needs no new async member. A custom synchronous filesystem that passes `mode` or `preserveExistingMode` must supply `fchmodSync`; omission fails before any file is created and never falls back to `chmod(destination)`. Existing synchronous adapters that request neither option may omit it. Copy fallback applies the mode through its pinned destination descriptor as well, preserving exact modes despite the process umask.
225
+
215
226
## See also
216
227
217
228
-[`root()`](root.md) — when you want method-style writes with the boundary baked in.
Copy file name to clipboardExpand all lines: docs/types.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,8 @@
2
2
3
3
The types most callers reach for. Shared data shapes are exported from `@openclaw/fs-safe/types`; method-specific option/result types live next to their subpath.
4
4
5
+
For atomic replacement, `ReplaceFileAtomicFileSystem` and `ReplaceFileAtomicSyncFileSystem` are exported from `@openclaw/fs-safe/atomic`. Async adapters use `chmod()` on the `FileHandle` returned by their required `open()` operation. The synchronous type adds optional `fchmodSync(fd, mode)`; custom sync adapters that explicitly request `mode` or `preserveExistingMode` must implement it. See [Atomic writes](atomic.md#test-injection).
0 commit comments