FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Validate origin header by Kehrlann · Pull Request #771 · modelcontextprotocol/java-sdk · GitHub

Validate origin header - #771

Merged
Kehrlann merged 2 commits into
mainfrom
dgarnier/validate-origin-header
Feb 5, 2026
Merged

Validate origin header#771
Kehrlann merged 2 commits into
mainfrom
dgarnier/validate-origin-header

Conversation

Kehrlann commented Feb 4, 2026
edited
Loading

Copy link
Copy Markdown
Contributor

Fixes #695 . Updates conformance tests.

Contents

Add Origin header validation for HTTP-based MCP server transports so servers can restrict which origins can connect (e.g. browser or cross-origin clients).

Introduces new ServerTransportSecurityValidator interface, to validate transport-level security. Default behavior stays permissive: transports use ServerTransportSecurityValidator.NOOP unless a validator is set via the builder.

The provided implementation is DefaultServerTransportSecurityValidator and only validates the Origin header, for now.

New APIs: example usage

HttpServletStreamableServerTransportProvider.builder()
// ...
.securityValidator(
    DefaultServerTransportSecurityValidator.builder()
        .allowedOrigin("http://localhost:*")
        .build())
// ...

Points of interest

Integration tests use the new JUnit 6 @ParameterizedClass API, which is another way of implementation "abstract base classes" for tests.

- Fixes #695
- Does not implement Host header validation yet

Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
Kehrlann requested review from chemicL and tzolov and removed request for chemicL February 4, 2026 20:19
Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
Kehrlann force-pushed the dgarnier/validate-origin-header branch from e5624d4 to 68ed795 Compare February 4, 2026 20:20
Kehrlann requested a review from chemicL February 4, 2026 20:23
Kehrlann marked this pull request as ready for review February 4, 2026 20:23

tzolov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM. Great work @Kehrlann !


@Test
void differentSchemeWithWildcard() {
var headers = originHeader("https://localhost:3000");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

shouldn't the port be 8080 to ensure that it fails because of the schema?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It's using the the wildcard validator, so the port does not matter:

private final DefaultServerTransportSecurityValidator wildcardValidator =
	DefaultServerTransportSecurityValidator
		.builder()
		.allowedOrigin("http://localhost:*")
		.build();

Kehrlann merged commit 5ed6063 into main Feb 5, 2026
6 checks passed
Kehrlann deleted the dgarnier/validate-origin-header branch February 5, 2026 09:20

chemicL left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thank you @Kehrlann for taking care of this! I added a few comments post merge. Perhaps eliminating the duplicated code can be addressed as a follow-up. Great job overall!

Comment on lines +138 to +145
try {
Map<String, List<String>> headers = extractHeaders(request);
this.securityValidator.validateHeaders(headers);
}
catch (ServerTransportSecurityException e) {
response.sendError(e.getStatusCode(), e.getMessage());
return;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This code is duplicated across three implementations, perhaps we can have a centralized static utility for it?

Kehrlann added a commit that referenced this pull request Feb 12, 2026
Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
Kehrlann added a commit that referenced this pull request Feb 12, 2026
Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Return HTTP 403 when Origin header is invalid

3 participants


Back | FazBrowse Home | New Git URL