carlos / platform Public

console: instance-report fetch never follows redirects (H4-M8)

The app answers /.carlos/instances, so a followed Location let a tenant
point the console's GET at any host the console can reach. The fetcher's
client now returns the 3xx as-is, which folds to "not reporting"; a
caller-given client is copied before CheckRedirect is set.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 23d789c06e46ae49f2f5db3c244f5e048ae91ccf parent 3793aa4
2 files changed, +70 −0
  • internal/console/hunt_H4-M8_test.go +60 −0
  • internal/console/instances.go +10 −0
diff --git a/internal/console/hunt_H4-M8_test.go b/internal/console/hunt_H4-M8_test.go
new file mode 100644
index 000000000..5b174770e
--- /dev/null
+++ b/internal/console/hunt_H4-M8_test.go
@@ -0,0 +1,60 @@
+package console
+
+// Hunt H4-M8 (verifier, opus): the app's /.carlos/instances answer is a
+// redirect, and the console's instance fetcher (production wiring: cmd never
+// sets Config.AppClient, so newInstanceFetcher's default http.Client is used)
+// follows it to a host the tenant chose. Here the target is a second loopback
+// server standing in for a service on the console host's own network.
+//
+// The assertion is the SECURE behaviour: the internal service is never hit.
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "sync/atomic"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+func TestHunt_H4_M8_InstanceReportFollowsRedirect(t *testing.T) {
+ fx := newConsoleFixture(t)
+ c := signInPaul(t, fx)
+
+ // The "internal" service: it records every hit, and answers in the one
+ // shape the fetcher reflects onto the page.
+ var internalHits atomic.Int64
+ var internalPath atomic.Value
+ internal := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ internalHits.Add(1)
+ internalPath.Store(r.URL.RequestURI())
+ w.Write([]byte(`[{"slug":"internal-secret-row","detail":"served by the internal service"}]`))
+ }))
+ t.Cleanup(internal.Close)
+
+ // The tenant's app: its report endpoint redirects into the internal one.
+ app := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path != InstancesPath {
+ http.NotFound(w, r)
+ return
+ }
+ http.Redirect(w, r, internal.URL+"/admin/anything?probe=1", http.StatusFound)
+ }))
+ t.Cleanup(app.Close)
+ host := strings.TrimPrefix(app.URL, "http://")
+ fx.addRoute(t, registry.Route{Host: host, Kind: "instance", Status: "ready", App: "kass"})
+
+ status, _, body := get(t, c, fx.u("/apps/kass/instances"))
+ if status != http.StatusOK {
+ t.Fatalf("instances page = %d", status)
+ }
+ if n := internalHits.Load(); n != 0 {
+ p, _ := internalPath.Load().(string)
+ t.Errorf("H4-M8: the console followed the app's redirect and hit the internal service %d time(s) (GET %s); want 0",
+ n, p)
+ }
+ if strings.Contains(body, "internal-secret-row") {
+ t.Errorf("H4-M8: the internal service's slug-shaped answer was rendered on the tenant's instances page")
+ }
+}
diff --git a/internal/console/instances.go b/internal/console/instances.go
index 876ceac06..c8b352a0b 100644
--- a/internal/console/instances.go
+++ b/internal/console/instances.go
@@ -72,10 +72,20 @@ type instanceCacheEntry struct {
// newInstanceFetcher builds the fetcher over client (nil is a plain client
// with the fetch timeout). It reads s.perf, so NewServer calls it once that
// is set.
+//
+// The fetcher never follows a redirect: the app chooses the answer, so a
+// followed Location would let a tenant point the console's GET at any host
+// the console can reach (its own loopback, another tenant's backend). A 3xx
+// is a non-200 like any other and folds to "not reporting". A given client
+// is copied, not changed, to set that.
func (s *Server) newInstanceFetcher(client *http.Client, scheme string) *instanceFetcher {
if client == nil {
client = &http.Client{Timeout: instancesTimeout}
+ } else {
+ c := *client
+ client = &c
}
+ client.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
if scheme == "" {
scheme = "https"
}