Skip to content

Create msal_cache.bin and the cache recipe file as owner-only - #986

Open
rumaisaaaaa263 wants to merge 1 commit into
AzureAD:devfrom
rumaisaaaaa263:token-cache-file-owner-only
Open

rumaisaaaaa263 wants to merge 1 commit into
AzureAD:devfrom
rumaisaaaaa263:token-cache-file-owner-only

Conversation

@rumaisaaaaa263

Copy link
Copy Markdown

Refresh tokens saved world-readable by python -m msal and the SerializableTokenCache recipe

Both write the cache with open(path, "w"), so under the usual 022 umask the file is created 0644: the tester's msal_cache.bin holds family refresh tokens for the Azure CLI or Visual Studio client it signs in as, and the recipe's file lands in the current directory whenever XDG_RUNTIME_DIR is unset, as on macOS and Windows and usually in cron jobs and containers. The tester also wrote into an existing msal_cache.bin with whatever mode and owner it already had, so the fchmod now tightens a file left by an older run and fails with EPERM instead of writing tokens into a file owned by another user.

The python -m msal tester saves its token cache to msal_cache.bin in
the current directory with open(..., "w"), so the file holding refresh
tokens is created 0644 under the usual umask, and an existing file is
reused with whatever mode and owner it already has. The
SerializableTokenCache docstring recipe has the same open() call.

Create the tester's file with mode 0600 and fchmod it before writing,
and create the recipe's file with mode 0600.
@rumaisaaaaa263
rumaisaaaaa263 requested a review from a team as a code owner November 13, 2026 10:51
Copilot AI balanced review requested due to automatic review settings November 13, 2026 10:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Cache writes can leave existing files world-readable, exposing refresh tokens, and truncate another user's writable cache before a failed permission check, destroying its contents.

2 open issues
What changed in this PR

This PR hardens refresh-token cache persistence in the MSAL command-line tester and the SerializableTokenCache documentation recipe.

Changes:

  • Creates cache files with owner-only permissions.
  • Adds a tester save function that also tightens existing file permissions.
  • Adds POSIX tests for new and existing tester cache files.
File Description
tests/​test_main.py Tests owner-only permissions and refresh-token persistence.
msal/​token_cache.py Updates the documentation recipe to create cache files with mode 0600.
msal/​__main__.py Extracts cache saving and applies owner-only permissions before writing tokens.

Review effort: Balanced

Comment thread msal/token_cache.py
atexit.register(lambda:
open(cache_filename, "w").write(cache.serialize())
open( # Readable only by its owner, because it contains refresh tokens
os.open(cache_filename, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600),
Comment thread msal/__main__.py
Comment on lines +22 to +25
_token_cache_filename, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
with os.fdopen(fd, "w") as f:
if hasattr(os, "fchmod"): # Also covers a file that already existed
os.fchmod(fd, 0o600)

This branch has not been deployed

No deployments
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.

2 participants