feat(security): add subdomain wildcard support to allowed_hosts and allowed_origins#2498
Open
Zelys-DFKH wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
feat(security): add subdomain wildcard support to allowed_hosts and allowed_origins#2498Zelys-DFKH wants to merge 1 commit intomodelcontextprotocol:mainfrom
Zelys-DFKH wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2141.
TransportSecuritySettingspreviously supported two matching modes forallowed_hostsandallowed_origins:"example.com""example.com:*"This adds subdomain wildcards:
"*.example.com"inallowed_hosts— matchesexample.comand any of its subdomains; port is stripped before matching"https://*.example.com"inallowed_origins— same, but scheme must also matchBehavior follows Django's
ALLOWED_HOSTSand Starlette'sTrustedHostMiddleware:*.example.comcovers the base domain too, so no separate entry forexample.comis needed.Also fixes a coverage gap:
_validate_host,_validate_origin, and the DNS-rebinding path invalidate_requestwere all# pragma: no cover. Those paths were only exercised by subprocess-based integration tests, which coverage never sees. This PR adds direct unit tests forTransportSecurityMiddleware(28 tests, 100% branch coverage) and removes all the no-cover markers.Files changed:
src/mcp/server/transport_security.pytests/server/test_transport_security.py(new)