Skip to content

fix(ast): count UTF-8 bytes without Node's Buffer - #92

Merged
frontegg-david merged 1 commit into
mainfrom
fix/ast-utf8-byte-length-without-buffer
Oct 4, 2026
Merged

frontegg-david merged 1 commit into
mainfrom
fix/ast-utf8-byte-length-without-buffer

Conversation

@frontegg-david

@frontegg-david frontegg-david commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Stack 1/10: main ← this ← #93 ← #94 ← #95 ← #96 ← #98 ← #99 ← #100 ← #101 ← #102

Fixes #87.

Validation

Reproduced. In a plain browser, BrowserEnclave.run('return 1;') failed with ReferenceError: Buffer is not defined. The cause is Buffer.byteLength in @enclave-vm/ast: libs/ast/src/pre-scanner/checks/size-check.ts:21 and libs/ast/src/transforms/string-extraction.transform.ts (3 call sites). The browser e2e suite and the browser demo app hid it with an injected Buffer shim.

Fix

  • New libs/ast/src/utils/utf8-byte-length.ts. It counts UTF-8 bytes by walking the string, with the same counts as Buffer.byteLength (a lone surrogate is 3 bytes). It doesn't allocate a copy: the pre-scanner measures untrusted input before any other check, so new TextEncoder().encode(source) would allocate a buffer as big as the attack input.
  • Used by the size check and the string extraction transform.
  • Removed the Buffer shims from libs/browser/e2e/build-test-bundle.mjs and apps/browser-demo/scripts/build-enclave-bundle.mjs, so the e2e suite runs the package the way a browser does.

Red → green

Suite Before (main sources) After
libs/ast/src/__tests__/utf8-byte-length.spec.ts (new: parity with Buffer.byteLength on 10 samples, plus pre-scan, validation and extraction with globalThis.Buffer deleted) 4 failed (ReferenceError: Buffer is not defined), 10 passed 14 passed
Browser e2e without the shim 40 of 64 failed 64 passed (new: validates without a Node.js Buffer global)

Verification

  • npx jest -c libs/ast/jest.config.ts: 25 suites, 814 passed
  • npx nx run browser:test-e2e: 64 passed
  • yarn lint, yarn format:check, yarn build: pass (pre-existing warnings only)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Input-size checks and string extraction now correctly count UTF-8 bytes, including multibyte characters and surrogate pairs.
    • Browser-based validation works without a global Buffer, including when returning Unicode strings.

@coderabbitai

coderabbitai Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: agentfront/enclave/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Essentials
  • Run ID: ec1c1c16-116c-4f5e-a2e0-89b373e98c53
📥 Commits

Reviewing files that changed from the base of the PR and between 25b3e03 and 94f68ab.

📒 Files selected for processing (9)
  • apps/browser-demo/scripts/buffer-shim.mjs
  • apps/browser-demo/scripts/build-enclave-bundle.mjs
  • libs/ast/src/__tests__/utf8-byte-length.spec.ts
  • libs/ast/src/pre-scanner/checks/size-check.ts
  • libs/ast/src/transforms/string-extraction.transform.ts
  • libs/ast/src/utils/utf8-byte-length.ts
  • libs/browser/e2e/buffer-shim.mjs
  • libs/browser/e2e/build-test-bundle.mjs
  • libs/browser/e2e/validation.spec.ts
💤 Files with no reviewable changes (4)
  • libs/browser/e2e/build-test-bundle.mjs
  • apps/browser-demo/scripts/buffer-shim.mjs
  • libs/browser/e2e/buffer-shim.mjs
  • apps/browser-demo/scripts/build-enclave-bundle.mjs

Included review availability: This review used your included allowance. 2 included reviews remain after this review. Your included PR review attempts over the past 7 days set your current allowance at 3 reviews per hour.


📝 Walkthrough

Walkthrough

The AST now uses a shared UTF-8 byte counter for input sizing and string extraction. Browser demo and e2e bundles no longer inject a Buffer shim. Tests cover sizing without a global Buffer and Unicode output in the browser.

Changes

Browser-compatible UTF-8 sizing

Layer / File(s) Summary
UTF-8 byte counting in AST sizing
libs/ast/src/utils/utf8-byte-length.ts, libs/ast/src/pre-scanner/checks/size-check.ts, libs/ast/src/transforms/string-extraction.transform.ts, libs/ast/src/__tests__/utf8-byte-length.spec.ts
The AST adds utf8ByteLength and uses it for input-size checks and string extraction byte counts. Tests compare byte counts with Buffer.byteLength and cover sizing and extraction when Buffer is absent.
Browser bundles without Buffer shim
apps/browser-demo/scripts/buffer-shim.mjs, apps/browser-demo/scripts/build-enclave-bundle.mjs, libs/browser/e2e/buffer-shim.mjs, libs/browser/e2e/build-test-bundle.mjs, libs/browser/e2e/validation.spec.ts
The browser demo and e2e builds stop injecting the Buffer shim. An e2e test checks that Buffer is undefined and that code returning a Unicode string produces no error and returns that string.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 94f68

No confirmed issue prevents merging. The browser validation test covers running a Unicode-returning script without a Buffer global.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Issue #87 requires browser validation without a Node.js Buffer global. The PR replaces AST Buffer.byteLength calls with utf8ByteLength, removes the browser test and demo shims, and adds tests fo…
Out of Scope Changes check ✅ Passed All reported changes support issue #87. The UTF-8 helper, AST call-site updates, shim removal, and unit and browser e2e tests implement or verify browser validation without a Node.js Buffer global. …
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 5 files.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: counting UTF-8 bytes without relying on Node.js Buffer.
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@enclave-vm/browser could not validate any script in a plain browser:
the pre-scanner's input size check and the string extraction transform
called Buffer.byteLength, so run() failed with "Buffer is not defined".
The browser e2e suite and the demo app hid it with an injected Buffer
shim.

Add an allocation-free utf8ByteLength helper (same counts as
Buffer.byteLength, lone surrogates included), use it in both places, and
drop the shims so the e2e suite runs the package as a browser would.

Fixes #87

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The implementation correctly preserves UTF-8 sizing semantics and includes focused unit and browser regression coverage.

Review effort: Balanced
Findings: None

What changed in this PR

Replaces Node-specific UTF-8 sizing in the AST package so browser validation works without a Buffer global.

Changes:

  • Adds allocation-free UTF-8 byte counting and adopts it across AST size checks and string extraction.
  • Removes browser/demo Buffer shims.
  • Adds unit and browser regression coverage.
File Description
libs/​ast/​src/​utils/​utf8-byte-length.ts Implements UTF-8 byte counting.
libs/​ast/​src/​pre-scanner/​checks/​size-check.ts Uses the browser-compatible utility.
libs/​ast/​src/​transforms/​string-extraction.transform.ts Replaces Buffer.byteLength calls.
libs/​ast/​src/​__tests__/​utf8-byte-length.spec.ts Tests parity and Buffer-free operation.
libs/​browser/​e2e/​validation.spec.ts Adds browser regression coverage.
libs/​browser/​e2e/​build-test-bundle.mjs Stops injecting the test shim.
libs/​browser/​e2e/​buffer-shim.mjs Removes the obsolete test shim.
apps/​browser-demo/​scripts/​build-enclave-bundle.mjs Stops injecting the demo shim.
apps/​browser-demo/​scripts/​buffer-shim.mjs Removes the obsolete demo shim.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@frontegg-david
frontegg-david merged commit a909613 into main Oct 4, 2026
8 checks passed
@frontegg-david
frontegg-david deleted the fix/ast-utf8-byte-length-without-buffer branch October 4, 2026 10:55
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.

@enclave-vm/browser: run() fails with "Buffer is not defined" in a plain browser (the AST size check uses Buffer.byteLength)

3 participants