Repository navigation
perf(workspace): validate source-bundle base64 in one pass - #878
Merged
Merged
Conversation
verifySourceBundle checked each file's base64 alphabet with a per-character
JS loop (~67 ms for a 343-file / 2.55 MB bundle), and a verification session
verifies the same bundle ~8 times across layers, blocking the event loop for
up to a second at a time. Node's encoder is canonical, so a value is canonical
standard base64 exactly when Buffer.from(v, "base64").toString("base64") === v;
that one native pass takes ~2 ms and rejects everything the loop did (foreign
characters, URL-safe alphabet, whitespace, misplaced or excess padding,
nonzero padding bits).
Size limits are still computed from encoded lengths, and now for every file
before any content is decoded.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
blove
temporarily deployed
to
vercel-preview
September 30, 2026 20:25 — with
GitHub Actions
Inactive
This branch was successfully deployed
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.
Problem
A CPU profile of the software factory's verifier showed
base64Sizeinpackages/workspace/src/source-bundle.tsvalidating the base64 alphabet one character at a time (BASE64.indexOf(value.charAt(i))).verifySourceBundleruns it for every file, and one verification session verifies the same bundle ~8 times across layers (captureWorkspaceDefinition,withWorkspace,ManagedWorkspaceManager, the sqlite-storage source store, the Docker managed workspace). For a 343-file / 2.55 MB bundle each verify cost ~55-62 ms, producing synchronous event-loop blocks of 0.1-1.2 s (~2.7 s per verification).Fix
Node's base64 decoder is lenient (it skips whitespace and foreign characters, accepts
-/_, ignores padding bits), but its encoder is canonical. So a string is canonical standard base64 exactly whenBuffer.from(v, "base64").toString("base64") === v. That is one native pass. (Bufferandnode:cryptowere already used in this module, so edge purity is unchanged.)base64Sizenow only works out the decoded size from the encoded length (string type, per-file limit,length % 4, padding count). The round-trip check runs after every file's size has been added to the 64 MiB total. As a result, no content is decoded until all size limits pass. Before, the alphabet loop ran file by file, interleaved with the total check.Benchmark on the same 343 files / 2,550,548 base64 chars (Node 24, busy laptop):
/^[A-Za-z0-9+/]*={0,2}$/), for referenceverifySourceBundle(incl. SHA256 digest)Strictness tests
A new
strict base64 contentsuite inpackages/workspace/test/source-bundle.test.tsre-signs the digest after each mutation, so only content validation can reject the input. It covers 25 inputs, all of which must throw/base64/:A====in the middle or before the data; trailing garbage after padding; two padded groups\n,\r\n, tab, NUL_,-, and mixed; punctuation; a non-ASCII letterAB==,AAB=,A/==)It also checks acceptance: the empty string, the canonical encodings of every length from 0 to 256 bytes (all byte values), and a few hand-written canonical values.
The existing noncanonical and limit tests are unchanged and pass. All the new strictness cases passed on the old implementation too, so they pin that strictness did not regress.
There are also two new tests. Both failed before the fix and pass after it:
verifySourceBundleruns must stay under 6x the best of 5createSourceBundleruns on the same content (floor 5 ms). The baseline uses native encoding plus SHA256 and runs in the same process, so machine speed cancels out. Before: 45 ms vs a 21 ms bound (baseline 3.5 ms). After: roughly 1-2x the baseline.Verification
@b4run/workspace: test 349/349 (13 files;source-bundle.test.ts124/124), typecheck clean, lint clean (2 existing warnings inwith-logging.test.ts)@b4run/clisource-bundle consumers (managed-workspace-{manager,reader,inspect},staged-workspace-{manager,endpoint},cleanup-workspaces): 111/111@b4run/sqlite-storage: 126/126@b4run/sandbox: 340 passed, 55 skipped (the Docker-gated suites)pnpm lint,node scripts/check-docs.mjs, andnode scripts/check-changesets.mjsall passThe changeset is a patch bump for
@b4run/workspace.🤖 Generated with Claude Code