diff --git a/cmd/livepeer/testdata/cli/orchestrator-help.golden b/cmd/livepeer/testdata/cli/orchestrator-help.golden index 4aea0c1..7349c51 100644 --- a/cmd/livepeer/testdata/cli/orchestrator-help.golden +++ b/cmd/livepeer/testdata/cli/orchestrator-help.golden @@ -25,6 +25,7 @@ Flags: -r, --runner-service-url string Runner-facing base URL for callbacks and trickle; defaults to service-url (env: LIVEPEER_ORCHESTRATOR_RUNNER_SERVICE_URL) --proxy-url-template string Generated proxy URL with {proxy} in a hostname label or final path segment (env: LIVEPEER_ORCHESTRATOR_PROXY_URL_TEMPLATE) --excluded-headers strings Headers to remove from app requests; a trailing * matches a prefix (env: LIVEPEER_ORCHESTRATOR_EXCLUDED_HEADERS) + --excluded-cookies strings Cookie names to remove from app requests; matching is case-sensitive (env: LIVEPEER_ORCHESTRATOR_EXCLUDED_COOKIES) --bootstrap-secret-file string File containing runner bootstrap credential (env: LIVEPEER_ORCHESTRATOR_BOOTSTRAP_SECRET_FILE) --runner-config string Static runner TOML path (env: LIVEPEER_ORCHESTRATOR_RUNNER_CONFIG) --redeemer-url string Remote winning-ticket redeemer base URL (env: LIVEPEER_ORCHESTRATOR_REDEEMER_URL) diff --git a/configs/orchestrator/config.example.toml b/configs/orchestrator/config.example.toml index 4eca4c3..fe150c6 100644 --- a/configs/orchestrator/config.example.toml +++ b/configs/orchestrator/config.example.toml @@ -17,6 +17,8 @@ SessionProxyGrants = [] HealthGrants = [] # ProxyURLTemplate = "https://{proxy}.apps.example" # Default: ServiceURL/run/{proxy}. # ExcludedHeaders = ["CF-Access-*", "Cookie"] # Remove matching headers and all cookies from app requests. +# To retain other cookies, omit "Cookie" from ExcludedHeaders. +# ExcludedCookies = ["CF_Authorization"] # Remove only the named cookies from app requests. # Paid operation requires an on-chain network, account password, RPC, face # value, winning probability, and one conversion source. diff --git a/docs/orchestrator.md b/docs/orchestrator.md index c6c333a..816169b 100644 --- a/docs/orchestrator.md +++ b/docs/orchestrator.md @@ -108,7 +108,10 @@ Client API requests use the Host from `APIOriginURL`, which defaults to HTTP(S) URL with the Host it sends, without a path. To remove headers from app requests, set `ExcludedHeaders`, for example: -`["CF-Access-*", "Cookie"]`. Matching is case-insensitive. +`["CF-Access-*"]`. Matching is case-insensitive. To remove selected cookies while +preserving other cookies, set `ExcludedCookies`, for example +`["CF_Authorization"]`. Cookie names match exactly and are case-sensitive. +Excluding the entire `Cookie` header still removes all cookies. ## Enable payments diff --git a/orchestrator/app.go b/orchestrator/app.go index e1a69fa..297bcd3 100644 --- a/orchestrator/app.go +++ b/orchestrator/app.go @@ -45,6 +45,7 @@ type Params struct { RunnerServiceURL boa.Text[*url.URL] `optional:"true" descr:"Runner-facing base URL for callbacks and trickle; defaults to service-url"` ProxyURLTemplate string `optional:"true" descr:"Generated proxy URL with {proxy} in a hostname label or final path segment"` ExcludedHeaders []string `optional:"true" descr:"Headers to remove from app requests; a trailing * matches a prefix"` + ExcludedCookies []string `optional:"true" descr:"Cookie names to remove from app requests; matching is case-sensitive"` BootstrapSecret string `secret:"true" optional:"true" descr:"Dynamic runner bootstrap credential"` BootstrapSecretFile string `basepath:"source" secretfor:"BootstrapSecret" descr:"File containing runner bootstrap credential"` RunnerConfig string `basepath:"source" optional:"true" file:"true" descr:"Static runner TOML path"` @@ -154,6 +155,9 @@ func (p Params) Validate() error { if err := validateExcludedHeaders(p.ExcludedHeaders); err != nil { return err } + if err := validateExcludedCookies(p.ExcludedCookies); err != nil { + return err + } if !p.Listen.IsValid() || !p.MetricsListen.IsValid() { return errors.New("--listen and --metrics-listen require IP:port addresses") } @@ -190,6 +194,15 @@ func (p Params) Validate() error { return nil } +func validateExcludedCookies(names []string) error { + for _, name := range names { + if err := (&http.Cookie{Name: name}).Valid(); err != nil { + return fmt.Errorf("invalid --excluded-cookies entry %q: %w", name, err) + } + } + return nil +} + func validateExcludedHeaders(patterns []string) error { for _, pattern := range patterns { name := strings.TrimSuffix(pattern, "*") @@ -268,8 +281,8 @@ func printConfig(ctx *boa.HookContext, p *Params, out io.Writer) error { values := map[string]any{ "Network": &p.Network, "Account": &p.Account, "OrchestratorAddress": &p.OrchestratorAddress, "Listen": &p.Listen, "MetricsListen": &p.MetricsListen, - "ExcludedHeaders": &p.ExcludedHeaders, - "RunnerGrants": &p.RunnerGrants, "SessionProxyGrants": &p.SessionProxyGrants, "HealthGrants": &p.HealthGrants, + "ExcludedHeaders": &p.ExcludedHeaders, "ExcludedCookies": &p.ExcludedCookies, + "RunnerGrants": &p.RunnerGrants, "SessionProxyGrants": &p.SessionProxyGrants, "HealthGrants": &p.HealthGrants, "HeartbeatInterval": &p.HeartbeatInterval, "HeartbeatTTL": &p.HeartbeatTTL, "DrainTimeout": &p.DrainTimeout, "RedeemerTxTimeout": &p.RedeemerTxTimeout, "RedeemerMaxTxReplacements": &p.RedeemerMaxTxReplacements, @@ -416,6 +429,7 @@ func Serve(parent context.Context, p Params, logOut io.Writer) (result error) { app.apiOrigin = p.APIOriginURL.Value } app.setExcludedHeaders(p.ExcludedHeaders) + app.excludedCookies = p.ExcludedCookies logger = app.logger if engine != nil { registry.metrics.PaymentObserver(observedChain.Observer, logger) diff --git a/orchestrator/app_test.go b/orchestrator/app_test.go index 653fc7f..aa1ee06 100644 --- a/orchestrator/app_test.go +++ b/orchestrator/app_test.go @@ -307,6 +307,25 @@ func TestExcludedHeadersEnvironmentAndFlag(t *testing.T) { } } +func TestExcludedCookiesEnvironmentAndFlag(t *testing.T) { + t.Setenv("LIVEPEER_ORCHESTRATOR_EXCLUDED_COOKIES", "CF_Authorization") + for _, args := range [][]string{nil, {"--excluded-cookies", "ingress_session"}} { + output, err := execute(t, append([]string{"--config", "", "--print-config"}, args...)...) + require.NoError(t, err) + var printed Params + require.NoError(t, toml.Unmarshal([]byte(output), &printed)) + want := []string{"CF_Authorization"} + if len(args) > 0 { + want = []string{"ingress_session"} + } + require.Equal(t, want, printed.ExcludedCookies) + } + require.NoError(t, validateExcludedCookies([]string{"CF_Authorization", "session-v1"})) + for _, name := range []string{"", "bad name", "name=value", "name;other", "name\r\n", "é"} { + require.ErrorContains(t, validateExcludedCookies([]string{name}), "--excluded-cookies") + } +} + func TestExcludedHeadersValidation(t *testing.T) { require.NoError(t, validateExcludedHeaders(nil)) require.NoError(t, validateExcludedHeaders([]string{"CF-Access-*", "Cookie", "X-Exact", "X_Valid!#$%&'+-.^`|~"})) diff --git a/orchestrator/http.go b/orchestrator/http.go index 857c1da..f2f3043 100644 --- a/orchestrator/http.go +++ b/orchestrator/http.go @@ -12,6 +12,7 @@ import ( "net/http" "net/http/httputil" "net/url" + "slices" "strings" "sync" "time" @@ -35,6 +36,7 @@ type Server struct { runnerTransport *http.Transport proxyTransport *http.Transport excludedHeaders []string + excludedCookies []string logger *slog.Logger payment *PaymentEngine mux *http.ServeMux @@ -190,6 +192,37 @@ func (s *Server) setExcludedHeaders(patterns []string) { s.proxyTransport.DisableCompression = disableCompression } +// Preserve application cookie bytes instead of parsing and reserializing values. +func excludeCookies(header http.Header, names []string) { + if len(names) == 0 { + return + } + for field, values := range header { + if !strings.EqualFold(field, "Cookie") { + continue + } + filtered := make([]string, 0, len(values)) + for _, value := range values { + parts := strings.Split(value, ";") + kept := parts[:0] + for _, part := range parts { + name, _, _ := strings.Cut(part, "=") + if strings.TrimSpace(part) != "" && !slices.Contains(names, strings.TrimSpace(name)) { + kept = append(kept, part) + } + } + if value := strings.TrimSpace(strings.Join(kept, ";")); value != "" { + filtered = append(filtered, value) + } + } + if len(filtered) == 0 { + delete(header, field) + } else { + header[field] = filtered + } + } +} + func headerExcluded(name string, patterns []string) bool { for _, pattern := range patterns { if prefix, ok := strings.CutSuffix(pattern, "*"); ok { @@ -540,6 +573,7 @@ func (s *Server) proxy(w http.ResponseWriter, r *http.Request, transport http.Ro req.Out.Header.Set("Livepeer-Session-Id", sid) req.Out.Header.Set("Livepeer-Session-Token", token) req.Out.Header.Set("Livepeer-Session-Control", control) + excludeCookies(req.Out.Header, s.excludedCookies) for name := range req.Out.Header { if headerExcluded(name, s.excludedHeaders) { delete(req.Out.Header, name) diff --git a/orchestrator/http_test.go b/orchestrator/http_test.go index 65b2ba2..12ea281 100644 --- a/orchestrator/http_test.go +++ b/orchestrator/http_test.go @@ -171,12 +171,14 @@ func TestProxyExcludedHeaders(t *testing.T) { policy, err := destination.New("runner", []string{strings.TrimPrefix(upstream.URL, "http://")}) require.NoError(t, err) for _, template := range []string{"", "https://{proxy}.apps.example", "https://orch.example/custom/{proxy}"} { - for _, patterns := range [][]string{ - nil, - {"cF-aCCess-*", "x-ingress-token", "cOOkie", "accept-encoding", "user-agent"}, - {"CF-Access-*", "X-Ingress-Token", "Cookie", "Accept-*", "User-*"}, + for _, excluded := range []struct{ headers, cookies []string }{ + {}, + {headers: []string{"cF-aCCess-*", "x-ingress-token", "cOOkie", "accept-encoding", "user-agent"}}, + {headers: []string{"CF-Access-*", "X-Ingress-Token", "Cookie", "Accept-*", "User-*"}}, + {cookies: []string{"CF_Authorization"}}, } { - t.Run(fmt.Sprintf("template=%s/excluded=%v", template, patterns), func(t *testing.T) { + patterns := excluded.headers + t.Run(fmt.Sprintf("template=%s/headers=%v/cookies=%v", template, patterns, excluded.cookies), func(t *testing.T) { registry := NewRegistry("secret", "https://orch.example", time.Second, time.Minute) registry.proxyTemplate = template for _, mode := range []string{"persistent", "single-shot"} { @@ -185,6 +187,7 @@ func TestProxyExcludedHeaders(t *testing.T) { server := NewServer(registry, policy, policy, slog.New(slog.NewTextHandler(io.Discard, nil))) defer server.Close() server.setExcludedHeaders(patterns) + server.excludedCookies = excluded.cookies sid, runnerProxy, _, _, err := registry.Reserve("persistent") require.NoError(t, err) target, token, _, err := registry.sessionTarget("persistent", sid) @@ -221,7 +224,15 @@ func TestProxyExcludedHeaders(t *testing.T) { var received http.Header require.NoError(t, json.Unmarshal(record.Body.Bytes(), &received)) for _, name := range []string{"Cf-Access-Jwt-Assertion", "Cf-Access-Client-Id", "X-Ingress-Token", "Cookie", "Accept-Encoding", "User-Agent"} { - if len(patterns) > 0 { + if name == "Cookie" && len(excluded.cookies) > 0 { + var cookies []string + for _, value := range received.Values("Cookie") { + for _, cookie := range strings.Split(value, ";") { + cookies = append(cookies, strings.TrimSpace(cookie)) + } + } + require.ElementsMatch(t, []string{"app=one", "app=two", "cf_authorization=another", "other=three"}, cookies) + } else if len(patterns) > 0 { require.Empty(t, received.Values(name), path+": "+name) } else { require.NotEmpty(t, received.Values(name), path+": "+name) @@ -277,7 +288,8 @@ func TestSessionProxyStreamsSSEAndWebSocket(t *testing.T) { _, _, err = registry.Heartbeat(registration, registered.HeartbeatSecret) require.NoError(t, err) app := NewServer(registry, policy, policy, slog.New(slog.NewTextHandler(io.Discard, nil))) - app.setExcludedHeaders([]string{"CF-Access-*", "Cookie"}) + app.setExcludedHeaders([]string{"CF-Access-*"}) + app.excludedCookies = []string{"CF_Authorization"} defer app.Close() server := httptest.NewServer(app) app.apiOrigin = mustURL(t, server.URL)