Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,7 +31,6 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -Ue . --group test
pip install -Ue .

- name: Run tests
run: |
Expand All @@ -47,7 +50,7 @@ jobs:
coveralls --service=github-actions

lint:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04

strategy:
matrix:
Expand Down
5 changes: 3 additions & 2 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -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 <space@wechall.net> Wed, 09 Sep 2026 23:36:58 +0200
-- Florian Best <space@wechall.net> Thu, 26 Nov 2026 04:26:24 +0100

httoop (0.2.1) unstable; urgency=medium

Expand Down
32 changes: 28 additions & 4 deletions httoop/header/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
28 changes: 28 additions & 0 deletions tests/headers/test_header_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion tests/tc2231/test_tc2231.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading