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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- **Hub: the contribute proxies and the lite enrollment repo-access check connect only to the address they checked** ([#19221](https://github.com/hivecommons/hive/issues/19221), part of [#18986](https://github.com/hivecommons/hive/issues/18986)). The `/api/contribute/register` and `/api/contribute/ws` proxies and the GitHub repo-access check behind lite enrollment and self-service registration validated a self-reported `DashboardURL` or caller-supplied `github_host` and then let the HTTP client resolve the name again, so a name that rebinds between the two lookups was checked as public and reached as private. They now use the hub's pinned transport: each dial resolves once, every answer is checked with the shared classifier, a lookup failure refuses the dial, and every redirect hop goes through the same check.
5 changes: 1 addition & 4 deletions src/pkg/hub/github_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ func hubGitHubHTTPClient() *http.Client {
// Honour a replaced http.DefaultTransport (tests and instrumented builds
// inject a RoundTripper there); otherwise use the proxy-trusting shared
// socket transport.
var base http.RoundTripper
if _, ok := http.DefaultTransport.(*http.Transport); !ok {
base = http.DefaultTransport
}
base, _ := replacedDefaultTransport()
return &http.Client{Transport: hgithub.NewHTTPTransport(base)}
}

Expand Down
18 changes: 17 additions & 1 deletion src/pkg/hub/lite_enrollment.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,25 @@ func validateLiteGitHubHost(ctx context.Context, host string) error {
return nil
}

// liteRepoAccessTransport dials only an address isPrivateURL passed (#19221,
// part of #18986). validateLiteGitHubHost checks the caller-supplied
// github_host before the request, and a default transport resolved the name
// again at dial time, so a host whose answer rebinds was checked as public
// and connected to as private. Every connection, redirect hops included, is
// resolved once and pinned to a checked address.
var liteRepoAccessTransport http.RoundTripper = pinnedFetchTransport(isPrivateURL)

// liteRepoAccessRoundTripper is liteRepoAccessTransport, unless
// http.DefaultTransport was replaced (tests inject a RoundTripper there, the
// same convention as hubGitHubHTTPClient); then that replacement is used.
func liteRepoAccessRoundTripper() http.RoundTripper {
return pinnedOrReplacedDefault(liteRepoAccessTransport)
}

func liteRepoAccessHTTPClient() *http.Client {
return &http.Client{
Timeout: liteRepoAccessHTTPTimeout,
Timeout: liteRepoAccessHTTPTimeout,
Transport: liteRepoAccessRoundTripper(),
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if len(via) >= 10 {
return fmt.Errorf("stopped after %d redirects", len(via))
Expand Down
104 changes: 104 additions & 0 deletions src/pkg/hub/pinned_fetch_contribute_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package hub

import (
"context"
"errors"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"

"github.com/hivecommons/hive/pkg/netguard"
)

// rebindingContributeHub registers one public contribute hive whose
// DashboardURL names host on upstream's loopback port, with the resolver
// answering public first and loopback after (stubRebindingResolver).
func rebindingContributeHub(t *testing.T, host string, upstream *httptest.Server) (*HubServer, func() int) {
t.Helper()
lookups := stubRebindingResolver(t, host)
srv := newHubServerForTest(t)
srv.mu.Lock()
srv.registry.Hives = []RegistryEntry{
{ID: "rebind-hive", Online: true, IsPublic: true, DashboardURL: "http://" + host + ":" + loopbackPort(t, upstream), Owner: "user1"},
}
srv.mu.Unlock()
return srv, lookups
}

// findContributeHive admits the hive on the resolver's first (public) answer.
// The registration proxy must resolve again at dial time through the same
// seam, see the rebound loopback answer, and refuse it (#19221).
func TestHandleContributeProxyPinsResolvedAddress(t *testing.T) {
var reached atomic.Bool
upstream := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { reached.Store(true) }))
defer upstream.Close()
srv, lookups := rebindingContributeHub(t, "contrib.rebind.example", upstream)

req := httptest.NewRequest(http.MethodPost, "/api/contribute/register", strings.NewReader(`{}`))
rec := httptest.NewRecorder()
srv.handleContributeProxy(rec, req)

if reached.Load() {
t.Fatal("contribute proxy reached the rebound loopback address")
}
if rec.Code != http.StatusBadGateway {
t.Fatalf("rebound proxy: status = %d, want 502", rec.Code)
}
if got := lookups(); got < 2 {
t.Fatalf("resolver consulted %d times, want the guard's lookup and the pinned dial's own", got)
}
}

func TestHandleContributeWSProxyPinsResolvedAddress(t *testing.T) {
var reached atomic.Bool
upstream := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { reached.Store(true) }))
defer upstream.Close()
srv, lookups := rebindingContributeHub(t, "contrib-ws.rebind.example", upstream)

req := httptest.NewRequest(http.MethodGet, "/api/contribute/ws", nil)
rec := httptest.NewRecorder()
srv.handleContributeWSProxy(rec, req)

if reached.Load() {
t.Fatal("contribute WS proxy reached the rebound loopback address")
}
if rec.Code != http.StatusBadGateway {
t.Fatalf("rebound WS proxy: status = %d, want 502", rec.Code)
}
if got := lookups(); got < 2 {
t.Fatalf("resolver consulted %d times, want the guard's lookup and the pinned dial's own", got)
}
}

// validateLiteGitHubHost passes a github_host on the first (public) answer;
// the repo access request must not then connect to the rebound loopback one.
func TestVerifyGitHubRepoAccessPinsResolvedAddress(t *testing.T) {
lookups := stubRebindingResolver(t, "ghe.rebind.example")

ok, err := verifyGitHubRepoAccess(context.Background(), "tok", "ghe.rebind.example", "o", "r")
if ok {
t.Fatal("a rebound github_host reported access")
}
if !errors.Is(err, netguard.ErrRefused) {
t.Fatalf("err = %v, want netguard.ErrRefused from the pinned dial", err)
}
if got := lookups(); got < 2 {
t.Fatalf("resolver consulted %d times, want the guard's lookup and the pinned dial's own", got)
}
}

// Without a replaced http.DefaultTransport the client carries the pinned
// transport; a replaced one (the test convention) is honoured instead.
func TestLiteRepoAccessHTTPClientUsesPinnedTransport(t *testing.T) {
if got := liteRepoAccessHTTPClient().Transport; got != liteRepoAccessTransport {
t.Fatalf("Transport = %T, want liteRepoAccessTransport", got)
}
srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
t.Cleanup(srv.Close)
swapDefaultTransport(t, srv)
if got := liteRepoAccessHTTPClient().Transport; got != http.DefaultTransport {
t.Fatalf("Transport = %T, want the replaced http.DefaultTransport", got)
}
}
33 changes: 33 additions & 0 deletions src/pkg/hub/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5095,6 +5095,7 @@ func (s *HubServer) handleContributeProxy(w http.ResponseWriter, r *http.Request
return
}
proxy := httputil.NewSingleHostReverseProxy(target)
proxy.Transport = pinnedOrReplacedDefault(contributeProxyTransport)
r.URL.Path = "/api/contribute/register"
r.Host = target.Host
s.logger.Info("proxying contribute registration", "hive", hive.ID, "target", target.String())
Expand Down Expand Up @@ -5132,6 +5133,9 @@ func (s *HubServer) handleContributeWSProxy(w http.ResponseWriter, r *http.Reque
return
}
proxy := newContributeWSReverseProxy(target)
if proxy.Transport == nil {
proxy.Transport = pinnedOrReplacedDefault(contributeProxyTransport)
}
r.URL.Path = "/api/contribute/ws"
r.Host = target.Host
s.logger.Info("proxying contribute WS", "hive", hive.ID, "target", target.String())
Expand All @@ -5140,6 +5144,35 @@ func (s *HubServer) handleContributeWSProxy(w http.ResponseWriter, r *http.Reque

var newContributeWSReverseProxy = httputil.NewSingleHostReverseProxy

// contributeProxyTransport carries the contribute registration and WS proxies
// to the selected hive (#19221, part of #18986). findContributeHive admits a
// hive only when its self-reported DashboardURL passes isPrivateURL, but
// httputil's default transport resolved the name again when it dialled, so a
// DashboardURL whose name rebinds was checked as public and proxied to as
// private. This transport resolves each dial once, checks every answer with
// isPrivateURL, and connects only to an address it checked.
var contributeProxyTransport http.RoundTripper = pinnedFetchTransport(isPrivateURL)

// replacedDefaultTransport reports http.DefaultTransport when it was replaced
// with something other than *http.Transport (tests and instrumented builds
// inject a RoundTripper there). Every pinned call site shares this check so
// they honour the override the same way.
func replacedDefaultTransport() (http.RoundTripper, bool) {
if _, ok := http.DefaultTransport.(*http.Transport); ok {
return nil, false
}
return http.DefaultTransport, true
}

// pinnedOrReplacedDefault returns pinned, unless http.DefaultTransport was
// replaced; then that replacement is used.
func pinnedOrReplacedDefault(pinned http.RoundTripper) http.RoundTripper {
if rt, ok := replacedDefaultTransport(); ok {
return rt
}
return pinned
}

// privateURLDNSTimeout bounds DNS resolution inside the SSRF guard so a
// slow or malicious DNS server cannot block the caller indefinitely.
const privateURLDNSTimeout = 5 * time.Second
Expand Down
Loading