High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix(auth): respect forwarded scheme/host for dashboard sessions (#324)
Jamil Zakirove7d61a076363
diff --git a/internal/config/config_file.go b/internal/config/config_file.goindex 7b2659ef..6272b49e 100644--- a/internal/config/config_file.go+++ b/internal/config/config_file.go@@ -129,6 +129,7 @@ type serverConfigJSON struct {StateDir string `json:"stateDir"`Engine string `json:"engine"`NetworkBufferSize *int `json:"networkBufferSize,omitempty"`+ TrustProxyHeaders *bool `json:"trustProxyHeaders,omitempty"`}type browserConfigJSON struct {@@ -254,6 +255,7 @@ func (fc FileConfig) MarshalJSON() ([]byte, error) {StateDir: fc.Server.StateDir,Engine: fc.Server.Engine,NetworkBufferSize: fc.Server.NetworkBufferSize,+ TrustProxyHeaders: fc.Server.TrustProxyHeaders,},Browser: browserConfigJSON{ChromeVersion: fc.Browser.ChromeVersion,@@ -401,6 +403,7 @@ func FileConfigFromRuntime(cfg *RuntimeConfig) FileConfig {StateDir: cfg.StateDir,Engine: cfg.Engine,NetworkBufferSize: netBufSize,+ TrustProxyHeaders: &cfg.TrustProxyHeaders,},Browser: BrowserConfig{ChromeVersion: cfg.ChromeVersion,diff --git a/internal/config/config_load.go b/internal/config/config_load.goindex e839473c..0b0a4933 100644--- a/internal/config/config_load.go+++ b/internal/config/config_load.go@@ -194,6 +194,9 @@ func applyFileConfig(cfg *RuntimeConfig, fc *FileConfig) {if fc.Server.NetworkBufferSize != nil && *fc.Server.NetworkBufferSize > 0 {cfg.NetworkBufferSize = ClampNetworkBufferSize(*fc.Server.NetworkBufferSize)}+ if fc.Server.TrustProxyHeaders != nil {+ cfg.TrustProxyHeaders = *fc.Server.TrustProxyHeaders+ }// Securityif fc.Security.AllowEvaluate != nil {cfg.AllowEvaluate = *fc.Security.AllowEvaluatediff --git a/internal/config/config_load_test.go b/internal/config/config_load_test.goindex d305f401..4965388e 100644--- a/internal/config/config_load_test.go+++ b/internal/config/config_load_test.go@@ -59,6 +59,9 @@ func TestLoadConfigDefaults(t *testing.T) {if cfg.AllowEvaluate {t.Errorf("default AllowEvaluate = %v, want false", cfg.AllowEvaluate)}+ if cfg.TrustProxyHeaders {+ t.Errorf("default TrustProxyHeaders = %v, want false", cfg.TrustProxyHeaders)+ }if len(cfg.DownloadAllowedDomains) != 0 {t.Errorf("default DownloadAllowedDomains = %v, want empty list", cfg.DownloadAllowedDomains)}@@ -390,6 +393,27 @@ func TestApplyFileConfigToRuntime_ClampsTransferLimits(t *testing.T) {}}+func TestApplyFileConfigToRuntime_TrustProxyHeaders(t *testing.T) {+ cfg := &RuntimeConfig{}+ if cfg.TrustProxyHeaders {+ t.Fatal("expected default TrustProxyHeaders to be false")+ }++ enabled := true+ fc := &FileConfig{Server: ServerConfig{TrustProxyHeaders: &enabled}}+ applyFileConfig(cfg, fc)+ if !cfg.TrustProxyHeaders {+ t.Fatal("expected TrustProxyHeaders to be true after apply")+ }++ disabled := false+ fc2 := &FileConfig{Server: ServerConfig{TrustProxyHeaders: &disabled}}+ applyFileConfig(cfg, fc2)+ if cfg.TrustProxyHeaders {+ t.Fatal("expected TrustProxyHeaders to be false after apply with false")+ }+}+// clearConfigEnvVars unsets all config-related env vars for clean tests.func clearConfigEnvVars(t *testing.T) {t.Helper()diff --git a/internal/config/config_types.go b/internal/config/config_types.goindex 264cb593..ea85517c 100644--- a/internal/config/config_types.go+++ b/internal/config/config_types.go@@ -12,6 +12,7 @@ type RuntimeConfig struct {InstancePortEnd int // Ending port for instances (default 9968)Token stringStateDir string+ TrustProxyHeaders bool // Only trust X-Forwarded-*/Forwarded headers when behind a trusted reverse proxy// Security settingsAllowEvaluate bool@@ -141,6 +142,7 @@ type ServerConfig struct {StateDir string `json:"stateDir,omitempty"`Engine string `json:"engine,omitempty"`NetworkBufferSize *int `json:"networkBufferSize,omitempty"`+ TrustProxyHeaders *bool `json:"trustProxyHeaders,omitempty"`}type BrowserConfig struct {diff --git a/internal/config/editor_get.go b/internal/config/editor_get.goindex 921f2c9d..beff7f3c 100644--- a/internal/config/editor_get.go+++ b/internal/config/editor_get.go@@ -46,6 +46,8 @@ func getServerField(s *ServerConfig, field string) (string, error) {return s.Token, nilcase "stateDir":return s.StateDir, nil+ case "trustProxyHeaders":+ return formatBoolPtr(s.TrustProxyHeaders), nildefault:return "", fmt.Errorf("unknown field server.%s", field)}diff --git a/internal/config/editor_set.go b/internal/config/editor_set.goindex df9096b2..a27e543a 100644--- a/internal/config/editor_set.go+++ b/internal/config/editor_set.go@@ -45,6 +45,12 @@ func setServerField(s *ServerConfig, field, value string) error {s.Token = valuecase "stateDir":s.StateDir = value+ case "trustProxyHeaders":+ b, err := parseBool(value)+ if err != nil {+ return fmt.Errorf("server.trustProxyHeaders must be true or false: %w", err)+ }+ s.TrustProxyHeaders = &bdefault:return fmt.Errorf("unknown field server.%s", field)}diff --git a/internal/handlers/middleware.go b/internal/handlers/middleware.goindex 53b409c7..88d54b3b 100644--- a/internal/handlers/middleware.go+++ b/internal/handlers/middleware.go@@ -87,7 +87,7 @@ func AuthMiddlewareWithSessions(cfg *config.RuntimeConfig, sessions *authn.Sessireturn}case authn.MethodCookie:- if !cookieOriginAllowed(r) {+ if !cookieOriginAllowed(r, cfg.TrustProxyHeaders) {httpx.ErrorCode(w, http.StatusForbidden, "origin_forbidden", "same-origin browser request required for session authentication", false, map[string]any{"sameOriginRequired": true,})@@ -164,6 +164,8 @@ func cookieAuthAllowed(r *http.Request) bool {switch {case path == "/api/auth/elevate":return true+ case path == "/action":+ return truecase path == "/instances/launch":return truecase strings.HasPrefix(path, "/instances/") && strings.HasSuffix(path, "/stop"):@@ -224,31 +226,32 @@ func corsAllowedOrigin(cfg *config.RuntimeConfig, r *http.Request) string {if strings.TrimSpace(cfg.Token) == "" {return "*"}- if sameOriginRequest(origin, r) {+ if sameOriginRequest(origin, r, cfg.TrustProxyHeaders) {return origin}return ""}-func sameOriginRequest(origin string, r *http.Request) bool {+func sameOriginRequest(origin string, r *http.Request, trustProxy ...bool) bool {parsed, err := url.Parse(origin)if err != nil || parsed.Scheme == "" || parsed.Host == "" {return false}- return strings.EqualFold(parsed.Scheme, requestScheme(r)) && strings.EqualFold(parsed.Host, requestHost(r))+ trust := len(trustProxy) > 0 && trustProxy[0]+ return strings.EqualFold(parsed.Scheme, requestScheme(r, trust)) && strings.EqualFold(parsed.Host, requestHost(r, trust))}-func cookieOriginAllowed(r *http.Request) bool {+func cookieOriginAllowed(r *http.Request, trustProxy bool) bool {if isWebSocketUpgrade(r) {origin := strings.TrimSpace(r.Header.Get("Origin"))- return origin != "" && sameOriginRequest(origin, r)+ return origin != "" && sameOriginRequest(origin, r, trustProxy)}if origin := strings.TrimSpace(r.Header.Get("Origin")); origin != "" {- return sameOriginRequest(origin, r)+ return sameOriginRequest(origin, r, trustProxy)}if referer := strings.TrimSpace(r.Header.Get("Referer")); referer != "" {- return sameOriginRequest(referer, r)+ return sameOriginRequest(referer, r, trustProxy)}return false}@@ -263,17 +266,48 @@ func isWebSocketUpgrade(r *http.Request) bool {return strings.Contains(strings.ToLower(r.Header.Get("Connection")), "upgrade")}-func requestScheme(r *http.Request) string {- if r != nil && r.TLS != nil {+func requestScheme(r *http.Request, trustProxy bool) string {+ if r == nil {+ return "http"+ }+ if trustProxy {+ if forwarded := strings.TrimSpace(r.Header.Get("X-Forwarded-Proto")); forwarded != "" {+ return strings.ToLower(strings.TrimSpace(strings.Split(forwarded, ",")[0]))+ }+ if forwarded := strings.TrimSpace(r.Header.Get("Forwarded")); forwarded != "" {+ for _, part := range strings.Split(forwarded, ";") {+ key, value, ok := strings.Cut(strings.TrimSpace(part), "=")+ if !ok || !strings.EqualFold(key, "proto") {+ continue+ }+ return strings.ToLower(strings.Trim(value, `"`))+ }+ }+ }+ if r.TLS != nil {return "https"}return "http"}-func requestHost(r *http.Request) string {+func requestHost(r *http.Request, trustProxy bool) string {if r == nil {return ""}+ if trustProxy {+ if forwarded := strings.TrimSpace(r.Header.Get("X-Forwarded-Host")); forwarded != "" {+ return strings.TrimSpace(strings.Split(forwarded, ",")[0])+ }+ if forwarded := strings.TrimSpace(r.Header.Get("Forwarded")); forwarded != "" {+ for _, part := range strings.Split(forwarded, ";") {+ key, value, ok := strings.Cut(strings.TrimSpace(part), "=")+ if !ok || !strings.EqualFold(key, "host") {+ continue+ }+ return strings.Trim(value, `"`)+ }+ }+ }return strings.TrimSpace(r.Host)}diff --git a/internal/handlers/middleware_action_auth_test.go b/internal/handlers/middleware_action_auth_test.gonew file mode 100644index 00000000..7b22be45--- /dev/null+++ b/internal/handlers/middleware_action_auth_test.go@@ -0,0 +1,13 @@+package handlers++import (+ "net/http/httptest"+ "testing"+)++func TestCookieAuthAllowed_ActionPost(t *testing.T) {+ req := httptest.NewRequest("POST", "/action", nil)+ if !cookieAuthAllowed(req) {+ t.Fatal("expected cookie auth to allow POST /action for dashboard same-origin sessions")+ }+}diff --git a/internal/handlers/middleware_proxy_test.go b/internal/handlers/middleware_proxy_test.gonew file mode 100644index 00000000..d6f24a52--- /dev/null+++ b/internal/handlers/middleware_proxy_test.go@@ -0,0 +1,50 @@+package handlers++import (+ "net/http/httptest"+ "testing"+)++func TestSameOriginRequest_UsesForwardedProtoAndHost(t *testing.T) {+ req := httptest.NewRequest("GET", "http://pinchtab/health", nil)+ req.Host = "pinchtab:9867"+ req.Header.Set("X-Forwarded-Proto", "https")+ req.Header.Set("X-Forwarded-Host", "browser.example.com")++ if !sameOriginRequest("https://browser.example.com/dashboard", req, true) {+ t.Fatal("expected same-origin when trustProxy=true and forwarded proto/host match")+ }+}++func TestSameOriginRequest_UsesForwardedHeader(t *testing.T) {+ req := httptest.NewRequest("GET", "http://pinchtab/health", nil)+ req.Host = "pinchtab:9867"+ req.Header.Set("Forwarded", `for=127.0.0.1;proto=https;host=browser.example.com`)++ if !sameOriginRequest("https://browser.example.com/dashboard", req, true) {+ t.Fatal("expected same-origin when trustProxy=true and RFC 7239 Forwarded header matches")+ }+}++func TestSameOriginRequest_IgnoresForwardedWhenTrustDisabled(t *testing.T) {+ req := httptest.NewRequest("GET", "http://pinchtab/health", nil)+ req.Host = "pinchtab:9867"+ req.Header.Set("X-Forwarded-Proto", "https")+ req.Header.Set("X-Forwarded-Host", "browser.example.com")++ if sameOriginRequest("https://browser.example.com/dashboard", req, false) {+ t.Fatal("expected NOT same-origin when trustProxy=false even with forwarded headers")+ }+}++func TestSameOriginRequest_DefaultIgnoresForwarded(t *testing.T) {+ req := httptest.NewRequest("GET", "http://pinchtab/health", nil)+ req.Host = "pinchtab:9867"+ req.Header.Set("X-Forwarded-Proto", "https")+ req.Header.Set("X-Forwarded-Host", "browser.example.com")++ // No trustProxy argument — defaults to false+ if sameOriginRequest("https://browser.example.com/dashboard", req) {+ t.Fatal("expected NOT same-origin when trustProxy not specified")+ }+}