1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Merge pull request #2993 from Steve-Mcl/master

ensure context get/set key is a string
This commit is contained in:
Nick O'Leary 2021-06-02 15:20:25 +01:00 committed by GitHub
commit 711794cfe1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 181 additions and 4 deletions

View File

@ -215,6 +215,22 @@ function followParentContext(parent, key) {
return null; return null;
} }
function validateContextKey(key) {
try {
const keys = Array.isArray(key) ? key : [key];
if(!keys.length) { return false }; //no key to get/set
for (let index = 0; index < keys.length; index++) {
const k = keys[index];
if (typeof k !== "string" || !k.length) {
return false; //not string or zero-length
}
}
} catch (error) {
return false;
}
return true;
}
function createContext(id,seed,parent) { function createContext(id,seed,parent) {
// Seed is only set for global context - sourced from functionGlobalContext // Seed is only set for global context - sourced from functionGlobalContext
var scope = id; var scope = id;
@ -251,11 +267,11 @@ function createContext(id,seed,parent) {
} }
} }
} }
Object.defineProperties(obj, { Object.defineProperties(obj, {
get: { get: {
value: function(key, storage, callback) { value: function(key, storage, callback) {
var context; var context;
if (!callback && typeof storage === 'function') { if (!callback && typeof storage === 'function') {
callback = storage; callback = storage;
storage = undefined; storage = undefined;
@ -263,7 +279,14 @@ function createContext(id,seed,parent) {
if (callback && typeof callback !== 'function'){ if (callback && typeof callback !== 'function'){
throw new Error("Callback must be a function"); throw new Error("Callback must be a function");
} }
if (!validateContextKey(key)) {
var err = Error("Invalid context key");
if(callback) {
return callback(err);
} else {
throw err;
}
}
if (!Array.isArray(key)) { if (!Array.isArray(key)) {
var keyParts = util.parseContextStore(key); var keyParts = util.parseContextStore(key);
key = keyParts.key; key = keyParts.key;
@ -337,7 +360,6 @@ function createContext(id,seed,parent) {
set: { set: {
value: function(key, value, storage, callback) { value: function(key, value, storage, callback) {
var context; var context;
if (!callback && typeof storage === 'function') { if (!callback && typeof storage === 'function') {
callback = storage; callback = storage;
storage = undefined; storage = undefined;
@ -345,7 +367,14 @@ function createContext(id,seed,parent) {
if (callback && typeof callback !== 'function'){ if (callback && typeof callback !== 'function'){
throw new Error("Callback must be a function"); throw new Error("Callback must be a function");
} }
if (!validateContextKey(key)) {
var err = Error("Invalid context key");
if(callback) {
return callback(err);
} else {
throw err;
}
}
if (!Array.isArray(key)) { if (!Array.isArray(key)) {
var keyParts = util.parseContextStore(key); var keyParts = util.parseContextStore(key);
key = keyParts.key; key = keyParts.key;

View File

@ -1006,7 +1006,155 @@ describe('context', function() {
done(); done();
}).catch(done); }).catch(done);
}); });
it('should throw an error in context.get if key is empty string', function (done) {
Context.init({ contextStorage: memoryStorage });
Context.load().then(function () {
var context = Context.get("1", "flow");
context.get("");
done("should throw an error.");
}).catch(function () {
done();
});
});
it('should throw an error in context.get if key is an object', function (done) {
Context.init({ contextStorage: memoryStorage });
Context.load().then(function () {
var context = Context.get("1", "flow");
context.get({});
done("should throw an error.");
}).catch(function () {
done();
});
});
it('should throw an error in context.get if key is a number', function (done) {
Context.init({ contextStorage: memoryStorage });
Context.load().then(function () {
var context = Context.get("1", "flow");
context.get(1);
done("should throw an error.");
}).catch(function () {
done();
});
});
it('should throw an error in context.get if key array contains an empty string', function (done) {
Context.init({ contextStorage: memoryStorage });
Context.load().then(function () {
var context = Context.get("1", "flow");
context.get(["ok1", "", "ok2"]);
done("should throw an error.");
}).catch(function () {
done();
});
});
it('should throw an error in context.get if key array contains an object', function (done) {
Context.init({ contextStorage: memoryStorage });
Context.load().then(function () {
var context = Context.get("1", "flow");
context.get(["ok1", {}, "ok2"]);
done("should throw an error.");
}).catch(function () {
done();
});
});
it('should throw an error in context.get if key array contains a number', function (done) {
Context.init({ contextStorage: memoryStorage });
Context.load().then(function () {
var context = Context.get("1", "flow");
context.get(["ok1", 1, "ok2"]);
done("should throw an error.");
}).catch(function () {
done();
});
});
it('should throw an error in context.set if key is empty string', function (done) {
Context.init({ contextStorage: memoryStorage });
Context.load().then(function () {
var context = Context.get("1", "flow");
context.set("", 1);
done("should throw an error.");
}).catch(function () {
done();
});
});
it('should throw an error in context.set if key is an object', function (done) {
Context.init({ contextStorage: memoryStorage });
Context.load().then(function () {
var context = Context.get("1", "flow");
context.set({}, 1);
done("should throw an error.");
}).catch(function () {
done();
});
});
it('should throw an error in context.set if key is a number', function (done) {
Context.init({ contextStorage: memoryStorage });
Context.load().then(function () {
var context = Context.get("1", "flow");
context.set(1, 1);
done("should throw an error.");
}).catch(function () {
done();
});
});
it('should throw an error in context.set if key array contains an empty string', function (done) {
Context.init({ contextStorage: memoryStorage });
Context.load().then(function () {
var context = Context.get("1", "flow");
context.set(["ok1", "", "ok2"], 1);
done("should throw an error.");
}).catch(function () {
done();
});
});
it('should throw an error in context.set if key array contains an object', function (done) {
Context.init({ contextStorage: memoryStorage });
Context.load().then(function () {
var context = Context.get("1", "flow");
context.set(["ok1", {}, "ok2"], 1);
done("should throw an error.");
}).catch(function () {
done();
});
});
it('should throw an error in context.set if key array contains a number', function (done) {
Context.init({ contextStorage: memoryStorage });
Context.load().then(function () {
var context = Context.get("1", "flow");
context.set(["ok1", 1, "ok2"], 1);
done("should throw an error.");
}).catch(function () {
done();
});
});
it('should have an err set in callback for invalid key in context.get', function (done) {
Context.init({ contextStorage: memoryStorage });
Context.load().then(function () {
var context = Context.get("1", "flow");
context.get("", function(err) {
if(err) {
done();
} else {
done("should throw an error.");
}
});
}).catch(done);
});
it('should have an err set in callback for invalid key in context.set', function (done) {
Context.init({ contextStorage: memoryStorage });
Context.load().then(function () {
var context = Context.get("1", "flow");
context.set("", "value", function(err) {
if(err) {
done();
} else {
done("should throw an error.");
}
});
}).catch(done);
});
}); });
describe('listStores', function () { describe('listStores', function () {