Skip to content

Fix: token fallback file briefly world-readable before chmod#76

Open
karbassi wants to merge 1 commit into
robcerda:mainfrom
karbassi:fix/token-file-perms
Open

Fix: token fallback file briefly world-readable before chmod#76
karbassi wants to merge 1 commit into
robcerda:mainfrom
karbassi:fix/token-file-perms

Conversation

@karbassi

@karbassi karbassi commented Jul 1, 2026

Copy link
Copy Markdown

What

Closes #75.

The file-based session fallback (used when no keyring backend is available — WSL, headless Linux) wrote the long-lived Monarch session token via Path.write_text() and narrowed permissions to 0600 only afterward:

_TOKEN_FILE.write_text(token)                   # created under umask, typically 0644
_TOKEN_FILE.chmod(stat.S_IRUSR | stat.S_IWUSR)  # 600, but too late

write_text() creates the file under the process umask (commonly 0644), so between the write and the chmod the token is world-readable by any local user. The payload is a long-lived, full-access financial credential, so the window is worth closing.

How

Create the file already locked to 0600 via os.open(..., O_CREAT, 0o600), then still chmod to enforce 0600 if the file already existed. No behavior change for keyring users (unaffected path).

Test

Adds TestFileFallbackPermissions, which intercepts creation and fails if the token file is created via write_text() or with any mode other than 0600. Verified the test fails against the old implementation and passes with the fix.

🤖 Generated with Claude Code

The file-based session fallback wrote the long-lived Monarch token via
Path.write_text() (created under the process umask, typically 0644) and
only narrowed it to 0600 afterward. That leaves a window where a
full-access financial credential is world-readable by other local users.

Create the file already locked via os.open(..., O_CREAT, 0o600), keeping
the trailing chmod to enforce 0600 on a pre-existing file. Adds a
regression test that fails if creation reverts to write_text().

Fixes robcerda#75

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Token file is briefly world-readable before chmod (file-fallback path)

1 participant