Severity:

HIGH RISK

(7.5)

Incomplete Input and Output Validation (CWE-20, CWE-116) in Python http.cookies Morsel & BaseCookie

CVE-2026-3644

Modified

Vulnerability type

Category:

Input and Output Validation Error

CWE:

CWE-20, CWE-116

Impact:

HTTP Response Manipulation / Header Injection

Severity:

HIGH RISK

(7.5)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

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 Morsel objects
  • 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.Morsel to build or update cookies
  • Use Morsel.update() or |= to incorporate untrusted input into cookie attributes or values
  • Deserialize (unpickle) Morsel instances 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, or BaseCookie.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.cookies operations, 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 as BaseCookie.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 Morsel or BaseCookie objects.
  • 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 with BaseCookie.output().
  • Detect any unpickling of Morsel or 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.

Sources

SourceLink
github.comgithub.com/python/cpython/commit/3974092b037f9a3b000fb15b48ea61ce3b25d330
github.comgithub.com/python/cpython/commit/556aa098e738b127c714866f819b4abe2f7593d8
github.comgithub.com/python/cpython/commit/57e88c1cf95e1481b94ae57abe1010469d47a6b4
github.comgithub.com/python/cpython/commit/62ceb396fcbe69da1ded3702de586f4072b590dd
github.comgithub.com/python/cpython/commit/d16ecc6c3626f0e2cc8f08c309c83934e8a979dd
github.comgithub.com/python/cpython/commit/dae4b1a21f8df4570e30986affd61bbe4ade4cef
github.comgithub.com/python/cpython/issues/145599
github.comgithub.com/python/cpython/pull/145600
mail.python.orgmail.python.org/archives/list/security-announce@python.org/thread/H6CADMBCDRFGWCMOXWUIHFJNV43GABJ7/

Frequently asked

questions

Yes, in many deployments. If a web application builds or updates cookies using user-influenced values through the affected http.cookies paths, a remote attacker may be able to inject control characters into responses by submitting crafted requests.

Not necessarily. Exploitation depends on where cookie values or attributes come from. If unauthenticated users can control the data that flows into Morsel.update(), |=, unpickling, or BaseCookie.js_output(), then the issue can be triggered without authentication. If only authenticated users can influence these paths, the risk is limited to those users.

Introduce a single, well-reviewed validation and encoding layer for all cookie handling. Normalize and sanitize any data before it becomes part of a Morsel or BaseCookie object, ensure no untrusted pickled data is used, and prefer output methods that enforce strict header-safe and JavaScript-safe encoding.

Design libraries and applications so that security checks are centralized and reused by all relevant code paths. During development, require tests that cover bypass scenarios (alternate methods, operators, and serialization helpers), adopt static analysis rules focused on input and output boundaries, and treat cookies as a sensitive channel on par with headers and query parameters when designing validation strategies.

Find out what's already
exploitable in your codebase.