Skip to content
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
31 changes: 29 additions & 2 deletions src/cloudevents/core/bindings/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions tests/test_core/test_bindings/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down