Skip to content
1 change: 1 addition & 0 deletions changelog.d/fixed-19223-relay-request-target.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- **Proxy: inspected relays refuse a request that names another host or carries a dot segment** ([#19223](https://github.com/hivecommons/hive/issues/19223)). Inside a MITM'd session, a Host header or absolute-form URL naming a host other than the CONNECT target is now refused with a 400 and the connection closed, as the sandbox read relay already did; the rule engine and credential were chosen for the CONNECT host. A `.` or `..` path segment, in any percent-encoded spelling, is refused on the inspected relay (400) and the sandbox read relay (403), since the gates match the literal path while an upstream that resolves dot segments acts on a different one. New table tests pin that CL+TE, duplicate or malformed Content-Length, unknown Transfer-Encoding, folded headers and bare LF never reach the upstream with ambiguous framing on `proxyHTTPHost`, `forwardPlainDirect` and the sandbox read relay.
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 @@ -45,6 +45,17 @@ func (p *GitHubProxy) proxyHTTPHost(client net.Conn, upstream net.Conn, host str
p.logClientRead("proxy client request read timed out", err, "agent", agentName)
return
}
// #19223: a request naming another host, or a path the upstream could
// resolve differently from the gates below, is refused before any gate
// reads it, and the connection is closed behind it.
if reason, refused := relayRequestTargetRefusal(req, host); refused {
_ = client.SetReadDeadline(time.Time{})
p.recordViolation(agentName, req.Method, reason)
_ = client.SetWriteDeadline(time.Now().Add(httpWriteTimeout))
_ = writeRelayBadRequest(client, reason)
_ = client.SetWriteDeadline(time.Time{})
return
}

blocked := false
blockReason := ""
Expand Down
79 changes: 79 additions & 0 deletions src/crux/proxy/relay_request_target.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package proxy

import (
"fmt"
"net"
"net/http"
"net/url"
"strings"
)

// Request-target hygiene for the parsed agent relays (#19223, egress audit
// section 4).
//
// Framing ambiguity (CL+TE, duplicate or malformed Content-Length, unknown
// Transfer-Encoding, folded headers, bare LF) is settled by net/http before a
// request reaches any gate: ReadRequest refuses what it cannot frame and
// req.Write regenerates one framing for the next hop, so the gates and the
// upstream always agree on where a request ends. relay_smuggling_test.go pins

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Header comment is truncated mid-sentence ("so the gates and the" / "that, per relay."). Please reword; comment-only.

// that, per relay.
//
// What net/http does NOT settle is which resource a request names, and two
// spellings let the gates and the upstream disagree:
//
// - A Host header or absolute-form URL naming a host other than the one the
// TLS session was forged for. The rule engine (GitHub vs Linear) and the
// credential were chosen for the CONNECT host; a request naming another
// host must not ride that decision.
// - A dot segment ("." or "..", in any percent-encoded spelling) in the path.
// The gates match the literal path, but an upstream that resolves dot
// segments acts on a different one: /repos/allowed/r/../../paused/r/merge
// is gated as allowed/r and merged as paused/r. No legitimate client sends
// one, so it is refused rather than resolved.
//
// Both refusals are fail-closed: a target that cannot be decoded is refused.

// relayPathHasDotSegment reports whether an escaped request path contains a
// "." or ".." segment in any percent-encoded spelling, or a segment that
// cannot be decoded at all.
func relayPathHasDotSegment(escapedPath string) bool {
for _, seg := range strings.Split(escapedPath, "/") {
decoded, err := url.PathUnescape(seg)
if err != nil {
return true
}
// A decoded segment can still carry an encoded slash ("..%2F"): split
// again so each piece the upstream might see is checked.
for _, piece := range strings.Split(decoded, "/") {
if piece == "." || piece == ".." {
return true
}
}
}
return false
}

// relayRequestTargetRefusal reports why a request parsed off a terminated TLS
// session for host must not be forwarded. An empty host is the GitHub-implied
// proxyHTTP form, which names no CONNECT target to compare against, so only
// the path check applies there.
func relayRequestTargetRefusal(req *http.Request, host string) (string, bool) {
if req.URL == nil {
return "the request target could not be parsed", true
}
if host != "" && !sandboxReadRequestTargetsHost(req, host) {
return "the request names a different host than the CONNECT target", true
}
if relayPathHasDotSegment(req.URL.EscapedPath()) {
return "the request path contains a dot segment", true
}
return "", false
}

// writeRelayBadRequest answers a refused request target. The connection is
// closed after it, so nothing pipelined behind the request is read.
func writeRelayBadRequest(w net.Conn, reason string) error {
body := "blocked by hive: " + reason + "\n"
_, err := fmt.Fprintf(w, "HTTP/1.1 400 Bad Request\r\nContent-Type: text/plain; charset=utf-8\r\nX-Hive-Proxy-Blocked: true\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s", len(body), body)
return err
}
Loading
Loading