mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-21 04:49:10 +02:00
[management] Close the review's nitpicks on live model discovery
Five smaller points from the same review as the four already fixed. **The no-redirect policy had no test.** Every Fetch test injects an HTTPClient, which bypasses httpClient() and therefore the policy entirely, so nothing asserted that a 302 is refused — and the policy is a security control: a redirect moves the request to a host checkPublicHost never resolved. TestRedirectsAreNotFollowed drives the real constructor against an httptest server that redirects to the cloud metadata address, and asserts exactly one request leaves the client. **An injected client silently lost that policy.** Production is safe today only because NewManager passes a nil HTTPClient; any future non-test injection would have dropped the guarantee with no signal. An injected client that states no policy now inherits ours. Implemented by copying the client rather than assigning into it. Writing c.HTTPClient.CheckRedirect from httpClient() would mutate a struct shared by every request goroutine for the process's lifetime — a data race, and the exact hazard the same review's next point warns about. The copy shares the Transport, which is safe for concurrent use by design. **Two comments described things that were not true.** The manager's doc claimed reading a stored credential "is permission-checked as one"; there is no per-record Read check, just the single Create check, which covers it because Create is stronger and the lookup is account-scoped. Said that instead. The modelDiscovery field now records that it is shared across requests and must stay read-only after construction. **The handler test asserted neither the label contract nor the upstream.** The response omits label entirely when a vendor supplies none, and the dashboard falls back to the id on absence — an empty string would render a blank row. The fixture had no label-less model to prove it with (the review described one, but both existing entries carry labels), so this adds one. The upstream assertion matters because Bedrock's region is read back out of it. Also fixes the "blank catalog provide" subtest name.
This commit is contained in:
@@ -357,22 +357,37 @@ func decorate(entry catalog.Provider, ids []listedModel) []Model {
|
||||
return out
|
||||
}
|
||||
|
||||
// refuseRedirect is the redirect policy every discovery request runs under. A
|
||||
// redirect is a way to move the request to a host checkPublicHost never saw,
|
||||
// so none are followed.
|
||||
func refuseRedirect(*http.Request, []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
}
|
||||
|
||||
func (c *Client) httpClient() *http.Client {
|
||||
if c.HTTPClient != nil {
|
||||
return c.HTTPClient
|
||||
if c.HTTPClient.CheckRedirect != nil {
|
||||
return c.HTTPClient
|
||||
}
|
||||
// An injected client that states no policy still gets ours: the
|
||||
// no-redirect guarantee should not depend on the caller remembering it.
|
||||
//
|
||||
// Copied rather than assigned into: one Client is shared by every
|
||||
// request for the process's lifetime, so writing to its fields here
|
||||
// would race across request goroutines. The copy shares the Transport,
|
||||
// which is safe for concurrent use by design.
|
||||
clone := *c.HTTPClient
|
||||
clone.CheckRedirect = refuseRedirect
|
||||
return &clone
|
||||
}
|
||||
transport := guardedTransport
|
||||
if c.AllowPrivateHosts {
|
||||
transport = http.DefaultTransport
|
||||
}
|
||||
return &http.Client{
|
||||
Timeout: fetchTimeout,
|
||||
Transport: transport,
|
||||
// A redirect is a way to move the request to a host the guard above
|
||||
// never checked, so none are followed.
|
||||
CheckRedirect: func(*http.Request, []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
Timeout: fetchTimeout,
|
||||
Transport: transport,
|
||||
CheckRedirect: refuseRedirect,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/netip"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -255,6 +256,63 @@ func TestHostGuardResolvesAndRejectsLocalhost(t *testing.T) {
|
||||
assert.Contains(t, err.Error(), "non-public")
|
||||
}
|
||||
|
||||
// TestRedirectsAreNotFollowed covers a gap the other tests leave open: they all
|
||||
// inject an HTTPClient, which bypasses httpClient() and therefore the redirect
|
||||
// policy entirely. The policy is a security control — a 302 moves the request
|
||||
// to a host checkPublicHost never resolved — so it needs a test that goes
|
||||
// through the constructor the manager actually uses.
|
||||
func TestRedirectsAreNotFollowed(t *testing.T) {
|
||||
var hits int
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
hits++
|
||||
http.Redirect(w, r, "http://169.254.169.254/latest/meta-data/", http.StatusFound)
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
|
||||
for name, cl := range map[string]*Client{
|
||||
// The production shape: no injected client at all.
|
||||
"default client": {AllowPrivateHosts: true},
|
||||
// An injected client that states no policy must inherit ours rather
|
||||
// than silently chasing the redirect.
|
||||
"injected client with no policy": {
|
||||
AllowPrivateHosts: true,
|
||||
HTTPClient: &http.Client{},
|
||||
},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
hits = 0
|
||||
req, err := http.NewRequest(http.MethodGet, srv.URL, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
resp, err := cl.httpClient().Do(req)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = resp.Body.Close() })
|
||||
|
||||
assert.Equal(t, http.StatusFound, resp.StatusCode,
|
||||
"the redirect must be surfaced, not followed to an unchecked host")
|
||||
assert.Equal(t, 1, hits, "exactly one request must leave the client")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestInjectedClientKeepsItsOwnRedirectPolicy pins that the default above is a
|
||||
// default, not an override, and that supplying it does not mutate the caller's
|
||||
// client — one Client is shared across every request, so a write here would
|
||||
// race.
|
||||
func TestInjectedClientKeepsItsOwnRedirectPolicy(t *testing.T) {
|
||||
own := func(*http.Request, []*http.Request) error { return nil }
|
||||
injected := &http.Client{CheckRedirect: own}
|
||||
cl := &Client{HTTPClient: injected}
|
||||
|
||||
assert.Same(t, injected, cl.httpClient(),
|
||||
"a client that states a policy must be handed back untouched")
|
||||
|
||||
bare := &http.Client{}
|
||||
cl = &Client{HTTPClient: bare}
|
||||
require.NotSame(t, bare, cl.httpClient(), "the policy must be applied to a copy")
|
||||
assert.Nil(t, bare.CheckRedirect, "the caller's client must not be written to")
|
||||
}
|
||||
|
||||
// TestDialGuardRejectsRebindingToANonPublicAddress covers the window between
|
||||
// the two DNS lookups. checkPublicHost resolves the host, then the transport
|
||||
// resolves it again to dial; a name whose owner answers the first with a public
|
||||
|
||||
Reference in New Issue
Block a user