Skip to content

fix: jsonata $base64decode/$base64encode in Web Worker#2275

Open
min23asdw wants to merge 4 commits intogchq:masterfrom
min23asdw:fix/jsonata-base64-web-worker
Open

fix: jsonata $base64decode/$base64encode in Web Worker#2275
min23asdw wants to merge 4 commits intogchq:masterfrom
min23asdw:fix/jsonata-base64-web-worker

Conversation

@min23asdw
Copy link
Copy Markdown

@min23asdw min23asdw commented Mar 22, 2026

Summary

Fixes $base64decode and $base64encode JSONata functions which throw Cannot read properties of undefined (reading 'from') when used in the browser.

Root cause: The jsonata library checks typeof window !== 'undefined' to choose between browser atob/btoa and Node.js Buffer.from. CyberChef runs operations in a Web Worker where window is undefined, so it falls back to global.Buffer — which also doesn't exist in workers.

Fix: Register custom $base64decode/$base64encode functions on the expression object using atob()/btoa(), which are available in both browser window and Web Worker scopes.

Closes #2063

Changes

File Change
src/core/operations/Jsonata.mjs Register base64 function overrides before evaluate()
tests/operations/tests/Jsonata.mjs Add 2 test cases for $base64decode and $base64encode

Test plan

▎ This pull request was created with the assistance of Claude and Antigravity

AI usage: I used AI (Claude and Antigravity) to suggest ideas on where it might be broken and to help write the tests.
I then searched online to verify whether those suggestions were correct,
confirmed it myself,
and then actually ran the tests locally.

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 gchq#2063
@GCHQDeveloper581 GCHQDeveloper581 self-requested a review March 22, 2026 16:39
Copy link
Copy Markdown
Contributor

@GCHQDeveloper581 GCHQDeveloper581 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a (single) test in tests/browser/02_ops.js to test this in a browser context.

@min23asdw
Copy link
Copy Markdown
Author

min23asdw commented Mar 22, 2026

Add browser test @GCHQDeveloper581 I do it right?. this

@GCHQDeveloper581
Copy link
Copy Markdown
Contributor

Looks good apart from the linting error, though (because of that) I haven't seen whether the test passes or fails yet!

Also - on PR #2270 you indicated that you'd used the assistance of AI tools. Is this also the case for your other PRs (including this one?)

@min23asdw
Copy link
Copy Markdown
Author

min23asdw commented Mar 22, 2026

I use AI to add Add browser test (antigravity) and i forgot to run lint check on last commit .... i feel bad . my bad sorry

@min23asdw
Copy link
Copy Markdown
Author

Before
image

Fix:
/#recipe=Jsonata_Query('$base64decode("SGVsbG8gV29ybGQh")')&input=e30
image
/#recipe=Jsonata_Query('$base64encode("Hello%20World!")')&input=e30
image

prove that. It Look good on http://localhost/

@GCHQDeveloper581

Copy link
Copy Markdown
Member

@C85297 C85297 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM from a code standpoint but from an AI standpoint we will have to confer

@min23asdw
Copy link
Copy Markdown
Author

min23asdw commented Mar 23, 2026

I won't be sad if the fix doesn't come from my PR ..... I just want to see it fixed.
If it helps feel free to cherry-pick the code.

  • AI usage: I used AI (Antigravity) to suggest ideas on where it might be broken and to help write the tests.

I then searched online to verify whether those suggestions were correct,
confirmed it myself,
and then actually ran the tests locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSONata base64decode does not work

3 participants