Repository navigation
fix(server): validate Host and Origin headers on HTTP and MCP routes - #1296
Conversation
There was a problem hiding this comment.
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.
Code Quality Report — ❌ Cloud Build
|
|
The failed CI is currently fine. It would be addressed in some upcoming PRs. |
Summary
Fixes missing
HostandOriginheader validation on KHI's Gin HTTP engine and MCP endpoints so requests with unexpectedHostor cross-origin headers are consistently rejected with403 Forbidden, while allowing configured hostnames to work across loopback proxies.--allowed-hostsCLI flag (KHI_ALLOWED_HOSTSenvironment variable) toServerParameterssupporting exact hosts,host:port,*.<domain>,*-<suffix>, and*.HostAllowlistandHostValidationMiddlewareinpkg/serverallowing loopback addresses (localhost,127.0.0.0/8,[::1]), non-wildcard--hostvalues, and configured--allowed-hostspatterns, rejecting malformed or unlisted hosts with a static403response.CrossOriginProtectionMiddleware(using stdlibnet/http.CrossOriginProtection) toGinEngineInitializerandMCPOriginProtectionMiddlewareto/mcpand/mcp/sseroutes inMCPServerInitializer.DisableLocalhostProtection: trueonmcp-go-sdkHTTP/SSE handlers so--allowed-hostsworks consistently behind loopback proxies viaHostValidationMiddleware.ServerParameters,HostAllowlist, middlewares,GinEngineInitializer, andMCPServerInitializer.Verification
make pre-commit(add-licenses,format-go,lint-go,codequality-fast): PASSgo test -count=1 ./pkg/parameters/... ./pkg/server/... ./pkg/core/init/...: PASSgo build ./...: PASS