fix: do not assume an empty content-type header is JSON (#444)

The parser for HTTP binary made the assumption that if there was no `content-type`
header in the incoming message, it should inject `application/json`. Discussion
about the rationale for this is in https://github.com/cloudevents/sdk-javascript/issues/441.

This commit, removes that injection and adds a test to ensure the bytes are
simply not parsed, but just passed along untouched.

Fixes: https://github.com/cloudevents/sdk-javascript/issues/441

Signed-off-by: Lance Ball <lball@redhat.com>
This commit is contained in:
Lance Ball 2021-11-24 11:02:50 -05:00 committed by GitHub
parent b4266b1f37
commit 52ea7de80d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14638 additions and 34 deletions

14652
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -65,11 +65,6 @@ export function sanitize(headers: Headers): Headers {
.filter((header) => Object.hasOwnProperty.call(headers, header))
.forEach((header) => (sanitized[header.toLowerCase()] = headers[header]));
// If no content-type header is sent, assume application/json
if (!sanitized[CONSTANTS.HEADER_CONTENT_TYPE]) {
sanitized[CONSTANTS.HEADER_CONTENT_TYPE] = CONSTANTS.MIME_JSON;
}
return sanitized;
}

View File

@ -41,6 +41,21 @@ const imageData = new Uint32Array(fs.readFileSync(path.join(process.cwd(), "test
const image_base64 = asBase64(imageData);
describe("HTTP transport", () => {
it("Handles events with no content-type and no datacontenttype", () => {
const body = "{Something[Not:valid}JSON";
const message: Message = {
body,
headers: {
"ce-source": "/test/type",
"ce-type": "test.type",
"ce-id": "1234",
},
};
const event: CloudEvent = HTTP.toEvent(message);
expect(event.data).to.equal(body);
expect(event.datacontentype).to.equal(undefined);
});
it("Can detect invalid CloudEvent Messages", () => {
// Create a message that is not an actual event
const message: Message = {