Skip to content

fix(memory): skip malformed encrypted session envelopes - #4566

Closed
hsusul wants to merge 1 commit into
openai:mainfrom
hsusul:fix/encrypted-session-malformed-envelope
Closed

fix(memory): skip malformed encrypted session envelopes#4566
hsusul wants to merge 1 commit into
openai:mainfrom
hsusul:fix/encrypted-session-malformed-envelope

Conversation

@hsusul

@hsusul hsusul commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Summary

EncryptedSession documents 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 _unwrap to skip those malformed envelopes while preserving valid response-item mappings.

Test plan

  • Added regression coverage for non-string, lone-surrogate, and non-object encrypted payloads.
  • Focused: 21 passed (tests/extensions/memory/test_encrypt_session.py).
  • Full verification: 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

  • I've added new tests, if relevant
  • I've run .agents/skills/code-change-verification/scripts/run.sh
  • I've confirmed all verification steps pass
  • If using Codex, I've run /review before submitting this PR

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@seratch

seratch commented Aug 21, 2026

Copy link
Copy Markdown
Member

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.

@seratch seratch closed this Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants