diff --git a/changelog.d/security-18993-relay-headers.md b/changelog.d/security-18993-relay-headers.md new file mode 100644 index 0000000000..e622cb8a2c --- /dev/null +++ b/changelog.d/security-18993-relay-headers.md @@ -0,0 +1 @@ +- Parsed agent HTTP relays now remove proxy credentials, forwarding metadata, internal Hive headers, and other hop-by-hop metadata before sending requests upstream. diff --git a/src/crux/proxy/github_proxy_connect.go b/src/crux/proxy/github_proxy_connect.go index 5e580e39eb..56d0aeac9c 100644 --- a/src/crux/proxy/github_proxy_connect.go +++ b/src/crux/proxy/github_proxy_connect.go @@ -662,13 +662,7 @@ func (p *GitHubProxy) forwardPlainDirect(conn net.Conn, r *http.Request) { // which is logged as such rather than guessed at. agentName := p.identifyAgentFromConn(conn, r) - // Proxy-* hop headers address THIS proxy and must not travel further: the - // identity they assert is the one identifyAgentFromConn just decided - // whether to believe, and forwarding it would hand an agent's claimed name - // (and, in the Basic form, whatever it put in the password field) to every - // plain-HTTP host it can name. - r.Header.Del("Proxy-Authorization") - r.Header.Del("Proxy-Connection") + stripRelayRequestHeaders(r) resp, err := p.plainForwardTransport().RoundTrip(r) if err != nil { @@ -686,6 +680,7 @@ func (p *GitHubProxy) forwardPlainDirect(conn net.Conn, r *http.Request) { return } defer func() { _ = resp.Body.Close() }() + stripRelayResponseHeaders(resp) _ = resp.Write(conn) } diff --git a/src/crux/proxy/github_proxy_mitm.go b/src/crux/proxy/github_proxy_mitm.go index 48576948f1..1e7ff0e992 100644 --- a/src/crux/proxy/github_proxy_mitm.go +++ b/src/crux/proxy/github_proxy_mitm.go @@ -358,6 +358,13 @@ func (p *GitHubProxy) proxyHTTPHost(client net.Conn, upstream net.Conn, host str } } + // Advisory attribution can use a caller-supplied name, so it + // cannot exempt a request from header filtering. + filterAgentHeaders := agentName != internalCallerName || p.proxyAdvisoryOK + if filterAgentHeaders { + stripRelayRequestHeaders(req) + } + // #1861: with injection enabled, the upstream credential is decided // HERE — after the ACMM/repo/canary gates, immediately before either // forwarding branch — so every byte that reaches GitHub has passed @@ -460,6 +467,10 @@ func (p *GitHubProxy) proxyHTTPHost(client net.Conn, upstream net.Conn, host str } } + if filterAgentHeaders { + stripRelayResponseHeaders(resp) + } + // Rolling per-Write deadline (see stallBoundedWriter): a response that // keeps flowing is never cut for being long, while a client that stops // draining still releases this handler within one stall window. diff --git a/src/crux/proxy/proxy_deep_test.go b/src/crux/proxy/proxy_deep_test.go index 09c3dc0dac..3aa3496e7d 100644 --- a/src/crux/proxy/proxy_deep_test.go +++ b/src/crux/proxy/proxy_deep_test.go @@ -348,6 +348,8 @@ func TestGenerateCAProperties(t *testing.T) { func TestForwardPlainDirectSuccess(t *testing.T) { // Create a mock upstream server upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assertRelayHeaderHygiene(t, r.Header, relayTestRequestHeaders()) + setRelayTestResponseHeaders(w.Header()) w.Header().Set("X-Test", "passed") w.WriteHeader(http.StatusOK) w.Write([]byte("upstream response")) @@ -360,9 +362,11 @@ func TestForwardPlainDirectSuccess(t *testing.T) { // successful relay, so it runs with the operator opt-in lifted. p.allowPrivateForward = true clientConn, proxyClient := net.Pipe() + defer proxyClient.Close() defer clientConn.Close() req, _ := http.NewRequest("GET", upstream.URL+"/test", nil) + req.Header = relayTestRequestHeaders() go p.forwardPlainDirect(proxyClient, req) resp, err := http.ReadResponse(bufio.NewReader(clientConn), nil) @@ -372,6 +376,7 @@ func TestForwardPlainDirectSuccess(t *testing.T) { if resp.StatusCode != http.StatusOK { t.Errorf("status = %d, want 200", resp.StatusCode) } + assertRelayResponseHeaderHygiene(t, resp) body, _ := io.ReadAll(resp.Body) if !strings.Contains(string(body), "upstream response") { t.Errorf("body = %q, expected 'upstream response'", body) diff --git a/src/crux/proxy/proxy_http_test.go b/src/crux/proxy/proxy_http_test.go index e071b595f4..40b847add0 100644 --- a/src/crux/proxy/proxy_http_test.go +++ b/src/crux/proxy/proxy_http_test.go @@ -33,15 +33,19 @@ func TestProxyHTTPAllowedGET(t *testing.T) { go p.proxyHTTP(proxyClient, proxyUpstream, "scanner", agent.ModeAdvisory, agent.AgentCapabilities{}) - go func() { - fmt.Fprintf(clientConn, "GET /repos/org/repo HTTP/1.1\r\nHost: api.github.com\r\n\r\n") - }() + req, err := http.NewRequest("GET", "https://api.github.com/repos/org/repo", nil) + if err != nil { + t.Fatal(err) + } + req.Header = relayTestRequestHeaders() + go func() { _ = req.Write(clientConn) }() go func() { req, err := http.ReadRequest(bufio.NewReader(upstreamConn)) if err != nil { return } + assertRelayHeaderHygiene(t, req.Header, relayTestRequestHeaders()) resp := &http.Response{ StatusCode: 200, Proto: "HTTP/1.1", @@ -51,6 +55,7 @@ func TestProxyHTTPAllowedGET(t *testing.T) { Body: http.NoBody, Request: req, } + setRelayTestResponseHeaders(resp.Header) resp.Write(upstreamConn) upstreamConn.Close() }() @@ -62,6 +67,7 @@ func TestProxyHTTPAllowedGET(t *testing.T) { if resp.StatusCode != 200 { t.Errorf("GET should be allowed, got %d", resp.StatusCode) } + assertRelayResponseHeaderHygiene(t, resp) } func TestProxyHTTPBlockedPOST(t *testing.T) { diff --git a/src/crux/proxy/relay_headers.go b/src/crux/proxy/relay_headers.go new file mode 100644 index 0000000000..1a5608a6a9 --- /dev/null +++ b/src/crux/proxy/relay_headers.go @@ -0,0 +1,38 @@ +package proxy + +import ( + "net/http" + "strings" +) + +// stripRelayHopHeaders removes metadata scoped to the inbound connection. +// The request/response writers generate framing for the outbound connection. +func stripRelayHopHeaders(h http.Header) { + for _, value := range h.Values("Connection") { + for name := range strings.SplitSeq(value, ",") { + h.Del(strings.TrimSpace(name)) + } + } + for _, name := range [...]string{ + "Connection", "Keep-Alive", "Proxy-Authenticate", "Proxy-Authorization", + "Proxy-Connection", "Te", "Trailer", "Transfer-Encoding", "Upgrade", + } { + delete(h, name) + } +} + +func stripRelayRequestHeaders(req *http.Request) { + stripRelayHopHeaders(req.Header) + for name := range req.Header { + if strings.HasPrefix(name, "X-Forwarded-") || strings.HasPrefix(name, "X-Hive-") { + delete(req.Header, name) + } + } + // Trailers arrive after the headers have been filtered; do not forward them. + req.Trailer = nil +} + +func stripRelayResponseHeaders(resp *http.Response) { + stripRelayHopHeaders(resp.Header) + resp.Trailer = nil +} diff --git a/src/crux/proxy/relay_headers_test.go b/src/crux/proxy/relay_headers_test.go new file mode 100644 index 0000000000..47a18490a6 --- /dev/null +++ b/src/crux/proxy/relay_headers_test.go @@ -0,0 +1,47 @@ +package proxy + +import ( + "net/http" + "testing" +) + +// Use ordinary HTTP messages with inert metadata. Each relay must pass the +// end-to-end field while removing the other fields before the next hop. +func relayTestRequestHeaders() http.Header { + h := make(http.Header) + setRelayTestResponseHeaders(h) + h.Set("Proxy-Authorization", "Basic dGVzdDpmaXh0dXJl") + h.Set("Proxy-Connection", "keep-alive") + h.Set("x-forwarded-example", "fixture") + h.Set("x-hive-example", "fixture") + return h +} + +func setRelayTestResponseHeaders(h http.Header) { + h.Add("Connection", "Keep-Alive, x-relay-hop") + h.Add("Connection", "X-Second-Hop") + h.Set("Keep-Alive", "timeout=5") + h.Set("X-Relay-Hop", "fixture") + h.Set("X-Second-Hop", "fixture") + h.Set("X-End-To-End", "preserved") +} + +func assertRelayHeaderHygiene(t *testing.T, got, sent http.Header) { + t.Helper() + for name := range sent { + if name == "X-End-To-End" { + if value := got.Get(name); value != "preserved" { + t.Errorf("%s = %q, want preserved", name, value) + } + } else if len(got.Values(name)) != 0 { + t.Errorf("%s was forwarded", name) + } + } +} + +func assertRelayResponseHeaderHygiene(t *testing.T, resp *http.Response) { + t.Helper() + sent := make(http.Header) + setRelayTestResponseHeaders(sent) + assertRelayHeaderHygiene(t, resp.Header, sent) +} diff --git a/src/crux/proxy/sandbox_read_relay.go b/src/crux/proxy/sandbox_read_relay.go index abee9f4c99..12c05890d9 100644 --- a/src/crux/proxy/sandbox_read_relay.go +++ b/src/crux/proxy/sandbox_read_relay.go @@ -295,7 +295,7 @@ func (r *SandboxReadRelay) readToken() string { // attached only when d allows injection. func applySandboxReadAuth(req *http.Request, d SandboxReadDecision, token string) { req.Header.Del("Authorization") - req.Header.Del("Proxy-Authorization") + stripRelayRequestHeaders(req) req.Header.Del("Expect") if !d.Allowed || !d.Inject || token == "" { return @@ -473,6 +473,7 @@ func (r *SandboxReadRelay) intercept(conn net.Conn, host string, kick *sandboxRe return } resp.Body = &stallBoundedBody{body: resp.Body, conn: upstream, idle: r.p.bodyStallTimeoutDuration()} + stripRelayResponseHeaders(resp) werr := resp.Write(&stallBoundedWriter{conn: client, idle: httpWriteTimeout}) _ = resp.Body.Close() _ = client.SetWriteDeadline(time.Time{}) diff --git a/src/crux/proxy/sandbox_read_relay_test.go b/src/crux/proxy/sandbox_read_relay_test.go index bc312321b8..c1ef4e591b 100644 --- a/src/crux/proxy/sandbox_read_relay_test.go +++ b/src/crux/proxy/sandbox_read_relay_test.go @@ -170,7 +170,11 @@ func newRelayHarnessWithLogger(t *testing.T, token SandboxReadTokenFunc, logger h.mu.Lock() h.upstream = append(h.upstream, req) h.mu.Unlock() - _, _ = fmt.Fprint(server, "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok") + resp := &http.Response{StatusCode: http.StatusOK, ProtoMajor: 1, ProtoMinor: 1, Header: make(http.Header), Body: io.NopCloser(strings.NewReader("ok")), ContentLength: 2, Request: req} + setRelayTestResponseHeaders(resp.Header) + if err := resp.Write(server); err != nil { + return + } } }() return client, nil @@ -271,14 +275,30 @@ func TestSandboxReadRelayInjectsOnReadAndRefusesWrites(t *testing.T) { if status != 200 { t.Fatalf("CONNECT status = %d", status) } - resp := roundTrip(t, tc, "GET", "api.github.com", "/repos/o/r/tags", map[string]string{"Authorization": "token AGENT_SUPPLIED"}) + headers := relayTestRequestHeaders() + req, err := http.NewRequest(http.MethodGet, "https://api.github.com/repos/o/r/tags", nil) + if err != nil { + t.Fatal(err) + } + req.Header = headers.Clone() + req.Header.Set("Authorization", "token AGENT_SUPPLIED") + if err := req.Write(tc); err != nil { + t.Fatal(err) + } + resp, err := http.ReadResponse(bufio.NewReader(tc), req) + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() if resp.StatusCode != 200 { t.Fatalf("GET status = %d", resp.StatusCode) } + assertRelayResponseHeaderHygiene(t, resp) seen := h.seen() if len(seen) != 1 { t.Fatalf("upstream saw %d requests, want 1", len(seen)) } + assertRelayHeaderHygiene(t, seen[0].Header, headers) if got := seen[0].Header.Get("Authorization"); got != "token "+testReadToken { t.Fatalf("upstream Authorization = %q, want the relay's read token (the agent's own header must be replaced)", got) } diff --git a/src/docs/security-model.md b/src/docs/security-model.md index 4f73cc4a27..39c8fa1f54 100644 --- a/src/docs/security-model.md +++ b/src/docs/security-model.md @@ -403,6 +403,14 @@ on the same evidence as a literal IP. Set destination is intended; it lifts the guard for all three legs at once, and the proxy warns at boot when it is set. +**HTTP relay header boundary.** Plain HTTP, inspected agent HTTP, and the +sandbox read relay remove `Proxy-Authorization`, `X-Forwarded-*`, +`X-Hive-*`, standard hop-by-hop fields, and fields nominated by `Connection` +from forwarded requests. The parsed response path removes hop-by-hop fields +in the other direction. These paths regenerate HTTP framing without +forwarding trailers. The inspected agent relay and sandbox read relay apply +the filter before adding their own upstream credentials. + **Server-side fetches of user- or spoke-chosen URLs.** The dashboard's agent import and workflow spec fetches, the hub's hive config proxy and webhook relay to a spoke's self-reported dashboard URL, and knowledge document imports diff --git a/src/docs/security-threat-model.md b/src/docs/security-threat-model.md index 73183e749f..feac137da2 100644 --- a/src/docs/security-threat-model.md +++ b/src/docs/security-threat-model.md @@ -100,6 +100,11 @@ Read each control's exceptions and the residual risks, not just its name. it with a placeholder, is opt-in. * **Network is limited, not absent.** Agents can still reach GitHub reads, model backends and opaque `github.com` tunnels. +* **Opaque forwarding has no HTTP header filter.** This includes uninspected + TLS tunnels and the response stream after the inspected Git smart-HTTP + handoff. The filter also leaves trusted internal inspected traffic + unchanged. Header filtering does not certify handling of ambiguous HTTP + framing or path normalization. * **The classifier fails open at L1–L4.** It is on by default from L4 and fails closed at L5–L6. The deterministic rules, canaries and proxy denials are the floor; the language-model classifier adds coverage above it.