From 47f1b689c2974a01e346cd3252c0706032459cf3 Mon Sep 17 00:00:00 2001 From: emir <52245396+EmiRZx@users.noreply.github.com> Date: Tue, 1 Dec 2026 00:08:32 +0300 Subject: [PATCH 1/3] fix(http): unescape quoted header values before percent-decoding HTTP binding 3.1.3.2 requires double-quoted strings in header values to be unescaped before percent-decoding. The core decoder kept the quotes, and a quoted ce-specversion was rejected. Signed-off-by: emir <52245396+EmiRZx@users.noreply.github.com> --- src/cloudevents/core/bindings/http.py | 33 +++++++++++++++- tests/test_core/test_bindings/test_http.py | 46 ++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/cloudevents/core/bindings/http.py b/src/cloudevents/core/bindings/http.py index e5d1b2be..8f4b0179 100644 --- a/src/cloudevents/core/bindings/http.py +++ b/src/cloudevents/core/bindings/http.py @@ -55,17 +55,46 @@ def _encode_header_value(value: Any) -> str: return quote(str(value), safe=_CE_SAFE_CHARS) +def _unescape_header_value(value: str) -> str: + """ + Undo HTTP quoting of a header value, as required before percent-decoding + (HTTP binding section 3.1.3.2). + + Surrounding whitespace is trimmed and double quotes that group text are + removed, honouring backslash escapes inside them. + + :param value: The raw header value + :return: The header value without HTTP quoting + """ + value = value.strip(" \t") + if '"' not in value: + return value + + unescaped = [] + in_quotes = False + chars = iter(value) + for char in chars: + if char == '"': + in_quotes = not in_quotes + elif char == "\\" and in_quotes: + unescaped.append(next(chars, "")) + else: + unescaped.append(char) + return "".join(unescaped) + + def _decode_header_value(attr_name: str, value: str) -> Any: """ Decode a CloudEvent attribute value from an HTTP header. - Applies percent-decoding and parses the 'time' attribute as datetime. + Removes HTTP quoting, applies percent-decoding and parses the 'time' + attribute as datetime. :param attr_name: The name of the CloudEvent attribute :param value: The percent-encoded header value :return: Decoded value (datetime for 'time' attribute, string otherwise) """ - decoded = unquote(value) + decoded = unquote(_unescape_header_value(value)) if attr_name == TIME_ATTR: return isoparse(decoded) return decoded diff --git a/tests/test_core/test_bindings/test_http.py b/tests/test_core/test_bindings/test_http.py index cf46ba4d..8a7956d5 100644 --- a/tests/test_core/test_bindings/test_http.py +++ b/tests/test_core/test_bindings/test_http.py @@ -429,6 +429,52 @@ def test_from_binary_header_decoding() -> None: assert event.get_subject() == "Hello World!" +@pytest.mark.parametrize( + "header_value,expected", + [ + ('"word1 word2"', "word1 word2"), + ('"say \\"hi\\""', 'say "hi"'), + ('"a%20b"', "a b"), + (" padded ", "padded"), + ("\tpadded\t", "padded"), + ], +) +@pytest.mark.parametrize("reader", [from_binary, from_http]) +def test_from_binary_unescapes_quoted_header_values( + reader: Any, header_value: str, expected: str +) -> None: + """Test that HTTP quoting is removed before percent-decoding""" + message = HTTPMessage( + headers={ + "ce-type": "com.example.test", + "ce-source": "/test", + "ce-id": "test-123", + "ce-specversion": "1.0", + "ce-subject": header_value, + }, + body=b"", + ) + event = reader(message, JSONFormat(), CloudEvent) + + assert event.get_subject() == expected + + +def test_from_binary_quoted_specversion() -> None: + """Test that a quoted ce-specversion is accepted""" + message = HTTPMessage( + headers={ + "ce-type": "com.example.test", + "ce-source": "/test", + "ce-id": "test-123", + "ce-specversion": '"1.0"', + }, + body=b"", + ) + event = from_binary(message, JSONFormat()) + + assert event.get_specversion() == "1.0" + + def test_from_binary_case_insensitive_headers() -> None: """Test that header parsing is case-insensitive""" message = HTTPMessage( From 861e44c20c370100ce3b0f5ae426aee604ed354e Mon Sep 17 00:00:00 2001 From: emir <52245396+EmiRZx@users.noreply.github.com> Date: Tue, 1 Dec 2026 17:51:58 +0300 Subject: [PATCH 2/3] docs: add changelog entry for quoted header values Signed-off-by: emir <52245396+EmiRZx@users.noreply.github.com> --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa654e2a..7af575ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Fixed HTTP binary mode decoding of `ce-` header values that use HTTP quoted-string + syntax (RFC 9110); they are now unescaped before percent-decoding. ([#333]) + ## [2.2.0] ### Changed @@ -368,3 +373,4 @@ CloudEvents v2 is a rewrite with ongoing development ([#271]) [#279]: https://github.com/cloudevents/sdk-python/pull/279 [#284]: https://github.com/cloudevents/sdk-python/pull/284 [#291]: https://github.com/cloudevents/sdk-python/pull/291 +[#333]: https://github.com/cloudevents/sdk-python/pull/333 From c7fa474a56a660c40695b46cafca7037b2849c93 Mon Sep 17 00:00:00 2001 From: emir <52245396+EmiRZx@users.noreply.github.com> Date: Wed, 2 Dec 2026 12:46:18 +0300 Subject: [PATCH 3/3] fix(http): link the spec section and drop the redundant quote check Signed-off-by: emir <52245396+EmiRZx@users.noreply.github.com> --- src/cloudevents/core/bindings/http.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/cloudevents/core/bindings/http.py b/src/cloudevents/core/bindings/http.py index 8f4b0179..ad45fc3a 100644 --- a/src/cloudevents/core/bindings/http.py +++ b/src/cloudevents/core/bindings/http.py @@ -57,19 +57,17 @@ def _encode_header_value(value: Any) -> str: def _unescape_header_value(value: str) -> str: """ - Undo HTTP quoting of a header value, as required before percent-decoding - (HTTP binding section 3.1.3.2). + Undo HTTP quoting of a header value, as required before percent-decoding. Surrounding whitespace is trimmed and double quotes that group text are removed, honouring backslash escapes inside them. + See https://github.com/cloudevents/spec/blob/main/cloudevents/bindings/http-protocol-binding.md#3132-http-header-values + :param value: The raw header value :return: The header value without HTTP quoting """ value = value.strip(" \t") - if '"' not in value: - return value - unescaped = [] in_quotes = False chars = iter(value)