From 4d6d4536769a18eb0c7d979a5abfbd0923dc3d45 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 13 Oct 2021 13:44:43 +0100 Subject: [PATCH] Add one second timeout to execution of JS bindings --- packages/string-templates/src/helpers/javascript.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/string-templates/src/helpers/javascript.js b/packages/string-templates/src/helpers/javascript.js index 301833fd34..ad8c96ae5d 100644 --- a/packages/string-templates/src/helpers/javascript.js +++ b/packages/string-templates/src/helpers/javascript.js @@ -27,6 +27,16 @@ const getContextValue = (path, context) => { return data } +// Node polyfill for base64 encoding +const btoa = plainText => { + return Buffer.from(plainText, "utf-8").toString("base64") +} + +// Node polyfill for base64 decoding +const atob = base64 => { + return Buffer.from(base64, "base64").toString("utf-8") +} + // Evaluates JS code against a certain context module.exports.processJS = (handlebars, context) => { try { @@ -39,7 +49,7 @@ module.exports.processJS = (handlebars, context) => { // Create a sandbox with out context and run the JS vm.createContext(sandboxContext) - return vm.runInNewContext(js, sandboxContext) + return vm.runInNewContext(js, sandboxContext, { timeout: 1000 }) } catch (error) { return "Error while executing JS" }