Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): create atomicWrite temp file with the restricted mode

[._.]/ Adam Eivy1d769d905929
diff --git a/server/lib/fileUtils.js b/server/lib/fileUtils.js
index 2683796380..db473ece94 100644
--- a/server/lib/fileUtils.js
+++ b/server/lib/fileUtils.js
@@ -182,17 +182,20 @@ export async function atomicWrite(filePath, data) {
// `{"type":"Buffer","data":[...]}` which corrupts binary writes (PNG, etc.).
const payload = typeof data === 'string' || Buffer.isBuffer(data) ? data : JSON.stringify(data, null, 2);
await ensureDir(dirname(filePath));
- const tmp = `${filePath}.${process.pid}.${Date.now()}.${randomUUID()}.tmp`;
- await writeFile(tmp, payload);
// Preserve the destination's existing permission bits. A plain
// writeFile(existing) truncated the inode in place and kept its mode, but
- // atomicWrite creates a fresh temp inode at the umask default (typically
- // 0644) and renames it over the target — which would silently widen a
- // hand-restricted file (e.g. a `chmod 600` OAuth tokens.json) on every
- // rewrite. Mirror the destination's mode onto the temp file before the
- // rename so atomicWrite is a true drop-in for the truncate pattern. New
- // files (stat fails with ENOENT) keep the umask default, exactly as before.
+ // atomicWrite creates a fresh temp inode and renames it over the target —
+ // which would silently widen a hand-restricted file (e.g. a `chmod 600`
+ // OAuth tokens.json) to the umask default (typically 0644) on every rewrite.
+ // Read the destination's mode FIRST and create the temp file with it from
+ // the start, so the secret bytes are never written to a world-readable temp
+ // (a crash between write and chmod would otherwise leave a readable copy, and
+ // a local user could race-read it during the window). A trailing chmod pins
+ // the exact mode in case the umask tightened the create below it. New files
+ // (stat fails with ENOENT) keep the umask default, exactly as before.
const existingMode = await stat(filePath).then((s) => s.mode & 0o777, () => null);
+ const tmp = `${filePath}.${process.pid}.${Date.now()}.${randomUUID()}.tmp`;
+ await writeFile(tmp, payload, existingMode !== null ? { mode: existingMode } : undefined);
if (existingMode !== null) {
await chmod(tmp, existingMode).catch(() => {});
}