fix(mcp): ignore a token cache that is not a JSON object 🤖🤖🤖 - #286
fix(mcp): ignore a token cache that is not a JSON object 🤖🤖🤖#286sushant-mishra-dtu wants to merge 1 commit into
Conversation
_load_cached_token() documents itself as loading a cached token "if present and well-formed", but it validated only the entry, never the top-level document. json.loads returns a list, str, int or None for any valid-but- non-object file, and the following data.get() then raised AttributeError -- which the (OSError, ValueError) guard does not cover. The exception escaped into the MCP connection path instead of falling back to a fresh OAuth flow, and recovering meant finding and deleting .nooa/mcp_tokens.json by hand. The sibling _save_cached_token() already guards the identical read twenty lines below (oauth.py:818-820), so the write path treated a non-object cache as recoverable while the read path did not. This brings the two into line: a cache file that is not a JSON object is now treated exactly like a missing or corrupt one. No signature change, and no behaviour change for a well-formed cache. Adds one parametrized regression test over the three shapes ([], "nope" and null), which fails on the unfixed tree with the AttributeError above. Signed-off-by: sushant-mishra-dtu <sushant.arh@gmail.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 12 included reviews per hour; 10 remain after this review. 📝 WalkthroughWalkthroughThe OAuth token cache loader now treats non-dictionary JSON contents as cache misses. Parameterized tests cover arrays, strings, and ChangesOAuth cache validation
Estimated code review effort: 1 (Trivial) | ~5 minutes Merge Risk: ⚪ Minimal · up to Non-object OAuth token-cache JSON now safely behaves as a cache miss instead of interrupting authentication, with regression coverage for the affected JSON shapes. The change is ready to merge. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
What this fixes
_load_cached_token()promises in its own docstring to load a cached token "if presentand well-formed", but it only validates the entry, never the top-level document:
json.loadsreturns alist,str,intorNonefor any valid-but-non-object file.data.getthen raisesAttributeError, which the(OSError, ValueError)guard does notcover — so instead of falling back to a fresh OAuth flow, the exception escapes into the
MCP connection path and the server simply fails to connect. Recovering requires the user
to find and delete
.nooa/mcp_tokens.jsonby hand.The inconsistency
Its sibling
_save_cached_token()guards the identical read, twenty lines below atoauth.py:818-820:So the write path already treats a non-object cache as recoverable. Only the read path
does not. This change brings the two into line.
Reproduction
The fix
Two lines, mirroring the sibling:
A cache file that is not a JSON object is now treated exactly like a missing or corrupt
one — return
Noneand let the caller start a fresh OAuth flow. No signature change, andno behaviour change for a well-formed cache.
Test
test_load_cached_token_ignores_non_object_cache, parametrized over the three shapes(
[],"nope",null), placed beside the existingtest_token_cache_roundtrip.Verified to fail on the unfixed tree before being kept:
Scope
Only the guard. Two adjacent observations in this file are deliberately left alone as
separate concerns:
_save_cached_tokenwrites the cache with a plainwrite_text()rather than the tmp+
replace()pattern used elsewhere, and theread_text()calls herecarry no explicit
encoding. Happy to raise either separately if useful.