Skip to content

Commit b659a4c

Browse files
committed
fix(switcher): create _write_json's temp file 0600 from the start
The temp file was written at the umask-derived mode (0644 by default) and only chmod'ed afterwards, so ~/.claude.json content (primaryApiKey, MCP env secrets) was readable by other local users for that window. Create it with tempfile.mkstemp beside the target instead, as transfer.py and settings.py already do.
1 parent 3a4e5c1 commit b659a4c

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎src/claude_swap/switcher.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import shutil
1111
import threading
1212
import sys
13+
import tempfile
1314
import time
1415
from concurrent.futures import ThreadPoolExecutor
1516
from pathlib import Path
@@ -560,9 +561,15 @@ def _write_json(self, path: Path, data: dict) -> None:
560561
"""Write JSON file with validation."""
561562
content = json.dumps(data, indent=2)
562563

563-
# Write to temp file first
564-
temp_path = path.with_suffix(f".{os.getpid()}.tmp")
565-
temp_path.write_text(content, encoding="utf-8")
564+
# Write to temp file first. mkstemp creates it 0600 beside the target
565+
# (same filesystem, so the rename below stays atomic), so the payload
566+
# is never on disk at the umask-derived mode.
567+
fd, temp_name = tempfile.mkstemp(
568+
dir=str(path.parent), prefix=f"{path.stem}.", suffix=".tmp"
569+
)
570+
temp_path = Path(temp_name)
571+
with os.fdopen(fd, "w", encoding="utf-8") as f:
572+
f.write(content)
566573

567574
# Validate written content
568575
try:

‎tests/test_swap_accounts.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,30 @@ def failing_chmod(path, mode):
291291

292292
assert switcher.sequence_file.read_text(encoding="utf-8") == before
293293

294+
def test_write_json_temp_file_is_private_from_creation(
295+
self, temp_home: Path
296+
):
297+
"""The temp file holds the whole payload (``~/.claude.json`` can carry
298+
``primaryApiKey`` and MCP env secrets), so it has to be 0600 when it
299+
is created. With chmod neutralised, the published file carries the
300+
mode the temp file was created with: a write-then-chmod writer shows
301+
up here as 0644 under the usual 022 umask."""
302+
if sys.platform == "win32":
303+
pytest.skip("POSIX file modes")
304+
switcher = ClaudeAccountSwitcher()
305+
target = temp_home / ".claude.json"
306+
307+
old_umask = os.umask(0o022)
308+
try:
309+
with pytest.MonkeyPatch.context() as mp:
310+
mp.setattr("claude_swap.switcher.os.chmod", lambda *a, **k: None)
311+
switcher._write_json(target, {"primaryApiKey": "sk-test"})
312+
finally:
313+
os.umask(old_umask)
314+
315+
assert target.stat().st_mode & 0o777 == 0o600
316+
assert not list(temp_home.glob(".claude.*.tmp"))
317+
294318
def test_swap_same_email_one_sided_clears_destination(
295319
self, temp_home: Path, sample_sequence_data_with_org: dict
296320
):

0 commit comments

Comments
 (0)