fix(memory): skip malformed encrypted session envelopes - #4566
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4beb5f5bf4
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| plaintext = self.cipher.decrypt(token, ttl=self.ttl) | ||
| return cast(TResponseInputItem, _from_json_bytes(plaintext)) | ||
| except (InvalidToken, KeyError): | ||
| decoded = _from_json_bytes(plaintext) |
There was a problem hiding this comment.
Handle ValueError from oversized JSON integers
When an authenticated envelope decrypts to a JSON object containing an integer longer than Python's configured digit limit (for example, more than 4,300 digits by default), json.loads() raises ValueError, not JSONDecodeError. That exception escapes here, so get_items() and pop_item() still abort instead of isolating the malformed record as intended; catch this deserialization failure at the same boundary.
AGENTS.md reference: AGENTS.md:L165-L166
Useful? React with 👍 / 👎.
|
Thanks for investigating this. Normal expiry, key mismatch, and ciphertext corruption already fail Fernet authentication and are skipped through the existing InvalidToken handling. The new branches require a structurally corrupted typed envelope or plaintext that was validly encrypted with the session key but was not produced by EncryptedSession._wrap(). I am going to close this PR for now because no supported backend, migration, or user report has been shown to produce those shapes. If you can provide a concrete stored record from a supported session backend and trace how ordinary SDK operation produced it, we can reconsider the narrow corruption boundary that needs handling. |
Summary
EncryptedSessiondocuments that invalid or expired stored envelopes are skipped, but malformed payload types, invalid UTF-8, invalid JSON, and valid JSON with a non-object shape could abort session reads. Harden_unwrapto skip those malformed envelopes while preserving valid response-item mappings.Test plan
21 passed(tests/extensions/memory/test_encrypt_session.py).UV_CACHE_DIR=/tmp/uv-verify-pr5-a2 UV_DEFAULT_INDEX=https://pypi.org/simple bash .agents/skills/code-change-verification/scripts/run.sh— all commands passed (format, lint, typecheck, tests).Issue number
N/A
Checks
.agents/skills/code-change-verification/scripts/run.sh/reviewbefore submitting this PR