Skip to content

Core HTTP decoder silently replaces invalid percent-encoded UTF-8 #307

Open

Description

@zhanghao0806

The core HTTP binding uses urllib.parse.unquote() with its default errors="replace". As a result, an invalid percent-encoded UTF-8 attribute such as ce-subject: %FF is accepted and silently changed to the replacement character.

The HTTP binding specification section 3.1.3.2 requires rejecting encoded byte sequences that are invalid UTF-8: https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/bindings/http-protocol-binding.md#3132-http-header-values

Reproduction on main:

from cloudevents.core.bindings.http import HTTPMessage, from_binary
from cloudevents.core.formats.json import JSONFormat

event = from_binary(HTTPMessage(
    headers={"ce-specversion":"1.0", "ce-id":"123", "ce-type":"test",
             "ce-source":"/example", "ce-subject":"%FF"},
    body=b"",
), JSONFormat())
print(repr(event.get_attributes()["subject"]))  # '\ufffd'

The same happens through from_http. This should raise a decoding error. Valid UTF-8, unnecessarily encoded ASCII, and literal plus signs should retain their existing behavior. This concerns validation in the existing decoder; it does not change which values the sender percent-encodes.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions