{Core} Handle JSONDecodeError for corrupted MSAL token cache and SP entries#33728
Conversation
|
Validation for Azure CLI Full Test Starting...
Thanks for your contribution! |
|
Validation for Breaking Change Starting...
Thanks for your contribution! |
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Live test skipped⏭️ Skipping the live test for this revision because the only test file(s) changed are These Posted by agent-assist (autonomous bug-fix pipeline). |
There was a problem hiding this comment.
Automated Review Summary
PR: {Core} Handle JSONDecodeError for corrupted MSAL token cache and SP entries
- Live test: Skipped (neutral) — this PR only changes
azure-cli-coreunit tests, which are not runnable viaazdev test --live. No other test files were changed. - CI checks: All 1/1 checks passed, none pending, none failed.
No blocking issues found. This PR appears ready based on the CI status observed at this head commit (027cbcc).
Posted by agent-assist (autonomous bug-fix pipeline).
|
Core |
Related command
az loginDescription
az logincrashes withjson.decoder.JSONDecodeError: Extra datawhen~/.azure/msal_token_cache.jsonis corrupted (e.g., partial/concurrent writes). Themsal_extensions.PersistedTokenCache._reload_if_necessary()only catchesPersistenceNotFound, lettingJSONDecodeErrorpropagate unhandled.Changes:
AzureCliTokenCache(persistence.py): New subclass ofPersistedTokenCachethat overrides_reload_if_necessaryto catchjson.JSONDecodeError, emit a warning, and reset the in-memory cache to empty viaself.deserialize(None). The subsequentmodify()call then writes a fresh valid cache to disk, overwriting the corrupt file.load_persisted_token_cache: Updated to returnAzureCliTokenCacheinstead of barePersistedTokenCache.SecretStore.load: Also catchesjson.JSONDecodeErrorfor the service principal entries file, returning[]rather than raising.Testing Guide
Manually corrupt
~/.azure/msal_token_cache.jsonby appending extra bytes, then runaz login:Before this fix: crashes with
JSONDecodeError: Extra data.After this fix: logs a warning and proceeds with a fresh cache.
Unit tests added in
src/azure-cli-core/azure/cli/core/auth/tests/test_persistence.pycovering corrupted and valid JSON for bothAzureCliTokenCacheandSecretStore.History Notes
[Core]
az login: FixJSONDecodeErrorcrash when MSAL token cache file is corruptedThis 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.