Skip to content

perf(workspace): validate source-bundle base64 in one pass - #878

Merged
blove merged 1 commit into
mainfrom
blove/workspace-base64-check
Sep 30, 2026
Merged

blove merged 1 commit into
mainfrom
blove/workspace-base64-check

Conversation

@blove

@blove blove commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Problem

A CPU profile of the software factory's verifier showed base64Size in packages/workspace/src/source-bundle.ts validating the base64 alphabet one character at a time (BASE64.indexOf(value.charAt(i))). verifySourceBundle runs 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 when Buffer.from(v, "base64").toString("base64") === v. That is one native pass. (Buffer and node:crypto were already used in this module, so edge purity is unchanged.)

base64Size now 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):

before after
the base64 check alone 67 ms 2.2 ms
a regex alternative (/^[A-Za-z0-9+/]*={0,2}$/), for reference 33 ms
full verifySourceBundle (incl. SHA256 digest) 140-220 ms 11-22 ms

Strictness tests

A new strict base64 content suite in packages/workspace/test/source-bundle.test.ts re-signs the digest after each mutation, so only content validation can reject the input. It covers 25 inputs, all of which must throw /base64/:

  • missing, short, or excess padding; padding only; A===
  • = in the middle or before the data; trailing garbage after padding; two padded groups
  • spaces, leading space, \n, \r\n, tab, NUL
  • URL-safe _, -, and mixed; punctuation; a non-ASCII letter
  • nonzero padding bits (AB==, 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:

  • Ordering: five 16 MiB files where the first is invalid base64 must throw the total-limit error, so no content is decoded before the limit check.
  • Relative performance: 350 files / ~2.5 MB. The best of 5 verifySourceBundle runs must stay under 6x the best of 5 createSourceBundle runs 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.ts 124/124), typecheck clean, lint clean (2 existing warnings in with-logging.test.ts)
  • @b4run/cli source-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)
  • root pnpm lint, node scripts/check-docs.mjs, and node scripts/check-changesets.mjs all pass

The changeset is a patch bump for @b4run/workspace.

🤖 Generated with Claude Code

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]>
@vercel

vercel Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
b4-run Ready Ready Preview Sep 30, 2026 8:23pm UTC

Request Review

@github-actions github-actions Bot 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.

Automated approval: this PR received an intelligent (AI) code review. See the review comments on this PR.

@blove
blove merged commit b0605d7 into main Sep 30, 2026
30 of 31 checks passed
@blove
blove deleted the blove/workspace-base64-check branch September 30, 2026 23:09

This branch was successfully deployed

1 active and 1 inactive deployments
vercel-preview — 62ce95a7 Deployed Sep 30, 2026 by blove via vercel-native #2500
Preview — 62ce95a7 Deployed Sep 30, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant