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:
Radoslav Dimitrov 2026-04-29 19:14:00 -04:00 • committed by GitHub
commit 1201cbd82b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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 {