Only add appsmith functions in global scope during trigger evaluation (#5760)

This commit is contained in:
Hetu Nandu 2021-07-13 18:27:59 +05:30 committed by hetunandu
parent 3cc145cd55
commit da67128e80
3 changed files with 38 additions and 26 deletions

View File

@ -97,7 +97,7 @@ export default {
//
getTableColumns: (props, moment, _) => {
let columns = [];
let allColumns = props.primaryColumns || {};
let allColumns = Object.assign({}, props.primaryColumns || {});
const data = props.sanitizedTableData || [];
if (data.length > 0) {
const columnIdsFromData = [];
@ -171,7 +171,7 @@ export default {
}
const allColumnProperties = Object.values(allColumns);
for (let index = 0; index < allColumnProperties.length; index++) {
const columnProperties = allColumnProperties[index];
const columnProperties = { ...allColumnProperties[index] };
columnProperties.isAscOrder =
columnProperties.id === sortColumn ? sortOrder : undefined;
const columnData = columnProperties;

View File

@ -82,7 +82,7 @@ describe("evaluate", () => {
});
it("gets triggers from a function", () => {
const js = "showAlert('message', 'info')";
const response = evaluate(js, dataTree);
const response = evaluate(js, dataTree, undefined, true);
expect(response.result).toBe(undefined);
expect(response.triggers).toStrictEqual([
{

View File

@ -94,7 +94,6 @@ export default function evaluate(
// so that eval can happen
const unescapedJS = unescapeJS(js.replace(beginsWithLineBreakRegex, ""));
const script = getScriptToEval(unescapedJS, evalArguments, isTriggerBased);
return (function() {
let errors: EvaluationError[] = [];
let result;
@ -103,25 +102,36 @@ export default function evaluate(
const GLOBAL_DATA: Record<string, any> = {};
///// Adding callback data
GLOBAL_DATA.ARGUMENTS = evalArguments;
//// Add internal functions to dataTree;
const dataTreeWithFunctions = addFunctions(data);
///// Adding Data tree
Object.keys(dataTreeWithFunctions).forEach((datum) => {
GLOBAL_DATA[datum] = dataTreeWithFunctions[datum];
});
///// Fixing action paths and capturing their execution response
if (dataTreeWithFunctions.actionPaths) {
GLOBAL_DATA.triggers = [];
const pusher = function(this: DataTree, action: any, ...payload: any[]) {
const actionPayload = action(...payload);
GLOBAL_DATA.triggers.push(actionPayload);
};
GLOBAL_DATA.actionPaths.forEach((path: string) => {
const action = _.get(GLOBAL_DATA, path);
const entity = _.get(GLOBAL_DATA, path.split(".")[0]);
if (action) {
_.set(GLOBAL_DATA, path, pusher.bind(data, action.bind(entity)));
}
if (isTriggerBased) {
//// Add internal functions to dataTree;
const dataTreeWithFunctions = addFunctions(data);
///// Adding Data tree with functions
Object.keys(dataTreeWithFunctions).forEach((datum) => {
GLOBAL_DATA[datum] = dataTreeWithFunctions[datum];
});
///// Fixing action paths and capturing their execution response
if (dataTreeWithFunctions.actionPaths) {
GLOBAL_DATA.triggers = [];
const pusher = function(
this: DataTree,
action: any,
...payload: any[]
) {
const actionPayload = action(...payload);
GLOBAL_DATA.triggers.push(actionPayload);
};
GLOBAL_DATA.actionPaths.forEach((path: string) => {
const action = _.get(GLOBAL_DATA, path);
const entity = _.get(GLOBAL_DATA, path.split(".")[0]);
if (action) {
_.set(GLOBAL_DATA, path, pusher.bind(data, action.bind(entity)));
}
});
}
} else {
///// Adding Data tree
Object.keys(data).forEach((datum) => {
GLOBAL_DATA[datum] = data[datum];
});
}
@ -150,9 +160,11 @@ export default function evaluate(
});
try {
result = Function(script)();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
triggers = [...self.triggers];
if (isTriggerBased) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
triggers = [...self.triggers];
}
} catch (e) {
errors.push({
errorMessage: `${e.stack.split(`\n`)[0]}`,