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

Ensure context.flow/global cannot be deleted or enumerated

This commit is contained in:
Nick O'Leary 2018-09-10 22:30:51 +01:00
parent fc0cf1ff51
commit 75e7c0e50d
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 111 additions and 85 deletions

View File

@ -221,93 +221,100 @@ function createContext(id,seed) {
} }
} }
} }
Object.defineProperties(obj, {
obj.get = function(key, storage, callback) { get: {
var context; value: function(key, storage, callback) {
if (!storage && !callback) { var context;
context = stores["_"]; if (!storage && !callback) {
} else { context = stores["_"];
if (typeof storage === 'function') { } else {
callback = storage; if (typeof storage === 'function') {
storage = "_"; callback = storage;
} storage = "_";
if (callback && typeof callback !== 'function'){
throw new Error("Callback must be a function");
}
context = getContextStorage(storage);
}
if (callback) {
if (!seed) {
context.get(scope,key,callback);
} else {
context.get(scope,key,function() {
if (arguments[0]) {
callback(arguments[0]);
return;
} }
var results = Array.prototype.slice.call(arguments,[1]); if (callback && typeof callback !== 'function'){
insertSeedValues(key,results); throw new Error("Callback must be a function");
// Put the err arg back }
results.unshift(undefined); context = getContextStorage(storage);
callback.apply(null,results); }
}); if (callback) {
} if (!seed) {
} else { context.get(scope,key,callback);
// No callback, attempt to do this synchronously } else {
var results = context.get(scope,key); context.get(scope,key,function() {
if (seed) { if (arguments[0]) {
if (Array.isArray(key)) { callback(arguments[0]);
insertSeedValues(key,results); return;
} else if (results === undefined){ }
results = util.getObjectProperty(seed,key); var results = Array.prototype.slice.call(arguments,[1]);
insertSeedValues(key,results);
// Put the err arg back
results.unshift(undefined);
callback.apply(null,results);
});
}
} else {
// No callback, attempt to do this synchronously
var results = context.get(scope,key);
if (seed) {
if (Array.isArray(key)) {
insertSeedValues(key,results);
} else if (results === undefined){
results = util.getObjectProperty(seed,key);
}
}
return results;
}
}
},
set: {
value: function(key, value, storage, callback) {
var context;
if (!storage && !callback) {
context = stores["_"];
} else {
if (typeof storage === 'function') {
callback = storage;
storage = "_";
}
if (callback && typeof callback !== 'function') {
throw new Error("Callback must be a function");
}
context = getContextStorage(storage);
}
context.set(scope, key, value, callback);
}
},
keys: {
value: function(storage, callback) {
var context;
if (!storage && !callback) {
context = stores["_"];
} else {
if (typeof storage === 'function') {
callback = storage;
storage = "_";
}
if (callback && typeof callback !== 'function') {
throw new Error("Callback must be a function");
}
context = getContextStorage(storage);
}
if (seed) {
if (callback) {
context.keys(scope, function(err,keys) {
callback(err,Array.from(new Set(seedKeys.concat(keys)).keys()));
});
} else {
var keys = context.keys(scope);
return Array.from(new Set(seedKeys.concat(keys)).keys())
}
} else {
return context.keys(scope, callback);
} }
} }
return results;
} }
}; });
obj.set = function(key, value, storage, callback) {
var context;
if (!storage && !callback) {
context = stores["_"];
} else {
if (typeof storage === 'function') {
callback = storage;
storage = "_";
}
if (callback && typeof callback !== 'function') {
throw new Error("Callback must be a function");
}
context = getContextStorage(storage);
}
context.set(scope, key, value, callback);
};
obj.keys = function(storage, callback) {
var context;
if (!storage && !callback) {
context = stores["_"];
} else {
if (typeof storage === 'function') {
callback = storage;
storage = "_";
}
if (callback && typeof callback !== 'function') {
throw new Error("Callback must be a function");
}
context = getContextStorage(storage);
}
if (seed) {
if (callback) {
context.keys(scope, function(err,keys) {
callback(err,Array.from(new Set(seedKeys.concat(keys)).keys()));
});
} else {
var keys = context.keys(scope);
return Array.from(new Set(seedKeys.concat(keys)).keys())
}
} else {
return context.keys(scope, callback);
}
};
return obj; return obj;
} }
@ -321,9 +328,13 @@ function getContext(localId,flowId) {
} }
var newContext = createContext(contextId); var newContext = createContext(contextId);
if (flowId) { if (flowId) {
newContext.flow = getContext(flowId); Object.defineProperty(newContext, 'flow', {
value: getContext(flowId)
});
} }
newContext.global = contexts['global']; Object.defineProperty(newContext, 'global', {
value: contexts['global']
})
contexts[contextId] = newContext; contexts[contextId] = newContext;
return newContext; return newContext;
} }

View File

@ -113,6 +113,21 @@ describe('context', function() {
context2.global.get("foo").should.equal("test"); context2.global.get("foo").should.equal("test");
}); });
it('context.flow/global are not enumerable', function() {
var context1 = Context.get("1","flowA");
Object.keys(context1).length.should.equal(0);
Object.keys(context1.flow).length.should.equal(0);
Object.keys(context1.global).length.should.equal(0);
})
it('context.flow/global cannot be deleted', function() {
var context1 = Context.get("1","flowA");
delete context1.flow;
should.exist(context1.flow);
delete context1.global;
should.exist(context1.global);
})
it('deletes context',function() { it('deletes context',function() {
var context = Context.get("1","flowA"); var context = Context.get("1","flowA");
should.not.exist(context.get("foo")); should.not.exist(context.get("foo"));