Skip to content

fix: validate Origin against localhost by default in DNS rebinding protection#840

Open
devcrocod wants to merge 1 commit into
mainfrom
fix/dns-rebinding-default-origin
Open

fix: validate Origin against localhost by default in DNS rebinding protection#840
devcrocod wants to merge 1 commit into
mainfrom
fix/dns-rebinding-default-origin

Conversation

@devcrocod

Copy link
Copy Markdown
Contributor

Default the Origin allowlist to localhost when DnsRebindingProtection relies on the localhost host defaults, so a request with a valid Host but a hostile (or null) Origin is no longer accepted.

How Has This Been Tested?

unit tests

Breaking Changes

Behavioral change only

Types of changes

  • 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

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Copilot AI 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.

Pull request overview

Tightens DNS rebinding protection for the Ktor server endpoints by making Origin validation secure-by-default when the server is using the built-in localhost Host allowlist, preventing requests with a valid Host but hostile Origin from being accepted.

Changes:

  • Default allowedOrigins to a localhost origin allowlist when allowedHosts is not customized (i.e., localhost defaults are in effect).
  • Add LOCALHOST_ALLOWED_ORIGINS to support URL-parsable localhost origins.
  • Add unit tests covering default origin validation behavior for Streamable HTTP.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
kotlin-sdk-server/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/server/DnsRebindingProtectionTest.kt Adds tests asserting default Origin validation behavior when using localhost defaults.
kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/KtorServer.kt Updates DNS rebinding protection installation to default Origin validation on localhost, and updates related KDoc (partially).
kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/HostValidation.kt Introduces a localhost Origin allowlist constant used for default Origin validation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +29 to +30
internal val LOCALHOST_ALLOWED_ORIGINS: List<String> =
listOf("http://localhost", "http://127.0.0.1", "http://[::1]")
Comment on lines 458 to 466
private fun Route.installDnsRebindingProtection(enabled: Boolean, hosts: List<String>?, origins: List<String>?) {
if (!enabled) return
install(DnsRebindingProtection) {
allowedHosts = hosts ?: LOCALHOST_ALLOWED_HOSTS
origins?.let { allowedOrigins = it }
// Secure-by-default: when relying on the localhost host defaults, validate the Origin
// header against localhost too, so a request with a valid Host but a hostile Origin
// (e.g. a DNS-rebinding page) is rejected. Callers with custom hosts opt in explicitly.
allowedOrigins = origins ?: LOCALHOST_ALLOWED_ORIGINS.takeIf { hosts == null }
}
response.shouldHaveStatus(HttpStatusCode.Forbidden)
response.bodyAsText() shouldContain "Invalid Origin host: evil.com"
}

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.

2 participants