Skip to content

fix: ensure loose validation for isEvent and toEvent #394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2021
Merged
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
30 changes: 9 additions & 21 deletions src/message/http/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { CloudEvent, CloudEventV03, CloudEventV1, CONSTANTS, Mode, Version } from "../..";
import { Message, Headers } from "..";

import { headersFor, sanitize, v03structuredParsers, v1binaryParsers, v1structuredParsers } from "./headers";
import {
headersFor,
sanitize,
v03binaryParsers,
v03structuredParsers,
v1binaryParsers,
v1structuredParsers,
} from "./headers";
import { isStringOrObjectOrThrow, ValidationError } from "../../event/validation";
import { JSONParser, MappedParser, Parser, parserByContentType } from "../../parsers";

Expand Down Expand Up @@ -122,23 +129,12 @@ function parseBinary(message: Message, version: Version): CloudEvent {
let body = message.body;

if (!headers) throw new ValidationError("headers is null or undefined");
if (body) {
isStringOrObjectOrThrow(body, new ValidationError("payload must be an object or a string"));
}

if (
headers[CONSTANTS.CE_HEADERS.SPEC_VERSION] &&
headers[CONSTANTS.CE_HEADERS.SPEC_VERSION] !== Version.V03 &&
headers[CONSTANTS.CE_HEADERS.SPEC_VERSION] !== Version.V1
) {
throw new ValidationError(`invalid spec version ${headers[CONSTANTS.CE_HEADERS.SPEC_VERSION]}`);
}

// Clone and low case all headers names
const sanitizedHeaders = sanitize(headers);

const eventObj: { [key: string]: unknown | string | Record<string, unknown> } = {};
const parserMap: Record<string, MappedParser> = version === Version.V1 ? v1binaryParsers : v1binaryParsers;
const parserMap: Record<string, MappedParser> = version === Version.V1 ? v1binaryParsers : v03binaryParsers;

for (const header in parserMap) {
if (sanitizedHeaders[header]) {
Expand Down Expand Up @@ -186,14 +182,6 @@ function parseStructured(message: Message, version: Version): CloudEvent {
if (!headers) throw new ValidationError("headers is null or undefined");
isStringOrObjectOrThrow(payload, new ValidationError("payload must be an object or a string"));

if (
headers[CONSTANTS.CE_HEADERS.SPEC_VERSION] &&
headers[CONSTANTS.CE_HEADERS.SPEC_VERSION] != Version.V03 &&
headers[CONSTANTS.CE_HEADERS.SPEC_VERSION] != Version.V1
) {
throw new ValidationError(`invalid spec version ${headers[CONSTANTS.CE_HEADERS.SPEC_VERSION]}`);
}

// Clone and low case all headers names
const sanitizedHeaders = sanitize(headers);

Expand Down
26 changes: 26 additions & 0 deletions test/integration/message_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@ describe("HTTP transport", () => {
expect(HTTP.isEvent(message)).to.be.true;
});

it("Can detect CloudEvent binary Messages with weird versions", () => {
// Now create a message that is an event
const message = {
body: `{ "greeting": "hello" }`,
headers: {
[CONSTANTS.CE_HEADERS.ID]: "1234",
[CONSTANTS.CE_HEADERS.SOURCE]: "test",
[CONSTANTS.CE_HEADERS.TYPE]: "test.event",
[CONSTANTS.CE_HEADERS.SPEC_VERSION]: "11.8",
},
};
expect(HTTP.isEvent(message)).to.be.true;
expect(HTTP.toEvent(message)).not.to.throw;
});

it("Can detect CloudEvent structured Messages with weird versions", () => {
// Now create a message that is an event
const message = {
body: `{ "source": "test", "type": "test.event", "specversion": "11.8"}`,
headers: {
[CONSTANTS.CE_HEADERS.ID]: "1234",
},
};
expect(HTTP.isEvent(message)).to.be.true;
expect(HTTP.toEvent(message)).not.to.throw;
});
// Allow for external systems to send bad events - do what we can
// to accept them
it("Does not throw an exception when converting an invalid Message to a CloudEvent", () => {
Expand Down