Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/livepeer/testdata/cli/orchestrator-help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions configs/orchestrator/config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion docs/orchestrator.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 16 additions & 2 deletions orchestrator/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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, "*")
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions orchestrator/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!#$%&'+-.^`|~"}))
Expand Down
34 changes: 34 additions & 0 deletions orchestrator/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"slices"
"strings"
"sync"
"time"
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 19 additions & 7 deletions orchestrator/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"} {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Loading