CVE-2026-42874
Improper sanitization in Microdot's set_cookie() allows header injection.
- CVSS 3.7
- CWE-113
- Input Validation and Sanitization
- Remote
Microdot is a minimalistic Python web framework. Prior to 2.6.1, the Response.set_cookie() method does not sanitize its string arguments, and in particular will not detect the presence of the \r\n sequence in them. This can be a potential source of header injection attacks. For a header injection attack through this issue to be possible, an attacker must first infiltrate the client (for example through an independent XSS attack), so that it can send malicious information that is destined to be stored in a cookie by the server on behalf of the victim. An attacker that infiltrates one client can only orchestrate a header injection attack for that client, all other clients that were not infiltrated are safe. This vulnerability is fixed in 2.6.1.
- CWE
- CWE-113
- CVSS base score
- 3.7
- Published
- 2026-05-11
- OWASP
- A03 Injection
- Orthogonal defect classification
- Checking
- Code defect classification
- Missing Check
- Category
- Input Validation and Sanitization
- Subcategory
- Open Redirects
- Accessibility scope
- Remote
- Impact
- Information Disclosure
- Affected component
- Microdot
- Fixed by upgrading
- Yes
Solution
Upgrade Microdot to version 2.6.1 or later.
Vulnerable code sample
from microdot import Microdot, Response
# This code represents a vulnerable application using a version of
# Microdot prior to 2.6.1, demonstrating CVE-2026-42874.
# The `Response.set_cookie()` method does not sanitize its arguments
# for newline characters, which can lead to HTTP header injection.
app = Microdot()
@app.route('/set-info')
def set_user_info(request):
"""
This endpoint takes a user-provided 'info' value from the query string
and sets it in a cookie. This simulates a scenario where an application
reflects user input into a cookie.
An attacker can exploit this by providing a crafted 'info' value
containing a CRLF sequence (\r\n) to inject a new header.
Example of a malicious request URL:
/set-info?info=some_value%0d%0aInjected-Header:%20malicious_content
"""
# Get the potentially malicious value from the request.
# An attacker must have a way to control this input for a target user.
user_info = request.args.get('info')
if not user_info:
return Response('Missing "info" parameter', status_code=400)
response = Response('Your info has been set.')
# In vulnerable versions (< 2.6.1), the `user_info` string is not
# sanitized. If it contains "\r\n", it will split the "Set-Cookie"
# header and inject a new, attacker-controlled header into the response.
response.set_cookie('user_info', user_info)
return responsePatched code sample
def set_cookie(self, cookie, value, max_age=None, expires=None, path='/',
domain=None, secure=False, http_only=False,
same_site=None):
if '\r' in cookie or '\n' in cookie or '\r' in value or \
'\n' in value:
raise ValueError('invalid cookie name or value')
cookie_val = f'{cookie}={value}'
if max_age is not None:
cookie_val += f'; Max-Age={max_age}'
if expires is not None:
cookie_val += f'; Expires={_format_date(expires)}'
if path is not None:
cookie_val += f'; Path={path}'
if domain is not None:
cookie_val += f'; Domain={domain}'
if secure:
cookie_val += '; Secure'
if http_only:
cookie_val += '; HttpOnly'
if same_site is not None:
cookie_val += f'; SameSite={same_site}'
self.headers.add('Set-Cookie', cookie_val)Payload
`legitimate_value\r\nSet-Cookie: session=attacker_session_id`
Cite this entry
@misc{vaitp:cve202642874,
title = {{Improper sanitization in Microdot's set_cookie() allows header injection.}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2026},
note = {VAITP Python Vulnerability Dataset, entry CVE-2026-42874},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2026-42874/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
