Skip to content

safe-bash brace expansion: ledger reservation per character ≈ 2 KB heap per source byte — 59 KB {a}{a}… script = fatal OOM on a 128 MB isolate under default limits #665

Closed

Description

@kamilio

Where: packages/safe-bash/src/shell/brace-expansion.ts:39-42 — admit() calls allocation.reserve(64, 0) per character/node; each reservation creates a record + closure in ValueScope.reserve (src/shell/value-state.ts ~L120) that is only released when the outer allocation.close() runs at generator end. Each 64-byte charge costs ~600 B of real heap, so the 16 MiB maxExpansionBytes / 262 144 maxParseUnits admit ~150 MB of bookkeeping. Parsing alone is fine (parseShellUnit of the same script: 28 ms, ~0 MB) — the blowup is in expansion admission.

PoC (59 KB source, ≤ maxSourceBytes, default limits):

echo {a}{a}{a}…×20000          # expands to literal text, no cardinality
echo {{{…×30000 a }}}…×30000   # nested

Run: NODE_OPTIONS=--max-old-space-size=128 npx tsx harness.mts "echo $(printf '{a}%.0s' $(seq 1 20000)) | wc -c"

Measured: {a}×10000 → +57 MB; ×20000 → +120 MB, exit 0, 608 ms; ×50000 → +140 MB then maxParseUnits; nested ×20000 → +67 MB; {,×20000 → +104 MB. 128 MB isolate: {a}×20000 FATAL ERROR: … heap out of memory, nested ×30000 FATAL (re-verified independently); ×10000 survives (49 MB). cloudflareWorkerLimits: caps at ~36–43 MB then maxParseUnits (survives).

Impact: (d) — ~2 KB of heap per source byte; 60 KB of source crashes the isolate under default limits before any expansion output exists. Introduced with brace expansion (e4b598542, #637).

Fix: charge one aggregated reservation per part (count units in a plain integer and reserve once), instead of a ledger record per character; or cap brace tokens per word (e.g. 4 096) with ShellLimitError.

Found in security audit v3 (2026-09-07).

Activity

  1. added
    bug
    on Sep 8, 2026
  2. kamilio commented on Sep 8, 2026

    @kamilio
    MemberAuthor

    Validated the current-source bookkeeping amplification, fixed and verified on remote main at 0511e60. Brace scratch admission now grows one scope-owned reservation instead of retaining one record and release closure per increment; all existing byte charges, parse limits and cancellation/cleanup boundaries remain. Literal witnesses retain two records instead of 82/322/1,282. TDD: 11 genuine RED failures, then 16/16 dedicated and 144/144 adjacent passes. Complete shell selection: 3,592/3,592. Normal build and maintained root lint pass. No unmeasured heap/OOM ratio is claimed; release monitoring is separate.

  3. kamilio commented on Sep 8, 2026

    @kamilio
    MemberAuthor

    Release verified: @poe-platform/safe-bash@0.1.432 was published successfully on September 8, 2026. Scoped release run 34192316139 completed successfully; its source commit 691aafe is a verified descendant of the fix commit. This is separate confirmation from the earlier remote-main delivery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugsecurityseverity:critical

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions