-
Notifications
You must be signed in to change notification settings - Fork 423
feat: add regex validation to server name in API types (#471) #479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: add regex validation to server name in API types (#471) #479
Conversation
…otocol#471) 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
…e-validation-inconsistency fix: validate server names to allow only single slash (modelcontextprotocol#471)
c43df8e
to
aad4d4b
Compare
aad4d4b
to
6a6e710
Compare
8352aca
to
6d19849
Compare
internal/validators/validators.go
Outdated
// Validate namespace and name format according to schema | ||
// Pattern: ^[a-zA-Z0-9.-]+/[a-zA-Z0-9._-]+$ | ||
namespacePattern := regexp.MustCompile(`^[a-zA-Z0-9.-]+$`) | ||
namePattern := regexp.MustCompile(`^[a-zA-Z0-9._-]+$`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@domdomegg - are server names case sensitive? NuGet and PyPI treat package names as case insensitive at the API level but npm and Docker do not (last I checked). Personally, I recommend enforcing lowercase everywhere and being case sensitive. It is has caused headaches in NuGet and the roughly "domain name + path" pattern of server names is not unlike URLs which have a lowercase domain name (ubiquitous) and kebab case path portion (not ubiquitous but common in say GH repo names).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joelverhagen Good point! Currently the regex allows uppercase letters in both namespace and name parts.
I agree that enforcing lowercase would be beneficial for consistency, especially since:
- The namespace part follows reverse DNS convention where domains are lowercase
- It avoids confusion between
Com.Example/server
vscom.example/server
- It aligns with web conventions
Should we update the regex to enforce lowercase in this PR or create a separate issue for it? If we do it here, we'd need to:
- Update the regex patterns to
^[a-z0-9.-]+/[a-z0-9._-]+$
- Update the error messages and documentation
- Add tests for uppercase rejection
What do you think @domdomegg?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Can we move the regex to utils.go? We have kept regex for the URLs there. It kind of make sense to have all regex used in validation at one place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Avish34 Thanks for the review! Good suggestions:
- Moved regex patterns to
utils.go
alongside other validation regexes - Added constant error messages in
constants.go
:ErrInvalidNamespaceCharacters
ErrInvalidNameCharacters
This keeps all validation patterns centralized and error messages consistent. Changes implemented in the latest commit!
Add descriptive documentation to the Name field in ServerJSON struct explaining the expected format 'reverse-dns-namespace/name' and allowed characters for namespace and name parts.
67a0979
to
251c65a
Compare
251c65a
to
a6d5cb8
Compare
…sages - Add regex validation in parseServerName with specific error messages - Validate namespace allows only alphanumeric, dots and hyphens - Validate name allows only alphanumeric, dots, underscores and hyphens - Update tests to expect descriptive error messages - Add tests for invalid character validation - Fix test server names to use valid format - Move regex patterns to utils.go for centralization - Add error constants to constants.go for consistency - Remove redundant validation checks Based on feedback from PR reviewers, this implementation: - Maintains HTTP 400 status instead of 422 for better developer experience - Provides clear, actionable error messages - Follows existing codebase patterns for validation - Centralizes validation logic for maintainability Co-authored-by: Joel Verhagen <[email protected]> Co-authored-by: Avish <[email protected]> Co-authored-by: Adam Jones <[email protected]>
a6d5cb8
to
bb64ce1
Compare
|
||
// Prevent renaming servers | ||
if currentServer.Name != input.Body.Name { | ||
// Validate the new name format before rejecting the rename |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems unnecessary, if we're going to reject the request anyway?
} | ||
|
||
// Check for multiple slashes - reject if found | ||
slashCount := strings.Count(name, "/") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would already be caught by the parts split and patterns below? Ideally I want to keep this simple as it's hard to follow when there are quite so many rules - eliminating overlapping/duplicate ones is valuable. Possibly can all be merged into one fairly simple regex.
Summary
Following @domdomegg's suggestion in #476, this PR started as adding regex validation to the server name field but evolved based on extensive reviewer feedback into a comprehensive validation improvement.
Changes
Initial Implementation
Name
field inpkg/api/v0/types.go
Based on Review Feedback
^[^/]+/[^/]+$
to the more specific^[a-zA-Z0-9.-]+/[a-zA-Z0-9._-]+$
to match the schema definitionutils.go
- Centralized all validation regexes in one place as suggested by @Avish34constants.go
- CreatedErrInvalidNamespaceCharacters
andErrInvalidNameCharacters
for consistent error messagesstrings.Contains(name, "//")
check that was already covered by slash count validationKey Decisions
Context
This PR demonstrates the value of code review - what started as a simple regex addition evolved into a better implementation that:
Related to #471
Builds on #476
Testing
Note: Special thanks to @joelverhagen, @Avish34, and @domdomegg for their thorough reviews that significantly improved this implementation.