From 62cb18665702b99e85865426e73fd45fa2350e0a Mon Sep 17 00:00:00 2001 From: Lance Ball Date: Fri, 21 May 2021 11:54:54 -0400 Subject: [PATCH] fix: do not modify incoming event's specversion Even if the specversion is totally invalid, we should not change the value received in an incoming `Message`. Previously we defaulted to 1.0 if we did not recognize the version number. This commit changes that, leaving the value unmodified. We default to parsing this mystery event with the 1.0 spec. When the event is validated with `event.validate()` we return `false`. One additional small change to eliminate a prettier warning about `parer` being previously declared. Fixes: https://github.com/cloudevents/sdk-javascript/issues/332 Fixes: https://github.com/cloudevents/sdk-javascript/issues/333 Signed-off-by: Lance Ball --- src/message/http/index.ts | 15 ++++++--------- test/integration/message_test.ts | 4 +++- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/message/http/index.ts b/src/message/http/index.ts index ad6356f6..0dc2e022 100644 --- a/src/message/http/index.ts +++ b/src/message/http/index.ts @@ -87,11 +87,8 @@ export function isEvent(message: Message): boolean { export function deserialize(message: Message): CloudEvent { const cleanHeaders: Headers = sanitize(message.headers); const mode: Mode = getMode(cleanHeaders); - let version = getVersion(mode, cleanHeaders, message.body); - if (version !== Version.V03 && version !== Version.V1) { - console.error(`Unknown spec version ${version}. Default to ${Version.V1}`); - version = Version.V1; - } + const version = getVersion(mode, cleanHeaders, message.body); + switch (mode) { case Mode.BINARY: return parseBinary(message, version); @@ -160,7 +157,7 @@ function parseBinary(message: Message, version: Version): CloudEvent { const sanitizedHeaders = sanitize(headers); const eventObj: { [key: string]: unknown | string | Record } = {}; - const parserMap: Record = version === Version.V1 ? v1binaryParsers : v03binaryParsers; + const parserMap: Record = version === Version.V03 ? v03binaryParsers : v1binaryParsers; for (const header in parserMap) { if (sanitizedHeaders[header]) { @@ -217,13 +214,13 @@ function parseStructured(message: Message, version: Version): CloudEvent { const incoming = { ...(parser.parse(payload as string) as Record) }; const eventObj: { [key: string]: unknown } = {}; - const parserMap: Record = version === Version.V1 ? v1structuredParsers : v03structuredParsers; + const parserMap: Record = version === Version.V03 ? v03structuredParsers : v1structuredParsers; for (const key in parserMap) { const property = incoming[key]; if (property) { - const parser: MappedParser = parserMap[key]; - eventObj[parser.name] = parser.parser.parse(property as string); + const mappedParser: MappedParser = parserMap[key]; + eventObj[mappedParser.name] = mappedParser.parser.parse(property as string); } delete incoming[key]; } diff --git a/test/integration/message_test.ts b/test/integration/message_test.ts index 85169de6..610604e6 100644 --- a/test/integration/message_test.ts +++ b/test/integration/message_test.ts @@ -76,7 +76,9 @@ describe("HTTP transport", () => { }, }; expect(HTTP.isEvent(message)).to.be.true; - expect(HTTP.toEvent(message)).not.to.throw; + const event: CloudEvent = HTTP.toEvent(message); + expect(event.specversion).to.equal("11.8"); + expect(event.validate()).to.be.false; }); it("Can detect CloudEvent structured Messages with weird versions", () => {