fix: validate server names to allow only single slash (#471) (#476)

## Summary
Added validation to reject server names with multiple slashes, ensuring
consistency between JSON schema pattern and API validation.

  - Count slashes in parseServerName() and reject if > 1
  - Add ErrMultipleSlashesInServerName error constant
  - Add unit tests in validators_test.go
  - Add integration test in publish_test.go

  ## Motivation and Context
Fixes #471. The JSON schema regex pattern `^[^/]+/[^/]+$` only allows a
single slash in server names, but the API validation was not enforcing
this constraint. This inconsistency
allowed invalid server names like `com.example/server/path` to be
published.

  ## How Has This Been Tested?
  - Unit tests added to verify slash validation logic
- Integration tests added to verify the publish endpoint rejects
multi-slash names
- Tested various edge cases: trailing slashes, consecutive slashes,
URL-like paths
  - All existing tests pass

  ## Breaking Changes
None. This change only adds validation to reject previously invalid
server names that shouldn't have been accepted.


## Types of changes
- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update

## Checklist
- [x] I have read the [MCP
Documentation](https://modelcontextprotocol.io)
- [x] My code follows the repository's style guidelines
- [x] New and existing tests pass locally
- [x] I have added appropriate error handling
- [x] I have added or updated documentation as needed
 
## Additional context
- The fix ensures server names follow the intended format:
`namespace/name` (e.g., `com.example/my-server`)
- This validation aligns the API behavior with the existing JSON schema
regex pattern
- Test coverage includes edge cases like `com.example//server`,
`com.example/server/`, and `a/b/c/d/e/f`
- The error message clearly indicates the issue: "server name cannot
contain multiple slashes"
This commit is contained in:
Ossama Lafhel 2025-09-15 13:05:51 +02:00 • committed by GitHub
commit 63cf08efad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 342 additions and 5 deletions

View file

@ -25,6 +25,9 @@ var (
ErrInvalidNamedArgumentName = errors.New("invalid named argument name format")
ErrArgumentValueStartsWithName = errors.New("argument value cannot start with the argument name")
ErrArgumentDefaultStartsWithName = errors.New("argument default cannot start with the argument name")
// Server name validation errors
ErrMultipleSlashesInServerName = errors.New("server name cannot contain multiple slashes")
)
// RepositorySource represents valid repository sources
@ -33,4 +36,4 @@ type RepositorySource string
const (
SourceGitHub RepositorySource = "github"
SourceGitLab RepositorySource = "gitlab"
)
)

View file

@ -402,6 +402,12 @@ func parseServerName(serverJSON apiv0.ServerJSON) (string, error) {
return "", fmt.Errorf("server name must be in format 'dns-namespace/name' (e.g., 'com.example.api/server')")
}
// Check for multiple slashes - reject if found
slashCount := strings.Count(name, "/")
if slashCount > 1 {
return "", ErrMultipleSlashesInServerName
}
parts := strings.SplitN(name, "/", 2)
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return "", fmt.Errorf("server name must be in format 'dns-namespace/name' with non-empty namespace and name parts")
@ -504,4 +510,4 @@ func isValidHostForDomain(hostname, publisherDomain string) bool {
}
return false
}
}

View file

@ -171,6 +171,25 @@ func TestValidate(t *testing.T) {
},
expectedError: validators.ErrVersionLooksLikeRange.Error(),
},
// Server name validation - multiple slashes
{
name: "server name with two slashes",
serverDetail: apiv0.ServerJSON{
Name: "com.example/server/path",
Description: "A test server",
Version: "1.0.0",
},
expectedError: validators.ErrMultipleSlashesInServerName.Error(),
},
{
name: "server name with three slashes",
serverDetail: apiv0.ServerJSON{
Name: "com.example/server/path/deep",
Description: "A test server",
Version: "1.0.0",
},
expectedError: validators.ErrMultipleSlashesInServerName.Error(),
},
{
name: "valid server detail with all fields",
serverDetail: apiv0.ServerJSON{
@ -847,11 +866,12 @@ func TestValidate_ServerNameFormat(t *testing.T) {
errorMsg: "non-empty namespace and name parts",
},
{
name: "multiple slashes - uses first as separator",
name: "multiple slashes - should be rejected",
serverDetail: apiv0.ServerJSON{
Name: "com.example/server/path",
},
expectError: false,
expectError: true,
errorMsg: "server name cannot contain multiple slashes",
},
}
@ -869,6 +889,84 @@ func TestValidate_ServerNameFormat(t *testing.T) {
}
}
func TestValidate_MultipleSlashesInServerName(t *testing.T) {
tests := []struct {
name string
serverName string
expectError bool
errorMsg string
}{
{
name: "single slash - valid",
serverName: "com.example/my-server",
expectError: false,
},
{
name: "two slashes - invalid",
serverName: "com.example/my-server/extra",
expectError: true,
errorMsg: "server name cannot contain multiple slashes",
},
{
name: "three slashes - invalid",
serverName: "com.example/my/server/name",
expectError: true,
errorMsg: "server name cannot contain multiple slashes",
},
{
name: "many slashes - invalid",
serverName: "com.example/a/b/c/d/e",
expectError: true,
errorMsg: "server name cannot contain multiple slashes",
},
{
name: "double slash - invalid",
serverName: "com.example//server",
expectError: true,
errorMsg: "server name cannot contain multiple slashes",
},
{
name: "trailing slash counts as two - invalid",
serverName: "com.example/server/",
expectError: true,
errorMsg: "server name cannot contain multiple slashes",
},
{
name: "no slash - still invalid for different reason",
serverName: "com.example.server",
expectError: true,
errorMsg: "server name must be in format 'dns-namespace/name'",
},
{
name: "complex valid namespace with single slash",
serverName: "com.microsoft.azure.service/webapp-server",
expectError: false,
},
{
name: "complex namespace with multiple slashes - invalid",
serverName: "com.microsoft.azure/service/webapp-server",
expectError: true,
errorMsg: "server name cannot contain multiple slashes",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
serverDetail := apiv0.ServerJSON{
Name: tt.serverName,
}
err := validators.ValidateServerJSON(&serverDetail)
if tt.expectError {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.errorMsg)
} else {
assert.NoError(t, err)
}
})
}
}
func TestValidateArgument_ValidNamedArguments(t *testing.T) {
validCases := []model.Argument{
{
@ -1492,4 +1590,4 @@ func createValidServerWithArgument(arg model.Argument) apiv0.ServerJSON {
},
},
}
}
}