fix(sessions): preserve ciphertext on wrong-key pop - #5018
harshitethic wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7366a7f3a8
ℹ️ 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".
| try: | ||
| self.cipher.decrypt(token) | ||
| except InvalidToken: | ||
| return None, True |
There was a problem hiding this comment.
Do not classify corrupt tokens as wrong-key ciphertext
When the newest envelope has a damaged, truncated, or tampered payload, the second decrypt raises the same InvalidToken as a wrong key, so this classifies the corrupt record as recoverable. pop_item() then restores it and returns None, permanently hiding any valid older items behind that corrupt tail instead of isolating it; the implementation needs an independent key identifier or another mechanism that can distinguish a key mismatch from record corruption.
AGENTS.md reference: AGENTS.md:L115-L115
Useful? React with 👍 / 👎.
| await _call_session_method( | ||
| self.underlying_session.add_items, | ||
| [cast(TResponseInputItem, enc)], | ||
| wrapper=wrapper, | ||
| ) |
There was a problem hiding this comment.
Restore popped ciphertext atomically
If another writer appends after the backend pop completes but before this add_items call, the old ciphertext is appended after the new item, so a correct-key reader observes reordered history and the next pop targets the wrong tail; if restoration fails, the ciphertext is lost outright. The pop and conditional restoration therefore need a backend-level atomic/transactional operation rather than two independently awaited mutations.
AGENTS.md reference: AGENTS.md:L117-L117
Useful? React with 👍 / 👎.
Summary
EncryptedSession.pop_item()unwraps the backend's atomic pop resultVerification
git diff --checkFixes #5005