Skip to content

read_message allocates whatever Content-Length the peer sends: an oversized header panics the server #53

Open

Description

@MelbourneDeveloper

TL;DR

read_message trusts the peer's Content-Length and allocates that many bytes up front. A header of 18446744073709551615 panics the server with capacity overflow, and any large value reserves that much memory before a single body byte arrives.

Details

crates/lspkit-server/src/jsonrpc.rs (read_message, let mut body = vec![0u8; length];, line 181 at v0.5.0, line 187 on fix/issues-21-26-48-49) has no upper bound on length. Every lspkit language server reads its stdin through this function, so one garbled or hostile header from the client kills the process or makes it reserve gigabytes. Deslop's deslop-lsp and deslop-mcp both use it, while Deslop is already chasing multi-GB RSS reports.

Steps To Reproduce

  1. Create a binary crate that depends on lspkit-server = "=0.5.0" and tokio.
  2. Feed read_message the frame Content-Length: 18446744073709551615\r\n\r\n{}.
  3. Observed: the process panics with capacity overflow. Expected: Err(FramingError::BadHeader(..)).

Details (for AI)

  • Add a named MAX_FRAME_BYTES constant, overridable by the server, and reject a larger Content-Length with a FramingError before allocating.
  • Read the body incrementally (take(length).read_to_end), so memory follows the bytes that actually arrive, never the claimed length.
  • Never panic on peer input (lspkit AGENTS.md: no panics in production).

Acceptance Criteria (for AI)

  • A failing-first black-box test sends Content-Length: 18446744073709551615 and gets Err, with no panic.
  • A test sends a length one byte over the cap and gets Err, while a length at the cap still decodes.
  • A claimed length larger than the bytes sent gets Err(Io(UnexpectedEof)) with no up-front allocation of the claimed size, shown by an allocator-bytes test (test-support/state.rs).
  • A spec ID cites the frame-size rule in the spec, the code and the tests.

Screenshots

N/A

Activity

  1. MelbourneDeveloper commented on Nov 17, 2026

    @MelbourneDeveloper
    CollaboratorAuthor

    The header half is still unbounded at 0dc2f04

    The body is now capped and read incrementally, which I checked in the code. content_length still uses reader.read_line(&mut line) with no limit, so a header line that never ends grows line without bound.

    Proof: a probe binary built against the 0dc2f04 sources read Content-Length followed by 512 MiB of A with no newline (tokio::io::repeat(b'A').take(512 MiB)). The result was Err("peer closed the stream"), after a maximum resident set size of 544,555,008 bytes. Memory follows the peer's header, which is exactly what this issue forbids.

    Needed:

    • A named MAX_HEADER_BYTES cap on each header line and on the header block, read through take or a bounded loop.
    • A red-first test: an endless header line returns Err (HeaderTooLarge), and an allocator-bytes test shows the cost is bounded by the cap.
    • A cap on the number of header lines, so endless short headers end too.
  2. MelbourneDeveloper commented on Nov 17, 2026

    @MelbourneDeveloper
    CollaboratorAuthor

    Fixed on PR #51 in 244650b, red first: a single MAX_HEADER_BYTES (8 KiB) budget is read through take() across every header line and terminator. One endless line and endless short lines (the line-count case) both end in FramingError::HeaderTooLarge. Tests: tests/jsonrpc.rs header_blocks_past_the_cap_are_rejected_and_one_at_the_cap_decodes (endless line, endless X: y lines, a block exactly at the cap, one byte over), plus the allocator-bytes check in tests/jsonrpc_frame_heap.rs (a 512 KiB header line allocates < 64 KiB; red was 1,056,768 bytes). DeslopClaude1 re-ran the 512 MiB probe: HeaderTooLarge at 7.6 MB RSS, down from 545 MB. The body cap is in aa25ac5. Red→green evidence: docs/plans/issue-fix-evidence-21-26-48-49.md.

  3. MelbourneDeveloper commented on Nov 17, 2026

    @MelbourneDeveloper
    CollaboratorAuthor

    ✅ Released in v0.6.0 and verified against the published crates

    Release: https://github.com/Nimblesite/lspkit/releases/tag/v0.6.0 (run 44186953235, tag at 8e39446). It contains lspkit, lspkit-server, lsp-profiler and lsp-profiler-dotnet 0.6.0 on crates.io, plus @nimblesite/lsp-profiler-ui@0.6.0 on npm (with provenance), the .NET engine for 6 runtime ids, the consumer kit, an SBOM, and SHA256SUMS signed with cosign.

    Method: a worktree at v0.6.0, with crates/lspkit/src and crates/lspkit-server/src replaced by the sources unpacked from the crates.io 0.6.0 downloads. The published source is byte-identical to the tag. Results against the released code:

    #21 sidecar_disconnect_session: test result: ok. 2 passed; 0 failed
    #26 cancel_during_a_scan: test result: ok. 1 passed; 0 failed
    #48 outbound: test result: ok. 3 passed; 0 failed
    #48 outbound_heap_leak: test result: ok. 1 passed; 0 failed
    #48 sidecar_correlator: test result: ok. 12 passed; 0 failed
    #49 uri: test result: ok. 17 passed; 0 failed
    #49 uri_authority: test result: ok. 1 passed; 0 failed
    #49 style_tests: test result: ok. 5 passed; 0 failed
    #52 pull_forget: test result: ok. 5 passed; 0 failed
    #52 pull_forget_heap: test result: ok. 1 passed; 0 failed
    #53 jsonrpc: test result: ok. 21 passed; 0 failed
    #53 jsonrpc_frame_heap: test result: ok. 1 passed; 0 failed
    

    Breaking: the lspkit-server/mcp feature is removed. lsp_profiler::session::rfc3339_now is now lsp_profiler::clock::rfc3339_now. A lone %, or an encoded separator, in a file URI is now an error. Consumers on didClose should call PullStore::forget(uri) instead of clear().

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions