Skip to content

Commit 5e22e9f

Browse files
author
Avish Porwal
committed
Refactor validation code
1 parent 8d7e471 commit 5e22e9f

File tree

7 files changed

+593
-5
lines changed

7 files changed

+593
-5
lines changed

internal/api/handlers/v0/publish.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/modelcontextprotocol/registry/internal/config"
1212
"github.com/modelcontextprotocol/registry/internal/model"
1313
"github.com/modelcontextprotocol/registry/internal/service"
14+
"github.com/modelcontextprotocol/registry/internal/validators"
1415
)
1516

1617
// PublishServerInput represents the input for publishing a server
@@ -63,6 +64,12 @@ func RegisterPublishEndpoint(api huma.API, registry service.RegistryService, cfg
6364
// Get server details from request body
6465
serverDetail := publishRequest.Server
6566

67+
// Validate the server detail
68+
validator := validators.NewObjectValidator()
69+
if err := validator.Validate(&serverDetail); err != nil {
70+
return nil, huma.Error400BadRequest(err.Error())
71+
}
72+
6673
// Verify that the token's repository matches the server being published
6774
if !jwtManager.HasPermission(serverDetail.Name, auth.PermissionActionPublish, claims.Permissions) {
6875
return nil, huma.Error403Forbidden("You do not have permission to publish this server")

internal/api/handlers/v0/publish_integration_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ func TestPublishIntegration(t *testing.T) {
105105
Name: "test-mcp-server-no-auth",
106106
Description: "A test MCP server without authentication",
107107
Repository: model.Repository{
108-
URL: "https://example.com/test-mcp-server",
109-
Source: "example",
110-
ID: "test-mcp-server",
108+
URL: "https://github.com/example/test-server",
109+
Source: "github",
110+
ID: "example/test-server",
111111
},
112112
VersionDetail: model.VersionDetail{
113113
Version: "1.0.0",
@@ -194,6 +194,11 @@ func TestPublishIntegration(t *testing.T) {
194194
VersionDetail: model.VersionDetail{
195195
Version: "1.0.0",
196196
},
197+
Repository: model.Repository{
198+
URL: "https://github.com/example/test-server",
199+
Source: "github",
200+
ID: "example/test-server",
201+
},
197202
},
198203
}
199204

internal/api/handlers/v0/publish_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ func TestPublishEndpoint(t *testing.T) {
112112
Name: "example/test-server",
113113
Description: "A test server without auth",
114114
Repository: model.Repository{
115-
URL: "https://example.com/test-server",
116-
Source: "example",
115+
URL: "https://github.com/example/test-server",
116+
Source: "github",
117117
ID: "example/test-server",
118118
},
119119
VersionDetail: model.VersionDetail{
@@ -173,6 +173,11 @@ func TestPublishEndpoint(t *testing.T) {
173173
VersionDetail: model.VersionDetail{
174174
Version: "1.0.0",
175175
},
176+
Repository: model.Repository{
177+
URL: "https://github.com/example/test-server",
178+
Source: "github",
179+
ID: "example/test-server",
180+
},
176181
},
177182
},
178183
tokenClaims: &auth.JWTClaims{
@@ -194,6 +199,11 @@ func TestPublishEndpoint(t *testing.T) {
194199
VersionDetail: model.VersionDetail{
195200
Version: "1.0.0",
196201
},
202+
Repository: model.Repository{
203+
URL: "https://github.com/example/test-server",
204+
Source: "github",
205+
ID: "example/test-server",
206+
},
197207
},
198208
},
199209
tokenClaims: &auth.JWTClaims{

internal/validators/constants.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package validators
2+
3+
import "errors"
4+
5+
// Error messages for validation
6+
var (
7+
// Server validation errors
8+
ErrNameRequired = errors.New("name is required")
9+
ErrServerNameTooLong = errors.New("server name is too long")
10+
ErrVersionRequired = errors.New("version is required")
11+
ErrDescriptionTooLong = errors.New("description is too long")
12+
13+
// Repository validation errors
14+
ErrInvalidRepositorySource = errors.New("invalid repository source")
15+
ErrInvalidRepositoryURL = errors.New("invalid repository URL")
16+
17+
// Package validation errors
18+
ErrPackageNameTooLong = errors.New("package name is too long")
19+
ErrPackageNameHasSpaces = errors.New("package name cannot contain spaces")
20+
21+
// Remote validation errors
22+
ErrInvalidRemoteURL = errors.New("invalid remote URL")
23+
)
24+
25+
// Constants for validation limits
26+
const (
27+
MaxLengthForServerName = 255
28+
MaxLengthForDescription = 1000
29+
MaxLengthForPackageName = 255
30+
)
31+
32+
// RepositorySource represents valid repository sources
33+
type RepositorySource string
34+
35+
const (
36+
SourceGitHub RepositorySource = "github"
37+
SourceGitLab RepositorySource = "gitlab"
38+
)

internal/validators/utils.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package validators
2+
3+
import (
4+
"net/url"
5+
"regexp"
6+
"strings"
7+
)
8+
9+
var (
10+
// Regular expressions for validating repository URLs
11+
// These regex patterns ensure the URL is in the format of a valid GitHub or GitLab repository
12+
// For example: // - GitHub: https://github.com/user/repo
13+
githubURLRegex = regexp.MustCompile(`^https?://(www\.)?github\.com/[\w.-]+/[\w.-]+/?$`)
14+
gitlabURLRegex = regexp.MustCompile(`^https?://(www\.)?gitlab\.com/[\w.-]+/[\w.-]+/?$`)
15+
)
16+
17+
// IsValidRepositoryURL checks if the given URL is valid for the specified repository source
18+
func IsValidRepositoryURL(source RepositorySource, url string) bool {
19+
switch source {
20+
case SourceGitHub:
21+
return githubURLRegex.MatchString(url)
22+
case SourceGitLab:
23+
return gitlabURLRegex.MatchString(url)
24+
}
25+
return false
26+
}
27+
28+
// HasNoSpaces checks if a string contains no spaces
29+
func HasNoSpaces(s string) bool {
30+
return !strings.Contains(s, " ")
31+
}
32+
33+
// IsValidURL checks if a URL is in valid format
34+
func IsValidURL(rawURL string) bool {
35+
// Parse the URL
36+
u, err := url.Parse(rawURL)
37+
if err != nil {
38+
return false
39+
}
40+
41+
// Check if scheme is present (http or https)
42+
if u.Scheme != "http" && u.Scheme != "https" {
43+
return false
44+
}
45+
46+
if u.Host == "" {
47+
return false
48+
}
49+
return true
50+
}

internal/validators/validators.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package validators
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/modelcontextprotocol/registry/internal/model"
7+
)
8+
9+
// ServerValidator validates server details
10+
type ServerValidator struct {
11+
*RespositoryValidator // Embedded RespositoryValidator for repository validation
12+
}
13+
14+
// Validate checks if the server details are valid
15+
func (v *ServerValidator) Validate(obj *model.ServerDetail) error {
16+
if obj.Name == "" {
17+
return ErrNameRequired
18+
}
19+
20+
// Add format validation according to https://github.com/modelcontextprotocol/modelcontextprotocol/issues/1086 in the next PR
21+
if len(obj.Name) > MaxLengthForServerName {
22+
return ErrServerNameTooLong
23+
}
24+
25+
// Version is required
26+
if obj.VersionDetail.Version == "" {
27+
return ErrVersionRequired
28+
}
29+
30+
if len(obj.Description) > MaxLengthForDescription {
31+
return ErrDescriptionTooLong
32+
}
33+
34+
if err := v.RespositoryValidator.Validate(&obj.Repository); err != nil {
35+
return err
36+
}
37+
return nil
38+
}
39+
40+
// NewServerValidator creates a new ServerValidator instance
41+
func NewServerValidator() *ServerValidator {
42+
return &ServerValidator{
43+
RespositoryValidator: NewRepositoryValidator(),
44+
}
45+
}
46+
47+
// RepositoryValidator validates repository details
48+
type RespositoryValidator struct {
49+
validSources map[RepositorySource]bool
50+
}
51+
52+
// Validate checks if the repository details are valid
53+
func (rv *RespositoryValidator) Validate(obj *model.Repository) error {
54+
// empty repository object, this check is needed because we get empty repo object (if not present in request) everytime we unmarshal the request json
55+
if len(obj.URL) == 0 && len(obj.Source) == 0 && len(obj.ID) == 0 {
56+
return nil
57+
}
58+
// validate the repository URL
59+
repoSource := RepositorySource(obj.Source)
60+
if _, ok := rv.validSources[repoSource]; !ok {
61+
return fmt.Errorf("%w: %s", ErrInvalidRepositorySource, obj.Source)
62+
}
63+
if !IsValidRepositoryURL(repoSource, obj.URL) {
64+
return fmt.Errorf("%w: %s", ErrInvalidRepositoryURL, obj.URL)
65+
}
66+
67+
// Add validator for repo ID after confirming ID type
68+
return nil
69+
}
70+
71+
// NewRepositoryValidator creates a new RespositoryValidator instance
72+
func NewRepositoryValidator() *RespositoryValidator {
73+
return &RespositoryValidator{
74+
validSources: map[RepositorySource]bool{SourceGitHub: true, SourceGitLab: true},
75+
}
76+
}
77+
78+
// PackageValidator validates package details
79+
type PackageValidator struct{}
80+
81+
// Validate checks if the package details are valid
82+
func (pv *PackageValidator) Validate(obj *model.Package) error {
83+
if len(obj.Name) > MaxLengthForPackageName {
84+
return ErrPackageNameTooLong
85+
}
86+
87+
if !HasNoSpaces(obj.Name) {
88+
return ErrPackageNameHasSpaces
89+
}
90+
91+
return nil
92+
}
93+
94+
// NewPackageValidator creates a new PackageValidator instance
95+
func NewPackageValidator() *PackageValidator {
96+
return &PackageValidator{}
97+
}
98+
99+
// RemoteValidator validates remote connection details
100+
type RemoteValidator struct{}
101+
102+
// Validate checks if the remote connection details are valid
103+
func (rv *RemoteValidator) Validate(obj *model.Remote) error {
104+
if !IsValidURL(obj.URL) {
105+
return fmt.Errorf("%w: %s", ErrInvalidRemoteURL, obj.URL)
106+
}
107+
return nil
108+
}
109+
110+
// NewRemoteValidator creates a new RemoteValidator instance
111+
func NewRemoteValidator() *RemoteValidator {
112+
return &RemoteValidator{}
113+
}
114+
115+
// ObjectValidator aggregates multiple validators for different object types
116+
// This allows for a single entry point to validate complex objects that may contain multiple fields
117+
// that need validation.
118+
type ObjectValidator struct {
119+
ServerValidator *ServerValidator
120+
PackageValidator *PackageValidator
121+
RemoteValidator *RemoteValidator
122+
}
123+
124+
func NewObjectValidator() *ObjectValidator {
125+
return &ObjectValidator{
126+
ServerValidator: NewServerValidator(),
127+
PackageValidator: NewPackageValidator(),
128+
RemoteValidator: NewRemoteValidator(),
129+
}
130+
}
131+
132+
func (ov *ObjectValidator) Validate(obj *model.ServerDetail) error {
133+
if err := ov.ServerValidator.Validate(obj); err != nil {
134+
return err
135+
}
136+
137+
for _, pkg := range obj.Packages {
138+
if err := ov.PackageValidator.Validate(&pkg); err != nil {
139+
return err
140+
}
141+
}
142+
143+
for _, remote := range obj.Remotes {
144+
if err := ov.RemoteValidator.Validate(&remote); err != nil {
145+
return err
146+
}
147+
}
148+
return nil
149+
}

0 commit comments

Comments
 (0)