Visitar URL original
fix(validators): don't treat x in a prerelease tag as a version wildcard by sholub-dev · Pull Request #1856 · modelcontextprotocol/registry · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions internal/validators/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ func validateVersion(ctx *ValidationContext, version string) *ValidationResult {
return result
}

// looksLikeVersionRange detects common semver range syntaxes and wildcard patterns.
// looksLikeVersionRange detects common semver range syntaxes and wildcard patterns
// that indicate the value is not a single, specific version.
// Examples that should return true:
// - "^1.2.3",
Expand All @@ -394,6 +394,9 @@ func validateVersion(ctx *ValidationContext, version string) *ValidationResult {
// - "1.2.*",
// - "1 - 2",
// - "1.2 || 1.3"
// Examples that should return false (an x inside a prerelease tag is not a wildcard):
// - "1.0.0-next.1",
// - "1.2.3-linux-x64"
func looksLikeVersionRange(version string) bool {
trimmed := strings.TrimSpace(version)
if trimmed == "" {
Expand All @@ -410,8 +413,10 @@ func looksLikeVersionRange(version string) bool {
return true
}
if dottedVersionLikeRe.MatchString(trimmed) {
// wildcard in a dotted version (x/X/*) implies range-like intent
return strings.Contains(trimmed, "x") || strings.Contains(trimmed, "X") || strings.Contains(trimmed, "*")
// A wildcard (x/X/*) in the numeric part implies range-like intent.
// The prerelease tag after the first "-" is not checked: "1.0.0-next.1" is a valid version.
core, _, _ := strings.Cut(trimmed, "-")
return strings.ContainsAny(core, "xX*")
}
return false
}
Expand Down
70 changes: 70 additions & 0 deletions internal/validators/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,76 @@ func TestValidate(t *testing.T) {
},
expectedError: "",
},
{
name: "Version allows x inside prerelease tag 1.0.0-next.1",
serverDetail: apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/test-server",
Description: "A test server",
Repository: &model.Repository{
URL: "https://github.com/owner/repo",
Source: "github",
},
Version: "1.0.0-next.1",
},
expectedError: "",
},
{
name: "Version allows x inside prerelease tag 2.0.0-experimental",
serverDetail: apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/test-server",
Description: "A test server",
Repository: &model.Repository{
URL: "https://github.com/owner/repo",
Source: "github",
},
Version: "2.0.0-experimental",
},
expectedError: "",
},
{
name: "Version allows x inside prerelease tag 1.2.3-linux-x64",
serverDetail: apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/test-server",
Description: "A test server",
Repository: &model.Repository{
URL: "https://github.com/owner/repo",
Source: "github",
},
Version: "1.2.3-linux-x64",
},
expectedError: "",
},
{
name: "Version rejects x-range with prerelease tag",
serverDetail: apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/test-server",
Description: "A test server",
Repository: &model.Repository{
URL: "https://github.com/owner/repo",
Source: "github",
},
Version: "1.x-beta",
},
expectedError: validators.ErrVersionLooksLikeRange.Error(),
},
{
name: "Version rejects wildcard * with prerelease tag",
serverDetail: apiv0.ServerJSON{
Schema: model.CurrentSchemaURL,
Name: "com.example/test-server",
Description: "A test server",
Repository: &model.Repository{
URL: "https://github.com/owner/repo",
Source: "github",
},
Version: "1.2.*-rc.1",
},
expectedError: validators.ErrVersionLooksLikeRange.Error(),
},
{
name: "Version rejects hyphen range of two versions",
serverDetail: apiv0.ServerJSON{
Expand Down
Loading