Skip to content

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

Merged
GCHQDeveloper581 merged 6 commits into
gchq:masterfrom
min23asdw:fix/jsonata-base64-web-worker
Jun 20, 2026
Merged

fix: jsonata $base64decode/$base64encode in Web Worker#2275
GCHQDeveloper581 merged 6 commits into
gchq:masterfrom
min23asdw:fix/jsonata-base64-web-worker

Conversation

@min23asdw

@min23asdw min23asdw commented Mar 22, 2026

Copy link
Copy Markdown
Contributor

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

@GCHQDeveloper581 GCHQDeveloper581 left a comment

Copy link
Copy Markdown
Contributor

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

min23asdw commented Mar 22, 2026

Copy link
Copy Markdown
Contributor Author

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

min23asdw commented Mar 22, 2026

Copy link
Copy Markdown
Contributor Author

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
Contributor 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

C85297
C85297 previously approved these changes Mar 23, 2026

@C85297 C85297 left a comment

Copy link
Copy Markdown
Member

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

min23asdw commented Mar 23, 2026

Copy link
Copy Markdown
Contributor Author

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.

@GCHQDeveloper581 GCHQDeveloper581 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good.

Thank you for your contribution.

@GCHQDeveloper581 GCHQDeveloper581 merged commit 0d2af8c into gchq:master Jun 20, 2026
2 checks passed
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

4 participants