From 5c1765a4d70298748dabaafbf896ed8d6ce36e4f Mon Sep 17 00:00:00 2001 From: Space One Date: Thu, 26 Nov 2026 04:16:07 +0100 Subject: [PATCH 1/3] fix(header): prevent ReDoS in header value splitting Replace the nested, backtracking regular expressions used to split comma-delimited header elements and semicolon-delimited parameters with a linear quote-aware scanner. Honor escaped bytes inside quoted strings while preserving trimming, empty elements, and parameter parsing behavior. Fixes: GHSA-cwh3-92pm-wf66 --- httoop/header/element.py | 32 +++++++++++++++++++++++---- tests/headers/test_header_elements.py | 28 +++++++++++++++++++++++ tests/tc2231/test_tc2231.py | 1 - 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/httoop/header/element.py b/httoop/header/element.py index bd1ee415..0b3c6bbc 100644 --- a/httoop/header/element.py +++ b/httoop/header/element.py @@ -39,8 +39,8 @@ class HeaderElement: # noqa: PLW1641 # Regular expression that matches `special' characters in parameters, the # existence of which force quoting of the parameter value. RE_TSPECIALS = re.compile(rb'[ \(\)<>@,;:\\"/\[\]\?=]') - RE_SPLIT = re.compile(rb',(?=(?:[^"]*"[^"]*")*[^"]*$)') - RE_PARAMS = re.compile(rb';(?=(?:[^"]*"[^"]*")*[^"]*$)') + RE_SPLIT = None + RE_PARAMS = None def __init__(self, value: str, params: Any | None = None) -> None: self.value = value @@ -85,7 +85,8 @@ def parseparams(cls, elementstr: bytes) -> tuple[bytes, dict[bytes, str]] | tupl """Transform 'token;key=val' to ('token', {'key': 'val'}).""" # Split the element into a value and parameters. The 'value' may # be of the form, "token=token", but we don't split that here. - atoms = [x.strip() for x in cls.RE_PARAMS.split(elementstr) if x.strip()] or [b''] + atoms = cls._split_quoted(elementstr, ord(';')) if cls.RE_PARAMS is None else cls.RE_PARAMS.split(elementstr) + atoms = [x.strip() for x in atoms if x.strip()] or [b''] value = atoms.pop(0) params = (cls.parseparam(atom) for atom in atoms) @@ -180,7 +181,30 @@ def parse(cls, element: bytes) -> HeaderElement: @classmethod def split(cls, fieldvalue: bytes) -> list[bytes]: - return [x.strip() for x in cls.RE_SPLIT.split(fieldvalue)] + parts = cls._split_quoted(fieldvalue, ord(',')) if cls.RE_SPLIT is None else cls.RE_SPLIT.split(fieldvalue) + return [x.strip() for x in parts] + + @staticmethod + def _split_quoted(value: bytes, separator: int) -> list[bytes]: + """Split a byte string at separators outside quoted strings.""" + parts = [] + start = 0 + quoted = False + escaped = False + + for index, character in enumerate(value): + if escaped: + escaped = False + elif quoted and character == ord('\\'): + escaped = True + elif character == ord('"'): + quoted = not quoted + elif character == separator and not quoted: + parts.append(value[start:index]) + start = index + 1 + + parts.append(value[start:]) + return parts @classmethod def join(cls, values: list[bytes]) -> bytes: diff --git a/tests/headers/test_header_elements.py b/tests/headers/test_header_elements.py index 47e9b178..2886af76 100644 --- a/tests/headers/test_header_elements.py +++ b/tests/headers/test_header_elements.py @@ -3,6 +3,9 @@ import pytest from httoop.exceptions import InvalidHeader +from httoop.header.element import HeaderElement +from httoop.header.messaging import Cookie +from httoop.header.security import ContentSecurityPolicy def test_connection_close(headers): @@ -50,6 +53,31 @@ def test_xss_protection(headers): assert headers.get_element('X-XSS-Protection').block +@pytest.mark.parametrize('value, expected', [ + (b'a,b,c', [b'a', b'b', b'c']), + (b'a,"b,c",d', [b'a', b'"b,c"', b'd']), + (b'a,"b' + b'\\' + b'",c",d', [b'a', b'"b' + b'\\' + b'",c"', b'd']), + (b',,', [b'', b'', b'']), +]) +def test_split_header_elements(value, expected): + assert HeaderElement.split(value) == expected + + +def test_split_header_elements_large_separator_only_value(): + assert len(HeaderElement.split(b',' * 32000)) == 32001 + + +def test_parseparams_large_separator_only_value(): + value, params = HeaderElement.parseparams(b'X' + b';' * 32000) + assert value == b'X' + assert params == {} + + +def test_subclass_split_regexes_remain_supported(): + assert Cookie.split(b'a=1; b=2') == [b'a=1', b'b=2'] + assert ContentSecurityPolicy.split(b'default-src; script-src') == [b'default-src', b'script-src'] + + def test_strict_transport_security(headers): headers['Strict-Transport-Security'] = 'max-age=16070400; includeSubDomains' assert headers.get_element('Strict-Transport-Security').include_sub_domains diff --git a/tests/tc2231/test_tc2231.py b/tests/tc2231/test_tc2231.py index 9c79f77c..b06dff7c 100644 --- a/tests/tc2231/test_tc2231.py +++ b/tests/tc2231/test_tc2231.py @@ -123,7 +123,6 @@ def test_attwithfilenameandextparam(content_disposition): assert h.filename == 'foo.html' -@pytest.mark.xfail(raises=InvalidHeader, reason='HeaderElement.RE_PARAMS.split') def test_attwithfilenameandextparamescaped(content_disposition): h = content_disposition(b'Content-Disposition: attachment; foo="\\"\\\\";filename="foo.html"') assert h.attachment From cebf92b354a1d3fa01f8766ff8287ca1c0a16361 Mon Sep 17 00:00:00 2001 From: Space One Date: Thu, 26 Nov 2026 04:27:11 +0100 Subject: [PATCH 2/3] chore: publish 0.2.2 release --- debian/changelog | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/debian/changelog b/debian/changelog index 5f1d2072..faee27ef 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,9 +1,10 @@ -httoop (0.2.2) UNRELEASED; urgency=medium +httoop (0.2.2) unstable; urgency=medium + * prevent ReDoS in header value splitting * Drop support for Python 3.8 * Add compatibility with mypy and ty - -- Florian Best Wed, 09 Sep 2026 23:36:58 +0200 + -- Florian Best Thu, 26 Nov 2026 04:26:24 +0100 httoop (0.2.1) unstable; urgency=medium From 49a65d9a3ce4b5780f5cc750ed75fd8caf904d46 Mon Sep 17 00:00:00 2001 From: Space One Date: Thu, 26 Nov 2026 04:32:28 +0100 Subject: [PATCH 3/3] ci(github): pin ubuntu version for Python 3.9 --- .github/workflows/ci.yml | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8f92df41..e7e60a66 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,12 +8,16 @@ name: CI jobs: test: - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: - python-version: ["3.9", "3.10", "3.11", "3.12", "3.13", "3.14"] + os: [ubuntu-latest] + python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"] + include: + - os: ubuntu-24.04 + python-version: "3.9" steps: - uses: actions/checkout@v6 @@ -27,7 +31,6 @@ jobs: run: | python -m pip install --upgrade pip pip install -Ue . --group test - pip install -Ue . - name: Run tests run: | @@ -47,7 +50,7 @@ jobs: coveralls --service=github-actions lint: - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 strategy: matrix: