registry/internal/validators/constants.go
Ossama Lafhel 63cf08efad
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"
2025-09-15 12:05:51 +01:00

39 lines
No EOL
1.5 KiB
Go

package validators
import "errors"
// Error messages for validation
var (
// Repository validation errors
ErrInvalidRepositoryURL = errors.New("invalid repository URL")
ErrInvalidSubfolderPath = errors.New("invalid subfolder path")
// Package validation errors
ErrPackageNameHasSpaces = errors.New("package name cannot contain spaces")
ErrReservedVersionString = errors.New("version string 'latest' is reserved and cannot be used")
ErrVersionLooksLikeRange = errors.New("version must be a specific version, not a range")
// Remote validation errors
ErrInvalidRemoteURL = errors.New("invalid remote URL")
// Registry validation errors
ErrUnsupportedRegistryBaseURL = errors.New("unsupported registry base URL")
ErrMismatchedRegistryTypeAndURL = errors.New("registry type and base URL do not match")
// Argument validation errors
ErrNamedArgumentNameRequired = errors.New("named argument name is required")
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
type RepositorySource string
const (
SourceGitHub RepositorySource = "github"
SourceGitLab RepositorySource = "gitlab"
)