Fix object injection issue

Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
Fabio José 2019-01-20 21:39:46 -02:00
parent b1c32064c0
commit d25cfc352a
2 changed files with 32 additions and 4 deletions

View File

@ -2,6 +2,18 @@ var uuid = require("uuid/v4");
var empty = require("is-empty"); var empty = require("is-empty");
var Ajv = require("ajv"); var Ajv = require("ajv");
// Reserved attributes names
const reserved = {
type: "type",
specversion: "specversion",
source: "source",
id: "id",
time: "time",
schemaurl: "schemaurl",
contenttype: "contenttype",
data: "data"
};
const schema = require("../../ext/spec_0_2.json"); const schema = require("../../ext/spec_0_2.json");
// Default options // Default options
@ -93,7 +105,11 @@ Spec02.prototype.getData = function() {
}; };
Spec02.prototype.addExtension = function(key, value){ Spec02.prototype.addExtension = function(key, value){
if(!reserved.hasOwnProperty(key)){
this.payload[key] = value; this.payload[key] = value;
} else {
throw {message: "Reserved attribute name: '" + key + "'"};
}
return this; return this;
}; };

View File

@ -9,7 +9,8 @@ const contenttype = "application/json";
const data = {}; const data = {};
const extensions = {}; const extensions = {};
var cloudevent = new Cloudevent(Cloudevent.specs["0.2"]) var cloudevent =
new Cloudevent(Cloudevent.specs["0.2"])
.type(type) .type(type)
.source(source); .source(source);
@ -65,6 +66,17 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
cloudevent.addExtension("extension2", "value2"); cloudevent.addExtension("extension2", "value2");
expect(cloudevent.format()["extension2"]).to.equal("value2"); expect(cloudevent.format()["extension2"]).to.equal("value2");
}); });
it("should throw an error when employ reserved name as extension", () => {
var cevt =
new Cloudevent(Cloudevent.specs["0.2"])
.type(type)
.source(source);
expect(cevt.addExtension.bind(cevt, "id"))
.to
.throw("Reserved attribute name: 'id'");
});
}); });
describe("The Constraints check", () => { describe("The Constraints check", () => {