Skip to content
Closed
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 changelog.d/security-18993-relay-headers.md
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 2 additions & 7 deletions src/crux/proxy/github_proxy_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down
11 changes: 11 additions & 0 deletions src/crux/proxy/github_proxy_mitm.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,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
Expand Down Expand Up @@ -449,6 +456,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.
Expand Down
5 changes: 5 additions & 0 deletions src/crux/proxy/proxy_deep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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)
Expand All @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions src/crux/proxy/proxy_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -51,6 +55,7 @@ func TestProxyHTTPAllowedGET(t *testing.T) {
Body: http.NoBody,
Request: req,
}
setRelayTestResponseHeaders(resp.Header)
resp.Write(upstreamConn)
upstreamConn.Close()
}()
Expand All @@ -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) {
Expand Down
38 changes: 38 additions & 0 deletions src/crux/proxy/relay_headers.go
Original file line number Diff line number Diff line change
@@ -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
}
47 changes: 47 additions & 0 deletions src/crux/proxy/relay_headers_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 2 additions & 1 deletion src/crux/proxy/sandbox_read_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -468,6 +468,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{})
Expand Down
24 changes: 22 additions & 2 deletions src/crux/proxy/sandbox_read_relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 8 additions & 0 deletions src/docs/security-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/docs/security-threat-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,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.
Expand Down
Loading