|
1 | 1 | package validators
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
4 | 5 | "fmt"
|
| 6 | + "net/url" |
| 7 | + "slices" |
| 8 | + "strings" |
5 | 9 |
|
| 10 | + apiv1 "github.com/modelcontextprotocol/registry/pkg/api/v1" |
6 | 11 | "github.com/modelcontextprotocol/registry/pkg/model"
|
7 | 12 | )
|
8 | 13 |
|
@@ -122,3 +127,163 @@ func (ov *ObjectValidator) Validate(obj *model.ServerJSON) error {
|
122 | 127 | }
|
123 | 128 | return nil
|
124 | 129 | }
|
| 130 | + |
| 131 | +// ValidatePublisherExtensions validates that publisher extensions are within size limits |
| 132 | +func ValidatePublisherExtensions(req apiv1.PublishRequest) error { |
| 133 | + const maxExtensionSize = 4 * 1024 // 4KB limit |
| 134 | + |
| 135 | + // Check size limit for x-publisher extension |
| 136 | + if req.XPublisher != nil { |
| 137 | + extensionsJSON, err := json.Marshal(req.XPublisher) |
| 138 | + if err != nil { |
| 139 | + return fmt.Errorf("failed to marshal x-publisher extension: %w", err) |
| 140 | + } |
| 141 | + if len(extensionsJSON) > maxExtensionSize { |
| 142 | + return fmt.Errorf("x-publisher extension exceeds 4KB limit (%d bytes)", len(extensionsJSON)) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +// ValidatePublishRequestExtensions validates that only allowed extension fields are present |
| 150 | +func ValidatePublishRequestExtensions(requestData []byte) error { |
| 151 | + // Parse the raw JSON to check for unknown fields |
| 152 | + var rawRequest map[string]interface{} |
| 153 | + if err := json.Unmarshal(requestData, &rawRequest); err != nil { |
| 154 | + return fmt.Errorf("failed to parse request JSON: %w", err) |
| 155 | + } |
| 156 | + |
| 157 | + // Define allowed top-level fields |
| 158 | + allowedFields := map[string]bool{ |
| 159 | + "server": true, |
| 160 | + "x-publisher": true, |
| 161 | + } |
| 162 | + |
| 163 | + // Check for any disallowed fields |
| 164 | + var invalidFields []string |
| 165 | + for field := range rawRequest { |
| 166 | + if !allowedFields[field] { |
| 167 | + invalidFields = append(invalidFields, field) |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + if len(invalidFields) > 0 { |
| 172 | + return fmt.Errorf("invalid extension fields: %v. Only 'server' and 'x-publisher' fields are allowed", invalidFields) |
| 173 | + } |
| 174 | + |
| 175 | + return nil |
| 176 | +} |
| 177 | + |
| 178 | +// ExtractPublisherExtensions extracts publisher extensions from a apiv1.PublishRequest |
| 179 | +func ExtractPublisherExtensions(req apiv1.PublishRequest) map[string]interface{} { |
| 180 | + publisherExtensions := make(map[string]interface{}) |
| 181 | + if req.XPublisher != nil { |
| 182 | + // Copy fields directly, avoiding double nesting |
| 183 | + for k, v := range req.XPublisher { |
| 184 | + publisherExtensions[k] = v |
| 185 | + } |
| 186 | + } |
| 187 | + return publisherExtensions |
| 188 | +} |
| 189 | + |
| 190 | +// ParseServerName extracts the server name from a model.ServerJSON for validation purposes |
| 191 | +func ParseServerName(serverDetail model.ServerJSON) (string, error) { |
| 192 | + name := serverDetail.Name |
| 193 | + if name == "" { |
| 194 | + return "", fmt.Errorf("server name is required and must be a string") |
| 195 | + } |
| 196 | + |
| 197 | + // Validate format: dns-namespace/name |
| 198 | + if !strings.Contains(name, "/") { |
| 199 | + return "", fmt.Errorf("server name must be in format 'dns-namespace/name' (e.g., 'com.example.api/server')") |
| 200 | + } |
| 201 | + |
| 202 | + parts := strings.SplitN(name, "/", 2) |
| 203 | + if len(parts) != 2 || parts[0] == "" || parts[1] == "" { |
| 204 | + return "", fmt.Errorf("server name must be in format 'dns-namespace/name' with non-empty namespace and name parts") |
| 205 | + } |
| 206 | + |
| 207 | + return name, nil |
| 208 | +} |
| 209 | + |
| 210 | +// ValidateRemoteNamespaceMatch validates that remote URLs match the reverse-DNS namespace |
| 211 | +func ValidateRemoteNamespaceMatch(serverDetail model.ServerJSON) error { |
| 212 | + namespace := serverDetail.Name |
| 213 | + |
| 214 | + for _, remote := range serverDetail.Remotes { |
| 215 | + if err := validateRemoteURLMatchesNamespace(remote.URL, namespace); err != nil { |
| 216 | + return fmt.Errorf("remote URL %s does not match namespace %s: %w", remote.URL, namespace, err) |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + return nil |
| 221 | +} |
| 222 | + |
| 223 | +// validateRemoteURLMatchesNamespace checks if a remote URL's hostname matches the publisher domain from the namespace |
| 224 | +func validateRemoteURLMatchesNamespace(remoteURL, namespace string) error { |
| 225 | + // Parse the URL to extract the hostname |
| 226 | + parsedURL, err := url.Parse(remoteURL) |
| 227 | + if err != nil { |
| 228 | + return fmt.Errorf("invalid URL format: %w", err) |
| 229 | + } |
| 230 | + |
| 231 | + hostname := parsedURL.Hostname() |
| 232 | + if hostname == "" { |
| 233 | + return fmt.Errorf("URL must have a valid hostname") |
| 234 | + } |
| 235 | + |
| 236 | + // Skip validation for localhost and local development URLs |
| 237 | + if hostname == "localhost" || strings.HasSuffix(hostname, ".localhost") || hostname == "127.0.0.1" { |
| 238 | + return nil |
| 239 | + } |
| 240 | + |
| 241 | + // Extract publisher domain from reverse-DNS namespace |
| 242 | + publisherDomain := extractPublisherDomainFromNamespace(namespace) |
| 243 | + if publisherDomain == "" { |
| 244 | + return fmt.Errorf("invalid namespace format: cannot extract domain from %s", namespace) |
| 245 | + } |
| 246 | + |
| 247 | + // Check if the remote URL hostname matches the publisher domain or is a subdomain |
| 248 | + if !isValidHostForDomain(hostname, publisherDomain) { |
| 249 | + return fmt.Errorf("remote URL host %s does not match publisher domain %s", hostname, publisherDomain) |
| 250 | + } |
| 251 | + |
| 252 | + return nil |
| 253 | +} |
| 254 | + |
| 255 | +// extractPublisherDomainFromNamespace converts reverse-DNS namespace to normal domain format |
| 256 | +// e.g., "com.example" -> "example.com" |
| 257 | +func extractPublisherDomainFromNamespace(namespace string) string { |
| 258 | + // Extract the namespace part before the first slash |
| 259 | + namespacePart := namespace |
| 260 | + if slashIdx := strings.Index(namespace, "/"); slashIdx != -1 { |
| 261 | + namespacePart = namespace[:slashIdx] |
| 262 | + } |
| 263 | + |
| 264 | + // Split into parts and reverse them to get normal domain format |
| 265 | + parts := strings.Split(namespacePart, ".") |
| 266 | + if len(parts) < 2 { |
| 267 | + return "" |
| 268 | + } |
| 269 | + |
| 270 | + // Reverse the parts to convert from reverse-DNS to normal domain |
| 271 | + slices.Reverse(parts) |
| 272 | + |
| 273 | + return strings.Join(parts, ".") |
| 274 | +} |
| 275 | + |
| 276 | +// isValidHostForDomain checks if a hostname is the domain or a subdomain of the publisher domain |
| 277 | +func isValidHostForDomain(hostname, publisherDomain string) bool { |
| 278 | + // Exact match |
| 279 | + if hostname == publisherDomain { |
| 280 | + return true |
| 281 | + } |
| 282 | + |
| 283 | + // Subdomain match - hostname should end with "." + publisherDomain |
| 284 | + if strings.HasSuffix(hostname, "."+publisherDomain) { |
| 285 | + return true |
| 286 | + } |
| 287 | + |
| 288 | + return false |
| 289 | +} |
0 commit comments