From 7ba41d388ae011c19cd2b1e8924a98d29911d8c3 Mon Sep 17 00:00:00 2001 From: YeonGyu-Kim Date: Wed, 8 Apr 2026 13:28:14 +0900 Subject: [PATCH] fix(oauth): atomic storage writes for token safety Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- src/features/mcp-oauth/storage.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/features/mcp-oauth/storage.ts b/src/features/mcp-oauth/storage.ts index d041bdfd1..2c705f5b7 100644 --- a/src/features/mcp-oauth/storage.ts +++ b/src/features/mcp-oauth/storage.ts @@ -1,4 +1,4 @@ -import { chmodSync, existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs" +import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs" import { dirname, join } from "node:path" import { getOpenCodeConfigDir } from "../../shared" @@ -82,8 +82,10 @@ function writeStore(store: TokenStore): boolean { mkdirSync(dir, { recursive: true }) } - writeFileSync(filePath, JSON.stringify(store, null, 2), { encoding: "utf-8", mode: 0o600 }) - chmodSync(filePath, 0o600) + const tempPath = `${filePath}.tmp.${Date.now()}` + writeFileSync(tempPath, JSON.stringify(store, null, 2), { encoding: "utf-8", mode: 0o600 }) + chmodSync(tempPath, 0o600) + renameSync(tempPath, filePath) return true } catch { return false