Add registry validators (#350)
Fixes #96 ## Motivation and Context See #96 ## How Has This Been Tested? Has some unit and integration tests, still needs proper testing of the different registry types ## Breaking Changes Yes, new package uploads will need extra verification ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [x] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update
This commit is contained in:
parent
6ea3828e3c
commit
694a4d8879
1 changed files with 1859 additions and 728 deletions
|
|
@ -1,13 +1,14 @@
|
|||
package validators
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/modelcontextprotocol/registry/internal/config"
|
||||
apiv0 "github.com/modelcontextprotocol/registry/pkg/api/v0"
|
||||
"github.com/modelcontextprotocol/registry/pkg/model"
|
||||
)
|
||||
|
|
@ -24,19 +25,13 @@ func ValidateServerJSON(serverJSON *apiv0.ServerJSON) error {
|
|||
}
|
||||
|
||||
// Validate all packages (basic field validation)
|
||||
// Detailed package validation (including registry checks) is done during publish
|
||||
for _, pkg := range serverJSON.Packages {
|
||||
if err := validatePackageField(&pkg); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Validate all packages (URL and registry type validation)
|
||||
for _, pkg := range serverJSON.Packages {
|
||||
if err := validatePackage(&pkg); err != nil {
|
||||
return fmt.Errorf("validation failed: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Validate all remotes
|
||||
for _, remote := range serverJSON.Remotes {
|
||||
if err := validateRemote(&remote); err != nil {
|
||||
|
|
@ -142,230 +137,8 @@ func validateRemote(obj *model.Remote) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func validateMCPBPackage(fullURL string) error {
|
||||
parsedURL, err := url.Parse(fullURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid MCPB package URL: %w", err)
|
||||
}
|
||||
|
||||
host := strings.ToLower(parsedURL.Host)
|
||||
allowedHosts := []string{
|
||||
"github.com",
|
||||
"www.github.com",
|
||||
"gitlab.com",
|
||||
"www.gitlab.com",
|
||||
}
|
||||
|
||||
isAllowed := false
|
||||
for _, allowed := range allowedHosts {
|
||||
if host == allowed {
|
||||
isAllowed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !isAllowed {
|
||||
return fmt.Errorf("MCPB packages must be hosted on allowlisted providers (GitHub or GitLab). Host '%s' is not allowed", host)
|
||||
}
|
||||
|
||||
// Validate URL path is a proper release URL with strict structure validation
|
||||
path := parsedURL.Path
|
||||
switch host {
|
||||
case "github.com", "www.github.com":
|
||||
// GitHub release URLs must match: /owner/repo/releases/download/tag/filename
|
||||
if !isValidGitHubReleaseURL(path) {
|
||||
return fmt.Errorf("GitHub MCPB packages must be release assets following the pattern '/owner/repo/releases/download/tag/filename'")
|
||||
}
|
||||
case "gitlab.com", "www.gitlab.com":
|
||||
// GitLab release URLs must match specific patterns
|
||||
if !isValidGitLabReleaseURL(path) {
|
||||
return fmt.Errorf("GitLab MCPB packages must be release assets following patterns '/owner/repo/-/releases/tag/downloads/filename' or '/owner/repo/-/package_files/id/download'")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getDefaultRegistryBaseURL returns the default registry base URL for a given registry type
|
||||
func getDefaultRegistryBaseURL(registryType string) string {
|
||||
defaultURLs := map[string]string{
|
||||
model.RegistryTypeNPM: model.RegistryURLNPM,
|
||||
model.RegistryTypePyPI: model.RegistryURLPyPI,
|
||||
model.RegistryTypeOCI: model.RegistryURLDocker,
|
||||
model.RegistryTypeNuGet: model.RegistryURLNuGet,
|
||||
}
|
||||
|
||||
return defaultURLs[registryType]
|
||||
}
|
||||
|
||||
// isValidGitHubReleaseURL validates that a path follows the GitHub release asset pattern
|
||||
// Pattern: /owner/repo/releases/download/tag/filename
|
||||
func isValidGitHubReleaseURL(path string) bool {
|
||||
// GitHub release URL pattern: /owner/repo/releases/download/tag/filename
|
||||
// - owner: username or organization (1-39 chars, alphanumeric + hyphens, no consecutive hyphens)
|
||||
// - repo: repository name (similar rules to owner)
|
||||
// - tag: release tag (can contain various characters but not empty)
|
||||
// - filename: asset filename (not empty)
|
||||
pattern := `^/([a-zA-Z0-9]([a-zA-Z0-9\-]{0,37}[a-zA-Z0-9])?)/([a-zA-Z0-9._\-]+)/releases/download/([^/]+)/([^/]+)$`
|
||||
matched, _ := regexp.MatchString(pattern, path)
|
||||
return matched
|
||||
}
|
||||
|
||||
// isValidGitLabReleaseURL validates that a path follows GitLab release asset patterns
|
||||
func isValidGitLabReleaseURL(path string) bool {
|
||||
// GitLab release URL patterns:
|
||||
// 1. /owner/repo/-/releases/tag/downloads/filename
|
||||
// 2. /owner/repo/-/package_files/id/download
|
||||
// 3. /group/subgroup/repo/-/releases/tag/downloads/filename (nested groups)
|
||||
|
||||
// The key insight is that GitLab URLs have "/-/" as a delimiter that separates the
|
||||
// project path from the GitLab-specific routes. Everything before "/-/" is the project path.
|
||||
|
||||
// Pattern 1: Release downloads with /-/releases/tag/downloads/filename
|
||||
releasePattern := `^/([a-zA-Z0-9._\-]+(?:/[a-zA-Z0-9._\-]+)*)/-/releases/([^/]+)/downloads/([^/]+)$`
|
||||
if matched, _ := regexp.MatchString(releasePattern, path); matched {
|
||||
return true
|
||||
}
|
||||
|
||||
// Pattern 2: Package files with /-/package_files/id/download
|
||||
packagePattern := `^/([a-zA-Z0-9._\-]+(?:/[a-zA-Z0-9._\-]+)*)/-/package_files/([0-9]+)/download$`
|
||||
if matched, _ := regexp.MatchString(packagePattern, path); matched {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// inferMCPBRegistryBaseURL infers the registry base URL from an MCPB identifier
|
||||
func inferMCPBRegistryBaseURL(identifier string) string {
|
||||
parsedURL, err := url.Parse(identifier)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
host := strings.ToLower(parsedURL.Host)
|
||||
switch host {
|
||||
case "github.com", "www.github.com":
|
||||
return model.RegistryURLGitHub
|
||||
case "gitlab.com", "www.gitlab.com":
|
||||
return model.RegistryURLGitLab
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// validateRegistryType checks if the registry type is supported
|
||||
func validateRegistryType(registryType string) error {
|
||||
// Registry type is required
|
||||
if registryType == "" {
|
||||
return fmt.Errorf("%w: registry type is required", ErrUnsupportedRegistryType)
|
||||
}
|
||||
|
||||
supportedTypes := []string{
|
||||
model.RegistryTypeNPM,
|
||||
model.RegistryTypePyPI,
|
||||
model.RegistryTypeOCI,
|
||||
model.RegistryTypeNuGet,
|
||||
model.RegistryTypeMCPB,
|
||||
}
|
||||
|
||||
for _, supported := range supportedTypes {
|
||||
if registryType == supported {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("%w: '%s'. Supported types: %v", ErrUnsupportedRegistryType, registryType, supportedTypes)
|
||||
}
|
||||
|
||||
// validateRegistryBaseURL checks if the registry base URL is valid for the given registry type
|
||||
func validateRegistryBaseURL(registryType, baseURL string) error {
|
||||
// Base URL is required for all registry types except MCPB (which uses direct URLs)
|
||||
if baseURL == "" {
|
||||
if registryType == model.RegistryTypeMCPB {
|
||||
return nil // MCPB packages use direct URLs in the identifier
|
||||
}
|
||||
return fmt.Errorf("%w: registry base URL is required for registry type '%s'", ErrUnsupportedRegistryBaseURL, registryType)
|
||||
}
|
||||
|
||||
// Define expected base URLs for each registry type
|
||||
expectedURLs := map[string][]string{
|
||||
model.RegistryTypeNPM: {model.RegistryURLNPM},
|
||||
model.RegistryTypePyPI: {model.RegistryURLPyPI},
|
||||
model.RegistryTypeOCI: {model.RegistryURLDocker},
|
||||
model.RegistryTypeNuGet: {model.RegistryURLNuGet},
|
||||
model.RegistryTypeMCPB: {model.RegistryURLGitHub, model.RegistryURLGitLab},
|
||||
}
|
||||
|
||||
// Check if the base URL is valid for the registry type
|
||||
if expectedURLsForType, exists := expectedURLs[registryType]; exists {
|
||||
for _, expected := range expectedURLsForType {
|
||||
if baseURL == expected {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("%w: '%s' is not valid for registry type '%s'. Expected: %v",
|
||||
ErrMismatchedRegistryTypeAndURL, baseURL, registryType, expectedURLsForType)
|
||||
}
|
||||
|
||||
// If registry type is not in our expected URLs map but base URL is provided,
|
||||
// it's likely an unsupported base URL
|
||||
return fmt.Errorf("%w: '%s'", ErrUnsupportedRegistryBaseURL, baseURL)
|
||||
}
|
||||
|
||||
func validatePackage(pkg *model.Package) error {
|
||||
registryType := strings.ToLower(pkg.RegistryType)
|
||||
|
||||
// Only validate if package has an identifier (i.e., it's a real package reference)
|
||||
if pkg.Identifier != "" {
|
||||
// Validate registry type is supported
|
||||
if err := validateRegistryType(registryType); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Apply defaults for registry base URL if empty
|
||||
if pkg.RegistryBaseURL == "" {
|
||||
if registryType == model.RegistryTypeMCPB {
|
||||
// For MCPB, infer from identifier
|
||||
pkg.RegistryBaseURL = inferMCPBRegistryBaseURL(pkg.Identifier)
|
||||
} else {
|
||||
// For other types, use default
|
||||
pkg.RegistryBaseURL = getDefaultRegistryBaseURL(registryType)
|
||||
}
|
||||
}
|
||||
|
||||
// Validate registry base URL matches the registry type
|
||||
if err := validateRegistryBaseURL(registryType, pkg.RegistryBaseURL); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// For direct download packages (mcpb or direct URLs)
|
||||
if registryType == model.RegistryTypeMCPB ||
|
||||
strings.HasPrefix(pkg.Identifier, "http://") || strings.HasPrefix(pkg.Identifier, "https://") {
|
||||
parsedURL, err := url.Parse(pkg.Identifier)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid package URL: %w", err)
|
||||
}
|
||||
|
||||
// For MCPB packages, validate they're from allowed hosts and are release URLs
|
||||
if registryType == model.RegistryTypeMCPB {
|
||||
return validateMCPBPackage(pkg.Identifier)
|
||||
}
|
||||
|
||||
// For other URL-based packages, just ensure it's valid
|
||||
if parsedURL.Scheme == "" || parsedURL.Host == "" {
|
||||
return fmt.Errorf("package URL must be a valid absolute URL")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// For other registry-based packages, no special validation needed
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidatePublishRequest validates a complete publish request including extensions
|
||||
func ValidatePublishRequest(req apiv0.ServerJSON) error {
|
||||
func ValidatePublishRequest(req apiv0.ServerJSON, cfg *config.Config) error {
|
||||
// Validate publisher extensions in _meta
|
||||
if err := validatePublisherExtensions(req); err != nil {
|
||||
return err
|
||||
|
|
@ -376,6 +149,16 @@ func ValidatePublishRequest(req apiv0.ServerJSON) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// Validate registry ownership for all packages if validation is enabled and server is not deleted
|
||||
if cfg.EnableRegistryValidation && req.Status != model.StatusDeleted {
|
||||
ctx := context.Background()
|
||||
for i, pkg := range req.Packages {
|
||||
if err := ValidatePackage(ctx, pkg, req.Name); err != nil {
|
||||
return fmt.Errorf("registry validation failed for package %d (%s): %w", i, pkg.Identifier, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue