Skip to content

Check base64 without a backtracking regex - #1144

Merged
helpcodeai merged 2 commits into
mainfrom
fix/base64-check-no-regex-stack
Oct 19, 2026
Merged

helpcodeai merged 2 commits into
mainfrom
fix/base64-check-no-regex-stack

Conversation

@helpcodeai

Copy link
Copy Markdown
Contributor

The 0.38.0 release PR (#1143) failed in CI on response-images.util.spec.ts › "caps the images of one result at 12 MiB together" with RangeError: Maximum call stack size exceeded at BASE64.test(s) in normalizeBase64.

/^[A-Za-z0-9+/]+={0,2}$/ on a 7 MB base64 string makes V8 keep a backtrack point per character, and depending on the stack left it overflows. The same thing can fail a real tool call that returns a large image (OpenAI/Gemini image tools).

The check now strips up to two = by hand and tests the rest with /[^A-Za-z0-9+/]/, a single-character class that never backtracks. Same answers as before on the padding edge cases (abcd, abc=, ab==, a===, ====, ab=c, base64url after the -_ swap). Existing response-images tests pass.

normalizeBase64 tested multi-megabyte image strings with
/^[A-Za-z0-9+/]+={0,2}$/. V8 can overflow its stack backtracking through
that pattern, which failed the 12 MiB cap test in CI and would fail a
tool call returning a large image. Strip the padding by hand and look for
any character outside the alphabet with a single-character class instead;
same results on every padding case.
@helpcodeai
helpcodeai enabled auto-merge (squash) October 19, 2026 08:49
@helpcodeai
helpcodeai merged commit 53551c6 into main Oct 19, 2026
14 checks passed
@helpcodeai
helpcodeai deleted the fix/base64-check-no-regex-stack branch October 19, 2026 08:55
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant