Summary
Incomplete input and output validation in Python's http.cookies Morsel and BaseCookie components leaves several update and serialization paths unprotected against control characters.
Attackers who can influence cookie values may inject control characters that bypass validation, potentially corrupting HTTP responses, manipulating headers, or enabling response-splitting style attacks in certain deployment contexts.
Affected context
This vulnerability affects applications using Python's http.cookies module where cookie values or attributes are derived from user-controlled or untrusted input.
While previous fixes rejected control characters in some code paths, the following paths remained insufficiently guarded:
Morsel.update()and the|=operator used to merge or update cookie attributes- Unpickling of
Morselobjects BaseCookie.js_output()when generating JavaScript-style cookie output
The issue becomes relevant when applications rely on these APIs without adding their own robust validation and when generated cookie data is embedded into HTTP responses or HTML/JavaScript contexts.
Exploit preconditions
- Attack surface: Web applications or services that:
- Use Python's
http.cookies.Morselto build or update cookies - Use
Morsel.update()or|=to incorporate untrusted input into cookie attributes or values - Deserialize (unpickle)
Morselinstances from untrusted or semi-trusted sources - Use
BaseCookie.js_output()to render cookies into JavaScript contexts - Required access: Ability to provide cookie values or attributes (e.g., via HTTP parameters, form data, headers, or APIs consumed by the cookie-building logic).
- Trigger point: Invocation of one of the vulnerable paths (
Morsel.update(),|=, unpickling, orBaseCookie.js_output()) with data containing control characters. - Why exploitation is possible: These specific code paths did not enforce the same control-character validation as other
http.cookiesoperations, allowing crafted values to pass through and appear in outbound responses.
Why this happens
The core bug is an incomplete application of input and output validation rules across all related code paths, mapping primarily to:
- CWE-20: Improper Input Validation – some entry points that accept cookie data (
Morsel.update(),|=operator, unpickling) did not consistently reject control characters. - CWE-116: Improper Encoding or Escaping of Output –
BaseCookie.js_output()did not apply the same output validation or sanitization asBaseCookie.output(), leaving a gap where unsafe characters could appear in serialized cookie output.
Conceptually, the library enforced stricter validation in some high-visibility methods but failed to propagate these rules to logically equivalent pathways, resulting in a patch bypass for the earlier fix and an inconsistent security model within the same API surface.
Potential impact
- Injection of control characters into HTTP response headers via cookie values, which may:
- Facilitate HTTP response splitting in certain server or proxy configurations
- Corrupt or confuse HTTP header parsing by intermediaries or clients
- Manipulation of cookie-related JavaScript output via
BaseCookie.js_output(), potentially impacting: - Client-side parsing of cookie data
- Integrity of embedded script or data blocks if surrounding code is fragile
- Undermining of upstream security assumptions that all cookie-related paths are sanitized, possibly enabling subtle bypasses of existing defenses.
- Increased difficulty in log analysis or monitoring if logs capture unvalidated control characters from cookie data.
General mitigation guidance
Effective mitigation centers on consistent validation of cookie-related data and defensive encoding of output, independent of specific library patches:
- Treat all cookie values and attributes as untrusted input; explicitly normalize or strip control characters, line breaks, and other non-printable characters before constructing
MorselorBaseCookieobjects. - Apply centralized validation utilities for cookies to ensure every update or merge path (
update(),|=, and similar helpers) passes through the same sanitation logic. - Avoid deserializing (including unpickling) cookie-related objects from untrusted sources; instead, reconstruct cookies from validated primitive values.
- Prefer output routines that enforce strict validation and encoding; if using specialized outputs like
js_output(), wrap them with additional checks that reject or escape unsafe characters. - Enforce a principle of least privilege at the application boundary: constrain where cookie values can be influenced, and avoid overloading cookies with arbitrary or free-form text from users.
How teams detect issues like this
Detection typically combines code-centric and behavior-centric approaches:
- Static analysis and linters:
- Flag use of
Morsel.update()or the|=operator combined with untrusted data sources without intervening validation. - Identify calls to
BaseCookie.js_output()and ensure surrounding code applies sanitation or encoding consistent withBaseCookie.output(). - Detect any unpickling of
Morselor cookie-related structures, especially from external input channels. - Code review patterns:
- Look for cookie construction or mutation logic that directly consumes request parameters, headers, or payload fields.
- Verify that all code paths handling cookies share common helpers for validation and encoding, rather than open-coding string concatenation.
- Runtime monitoring and testing:
- Add tests that inject control characters into candidate cookie inputs and confirm they are rejected or properly encoded in responses.
- Use HTTP-level security testing tools to probe for response-splitting behavior, malformed headers, or unusual cookie-related response patterns.
- Policy enforcement:
- Adopt internal guidelines mandating centralized cookie handling utilities and banning ad-hoc serialization of cookies into JavaScript or other contexts.

