{Core} Handle JSONDecodeError for corrupted MSAL token cache and SP entries - #33728
{Core} Handle JSONDecodeError for corrupted MSAL token cache and SP entries#33728Aditya Pujara (a0x1ab) with Copilot wants to merge 5 commits into
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 |
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Pull request overview
Note
Copilot could not run the full agentic suite for this review because it was automatically requested on a bot-authored pull request. Request a review from Copilot under Reviewers to retry with the full agentic suite. Improved support for bot-authored pull requests is coming soon.
Adds resilience to auth persistence by tolerating corrupted JSON files and introduces tests to validate the behavior.
Changes:
- Introduced
AzureCliTokenCacheto swallowJSONDecodeErrorduring cache reload and reset in-memory state. - Updated persisted token cache loading to return
AzureCliTokenCache. - Added unit tests covering corrupted/valid JSON for both token cache and secret store.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/azure-cli-core/azure/cli/core/auth/persistence.py | Adds JSON corruption handling for token cache reload and secret store load. |
| src/azure-cli-core/azure/cli/core/auth/tests/test_persistence.py | Adds regression tests ensuring corrupted JSON does not crash token cache/secret store loads. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def _reload_if_necessary(self): | ||
| try: | ||
| super()._reload_if_necessary() | ||
| except json.JSONDecodeError as ex: | ||
| # The token cache file may be corrupted due to incomplete or concurrent writes. | ||
| # Reset to a fresh empty cache so that the current operation can continue. | ||
| logger.warning("Failed to deserialize token cache '%s': %s. " | ||
| "The cache will be reset.", self._persistence.get_location(), ex) | ||
| self.deserialize(None) |
| logger.warning("Failed to deserialize token cache '%s': %s. " | ||
| "The cache will be reset.", self._persistence.get_location(), ex) | ||
| self.deserialize(None) |
| logger.warning("Failed to deserialize service principal entries '%s': %s. " | ||
| "The entries will be reset.", self._persistence.get_location(), ex) | ||
| return [] |
| def _make_persistence(self, tmp_path): | ||
| """Build a FilePersistence from a .json tmp file path.""" | ||
| # build_persistence appends the .json extension, so strip it first | ||
| return build_persistence(tmp_path.replace('.json', ''), encrypt=False) |
|
|
||
| # Use float('inf') so that the condition `_last_sync < time_last_modified()` is | ||
| # always True, forcing a reload on every call to _reload_if_necessary. | ||
| with mock.patch.object(type(cache._persistence), 'time_last_modified', return_value=float('inf')): |
|
Copilot please review and implement all unaddressed human feedback through Yong Zhang (@yonzhan)'s latest comment at #33728 (comment), then push the fix to this PR. Posted by agent-assist (autonomous bug-fix pipeline). |
Co-authored-by: a0x1ab <59631311+a0x1ab@users.noreply.github.com>
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.
❌ CI Failures Detected
This PR has 2 failing checks out of 90 total (0 pending):
- Azure.azure-cli — Build #20260804.12 failed. See: https://github.com/Azure/azure-cli/runs/91943884389
- Azure.azure-cli (Test Homebrew Formula) — Test Homebrew Formula failed. See: https://github.com/Azure/azure-cli/runs/91902064118
Please investigate and fix the underlying cause of these failures, then push an update to this PR.
|
Started a Copilot task using |
…_modified Co-authored-by: a0x1ab <59631311+a0x1ab@users.noreply.github.com>
Live test skipped⏭️ Skipping the live test for this revision because the only test file(s) changed are These |
|
🔔 Routing this PR to @Azure/act-identity-squad. |
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.