fix(browser): add btoa/atob/TextEncoder/TextDecoder to the run sandbox - #344
Merged
Conversation
The QuickJS sandbox for `browser run` exposed working base64 and UTF-8 codecs only under private-looking `__webcmd*` names, so programs reaching for the standard globals hit `'TextEncoder' is not defined`. Alias them to the platform names in the existing platform shim. btoa/atob are implemented as latin1 binary-string codecs rather than composed with the UTF-8 helpers, which would corrupt binary input. Buffer stays absent — it is Node-only. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Contributor
🟠 Maintainer review suggested — low confidenceThe automated review could not reach a fully supported conclusion. Limitations
This review is advisory and does not block merging. |
…it (#345) __webcmdWriteArtifact was the only write path out of the QuickJS sandbox and its dunder prefix reads as private, so programs would not call it. Expose the same function as globalThis.writeArtifact, keep the dunder as an alias, and return the receipt instead of swallowing it. Also accept a null contentType, which is what JSON.stringify produces for the two-argument call, and document artifacts in the browser-run reference including the download path and how a receipt is redeemed. Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
# Conflicts: # skills/webcmd-browser/references/browser-run-playwright.md # src/browser/run/runner.test.ts # src/skills.test.ts
* benchmakrs added * fix: removed missing refs from readme * feat(benchmarks): support OpenAI as judge provider * fix: screenshot upload * feat: added cost support * docs: design benchmark-specific judge prompts * feat(benchmarks): split general and stealth judge prompts * Delete docs/superpowers/specs directory * chore: bump version metadata for 0.7.0 release * Revert "chore: bump version metadata for 0.7.0 release" This reverts commit c69011f. * feat(benchmarks): expose full Webcmd skill pack to Codex evals * feat(benchmarks): clarify stealth captcha handling * fix: added support for webcmd browser skills only * feat(benchmarks): run eval attempts in parallel * fix(benchmarks): isolate parallel webcmd sessions * test(benchmarks): cover parallel webcmd sessions * pi support (#313) * chore(benchmarks): pin webcmd 0.7.1 * refactor: disable concurrency in benchmark evaluation and remove integration tests * feat(benchmarks): support Codex subscription evals * docs(skills): improve browser diff and form guidance * docs(skills): sync browser skill sources --------- Co-authored-by: beubax <tejasr@bu.edu> Co-authored-by: Tejas <48682479+beubax@users.noreply.github.com>
# Conflicts: # src/browser/run/runner.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #335.
What
The
browser runQuickJS sandbox shipped working base64 and UTF-8 codecs under private-looking names (__webcmdEncodeBase64,__webcmdDecodeBase64,__webcmdEncodeText,__webcmdDecodeText) and nothing under the standard ones. An internal agent-behaviour eval showed agents walking the standard-library ladder down —Buffer, thenTextEncoder, thenbtoa— and finding every rung missing, then abandoning the real task.This aliases the existing implementations to their platform names inside the same shim block in
src/browser/run/runner.ts:btoa/atobTextEncoder/TextDecoderBufferdeliberately stays absent (Node-only; #336 covers pointing agents at the replacement). No new dependency, no new file.Latin1, not UTF-8
btoa/atobare binary-string codecs. The obvious composition__webcmdEncodeBase64(__webcmdEncodeText(v))would silently UTF-8-expand any byte above 0x7F, sobtoa(String.fromCharCode(255))would return the base64 of two bytes instead of one.btoahere mapscharCodeAtper character and throws anInvalidCharacterErrorabove 255;atobis its inverse and returns a binary string.TextEncoder/TextDecoderdo wrap the UTF-8 helpers, since those are UTF-8 by definition.TextDecoder.decodeacceptsUint8Array,ArrayBuffer, or any view.Tests
Five tests added to
src/browser/run/runner.test.ts(the shim only exists inside a realrunBrowserProgramrun, soquickjs-host.test.tscannot see it):Bufferstill absentbtoa('hello') === 'aGVsbG8=',atob(btoa(x))round-trip, and a[0, 128, 255]binary round-trip matching Node's base64btoaabove the latin1 range throwsInvalidCharacterErrorTextEncoder/TextDecoderUTF-8 round-trip throughcafé € 🎉, byte-for-byte against Node, including decoding from a rawArrayBuffermaintooCounts
npm run buildclean.npx vitest run --project unit:main, no Chromium installedmain, Chromium installedrunner.test.tsis gated on a local Chromium being present, so the stated 2457 baseline is the no-Chromium figure. I installed Chromium vianode_modules/playwright-core/cli.js install chromiumso these tests actually execute; the +5 delta is exactly the five tests above. 0 failed throughout.Docs
Left alone deliberately.
skills/webcmd-browser/references/browser-run-playwright.mdis being rewritten in #342; adding a row there now would just conflict. The "What is available" table in that rewrite should gain a text/binary row once both land — its existing "noBuffer" line stays accurate.Cloud
No change needed.
webcmd-cloudimportsrunBrowserProgramfrom@agentrhq/webcmd/browser/runatsrc/browser/hosted-browser.ts:17, so hosted runs execute this exact sandbox. The fix reaches hosted when the cloud bumps its pinned@agentrhq/webcmd(currently 0.7.2 on itsmain) — not bumped here.webcmd-cloud/src/browser/run-artifact-sink.tsimplementsBrowserRunArtifactSink, which this PR does not touch.🤖 Generated with Claude Code