-
Notifications
You must be signed in to change notification settings - Fork 213
Add Origin header validation for DNS-rebind protection #4908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -330,6 +330,18 @@ func WithAllowDockerGateway(allow bool) RunConfigBuilderOption { | |
| } | ||
| } | ||
|
|
||
| // WithAllowedOrigins sets the HTTP Origin-header allowlist used for | ||
| // DNS-rebinding protection (MCP 2025-11-25 §"Security Warning"). | ||
| // An empty slice defers the choice to middleware wiring, which derives a | ||
| // loopback-only default when the bind host is loopback and otherwise leaves | ||
| // the middleware disabled. | ||
| func WithAllowedOrigins(origins []string) RunConfigBuilderOption { | ||
| return func(b *runConfigBuilder) error { | ||
| b.config.AllowedOrigins = origins | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new Is this intentional, or an omission? If intentional, what's the rationale for excluding |
||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // WithTrustProxyHeaders sets whether to trust X-Forwarded-* headers from reverse proxies | ||
| func WithTrustProxyHeaders(trust bool) RunConfigBuilderOption { | ||
| return func(b *runConfigBuilder) error { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ package runner | |
|
|
||
| import ( | ||
| "fmt" | ||
| "log/slog" | ||
|
|
||
| "github.com/stacklok/toolhive/pkg/audit" | ||
| "github.com/stacklok/toolhive/pkg/auth" | ||
|
|
@@ -20,6 +21,7 @@ import ( | |
| "github.com/stacklok/toolhive/pkg/recovery" | ||
| "github.com/stacklok/toolhive/pkg/telemetry" | ||
| headerfwd "github.com/stacklok/toolhive/pkg/transport/middleware" | ||
| "github.com/stacklok/toolhive/pkg/transport/middleware/origin" | ||
| "github.com/stacklok/toolhive/pkg/transport/types" | ||
| "github.com/stacklok/toolhive/pkg/usagemetrics" | ||
| "github.com/stacklok/toolhive/pkg/webhook/mutating" | ||
|
|
@@ -43,6 +45,7 @@ func GetSupportedMiddlewareFactories() map[string]types.MiddlewareFactory { | |
| audit.MiddlewareType: audit.CreateMiddleware, | ||
| recovery.MiddlewareType: recovery.CreateMiddleware, | ||
| headerfwd.HeaderForwardMiddlewareName: headerfwd.CreateMiddleware, | ||
| origin.MiddlewareType: origin.CreateMiddleware, | ||
| validating.MiddlewareType: validating.CreateMiddleware, | ||
| mutating.MiddlewareType: mutating.CreateMiddleware, | ||
| } | ||
|
|
@@ -56,13 +59,21 @@ func PopulateMiddlewareConfigs(config *RunConfig) error { | |
| var middlewareConfigs []types.MiddlewareConfig | ||
| // TODO: Consider extracting other middleware setup into helper functions like addUsageMetricsMiddleware | ||
|
|
||
| // Origin-validation middleware (DNS-rebinding protection per MCP 2025-11-25). | ||
| // Positioned first in the slice so it runs earliest in the chain — disallowed | ||
| // Origin values are rejected before any authentication or business logic. | ||
| middlewareConfigs, err := addOriginMiddleware(middlewareConfigs, config) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Authentication middleware (always present) | ||
| authParams := auth.MiddlewareParams{ | ||
| OIDCConfig: config.OIDCConfig, | ||
| } | ||
| authConfig, err := types.NewMiddlewareConfig(auth.MiddlewareType, authParams) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create auth middleware config: %w", err) | ||
| authConfig, authErr := types.NewMiddlewareConfig(auth.MiddlewareType, authParams) | ||
| if authErr != nil { | ||
| return fmt.Errorf("failed to create auth middleware config: %w", authErr) | ||
| } | ||
| middlewareConfigs = append(middlewareConfigs, *authConfig) | ||
|
|
||
|
|
@@ -419,6 +430,35 @@ func addAWSStsMiddleware(middlewares []types.MiddlewareConfig, config *RunConfig | |
| return append(middlewares, *awsStsMwConfig), nil | ||
| } | ||
|
|
||
| // addOriginMiddleware adds Origin-header validation middleware for DNS-rebind | ||
| // protection per MCP 2025-11-25 §"Security Warning". Default-derivation logic | ||
| // lives in origin.ResolveAllowedOrigins so the standalone `thv proxy` command | ||
| // and the runner path agree on behavior. | ||
| // | ||
| // When the effective allowlist is empty — which happens when the operator | ||
| // binds to a non-loopback host without supplying --allowed-origins — the | ||
| // middleware is skipped entirely and a WARN is logged so the security-disabled | ||
| // state is visible in operator logs. A follow-up PR hardens the non-loopback | ||
| // path by requiring an explicit opt-in flag (see audit row 22). | ||
| func addOriginMiddleware(middlewares []types.MiddlewareConfig, config *RunConfig) ([]types.MiddlewareConfig, error) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The commit message says the middleware is wired into "thv run / thv-proxyrunner / vMCP", but vMCP composes its own chain via Should vMCP be wired here too, or should the commit message be corrected to drop the vMCP claim and tracked as a follow-up? |
||
| allowed := origin.ResolveAllowedOrigins(config.Host, config.Port, config.AllowedOrigins) | ||
| if len(allowed) == 0 { | ||
| slog.Warn("Origin validation disabled — no allowlist configured for non-loopback bind", | ||
| "host", config.Host, | ||
| "port", config.Port, | ||
| "hint", "pass --allowed-origins=https://your-client.example to enable DNS-rebind protection", | ||
| ) | ||
| return middlewares, nil | ||
| } | ||
|
|
||
| params := origin.MiddlewareParams{AllowedOrigins: allowed} | ||
| mwCfg, err := types.NewMiddlewareConfig(origin.MiddlewareType, params) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create origin middleware config: %w", err) | ||
| } | ||
| return append(middlewares, *mwCfg), nil | ||
| } | ||
|
|
||
| // addRateLimitMiddleware adds rate limit middleware if configured. | ||
| func addRateLimitMiddleware(middlewares []types.MiddlewareConfig, config *RunConfig) ([]types.MiddlewareConfig, error) { | ||
| if config.RateLimitConfig == nil { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The operator path uses
PopulateMiddlewareConfigsso the factory side is wired, butMCPServerSpec/MCPRemoteProxySpec/VirtualMCPServerSpechave noAllowedOriginsfield. Combined with operator-deployed pods binding to non-loopback addresses,ResolveAllowedOriginsreturnsnilandaddOriginMiddlewareskips registration with a WARN — so K8s deployments ship with Origin validation disabled.Is this expected, planned for a follow-up PR, or considered out of scope for the CRDs? If a follow-up, would it be worth a
// TODOor a note in the PR description so it's tracked?