You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Related command az login, az account get-access-token, and any authenticated az command that triggers MSAL token acquisition.
Description
Replaces unsafe pickle.load()/pickle.dump() with json.load()/json.dump() in BinaryCache (MSAL HTTP cache at ~/.azure/msal_http_cache.bin).
pickle.load() can execute arbitrary code during deserialization (CWE-502). If the cache file is tampered with, it results in code execution on the next az command. While default file permissions (0644) limit exploitability, pickle is unnecessary here, the cached data is simple dicts/strings (MSAL tenant discovery metadata). This was the only import pickle in the entire src/ tree.
Changes:
pickle → json for serialization/deserialization
open() → os.open(..., 0o600) to enforce owner-only file permissions regardless of umask
Updated comments to reflect JSON error types
Backward compatible, old pickle .bin files fail JSON parsing, hit the existing except Exception fallback → self.data = {} → MSAL re-fetches tenant discovery on the next command. This is the same recovery path already used for corrupted caches.
Testing Guide
Verify existing cache is gracefully discarded: az login az account show # Should work normally; old pickle cache silently replaced with JSON
Verify new cache is created with correct permissions: ls -la ~/.azure/msal_http_cache.bin # Should show -rw------- (0600)
Verify cache disable still works: az config set core.use_msal_http_cache=false az account show # Works without cache
History Notes
[Core] Replace unsafe pickle deserialization with JSON in MSAL HTTP cache
This checklist is used to make sure that common guidelines for a pull request are followed.
The git hooks are available for azure-cli and azure-cli-extensions repos. They could help you run required checks before creating the PR.
Please sync the latest code with latest dev branch (for azure-cli) or main branch (for azure-cli-extensions).
After that please run the following commands to enable git hooks:
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR hardens Azure CLI’s MSAL HTTP cache handling (used during az login and authenticated token acquisition) by removing unsafe pickle deserialization and writing the cache with stricter file permissions.
Changes:
Replace pickle.load()/pickle.dump() with json.load()/json.dump() for the MSAL HTTP cache file.
Switch cache writes to os.open(..., 0o600) + os.fdopen(...) to create new cache files with owner-only permissions.
Azure Pipelines successfully started running 3 pipeline(s).
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
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.
Related command
az login,az account get-access-token, and any authenticatedazcommand that triggers MSAL token acquisition.Description
Replaces unsafe
pickle.load()/pickle.dump()withjson.load()/json.dump()inBinaryCache(MSAL HTTP cache at~/.azure/msal_http_cache.bin).pickle.load()can execute arbitrary code during deserialization (CWE-502). If the cache file is tampered with, it results in code execution on the nextazcommand. While default file permissions (0644) limit exploitability, pickle is unnecessary here, the cached data is simple dicts/strings (MSAL tenant discovery metadata). This was the onlyimport picklein the entiresrc/tree.Changes:
pickle→jsonfor serialization/deserializationopen()→os.open(..., 0o600)to enforce owner-only file permissions regardless of umaskBackward compatible, old pickle
.binfiles fail JSON parsing, hit the existingexcept Exceptionfallback →self.data = {}→ MSAL re-fetches tenant discovery on the next command. This is the same recovery path already used for corrupted caches.Testing Guide
Verify existing cache is gracefully discarded:
az loginaz account show# Should work normally; old pickle cache silently replaced with JSONVerify new cache is created with correct permissions:
ls -la ~/.azure/msal_http_cache.bin# Should show -rw------- (0600)Verify cache disable still works:
az config set core.use_msal_http_cache=falseaz account show# Works without cacheHistory Notes
[Core] Replace unsafe pickle deserialization with JSON in MSAL HTTP cache
This checklist is used to make sure that common guidelines for a pull request are followed.
The PR title and description has followed the guideline in Submitting Pull Requests.
I adhere to the Command Guidelines.
I adhere to the Error Handling Guidelines.