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>
97 lines
4 KiB
Go
97 lines
4 KiB
Go
package registries
|
|
|
|
import "strings"
|
|
|
|
// isServerNameChar reports whether c can appear in an MCP server name.
|
|
//
|
|
// Server names follow the schema pattern ^[a-zA-Z0-9.-]+/[a-zA-Z0-9._-]+$
|
|
// (reverse-DNS namespace + "/" + name), so the full set of characters that may
|
|
// continue a server name is [A-Za-z0-9._/-].
|
|
func isServerNameChar(c byte) bool {
|
|
switch {
|
|
case c >= 'a' && c <= 'z', c >= 'A' && c <= 'Z', c >= '0' && c <= '9':
|
|
return true
|
|
case c == '.', c == '-', c == '_', c == '/':
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// isMCPNameBoundary reports whether the content immediately following a matched
|
|
// server name terminates the token, so that the matched name is not merely a
|
|
// prefix of a longer declared name.
|
|
//
|
|
// A boundary is the end of content, any non-server-name character (whitespace, a
|
|
// newline, or an HTML tag delimiter such as `<`), or the start of an HTML comment
|
|
// close (`-->` / `--!>`). The comment-close case matters because PyPI and NuGet
|
|
// publishers commonly hide the token in `<!-- mcp-name: NAME -->`, and authors
|
|
// (or minifiers) frequently omit the space before `-->`, producing
|
|
// `<!-- mcp-name: NAME-->`. There the byte after NAME is `-`, which is a
|
|
// server-name character, so without this case the documented hidden-comment form
|
|
// would fail to validate.
|
|
func isMCPNameBoundary(rest string) bool {
|
|
if rest == "" {
|
|
return true
|
|
}
|
|
if !isServerNameChar(rest[0]) {
|
|
return true
|
|
}
|
|
return strings.HasPrefix(rest, "-->") || strings.HasPrefix(rest, "--!>")
|
|
}
|
|
|
|
// containsMCPNameToken reports whether the package README/description contains
|
|
// the ownership token "mcp-name: <serverName>" as a complete token — i.e. the
|
|
// matched server name is not merely a prefix of a longer declared name.
|
|
//
|
|
// A bare strings.Contains check is vulnerable to prefix confusion: a README that
|
|
// legitimately declares `mcp-name: io.github.acme/widget-pro` would otherwise
|
|
// satisfy an ownership claim for the shorter `io.github.acme/widget`, because the
|
|
// shorter string is a substring of the longer one. This is contained by namespace
|
|
// authorization (a publisher can only claim names within a namespace they own),
|
|
// but it still weakens the package↔server-name binding the token is meant to
|
|
// prove, so we require a trailing boundary after the server name (see
|
|
// isMCPNameBoundary).
|
|
//
|
|
// Shared by the README-token validators (PyPI, NuGet, Cargo). NPM is unaffected
|
|
// because it compares an exact metadata field rather than scanning README text.
|
|
func containsMCPNameToken(content, serverName string) bool {
|
|
token := "mcp-name: " + serverName
|
|
searchFrom := 0
|
|
for {
|
|
idx := strings.Index(content[searchFrom:], token)
|
|
if idx < 0 {
|
|
return false
|
|
}
|
|
tokenEnd := searchFrom + idx + len(token)
|
|
if isMCPNameBoundary(content[tokenEnd:]) {
|
|
return true
|
|
}
|
|
// This occurrence is a prefix of a longer name; keep scanning in case a
|
|
// properly-terminated occurrence appears later in the content.
|
|
searchFrom = searchFrom + idx + 1
|
|
}
|
|
}
|
|
|
|
// mcpNameTokenGluedTrailing explains why a visibly-present token failed to match.
|
|
// When containsMCPNameToken has already returned false, it reports whether the
|
|
// literal "mcp-name: <serverName>" string is nonetheless present and, if so, the
|
|
// character immediately following it — i.e. the trailing character that made the
|
|
// occurrence look like a prefix of a longer name rather than a complete token.
|
|
// Validators use it to turn an unhelpful "token must appear" message into an
|
|
// actionable "found it, but it's glued to %q — put it on its own line" message.
|
|
// Returns ("", false) when the literal token is absent (a genuinely missing token).
|
|
func mcpNameTokenGluedTrailing(content, serverName string) (trailing string, glued bool) {
|
|
token := "mcp-name: " + serverName
|
|
idx := strings.Index(content, token)
|
|
if idx < 0 {
|
|
return "", false
|
|
}
|
|
end := idx + len(token)
|
|
if end >= len(content) {
|
|
// A token at end-of-content is a valid boundary, so containsMCPNameToken
|
|
// would not have failed; be defensive and treat it as not-glued.
|
|
return "", false
|
|
}
|
|
return string(content[end]), true
|
|
}
|