|
| 1 | +package registries |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/modelcontextprotocol/registry/pkg/model" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + ErrMissingIdentifierForGo = errors.New("package identifier (the module path, e.g. github.com/owner/repo) is required for Go packages") |
| 16 | + ErrMissingVersionForGo = errors.New("package version is required for Go packages") |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + goGitHubNamespacePrefix = "io.github." |
| 21 | + goGitHubModulePrefix = "github.com/" |
| 22 | +) |
| 23 | + |
| 24 | +// ValidateGo validates that a Go module package is owned by the publisher and |
| 25 | +// exists on the Go module proxy. |
| 26 | +// |
| 27 | +// Ownership: a Go module path is its own source location, so for the v1 |
| 28 | +// GitHub-namespace model a server published under "io.github.<owner>" must use |
| 29 | +// a module path rooted at "github.com/<owner>/...". The registry already |
| 30 | +// authenticated the publisher for that GitHub namespace, so matching the owner |
| 31 | +// segment proves ownership without an mcpName-style marker (which would not be |
| 32 | +// idiomatic in a go.mod). Non-GitHub module paths are intentionally left for a |
| 33 | +// follow-up. |
| 34 | +// |
| 35 | +// Existence: the module version is verified against the Go module proxy |
| 36 | +// (proxy.golang.org), the canonical source of truth. |
| 37 | +func ValidateGo(ctx context.Context, pkg model.Package, serverName string) error { |
| 38 | + if pkg.RegistryBaseURL == "" { |
| 39 | + pkg.RegistryBaseURL = model.RegistryURLGoProxy |
| 40 | + } |
| 41 | + if pkg.Identifier == "" { |
| 42 | + return ErrMissingIdentifierForGo |
| 43 | + } |
| 44 | + if pkg.Version == "" { |
| 45 | + return ErrMissingVersionForGo |
| 46 | + } |
| 47 | + // Validate that MCPB-specific fields are not present |
| 48 | + if pkg.FileSHA256 != "" { |
| 49 | + return fmt.Errorf("go packages must not have 'fileSha256' field - this is only for MCPB packages") |
| 50 | + } |
| 51 | + // Validate that the registry base URL matches the Go module proxy exactly |
| 52 | + if pkg.RegistryBaseURL != model.RegistryURLGoProxy { |
| 53 | + return fmt.Errorf("registry type and base URL do not match: '%s' is not valid for registry type '%s'. Expected: %s", |
| 54 | + pkg.RegistryBaseURL, model.RegistryTypeGo, model.RegistryURLGoProxy) |
| 55 | + } |
| 56 | + |
| 57 | + if err := ValidateGoModuleOwnership(pkg.Identifier, serverName); err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + return validateGoModuleExists(ctx, pkg.RegistryBaseURL, pkg.Identifier, pkg.Version) |
| 62 | +} |
| 63 | + |
| 64 | +// ValidateGoModuleOwnership checks that the module path is rooted at the GitHub |
| 65 | +// owner that owns the server's io.github.* namespace. Comparison is |
| 66 | +// case-insensitive because GitHub owners are case-insensitive. |
| 67 | +func ValidateGoModuleOwnership(modulePath, serverName string) error { |
| 68 | + namespace, _, found := strings.Cut(serverName, "/") |
| 69 | + if !found || !strings.HasPrefix(namespace, goGitHubNamespacePrefix) { |
| 70 | + return fmt.Errorf("go packages are currently only supported for 'io.github.*' server namespaces; '%s' is not supported", serverName) |
| 71 | + } |
| 72 | + owner := strings.ToLower(strings.TrimPrefix(namespace, goGitHubNamespacePrefix)) |
| 73 | + |
| 74 | + lowerModule := strings.ToLower(modulePath) |
| 75 | + if !strings.HasPrefix(lowerModule, goGitHubModulePrefix) { |
| 76 | + return fmt.Errorf("go module path '%s' must be hosted on github.com to be published under '%s'", modulePath, namespace) |
| 77 | + } |
| 78 | + moduleOwner, _, _ := strings.Cut(lowerModule[len(goGitHubModulePrefix):], "/") |
| 79 | + if moduleOwner == "" { |
| 80 | + return fmt.Errorf("go module path '%s' is missing an owner segment", modulePath) |
| 81 | + } |
| 82 | + if moduleOwner != owner { |
| 83 | + return fmt.Errorf("go module ownership validation failed: server '%s' is owned by GitHub user '%s', but module path '%s' belongs to '%s'", serverName, owner, modulePath, moduleOwner) |
| 84 | + } |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +// validateGoModuleExists confirms the module version is available on the Go |
| 89 | +// module proxy. |
| 90 | +func validateGoModuleExists(ctx context.Context, baseURL, modulePath, version string) error { |
| 91 | + escapedModule, err := EscapeGoModulePath(modulePath) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("invalid Go module path '%s': %w", modulePath, err) |
| 94 | + } |
| 95 | + escapedVersion, err := EscapeGoModulePath(version) |
| 96 | + if err != nil { |
| 97 | + return fmt.Errorf("invalid Go module version '%s': %w", version, err) |
| 98 | + } |
| 99 | + |
| 100 | + requestURL := fmt.Sprintf("%s/%s/@v/%s.info", strings.TrimRight(baseURL, "/"), escapedModule, escapedVersion) |
| 101 | + |
| 102 | + client := &http.Client{Timeout: 10 * time.Second} |
| 103 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil) |
| 104 | + if err != nil { |
| 105 | + return fmt.Errorf("failed to create request: %w", err) |
| 106 | + } |
| 107 | + req.Header.Set("User-Agent", "MCP-Registry-Validator/1.0") |
| 108 | + |
| 109 | + resp, err := client.Do(req) |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("failed to fetch module metadata from the Go module proxy: %w", err) |
| 112 | + } |
| 113 | + defer resp.Body.Close() |
| 114 | + |
| 115 | + switch resp.StatusCode { |
| 116 | + case http.StatusOK: |
| 117 | + return nil |
| 118 | + case http.StatusNotFound, http.StatusGone: |
| 119 | + return fmt.Errorf("go module '%s' version '%s' was not found on the Go module proxy. If you published it recently, allow a few minutes for the proxy to fetch it", modulePath, version) |
| 120 | + default: |
| 121 | + return fmt.Errorf("go module proxy returned unexpected status %d for '%s@%s'", resp.StatusCode, modulePath, version) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// EscapeGoModulePath applies the goproxy case-encoding: every uppercase ASCII |
| 126 | +// letter is replaced by '!' followed by its lowercase form. It rejects control |
| 127 | +// characters and "."/".." path elements so a crafted identifier cannot escape |
| 128 | +// the proxy path. |
| 129 | +func EscapeGoModulePath(s string) (string, error) { |
| 130 | + if s == "" { |
| 131 | + return "", errors.New("empty path") |
| 132 | + } |
| 133 | + for _, segment := range strings.Split(s, "/") { |
| 134 | + if segment == "." || segment == ".." { |
| 135 | + return "", fmt.Errorf("contains a %q path element", segment) |
| 136 | + } |
| 137 | + } |
| 138 | + var b strings.Builder |
| 139 | + for _, r := range s { |
| 140 | + switch { |
| 141 | + case r >= 'A' && r <= 'Z': |
| 142 | + b.WriteByte('!') |
| 143 | + b.WriteRune(r + ('a' - 'A')) |
| 144 | + case r < 0x20 || r == 0x7f: |
| 145 | + return "", errors.New("contains a control character") |
| 146 | + default: |
| 147 | + b.WriteRune(r) |
| 148 | + } |
| 149 | + } |
| 150 | + return b.String(), nil |
| 151 | +} |
0 commit comments