fix: Fix panic on a malformed server cookie - #491
Merged
Conversation
`set_cookies` runs inside reqwest's cookie-store callback, a Rust callback driven by the HTTP stack. The `.unwrap()`s on the cookie constructor and `set_cookie` call meant a hostile/buggy server's malformed `Set-Cookie` (bad domain/expiry that `http.cookiejar.Cookie` rejects), or a custom cookie jar whose `set_cookie` raises, would panic and unwind across the FFI boundary — aborting the host process instead of raising a catchable exception. Mirror the Node wrapper's behavior of skipping cookies the jar rejects, but emit a Python `UserWarning` (falling back to stderr) so the dropped cookie is surfaced rather than silently ignored. Closes #478 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015p2biLkkRLNEs9xWyqdpby
`set_cookies` runs inside reqwest's cookie-store callback, a Rust callback driven by the HTTP stack. The `.unwrap()`s on the cookie constructor and `set_cookie` call meant a hostile/buggy server's malformed `Set-Cookie` (bad domain/expiry that `http.cookiejar.Cookie` rejects), or a custom cookie jar whose `set_cookie` raises, would panic and unwind across the FFI boundary — aborting the host process instead of raising a catchable exception. Skip the offending cookie and continue, ignoring parsing errors silently to match the Node binding's `setCookie` handling in `index.wrapper.js`. Closes #478 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015p2biLkkRLNEs9xWyqdpby
Add a regression test (sync and async) for #478: a custom cookie jar whose `set_cookie` raises must not abort the interpreter. The request completes, the offending cookie is skipped, and a valid cookie in the same response is still stored. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015p2biLkkRLNEs9xWyqdpby
Pijukatel
force-pushed
the
claude/malformed-cookie-handling-mhdccv
branch
from
June 30, 2026 11:02
32d1ee1 to
fbb575f
Compare
Pijukatel
marked this pull request as ready for review
June 30, 2026 11:32
There was a problem hiding this comment.
Pull request overview
This PR prevents the Python bindings from panicking when encountering malformed server Set-Cookie headers (or cookie-jar rejections), aligning behavior with the JS implementation by silently skipping problematic cookies.
Changes:
- Update
PythonCookieJar::set_cookiesto stop unwrapping Python cookie construction / insertion and instead skip on errors. - Add sync + async regression tests ensuring a rejecting
CookieJardoesn’t crash the client and still stores valid cookies. - Update the Python lockfile version for
impit.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| impit-python/src/cookies.rs | Avoid panics by skipping cookies that fail Python construction or insertion into the provided cookie jar. |
| impit-python/test/basic_client_test.py | Add regression test covering rejected cookie handling in the sync client. |
| impit-python/test/async_client_test.py | Add regression test covering rejected cookie handling in the async client. |
| impit-python/uv.lock | Bump the locked impit package version. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Malformed cookies are silently ignored like in JS version.