Skip to content
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

lib: validate extension values #251

Merged
merged 5 commits into from
Jul 27, 2020
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
9 changes: 8 additions & 1 deletion src/event/cloudevent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
CloudEventV1OptionalAttributes,
} from "./interfaces";
import { validateV1, validateV03 } from "./spec";
import { ValidationError, isBinary, asBase64 } from "./validation";
import { ValidationError, isBinary, asBase64, isValidType } from "./validation";
import CONSTANTS from "../constants";
import { isString } from "util";

Expand Down Expand Up @@ -108,6 +108,13 @@ export class CloudEvent implements CloudEventV1, CloudEventV03 {
if (!key.match(/^[a-z0-9]{1,20}$/)) {
throw new ValidationError("invalid extension name");
}

// Value should be spec compliant
// https://github.com/cloudevents/spec/blob/master/spec.md#type-system
if (!isValidType(value)) {
throw new ValidationError("invalid extension value");
}

this[key] = value;
}

Expand Down
4 changes: 2 additions & 2 deletions src/event/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ export const asData = (data: unknown, contentType: string): string => {
return isBinary(maybeJson) ? asBase64(maybeJson) : maybeJson;
};

export const isValidType = (v: boolean | number | string | Date | Uint32Array): boolean =>
isBoolean(v) || isInteger(v) || isString(v) || isDate(v) || isBinary(v);
export const isValidType = (v: boolean | number | string | Date | Uint32Array | unknown): boolean =>
isBoolean(v) || isInteger(v) || isString(v) || isDate(v) || isBinary(v) || isObject(v);
6 changes: 6 additions & 0 deletions test/integration/http_emitter_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const ext1Name = "lunch";
const ext1Value = "tacos";
const ext2Name = "supper";
const ext2Value = "sushi";
const ext3Name = "snack";
const ext3Value = { value: "chips" };

const data = {
lunchBreak: "noon",
Expand Down Expand Up @@ -45,6 +47,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
data,
[ext1Name]: ext1Value,
[ext2Name]: ext2Value,
[ext3Name]: ext3Value,
});

it("Sends a binary 1.0 CloudEvent by default", () => {
Expand All @@ -59,6 +62,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
// Ensure extensions are handled properly
expect(response.data[`${CONSTANTS.EXTENSIONS_PREFIX}${ext1Name}`]).to.equal(ext1Value);
expect(response.data[`${CONSTANTS.EXTENSIONS_PREFIX}${ext2Name}`]).to.equal(ext2Value);
expect(response.data[`${CONSTANTS.EXTENSIONS_PREFIX}${ext3Name}`].value).to.equal(ext3Value.value);
})
.catch(expect.fail);
});
Expand Down Expand Up @@ -142,6 +146,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
data,
[ext1Name]: ext1Value,
[ext2Name]: ext2Value,
[ext3Name]: ext3Value,
});

it("Sends a binary 0.3 CloudEvent", () => {
Expand All @@ -156,6 +161,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
// Ensure extensions are handled properly
expect(response.data[`${CONSTANTS.EXTENSIONS_PREFIX}${ext1Name}`]).to.equal(ext1Value);
expect(response.data[`${CONSTANTS.EXTENSIONS_PREFIX}${ext2Name}`]).to.equal(ext2Value);
expect(response.data[`${CONSTANTS.EXTENSIONS_PREFIX}${ext3Name}`].value).to.equal(ext3Value.value);
})
.catch(expect.fail);
});
Expand Down
8 changes: 4 additions & 4 deletions test/integration/spec_1_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ describe("CloudEvents Spec v1.0", () => {
expect(cloudevent.cloneWith({ extdate: myDate }).validate()).to.equal(true);
});

// even though the spec doesn't allow object types for
// extensions, it could be JSON. And before a JS CE
// is transmitted across the wire, this value will be
// converted to JSON
it("should be ok when the type is an object", () => {
expect(cloudevent.cloneWith({ objectextension: { some: "object" } }).validate()).to.equal(true);
});

it("should be ok when the type is an string converted from an object", () => {
expect(cloudevent.cloneWith({ objectextension: JSON.stringify({ some: "object" }) }).validate()).to.equal(true);
});
});

describe("The Constraints check", () => {
Expand Down