Skip to content
This repository was archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
(fix) Should use validated parameters for VM execution
Browse files Browse the repository at this point in the history
Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Mar 21, 2019
1 parent d5ecef5 commit 9416dfd
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions packages/ergo-engine/lib/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ class Engine {
});

// add immutables to the context
vm.freeze(contract, 'data'); // Second argument adds object to global.
vm.freeze(state, 'state'); // Second argument adds object to global.
vm.freeze(now, 'now'); // Second argument adds object to global.
vm.freeze(request, 'request'); // Second argument adds object to global.
vm.freeze(serializer.toJSON(validContract, {convertResourcesToRelationships: true}), 'data');
vm.freeze(serializer.toJSON(validState, {convertResourcesToRelationships: true}), 'state');
vm.freeze(now, 'now');
vm.freeze(serializer.toJSON(validRequest, {convertResourcesToRelationships: true}), 'request');

// execute the logic
vm.run(script);
Expand Down Expand Up @@ -195,11 +195,15 @@ class Engine {
validContract.validate();

// ensure the parameters are valid
let validParams = {};
for(const key in params) {
if (params[key] instanceof Object) {
const validParam = serializer.fromJSON(params[key], {validate: false, acceptResourcesForRelationships: true});
validParam.$validator = new ResourceValidator({permitResourcesForRelationships: true});
validParam.validate();
validParams[key] = serializer.toJSON(validParam, {convertResourcesToRelationships: true});
} else {
validParams[key] = params[key];
}
}

Expand Down Expand Up @@ -231,10 +235,10 @@ class Engine {
});

// add immutables to the context
vm.freeze(contract, 'data'); // Second argument adds object to global.
vm.freeze(state, 'state'); // Second argument adds object to global.
vm.freeze(now, 'now'); // Second argument adds object to global.
vm.freeze(params, 'params'); // Second argument adds object to global.
vm.freeze(serializer.toJSON(validContract, {convertResourcesToRelationships: true}), 'data');
vm.freeze(serializer.toJSON(validState, {convertResourcesToRelationships: true}), 'state');
vm.freeze(now, 'now');
vm.freeze(validParams, 'params');

// execute the logic
vm.run(script);
Expand Down Expand Up @@ -320,8 +324,8 @@ class Engine {
});

// add immutables to the context
vm.freeze(contract, 'data'); // Second argument adds object to global.
vm.freeze(now, 'now'); // Second argument adds object to global.
vm.freeze(serializer.toJSON(validContract, {convertResourcesToRelationships: true}), 'data');
vm.freeze(now, 'now');

// execute the logic
//console.log(script);
Expand Down

0 comments on commit 9416dfd

Please sign in to comment.