Skip to content

fix(auth): cancel CIMD response body on early-return paths - #354

Merged
punkpeye merged 3 commits into
punkpeye:mainfrom
pacocartones:fix/cimd-cancel-response-body
Sep 4, 2026
Merged

fix(auth): cancel CIMD response body on early-return paths#354
punkpeye merged 3 commits into
punkpeye:mainfrom
pacocartones:fix/cimd-cancel-response-body

Conversation

@pacocartones

Copy link
Copy Markdown
Contributor

Summary

src/auth/utils/cimd.ts leaked the fetch response body on two early-return paths — it was the only fetch site in the repo not calling cancelResponseBody, and didn't import it:

  • if (!response.ok) { return null; } — returned without consuming/cancelling response.body.
  • readBoundedText if (contentLength && Number(contentLength) > CIMD_MAX_RESPONSE_BYTES) { return null; } — returned before the body reader is created; body never cancelled.

Per src/cancelResponseBody.ts, Node won't free the socket until the body is consumed/cancelled, so a stream of failing CIMD lookups (reachable from the OAuth token/authorize endpoints via resolveCimdClient() for an HTTPS client_id, SEP-991) strands one socket each. Every other fetch site already guards this way (DiscoveryDocumentCache.ts, FastMCP.ts imageContent/audioContent).

Fix: await cancelResponseBody(response) before each early return null, matching the sibling convention.

Tests

Two tests in cimd.test.ts with a ReadableStream body whose cancel() sets a flag: a non-2xx response and an over-Content-Length response both assert resolveCimdClient returns null and body.cancel was called. RED before, GREEN after. Full auth suite 315 pass; tsc/prettier clean.

pacocartones and others added 3 commits September 4, 2026 00:53
resolveCimdClient() left the fetch response body unconsumed on two early
returns: the non-2xx branch and readBoundedText's Content-Length cap. Node
does not free the socket behind a fetch response until its body is consumed
or cancelled, so a client_id pointing at a server that answers non-2xx or
advertises an oversized Content-Length stranded one connection per request.

Await cancelResponseBody(response) before each early return, matching the
existing convention at DiscoveryDocumentCache and FastMCP.
@punkpeye
punkpeye merged commit 5b68657 into punkpeye:main Sep 4, 2026
2 checks passed
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 4.19.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants