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 diff --git a/src/cloudevents/core/bindings/http.py b/src/cloudevents/core/bindings/http.py index e5d1b2be..ad45fc3a 100644 --- a/src/cloudevents/core/bindings/http.py +++ b/src/cloudevents/core/bindings/http.py @@ -55,17 +55,44 @@ 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. + + 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") + 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(