fix(registry): block SSRF redirects and cap response bodies (#21)#47
Merged
Conversation
The shared RegistryClient HTTP path had two SSRF/DoS gaps: 1. Redirect following — fetch ran without redirect:"manual", so a registry (or attacker-supplied baseUrl) could 30x-redirect us to internal hosts (169.254.169.254, localhost). Now we set redirect:"manual" and treat any opaqueredirect / 3xx as a RegistryError instead of following it. 2. Unbounded body — await response.json() read the whole body with transparent gzip/br decompression and no cap (decompression-bomb → OOM). Now we reject on an over-cap Content-Length and read streamed bodies chunk-by-chunk, aborting once MAX_RESPONSE_BYTES (10 MB) is exceeded. Also validate the caller-overridable baseUrl up front (https-only, no embedded creds, reject loopback/private/IPv4-mapped-IPv6 hosts) by reusing isPrivateHost from publish-client (now exported). Mirrors validateRegistryUrl for #17. Adds tests covering redirect rejection, oversized Content-Length, oversized streamed body, and baseUrl validation. Closes #21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Hardens the shared
RegistryClientHTTP path (src/registry/client.ts) against SSRF and decompression-bomb DoS, addressing the two issues in #21.fetchnow runs withredirect:"manual". Anyopaqueredirect(status 0) or explicit 3xx is rejected as aRegistryErrorinstead of being followed to an attacker-chosen/internal host (169.254.169.254,localhost, ...).response.json()with a capped read: an over-capContent-Lengthis rejected before reading; streamed bodies are read chunk-by-chunk and aborted onceMAX_RESPONSE_BYTES(10 MB) is exceeded — before fully decompressing a bomb. Non-stream/injected responses fall back tojson()under the Content-Length guard.baseUrlvalidation. The caller-overridablebaseUrlis now validated in the constructor (https-only, no embedded credentials, reject loopback/private/IPv4-mapped-IPv6 hosts) by reusingisPrivateHostfrompublish-client.ts(now exported). MirrorsvalidateRegistryUrlfrom [security][HIGH] mcpm publish sends GitHub token to arbitrary --registry host (token exfiltration) #17.Why
baseUrlis fully overridable and responses were host-validated only by Zod (shape), not by origin. A registry response or maliciousbaseUrlcould redirect the client to internal addresses, andawait response.json()read unbounded bodies with transparent gzip/br decompression — a small compressed payload could expand to GBs (OOM).Tests
Added to
src/registry/registry.test.ts(all fail on pre-fix code):opaqueredirect/ 302) is rejected, andredirect:"manual"is passed to fetchContent-Lengthrejected before body readbaseUrlvalidation: http, loopback,169.254.169.254,10.x, IPv4-mapped IPv6 loopback, embedded creds, malformed — all rejected; public https and default acceptedpnpm lint && pnpm test && pnpm buildall pass (1128 tests, +15).Scope is limited to
src/registry/so this PR stays independently mergeable.Closes #21
🤖 Generated with Claude Code