Skip to content

hosted-worker user-do: KV and blob PUT buffer the whole request body before the storage ceiling check #11332

Closed

Description

@dezren39

Evidence (origin/main ccb195b)

  • `pkgs/id/hosted-worker/src/user-do.ts:291-300` (KV PUT): `const value = await req.text();` runs before any size check. The byte delta is computed only afterwards, in `this.assertWritable(new TextEncoder().encode(value).byteLength - priorBytes)` at `:295`.
  • `pkgs/id/hosted-worker/src/user-do.ts:237` (blob PUT): `const bytes = new Uint8Array(await req.arrayBuffer());` has the same order. `assertWritable` runs at `:246`.
  • `assertWritable` (`:135-146`) checks the storage ceiling only. Nothing in `hosted-worker/src` caps the request body size (grep for content-length, 413, too_large and bodyLimit finds only the storage 413 in `assertWritable`).

Result: a write that is already over the ceiling is still fully buffered into the Durable Object's memory before it is refused with a 413. A body larger than the DO's memory limit would fail inside the isolate rather than return the documented `ceiling_exceeded` response.

Not yet reproduced at runtime. The claim is about the code path, not a measured OOM.

Why it matters

The ceiling is documented as a fail-closed guard for writes. The guard runs after the most expensive step, so an oversized write can crash the DO instead of being refused cleanly. The user then gets an opaque error and the DO may reset.

Fix shape

  • Reject early from `Content-Length` when it is present and already exceeds the remaining headroom (`ceiling - usage`), before reading the body.
  • For bodies without a length, read through a size-capped reader that stops at the remaining headroom plus one byte, then returns `ceiling_exceeded`.
  • Keep the existing `assertWritable` check as the authoritative delta check.

Acceptance criteria

  • A PUT to `/v1/kv/:key` or `/v1/blobs` whose declared length exceeds the remaining headroom returns 413 `ceiling_exceeded` without reading the body.
  • A chunked PUT larger than the headroom stops reading at the cap and returns the same 413.
  • Existing under-cap writes and overwrite deltas behave as before.
  • A test covers the early reject and the chunked cap.

Dedupe

Searched open and closed issues for "user-do KV body", "request body size limit hosted-worker" and "user-do": no matching issue or open PR. Related but different: #6880 and #2791 (cloudflare-export numeric handling) and #3000 (aggregator regex), none of which touch this path.

Queue: `repo-queue` is not available in this environment, so no queue item was enqueued. Coverage needs a follow-up pass.

Activity

  1. added
    kind/bugSomething isn't working (prefixed form of the classic bug label)
    task/triageQueue packet kind: verify, reproduce, and route this item
    source/creatorFiled by the new-issue-creator job
    priority/normalOrdinary priority (the default)
    app/idPackage/app: id (pkgs/ or apps/ in this repo)
    effort/sSmall: expected under 2 hours of work
    on Nov 17, 2026
  2. developing-today commented on Nov 17, 2026

    @developing-today
    Contributor
  3. dezren39 commented on Nov 17, 2026

    @dezren39
    MemberAuthor

    Fixed in #11336 (merged as a merge commit).

    • KV PUT now computes headroom (ceiling - usage + prior value bytes) before reading the body. A declared Content-Length over headroom returns 413 ceiling_exceeded without reading. A chunked body is read through a capped reader that stops at headroom.
    • assertWritable remains the authoritative check. Under-cap writes and overwrites are unchanged.
    • Blob PUT is deliberately not changed: it hashes first because a re-upload of an already-referenced blob has zero storage delta and must stay allowed at cap. The buffering there is a follow-up if it matters.
    • Tests: two new cases in test/usage.test.ts. node --test over routing, blobs, usage and currency: 22/22 pass.

    Not reproduced at runtime; this is from the code path.

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

    app/idPackage/app: id (pkgs/ or apps/ in this repo)effort/sSmall: expected under 2 hours of workkind/bugSomething isn't working (prefixed form of the classic bug label)priority/normalOrdinary priority (the default)source/creatorFiled by the new-issue-creator jobstatus:donetask/triageQueue packet kind: verify, reproduce, and route this item

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions