|
| 1 | +package registries |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/xml" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/modelcontextprotocol/registry/pkg/model" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + ErrMissingIdentifierForMaven = errors.New("package identifier is required for Maven packages") |
| 19 | + ErrMissingVersionForMaven = errors.New("package version is required for Maven packages") |
| 20 | + ErrInvalidMavenIdentifier = errors.New("maven package identifier must be in 'groupId:artifactId' form (e.g., 'org.example:my-mcp-server')") |
| 21 | +) |
| 22 | + |
| 23 | +// mavenPOM represents the subset of Maven POM XML fields used to validate |
| 24 | +// MCP server ownership. Only the fields we read are declared. |
| 25 | +type mavenPOM struct { |
| 26 | + XMLName xml.Name `xml:"project"` |
| 27 | + GroupID string `xml:"groupId"` |
| 28 | + ArtifactID string `xml:"artifactId"` |
| 29 | + Description string `xml:"description"` |
| 30 | + Properties struct { |
| 31 | + // Maven properties is a free-form element; we look for an |
| 32 | + // `mcpName` property as the most explicit ownership signal. |
| 33 | + MCPName string `xml:"mcpName"` |
| 34 | + } `xml:"properties"` |
| 35 | + // Parent POM may set groupId for the project; we do not currently |
| 36 | + // follow parent inheritance because the published POM should declare |
| 37 | + // its own groupId for ownership purposes. |
| 38 | +} |
| 39 | + |
| 40 | +// ValidateMaven validates that a Maven Central package contains the correct |
| 41 | +// MCP server name. Identifier is "groupId:artifactId" (e.g., "org.example:my-mcp-server") |
| 42 | +// and Version must be a concrete release (no version ranges). |
| 43 | +// |
| 44 | +// Ownership is verified by fetching the published POM at: |
| 45 | +// |
| 46 | +// {baseURL}/{groupPath}/{artifactId}/{version}/{artifactId}-{version}.pom |
| 47 | +// |
| 48 | +// and looking for the server name as either: |
| 49 | +// 1. an `<mcpName>` Maven property, or |
| 50 | +// 2. an `mcp-name: <serverName>` line in the POM `<description>`. |
| 51 | +// |
| 52 | +// Maven Central already validates `groupId` domain ownership at publish time, |
| 53 | +// so a matching declaration in the POM is sufficient to bind a package to an |
| 54 | +// MCP server name in the same namespace. |
| 55 | +func ValidateMaven(ctx context.Context, pkg model.Package, serverName string) error { |
| 56 | + if pkg.RegistryBaseURL == "" { |
| 57 | + pkg.RegistryBaseURL = model.RegistryURLMaven |
| 58 | + } |
| 59 | + |
| 60 | + if pkg.Identifier == "" { |
| 61 | + return ErrMissingIdentifierForMaven |
| 62 | + } |
| 63 | + if pkg.Version == "" { |
| 64 | + return ErrMissingVersionForMaven |
| 65 | + } |
| 66 | + |
| 67 | + // Maven packages must not carry MCPB-only fields. |
| 68 | + if pkg.FileSHA256 != "" { |
| 69 | + return fmt.Errorf("maven packages must not have 'fileSha256' field - this is only for MCPB packages") |
| 70 | + } |
| 71 | + |
| 72 | + // Maven Central is the only allowed base URL for the official registry. |
| 73 | + if pkg.RegistryBaseURL != model.RegistryURLMaven { |
| 74 | + return fmt.Errorf("registry type and base URL do not match: '%s' is not valid for registry type '%s'. Expected: %s", |
| 75 | + pkg.RegistryBaseURL, model.RegistryTypeMaven, model.RegistryURLMaven) |
| 76 | + } |
| 77 | + |
| 78 | + return validateMavenAgainst(ctx, pkg.RegistryBaseURL, pkg, serverName) |
| 79 | +} |
| 80 | + |
| 81 | +// validateMavenAgainst is the common validation routine, parameterized on |
| 82 | +// the base URL so tests can point it at an httptest server without going |
| 83 | +// through the Maven Central allowlist check. |
| 84 | +func validateMavenAgainst(ctx context.Context, baseURL string, pkg model.Package, serverName string) error { |
| 85 | + if pkg.Identifier == "" { |
| 86 | + return ErrMissingIdentifierForMaven |
| 87 | + } |
| 88 | + if pkg.Version == "" { |
| 89 | + return ErrMissingVersionForMaven |
| 90 | + } |
| 91 | + |
| 92 | + groupID, artifactID, err := parseMavenIdentifier(pkg.Identifier) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + pom, err := fetchMavenPOM(ctx, baseURL, groupID, artifactID, pkg.Version) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + // Sanity-check that the POM matches the declared coordinates. This catches |
| 103 | + // publishing mistakes and proxy/mirror misconfiguration. |
| 104 | + if pom.GroupID != "" && pom.GroupID != groupID { |
| 105 | + return fmt.Errorf("maven POM groupId '%s' does not match identifier groupId '%s'", pom.GroupID, groupID) |
| 106 | + } |
| 107 | + if pom.ArtifactID != "" && pom.ArtifactID != artifactID { |
| 108 | + return fmt.Errorf("maven POM artifactId '%s' does not match identifier artifactId '%s'", pom.ArtifactID, artifactID) |
| 109 | + } |
| 110 | + |
| 111 | + // Preferred: explicit `<mcpName>` property in the POM. |
| 112 | + if pom.Properties.MCPName != "" { |
| 113 | + if pom.Properties.MCPName != serverName { |
| 114 | + return fmt.Errorf("maven package ownership validation failed. Expected mcpName '%s', got '%s'", serverName, pom.Properties.MCPName) |
| 115 | + } |
| 116 | + return nil |
| 117 | + } |
| 118 | + |
| 119 | + // Fallback: `mcp-name: <serverName>` in the POM description (mirrors the PyPI/NuGet pattern). |
| 120 | + mcpNamePattern := "mcp-name: " + serverName |
| 121 | + if strings.Contains(pom.Description, mcpNamePattern) { |
| 122 | + return nil |
| 123 | + } |
| 124 | + |
| 125 | + return fmt.Errorf( |
| 126 | + "maven package '%s:%s' ownership validation failed. Add either an `<mcpName>%s</mcpName>` property to the published POM or an 'mcp-name: %s' line to the POM `<description>`", |
| 127 | + groupID, artifactID, serverName, serverName, |
| 128 | + ) |
| 129 | +} |
| 130 | + |
| 131 | +// parseMavenIdentifier splits a "groupId:artifactId" coordinate. Both parts |
| 132 | +// must be non-empty; we deliberately reject the three-part "g:a:v" form so |
| 133 | +// version always lives in the dedicated Version field (consistent with how |
| 134 | +// other JVM-aware MCP clients consume coordinates). |
| 135 | +func parseMavenIdentifier(identifier string) (groupID, artifactID string, err error) { |
| 136 | + parts := strings.Split(identifier, ":") |
| 137 | + if len(parts) != 2 { |
| 138 | + return "", "", ErrInvalidMavenIdentifier |
| 139 | + } |
| 140 | + groupID, artifactID = parts[0], parts[1] |
| 141 | + if groupID == "" || artifactID == "" { |
| 142 | + return "", "", ErrInvalidMavenIdentifier |
| 143 | + } |
| 144 | + return groupID, artifactID, nil |
| 145 | +} |
| 146 | + |
| 147 | +// fetchMavenPOM downloads and parses the published POM for the coordinate. |
| 148 | +func fetchMavenPOM(ctx context.Context, baseURL, groupID, artifactID, version string) (*mavenPOM, error) { |
| 149 | + pomURL := buildPOMURL(baseURL, groupID, artifactID, version) |
| 150 | + |
| 151 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, pomURL, nil) |
| 152 | + if err != nil { |
| 153 | + return nil, fmt.Errorf("failed to create Maven POM request: %w", err) |
| 154 | + } |
| 155 | + req.Header.Set("User-Agent", userAgent) |
| 156 | + req.Header.Set("Accept", "application/xml") |
| 157 | + |
| 158 | + client := &http.Client{Timeout: 10 * time.Second} |
| 159 | + resp, err := client.Do(req) |
| 160 | + if err != nil { |
| 161 | + return nil, fmt.Errorf("failed to fetch Maven POM: %w", err) |
| 162 | + } |
| 163 | + defer resp.Body.Close() |
| 164 | + |
| 165 | + if resp.StatusCode == http.StatusNotFound { |
| 166 | + return nil, fmt.Errorf("maven package '%s:%s' version '%s' not found at %s", groupID, artifactID, version, pomURL) |
| 167 | + } |
| 168 | + if resp.StatusCode != http.StatusOK { |
| 169 | + return nil, fmt.Errorf("maven POM request returned status %d", resp.StatusCode) |
| 170 | + } |
| 171 | + |
| 172 | + // Cap the body so a hostile mirror can't exhaust memory; published POMs |
| 173 | + // are typically only a few KB. |
| 174 | + body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) |
| 175 | + if err != nil { |
| 176 | + return nil, fmt.Errorf("failed to read Maven POM: %w", err) |
| 177 | + } |
| 178 | + |
| 179 | + var pom mavenPOM |
| 180 | + if err := xml.Unmarshal(body, &pom); err != nil { |
| 181 | + return nil, fmt.Errorf("failed to parse Maven POM: %w", err) |
| 182 | + } |
| 183 | + return &pom, nil |
| 184 | +} |
| 185 | + |
| 186 | +// buildPOMURL constructs the canonical POM URL on the Maven repository |
| 187 | +// layout: groupId dots become path slashes. |
| 188 | +func buildPOMURL(baseURL, groupID, artifactID, version string) string { |
| 189 | + groupPath := strings.ReplaceAll(groupID, ".", "/") |
| 190 | + // PathEscape each segment to keep e.g. unusual artifact ids safe. |
| 191 | + return strings.TrimRight(baseURL, "/") + "/" + |
| 192 | + groupPath + "/" + |
| 193 | + url.PathEscape(artifactID) + "/" + |
| 194 | + url.PathEscape(version) + "/" + |
| 195 | + url.PathEscape(artifactID) + "-" + url.PathEscape(version) + ".pom" |
| 196 | +} |
0 commit comments