security: fix open redirect and add small hardening (#1227)
## What this does Fixes an open redirect plus a handful of small, mostly-invisible hardening changes around it. ## What's in it - **The fix**: trailing-slash redirects no longer let `//evil.com/` redirect off-host. - **SSRF block** on the DNS/HTTP namespace verification — refuses to dial loopback, RFC1918, link-local, multicast, CGNAT, or IP-literal/single-label \"domains\". - **Slow-client guard**: `ReadTimeout` and `IdleTimeout` on the HTTP server (no `WriteTimeout` — would risk cutting off legitimate multi-package publishes). - **Defence-in-depth headers** on the UI (`/`) and 404 responses. - **5xx responses** stop leaking raw DB error text to clients (logged server-side instead). - **Search filter** stops treating `%` and `_` as wildcards. - **Validators**: PyPI/NuGet identifiers URL-escaped before fetching; MCPB no longer follows redirects. - **Misc**: `Version` field bounded to 255 chars; logs quote user input; `BlockedNamespaces` denylist now catches subdomain claimants. Each change is its own commit so they can be reviewed (or reverted) independently. ## Test plan - [ ] CI green - [ ] Synthetic publish in staging for each package type (npm, pypi, nuget, oci, mcpb) — confirms validator changes don't regress - [ ] Quick browser check of `/` after deploy to confirm UI still loads with the new CSP - [ ] `curl https://<host>//evil.com/` — confirm the open redirect is closed 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
f7663fe91a
commit
1201cbd82b
4 changed files with 330 additions and 48 deletions
|
|
@ -54,8 +54,17 @@ func ValidateMCPB(ctx context.Context, pkg model.Package, _ string) error {
|
|||
return fmt.Errorf("MCPB package URL must contain 'mcp': %s", pkg.Identifier)
|
||||
}
|
||||
|
||||
// Verify the file exists and is publicly accessible
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
// Verify the file exists and is publicly accessible. Refuse to follow
|
||||
// redirects: the URL allowlist (github.com / gitlab.com) only constrains
|
||||
// the FIRST hop, and a 30x bouncing through CDN/release-asset hosts
|
||||
// could otherwise be steered toward attacker infrastructure or
|
||||
// internal-only endpoints reached via DNS quirks.
|
||||
client := &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodHead, pkg.Identifier, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create request: %w", err)
|
||||
|
|
@ -69,7 +78,15 @@ func ValidateMCPB(ctx context.Context, pkg model.Package, _ string) error {
|
|||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
// GitHub serves release assets via a 302 to a signed S3 URL; a HEAD that
|
||||
// returns 200 directly OR a 302 with a Location header is acceptable.
|
||||
// Any other 3xx without a usable Location is treated as inaccessible.
|
||||
switch {
|
||||
case resp.StatusCode == http.StatusOK:
|
||||
// fine
|
||||
case resp.StatusCode >= 300 && resp.StatusCode < 400 && resp.Header.Get("Location") != "":
|
||||
// fine — first-hop allowlist + we don't actually need the body
|
||||
default:
|
||||
return fmt.Errorf("MCPB package '%s' is not publicly accessible (status: %d)", pkg.Identifier, resp.StatusCode)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
|
@ -209,9 +210,11 @@ func validateReadme(ctx context.Context, serverName, lowerID, lowerVersion strin
|
|||
return NoReadme, fmt.Errorf("failed to get README URL template: %w", err)
|
||||
}
|
||||
|
||||
// Replace placeholders in the template
|
||||
readmeURL := strings.ReplaceAll(readmeURLTemplate, "{lower_id}", lowerID)
|
||||
readmeURL = strings.ReplaceAll(readmeURL, "{lower_version}", lowerVersion)
|
||||
// Replace placeholders in the template. PathEscape both the id and version
|
||||
// so a publisher cannot smuggle "/" / ".." through the template into a
|
||||
// fetch against an unrelated package's README.
|
||||
readmeURL := strings.ReplaceAll(readmeURLTemplate, "{lower_id}", url.PathEscape(lowerID))
|
||||
readmeURL = strings.ReplaceAll(readmeURL, "{lower_version}", url.PathEscape(lowerVersion))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, readmeURL, nil)
|
||||
if err != nil {
|
||||
return NoReadme, fmt.Errorf("failed to create NuGet README request: %w", err)
|
||||
|
|
@ -266,8 +269,10 @@ func validatePackageExists(ctx context.Context, lowerID, lowerVersion string, cl
|
|||
return PackageIDNotFound, fmt.Errorf("failed to get Package Base URL: %w", err)
|
||||
}
|
||||
|
||||
// Fetch the package content index to check if package ID and version exist
|
||||
indexURL := fmt.Sprintf("%s/%s/index.json", strings.TrimRight(packageBaseURL, "/"), lowerID)
|
||||
// Fetch the package content index to check if package ID and version exist.
|
||||
// PathEscape so an identifier that smuggles "/" or ".." cannot redirect
|
||||
// the metadata fetch to a different package than the one being claimed.
|
||||
indexURL := fmt.Sprintf("%s/%s/index.json", strings.TrimRight(packageBaseURL, "/"), url.PathEscape(lowerID))
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, indexURL, nil)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
|
@ -52,8 +53,13 @@ func ValidatePyPI(ctx context.Context, pkg model.Package, serverName string) err
|
|||
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
|
||||
url := fmt.Sprintf("%s/pypi/%s/%s/json", pkg.RegistryBaseURL, pkg.Identifier, pkg.Version)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
// PathEscape so an identifier that smuggles "/" or ".." cannot redirect
|
||||
// the metadata fetch to a different package than the one being claimed.
|
||||
fetchURL := fmt.Sprintf("%s/pypi/%s/%s/json",
|
||||
pkg.RegistryBaseURL,
|
||||
url.PathEscape(pkg.Identifier),
|
||||
url.PathEscape(pkg.Version))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fetchURL, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -283,6 +283,7 @@
|
|||
"description": "Package version. Must be a specific version. Version ranges are rejected (e.g., '^1.2.3', '~1.2.3', '\u003e=1.2.3', '1.x', '1.*').",
|
||||
"example": "1.0.2",
|
||||
"minLength": 1,
|
||||
"maxLength": 255,
|
||||
"not": {
|
||||
"const": "latest"
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in a new issue