Skip to content

XAML server: bound Content-Length and header sizes in JsonRpcConnection #214

Closed

Description

Follow-up from the PR #50 review (finding L1).

server/src/WinUiXaml.LanguageServer/Lsp/JsonRpcConnection.cs line 226 allocates new byte[contentLength] directly from the Content-Length header with no upper bound, and the header-reading loop has no cap on individual header-line length or total header size. A malformed or hostile Content-Length therefore turns into an unbounded allocation.

Additionally, when int.TryParse fails on the header value the code silently leaves contentLength at 0 rather than treating the frame as a protocol error, which makes malformed input hard to diagnose.

Severity is low in practice: the only client on this pipe is the extension host over stdio, so this is defense-in-depth rather than a reachable attack path.

Suggested work:

  • Cap Content-Length at a sane maximum (a few MB) and fail the frame beyond that.
  • Cap header-line and total-header size.
  • Treat a Content-Length parse failure as an explicit protocol error instead of falling through with 0.

Activity

  1. chiaramooney commented on Sep 8, 2026

    @chiaramooney
    CollaboratorAuthor

    Fixed in PR #50 (dada006, 5894e98), which closes this issue. All three suggested items are implemented.

    Bounds. MaxContentLength 32 MB, MaxHeaderLineLength 8 KB, MaxHeaderBlockLength 64 KB, each failing the frame with its own log message rather than a shared generic one.

    Parse failure is now a protocol error. Worth recording that this was slightly worse than the issue described. int.TryParse writes 0 on failure, which overwrote the -1 sentinel and so bypassed the contentLength < 0 missing-header guard entirely. The frame was completed with an empty body while the real body bytes stayed in the stream. Tracing what follows: the next read consumes those bytes as a header line, finds no Content-Length, and terminates the connection. So it failed closed rather than corrupting the session, but a malformed header killed the connection and reported the misleading message "missing Content-Length header".

    A follow-on commit also switched the parse to NumberStyles.None with InvariantCulture. LSP specifies digits only, and a default int.TryParse is culture-sensitive and accepts a leading sign, so Content-Length: +39 previously parsed as 39 and dispatched the frame. Rejecting the sign also makes a negative value unparsable, removing the need for a separate < 0 guard.

    On severity, the original assessment holds and I did not inflate it. Program.cs is the only construction site and it is stdio, so the extension host is the sole writer on this pipe. This is not attacker-reachable. The value is that a truncated or half-written frame from a crashing client fails fast with a diagnostic instead of an OutOfMemoryException or a read loop that never terminates.

    Test honesty. Eight tests were added and each bound was verified by reverting it and confirming a specific failure. Two rounds of that were necessary: the first pass had six tests of which only two failed without the fix. One was vacuous, asserting a handler was not called for a body of {} that has no method and so never reached a handler either way, and another let a MemoryStream end naturally, which masked the missing cap. Both were rewritten to place a valid request frame behind the oversized header. Final state is five load-bearing tests plus three deliberate non-regression guards (a negative length the old < 0 check already caught, a culture guard that passes either way, and a positive control that headers within the limits still dispatch).

    Server suite 1,008 passing. The end-to-end LSP smoke also passes, which mattered here because every message traverses this framing layer.

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

Metadata

Metadata

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