Skip to content

fix(server): validate Host and Origin headers on HTTP and MCP routes - #1296

Merged
kyasbal merged 1 commit into
mainfrom
push-utxpmvslqkpr
Nov 20, 2026
Merged

kyasbal merged 1 commit into
mainfrom
push-utxpmvslqkpr

Conversation

@kyasbal

@kyasbal kyasbal commented Nov 20, 2026

Copy link
Copy Markdown
Member

Summary

Fixes missing Host and Origin header validation on KHI's Gin HTTP engine and MCP endpoints so requests with unexpected Host or cross-origin headers are consistently rejected with 403 Forbidden, while allowing configured hostnames to work across loopback proxies.

  • Add --allowed-hosts CLI flag (KHI_ALLOWED_HOSTS environment variable) to ServerParameters supporting exact hosts, host:port, *.<domain>, *-<suffix>, and *.
  • Add HostAllowlist and HostValidationMiddleware in pkg/server allowing loopback addresses (localhost, 127.0.0.0/8, [::1]), non-wildcard --host values, and configured --allowed-hosts patterns, rejecting malformed or unlisted hosts with a static 403 response.
  • Add CrossOriginProtectionMiddleware (using stdlib net/http.CrossOriginProtection) to GinEngineInitializer and MCPOriginProtectionMiddleware to /mcp and /mcp/sse routes in MCPServerInitializer.
  • Set DisableLocalhostProtection: true on mcp-go-sdk HTTP/SSE handlers so --allowed-hosts works consistently behind loopback proxies via HostValidationMiddleware.
  • Add unit and integration tests covering ServerParameters, HostAllowlist, middlewares, GinEngineInitializer, and MCPServerInitializer.

Verification

  • make pre-commit (add-licenses, format-go, lint-go, codequality-fast): PASS
  • go test -count=1 ./pkg/parameters/... ./pkg/server/... ./pkg/core/init/...: PASS
  • go build ./...: PASS
@kyasbal kyasbal added the type:bug Something isn't working as expected label Nov 20, 2026

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces robust security enhancements to the KHI server by implementing host validation and cross-origin protection. It adds a new --allowed-hosts CLI flag (and KHI_ALLOWED_HOSTS environment variable) to configure allowed Host header patterns, which are validated via a new HostAllowlist utility supporting exact hosts, subdomains, and prefix wildcards. Global Gin middlewares (HostValidationMiddleware and CrossOriginProtectionMiddleware) are integrated into the server engine, and a specialized MCPOriginProtectionMiddleware is applied to the MCP endpoints. Additionally, the MCP server handlers are updated to delegate localhost protection to KHI's global middleware. Comprehensive unit and integration tests have been added to verify these security controls. I have no additional feedback to provide as the implementation is thorough and well-tested.

@github-actions

Copy link
Copy Markdown

Code Quality Report — ❌ Cloud Build failure

Cloud Build exited before codequality finished emitting its report (for example, compilation or test setup failed).


Commit: ec5de4401628 · Cloud Build Logs

@kyasbal
kyasbal enabled auto-merge November 20, 2026 07:42
@kyasbal
kyasbal disabled auto-merge November 20, 2026 07:42
@kyasbal

kyasbal commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

The failed CI is currently fine. It would be addressed in some upcoming PRs.

@kyasbal
kyasbal merged commit ab75814 into main Nov 20, 2026
21 of 23 checks passed
@kyasbal
kyasbal deleted the push-utxpmvslqkpr branch November 20, 2026 07:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:bug Something isn't working as expected

1 participant