From 597e7f33f746a70b9cd67a981401300a440d07c0 Mon Sep 17 00:00:00 2001 From: min23asdw Date: Sun, 22 Mar 2026 19:18:30 +0700 Subject: [PATCH 1/3] fix: jsonata $base64decode/$base64encode in Web Worker The jsonata library checks `typeof window !== 'undefined'` to decide whether to use browser `atob`/`btoa` or Node.js `Buffer.from` for base64 operations. Since CyberChef runs operations in a Web Worker where `window` is undefined, it falls back to `global.Buffer` which also does not exist, causing "Cannot read properties of undefined (reading 'from')". Fix by registering custom base64 functions on the expression that use `atob`/`btoa` directly, which are available in both browser and Web Worker scopes. Closes #2063 --- src/core/operations/Jsonata.mjs | 12 ++++++++++++ tests/operations/tests/Jsonata.mjs | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/core/operations/Jsonata.mjs b/src/core/operations/Jsonata.mjs index 82cc4d3976..04259343ae 100644 --- a/src/core/operations/Jsonata.mjs +++ b/src/core/operations/Jsonata.mjs @@ -51,6 +51,18 @@ class JsonataQuery extends Operation { try { const expression = jsonata(query); + // Override built-in base64 functions which fail in Web Worker + // context where `window` is undefined. The jsonata library falls + // back to `global.Buffer` which also does not exist in workers. + // `atob`/`btoa` are available in both browser and worker scopes. + expression.registerFunction("base64decode", (str) => { + if (typeof str === "undefined") return undefined; + return atob(str); + }, ""); + expression.registerFunction("base64encode", (str) => { + if (typeof str === "undefined") return undefined; + return btoa(str); + }, ""); result = await expression.evaluate(jsonObj); } catch (err) { throw new OperationError( diff --git a/tests/operations/tests/Jsonata.mjs b/tests/operations/tests/Jsonata.mjs index fb46a961f2..54ecf34c92 100644 --- a/tests/operations/tests/Jsonata.mjs +++ b/tests/operations/tests/Jsonata.mjs @@ -548,4 +548,27 @@ TestRegister.addTests([ }, ], }, + // Base64 functions (issue #2063) + { + name: "Jsonata: $base64decode", + input: "{}", + expectedOutput: '"Hello World!"', + recipeConfig: [ + { + op: "Jsonata Query", + args: ['$base64decode("SGVsbG8gV29ybGQh")'], + }, + ], + }, + { + name: "Jsonata: $base64encode", + input: "{}", + expectedOutput: '"SGVsbG8gV29ybGQh"', + recipeConfig: [ + { + op: "Jsonata Query", + args: ['$base64encode("Hello World!")'], + }, + ], + }, ]); From f7b2e4b071220f27c3550c0743a3ab716de503ce Mon Sep 17 00:00:00 2001 From: min23asdw Date: Mon, 23 Mar 2026 00:40:24 +0700 Subject: [PATCH 2/3] test: add browser test for Jsonata Query $base64decode Ref: #2063 --- tests/browser/02_ops.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/browser/02_ops.js b/tests/browser/02_ops.js index 5ab55451bd..5da2b27ce9 100644 --- a/tests/browser/02_ops.js +++ b/tests/browser/02_ops.js @@ -218,6 +218,7 @@ module.exports = { testOpHtml(browser, "JSON Beautify", "{a:1}", ".json-dict .json-literal", "1"); // testOp(browser, "JSON Minify", "test input", "test_output"); // testOp(browser, "JSON to CSV", "test input", "test_output"); + testOp(browser, "Jsonata Query", '{}', '"Hello World!"', ['$base64decode("SGVsbG8gV29ybGQh")']); // testOp(browser, "JWT Decode", "test input", "test_output"); // testOp(browser, "JWT Sign", "test input", "test_output"); // testOp(browser, "JWT Verify", "test input", "test_output"); From cfbc688216d6e4303297019006153e164c1603b8 Mon Sep 17 00:00:00 2001 From: min23asdw Date: Mon, 23 Mar 2026 01:23:30 +0700 Subject: [PATCH 3/3] fix: use double quotes to satisfy eslint quotes rule --- tests/browser/02_ops.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/browser/02_ops.js b/tests/browser/02_ops.js index 5da2b27ce9..0baded8229 100644 --- a/tests/browser/02_ops.js +++ b/tests/browser/02_ops.js @@ -218,7 +218,7 @@ module.exports = { testOpHtml(browser, "JSON Beautify", "{a:1}", ".json-dict .json-literal", "1"); // testOp(browser, "JSON Minify", "test input", "test_output"); // testOp(browser, "JSON to CSV", "test input", "test_output"); - testOp(browser, "Jsonata Query", '{}', '"Hello World!"', ['$base64decode("SGVsbG8gV29ybGQh")']); + testOp(browser, "Jsonata Query", "{}", '"Hello World!"', ['$base64decode("SGVsbG8gV29ybGQh")']); // testOp(browser, "JWT Decode", "test input", "test_output"); // testOp(browser, "JWT Sign", "test input", "test_output"); // testOp(browser, "JWT Verify", "test input", "test_output");