Repository navigation
Create msal_cache.bin and the cache recipe file as owner-only - #986
Open
rumaisaaaaa263 wants to merge 1 commit into
Open
rumaisaaaaa263 wants to merge 1 commit into
rumaisaaaaa263 wants to merge 1 commit into
Conversation
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.
Contributor
There was a problem hiding this comment.
🟡 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
| 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 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Refresh tokens saved world-readable by
python -m msaland theSerializableTokenCacherecipeBoth write the cache with
open(path, "w"), so under the usual 022 umask the file is created 0644: the tester'smsal_cache.binholds 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 wheneverXDG_RUNTIME_DIRis unset, as on macOS and Windows and usually in cron jobs and containers. The tester also wrote into an existingmsal_cache.binwith whatever mode and owner it already had, so thefchmodnow tightens a file left by an older run and fails withEPERMinstead of writing tokens into a file owned by another user.