Skip to content

tsx: Limits has no memory bound; a sandboxed bundle can exhaust the host process's memory #760

Closed

Description

@houko

Context

tsx.Limits (tsx/sandbox.go:24-47) bounds load and call time, stack depth, nodes, timers, requests, body size and storage, but has no bound on the memory a bundle allocates. Inside one call budget a bundle can allocate gigabytes (new Array(1e9).fill(0), string doubling), and the whole app runs out of memory.

Why it matters

tsx/pkg is for code an app downloads, plugins included. Every other resource a plugin can exhaust has a quota; memory is the one that takes the host process down with it, so an app cannot promise its users that a misbehaving plugin only breaks itself.

Proposal

  • Add a Limits.Memory (bytes) that the runtime enforces: count allocations of large arrays, strings, ArrayBuffers and objects in the goja runtime the bundle owns and stop it with a quota error past the bound, the same way Nodes stops it.
  • If an exact count is not practical, an approximate one (array length × element size, string length) that catches the common exhaustion patterns is still worth having; document what it does not count.

Acceptance

  • A bundle that allocates past Limits.Memory stops with a quota error and the host process keeps running.
  • DefaultLimits sets a value.

Activity

  1. self-assigned this
    on Nov 19, 2026
  2. houko commented on Nov 19, 2026

    @houko
    CollaboratorAuthor

    Status after #802 (merged as 3dd494f): with Limits.Memory set, the sandbox now counts memory where goja's built-ins allocate by size (ArrayBuffer and typed arrays, repeat/padStart/padEnd, String.raw, Array.from, join, fill, sort and the copying array methods, apply/Reflect.apply/Reflect.construct argument lists, JSON.stringify output), checks before allocating, reads each size once, and charges across calls until the memory is collected. Memory is still off by default.

    Still not counted, so a bundle written to exhaust memory can still do it within one call's time budget: concat, flat, slice, spreads, replace/replaceAll, string + and template literals (goja's lazy ropes), Blob/TextEncoder copies, and plain objects. For example new Array(1e4).fill(new Array(1e4).fill(0)).flat() allocates about 1.6 GB in one call.

    Next steps for this issue: wrap replace, replaceAll, flat, concat, slice and spread the same way, then decide the default for DefaultLimits once the remaining paths are bounded or explicitly accepted.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions