Skip to content

fix(mcp): don't panic when a transport has no request echo - #14

Open
S3DFX-CYBER wants to merge 1 commit into
hackwither:mainfrom
S3DFX-CYBER:fix/ws-nil-exchange
Open

S3DFX-CYBER wants to merge 1 commit into
hackwither:mainfrom
S3DFX-CYBER:fix/ws-nil-exchange

Conversation

@S3DFX-CYBER

Copy link
Copy Markdown

What happens

A WebSocket MCP endpoint that answers tools/list anonymously crashes reap:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x60]
github.com/hackwither/reap/internal/probe/mcp.(*unauthToolsListProbe).Run(...)
	internal/probe/mcp/checks.go:673

Exit code 2, no report written. mcp-resources-prompts-exposure crashes the same way a few lines further down.

Why

RawResult.Exchange() documents its nil case precisely — it returns nil when ReqMethod is empty, "e.g. a WebSocket transport, which has no per-message HTTP request to curl-replay". WSSession honours that: handleMessage builds &probe.RawResult{StatusCode: 200, Body: payload} with no ReqMethod.

Two anyTransport probes then do:

reqExch := raw.Exchange()
reqExch.Expected = "..."

Every other call site is safe — the rest assign Exchange() straight into Finding.Request, where nil is fine, or sit behind httpOnlyTransports. These two are the only ones that dereference it, and they are reachable over WebSocket.

WSSession also sets StatusCode: 200 on every dispatched frame, so the raw.StatusCode != 200 guard that would otherwise stop the probe never fires on this transport.

Impact

Availability of the scan, which for a recon tool is the whole product: a target that can abort the scan can also hide everything else it exposes. In batch mode the panic happens inside a worker goroutine, which is unrecoverable in Go — one hostile host in a --targets-file of a thousand takes down the run and discards every other report, since results are only written after wg.Wait().

Reproduction

A minimal MCP-over-WebSocket server is enough: complete the upgrade, answer initialize, then answer tools/list with at least one tool. No auth, no interaction.

reap -t http://127.0.0.1:8802/mcp --protocol auto

Discovery resolves websocket via the mcp-websocket detector, newSessionForTransport builds a WSSession, and the process dies in unauthToolsListProbe.

The fix

Three parts:

  1. Guard the two dereferences. Annotating the reproduction is optional; recording the finding is not.
  2. Teach WSSession to populate the echo it can know — a small fillEcho helper sets ReqMethod: "WS-FRAME", ReqURL: s.targetURL, ReqBody: body. This is done in Do, where the outgoing frame body is in scope, so it costs nothing, and it means a WebSocket finding carries the same request-side evidence an HTTP finding does.
  3. Don't render a fiction. HTTPExchange.Curl() and ReproScript() return "" for a non-HTTP scheme, so a WS finding shows Evidence WS-FRAME ws://… → 200 • 229 B instead of a curl command that cannot work. SARIF already omits properties.reproduce when the string is empty.

The guard alone stops the panic but leaves the finding with no reproduction evidence at all, which is the opposite of what the rest of this codebase optimises for — hence part 2.

Verification

  • TestAnyTransportProbesSurviveEmptyRequestEcho runs every anyTransport probe against a stub session whose results have an empty ReqMethod — the exact shape WSSession used to produce — and asserts no panic. It fails on main (both probes) and passes with the patch.
  • TestFindingsSurviveNilExchange asserts the anonymous tools/list finding is still recorded over such a transport.
  • End-to-end against a mock WebSocket endpoint: before, exit 2 with the panic above; after, exit 0, 6 findings, and the anonymous tools/list finding carries WS-FRAME … → 200 • 229 B.
  • go test ./... green (122 tests), go vet ./... clean.

Not in scope, noticed while here

WSSession.runReader caps each frame at 4 MB but accumulates continuation fragments into acc with no total bound (session_ws.go:160), so a server can stream fragments until the scanner exhausts memory. Same class, lower likelihood — happy to send a follow-up that applies the existing maxResponseBytes cap to the reassembled message if you'd like it in the same release.

RawResult.Exchange() documents that it returns nil when ReqMethod is empty —
"e.g. a WebSocket transport, which has no per-message HTTP request to
curl-replay" — and WSSession honours that. Two anyTransport probes then
dereference the result anyway:

    reqExch := raw.Exchange()
    reqExch.Expected = "..."

A WebSocket MCP endpoint that answers tools/list anonymously therefore
crashes reap with a nil pointer dereference at checks.go:650 (and the same in
resourcesPromptsExposureProbe). WSSession also sets StatusCode 200 on every
dispatched frame, so the status guard that would otherwise stop the probe
never fires on that transport. In batch mode the panic happens inside a
worker goroutine, which is unrecoverable: one hostile host takes down the
whole run and discards every other report.

Fix, three parts:

  - guard the two dereferences, since annotating the reproduction is optional
    and recording the finding is not;
  - teach WSSession to populate the echo it can know (ReqMethod "WS-FRAME",
    the target URL, and the frame body), where the outgoing body is in scope,
    so a WebSocket finding carries the same request-side evidence an HTTP
    finding does;
  - return "" from Curl/ReproScript for a non-HTTP scheme rather than
    rendering a curl command that cannot work.

Tests: TestAnyTransportProbesSurviveEmptyRequestEcho runs every anyTransport
probe against a stub session whose results have an empty ReqMethod, and
TestFindingsSurviveNilExchange asserts the finding is still recorded.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant