8000 fix: ensure that the HTTP receiver sanitizes headers in accept() (#239) · lholmquist/sdk-javascript@51035dc · GitHub
[go: up one dir, main page]

Skip to content

Commit 51035dc

Browse files
authored
fix: ensure that the HTTP receiver sanitizes headers in accept() (cloudevents#239)
Even though the underlying structured and binary receivers already sanitize the headers, this needs to be done at the receiver.accept() level since the headers are inspected there to determine what mode the event is being sent as. Signed-off-by: Lance Ball <lball@redhat.com>
1 parent d65b013 commit 51035dc

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/transport/receiver.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Headers } from "./http/headers";
1+
import { Headers, sanitize } from "./http/headers";
22
import { CloudEvent, Version, ValidationError } from "..";
33
import { BinaryHTTPReceiver as BinaryReceiver } from "./http/binary_receiver";
44
import { StructuredHTTPReceiver as StructuredReceiver } from "./http/structured_receiver";
@@ -60,8 +60,9 @@ export class Receiver {
6060
* @return {CloudEvent} A new {CloudEvent} instance
6161
*/
6262
accept(headers: Headers, body: string | Record<string, unknown> | CloudEventV1 | CloudEventV03): CloudEvent {
63-
const mode: Mode = getMode(headers);
64-
const version = getVersion(mode, headers, body);
63+
const cleanHeaders: Headers = sanitize(headers);
64+
const mode: Mode = getMode(cleanHeaders);
65+
const version = getVersion(mode, cleanHeaders, body);
6566
switch (version) {
6667
case Version.V1:
6768
return this.receivers.v1[mode].parse(body, headers);

test/http_receiver_test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,35 @@ describe("HTTP Transport Binding Receiver for CloudEvents", () => {
5252
expect(typeof event.data).to.equal("object");
5353
expect((event.data as Record<string, string>).lunch).to.equal("sushi");
5454
});
55+
56+
it("Recognizes headers in title case for binary events", () => {
57+
const binaryHeaders = {
58+
"Content-Type": "application/json; charset=utf-8",
59+
"ce-specversion": specversion,
60+
"ce-id": id,
61+
"ce-type": type,
62+
"ce-source": source,
63+
};
64+
65+
const event: CloudEvent = receiver.accept(binaryHeaders, data);
66+
expect(event.validate()).to.be.true;
67+
expect((event.data as Record<string, string>).lunch).to.equal("sushi");
68+
});
69+
70+
it("Recognizes headers in title case for structured events", () => {
71+
const structuredHeaders = { "Content-Type": "application/cloudevents+json" };
72+
const payload = {
73+
id,
74+
type,
75+
source,
76+
data,
77+
specversion,
78+
};
79+
80+
const event: CloudEvent = receiver.accept(structuredHeaders, payload);
81+
expect(event.validate()).to.be.true;
82+
expect((event.data as Record<string, string>).lunch).to.equal("sushi");
83+
});
5584
});
5685

5786
describe("V1", () => {

0 commit comments

Comments
 (0)
0