fix: do not modify incoming event's specversion (#419)

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 <lball@redhat.com>
This commit is contained in:
Lance Ball 2021-05-25 11:10:20 -04:00 committed by GitHub
parent 7f6b658858
commit 7c05adee7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 10 deletions

View File

@ -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<string, unknown> } = {};
const parserMap: Record<string, MappedParser> = version === Version.V1 ? v1binaryParsers : v03binaryParsers;
const parserMap: Record<string, MappedParser> = version === Version.V03 ? v03binaryParsers : v1binaryParsers;
for (const header in parserMap) {
if (sanitizedHeaders[header]) {
@ -218,13 +215,13 @@ function parseStructured(message: Message, version: Version): CloudEvent {
const incoming = { ...(parser.parse(payload as string) as Record<string, unknown>) };
const eventObj: { [key: string]: unknown } = {};
const parserMap: Record<string, MappedParser> = version === Version.V1 ? v1structuredParsers : v03structuredParsers;
const parserMap: Record<string, MappedParser> = 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];
}

View File

@ -96,7 +96,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", () => {