fix(validators): harden mcp-name matching (PyPI/NuGet anchoring, comment-form safe) + cargo follow-ups (#1331)

Registry-wide hardening of the `mcp-name:` ownership-token match, plus
the cargo follow-up fixes from the #1330 review — consolidated here per
maintainer request. Rebased onto current `main` (the merged #1330 cargo
commit drops out), so the diff is just the net-new work.

### Changes
- **PyPI/NuGet: boundary-anchored token match.** Replace
`strings.Contains(readme, "mcp-name: "+name)` with the shared
`containsMCPNameToken`, so a README declaring a longer name
(`…/widget-pro`) no longer satisfies an ownership claim for a shorter
prefix (`…/widget`). (Cargo already got this in #1330; NPM is unaffected
— it compares an exact metadata field.)
- **Matcher: treat the HTML comment close as a boundary.** `<!--
mcp-name: NAME-->` / `<!--mcp-name: NAME-->` (any spacing) validate
again — the documented hidden-comment form for PyPI/NuGet — while a
genuine longer name (`…/widget--pro`) still does not.
- **Cargo hardening (review of #1330):** pin scheme+port (not just host)
on the README fetch and redirects; a rate-limited/failed crate-version
existence probe now reports *transient* instead of "not found"; a 403
with the crate present no longer flatly asserts "no README".
- **Docs:** note the token must be followed by a boundary.

### ⚠️ Behavior change for PyPI/NuGet — read before merging
The match is **strictly stricter** (verified by fuzz, 2.3M execs: it can
only flip pass→fail, never fail→pass). After the comment-close fix, the
**only** forms that newly fail are unusual *inline* ones: the token
ending a sentence (`…/my-mcp.`) or glued to a trailing `/`. The
documented forms (own line, in `<!-- … -->`) are unaffected.

**Correcting an earlier claim:** this re-validates on **edits too**, not
just new versions — `edit.go → UpdateServer → ValidateUpdateRequest →
validateRegistryOwnership` runs the token check on any edit of a live
server (only delete-transitions skip it). So an existing PyPI/NuGet
server whose README uses one of the breaking inline forms would fail on
its next publish *or* edit. Given the v0.1 API-freeze posture, this
should land as a deliberate, noted change.

### Testing
`go build`, `vet`, `gofmt`, `golangci-lint` (prod files),
`check-schema`, `validate-examples` all clean. Hermetic + **live**
PyPI/NuGet/cargo suite passes. New: `TestCargoURLAllowed`, a
combined-fixture probe-429→transient case, comment-form matcher cases,
and `FuzzContainsMCPNameToken` (the strictly-stricter property, 2.3M
execs).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Radoslav Dimitrov 2026-06-05 02:28:41 -04:00 • committed by GitHub
commit 04623ed925
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -340,6 +340,7 @@ func TestValidateCargoCombinedFixture(t *testing.T) {
readmeStatus int
readmeBody string
versionExists bool // response for the /api/v1/crates/{n}/{v} existence probe (403 disambiguation)
versionProbe int // if non-zero, the existence probe returns this status (overrides versionExists)
wantErr bool
wantContains []string
wantNotContains []string
@ -377,8 +378,9 @@ func TestValidateCargoCombinedFixture(t *testing.T) {
wantNotContains: []string{"has no rendered README"},
},
{
// Crate/version exists but has no rendered README: CDN 403 + existence
// probe 200. Must NOT be reported as "not found".
// Crate/version exists but the README CDN 403s: existence probe 200.
// Must NOT be reported as "not found", and must not flatly assert the
// README is absent (a 403 isn't definitive proof of that).
name: "readme_403_no_readme",
crateName: "combined-readme403-noreadme",
version: "0.1.0",
@ -386,7 +388,20 @@ func TestValidateCargoCombinedFixture(t *testing.T) {
readmeStatus: http.StatusForbidden,
versionExists: true,
wantErr: true,
wantContains: []string{"has no rendered README"},
wantContains: []string{"exists on crates.io", "could not be retrieved"},
wantNotContains: []string{"not found"},
},
{
// CDN 403 + the existence probe itself is rate-limited (429): existence
// is undetermined, so report transient/retryable, NOT "not found".
name: "readme_403_probe_transient",
crateName: "combined-readme403-probe429",
version: "0.1.0",
metaStatus: http.StatusOK,
readmeStatus: http.StatusForbidden,
versionProbe: http.StatusTooManyRequests,
wantErr: true,
wantContains: []string{"transient"},
wantNotContains: []string{"not found"},
},
{
@ -421,6 +436,18 @@ func TestValidateCargoCombinedFixture(t *testing.T) {
wantErr: true,
wantContains: []string{"ownership validation failed"},
},
{
// Token present but glued to a trailing period — the error must explain
// the boundary cause, not tell the publisher to add a token they can see.
name: "glued_trailing_period_explained",
crateName: "combined-glued",
version: "0.1.0",
metaStatus: http.StatusOK,
readmeStatus: http.StatusOK,
readmeBody: fmt.Sprintf("<html><body><p>mcp-name: %s.</p></body></html>", serverName),
wantErr: true,
wantContains: []string{"immediately followed by", `"."`},
},
}
// lastMetaPath captures the metadata request path seen by the handler so
@ -450,10 +477,13 @@ func TestValidateCargoCombinedFixture(t *testing.T) {
}
// Existence probe used to disambiguate a README 403.
if r.URL.Path == versionPath {
if tt.versionExists {
switch {
case tt.versionProbe != 0:
http.Error(w, "simulated probe status", tt.versionProbe)
case tt.versionExists:
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{"version": map[string]string{"num": tt.version}})
} else {
default:
http.Error(w, "not found", http.StatusNotFound)
}
return