fix(runtime): atob WHATWG base64 - #1875
Open
izayl wants to merge 3 commits into
Open
Conversation
Fallback atob/btoa should match Node and Web error behavior so guest code sees InvalidCharacterError for malformed input instead of runtime-specific decoder behavior. The conformance case locks the guest V8 globals against host Node.
Document why the fallback atob path cannot pass input directly to base64-js and cover the WHATWG unpadded-input behavior alongside rejected base64url characters.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ba0071436a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Only synthesize padding for unpadded forgiving-base64 input. Inputs that already contain padding must be complete and correctly positioned, matching WHATWG and Node atob behavior.
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.
What
atob/btoabehavior with Node/Web globals.base64-js.-,_) foratob.InvalidCharacterErrorshape, including legacycode = 5.btoainvalid-character cases.Impact
I hit this building an app on
@rivet-dev/agentos0.2.14 (macOS arm64) that runs thepiagent (@agentos-software/pi) inside a VM with theopenai-codexprovider.Every prompt failed, with the agent returning a single opaque line:
The credentials were fine. The same
~/.pi/agent/auth.jsonworks when I run pi directlyon the host; inside the VM the token is intact (not expired,
chatgpt_account_idclaimpresent) — I verified that by reading the file back out of the VM. The agent is simply
unable to decode it.
The failing path: pi decodes its OAuth access token on every request to derive the
chatgpt-account-idheader, usingatob(token.split(".")[1]). Per RFC 7515 §2, JWTsegments are base64url with padding stripped, so that payload's length is rarely a
multiple of 4 (mine: 1351 bytes,
% 4 === 3).This makes the agent completely unusable inside a VM, while working fine on the host —
which is a confusing failure mode to debug, since nothing about the credentials, the
config, or the agent differs between the two.
Root cause
atobis a WHATWG API, not an ECMAScript one, so the bare V8 runtime does not provide itand the bridge polyfills it in
packages/build-tools/bridge-src/builtins/process.ts#L1162-L1173by delegating straight to the vendored base64-js (
bridge-src/vendor/buffer.ts, importedon line 12). base64-js implements RFC 4648 §4, so the polyfill deviates from
forgiving-base64 decode in both
directions:
getLens()(vendor/buffer.ts#L22-L25) throwsInvalid string. Length must be a multiple of 4on unpadded input. The spec only failsat
length % 4 === 1. This is the bug I hit.vendor/buffer.ts#L20-L21maps-/_to 62/63, so base64url input isaccepted where the spec requires rejection.
Reproduction
Inside any agentOS VM:
Validation
pnpm --dir packages/build-tools run build:v8-bridgeAGENTOS_BUILTIN_CONFORMANCE_CASE=global_base64 cargo +1.91.1 test -p agentos-native-sidecar --test builtin_conformance __builtin_conformance_case_runner -- --nocapturepnpm --dir packages/build-tools testgit diff --check