carlos / platform Public

edge: keep the :80 redirect on its own host for a rootless target

httpFallbackHandler built its Location as "https://" + host + the
request-target, so a rootless target such as "x:@evil.example/p" (or
"*") produced https://<host>@evil.example/p. Any target that is not a
rooted path now redirects to "/", the rule console.redirectTarget
already applies.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 89d69cf8aef05a76cc59f77d3cf91d75200a3415 parent 3793aa4
2 files changed, +93 −1
  • internal/edge/edge.go +8 −1
  • internal/edge/http_fallback_redirect_test.go +85 −0
diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index 8169239ed..4c211274f 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -3864,7 +3864,14 @@ func (e *Edge) httpFallbackHandler() http.Handler {
http.Error(w, "Use HTTPS", http.StatusBadRequest)
return
}
- http.Redirect(w, r, "https://"+host+r.URL.RequestURI(), http.StatusFound)
+ // Only a rooted path may follow the host: a rootless target
+ // ("x:@evil.example/p", "*") would otherwise splice itself onto the
+ // authority. Twin of console.redirectTarget (console imports edge).
+ uri := r.URL.RequestURI()
+ if !strings.HasPrefix(uri, "/") {
+ uri = "/"
+ }
+ http.Redirect(w, r, "https://"+host+uri, http.StatusFound)
})
}
diff --git a/internal/edge/http_fallback_redirect_test.go b/internal/edge/http_fallback_redirect_test.go
new file mode 100644
index 000000000..bce6fcf27
--- /dev/null
+++ b/internal/edge/http_fallback_redirect_test.go
@@ -0,0 +1,85 @@
+package edge
+
+import (
+ "bufio"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+// TestHTTPFallbackRedirectRootlessTarget pins the :80 redirect to its own
+// authority. The redirect is "https://" + host + the request-target, and a
+// request-target that does not start with "/" (a rootless "x:@evil.example/p"
+// keeps "@evil.example/p" as its opaque part; "*" stays "*") would splice
+// itself onto the host and turn the Location into userinfo@another-host. The
+// rule is console.redirectTarget's: anything not rooted becomes "/".
+//
+// The requests are parsed off the wire with http.ReadRequest, as the :80
+// server parses them, because httptest.NewRequest cannot express a rootless
+// target.
+func TestHTTPFallbackRedirectRootlessTarget(t *testing.T) {
+ const host = "shop.example.com"
+ backend, _ := hostEchoBackend(t, "primary")
+ reg := openReg(t)
+ if err := reg.Upsert(registry.Route{
+ Host: host, Kind: "service",
+ Addr: backend.Listener.Addr().String(), Status: "ready",
+ }); err != nil {
+ t.Fatalf("upsert: %v", err)
+ }
+ e, err := New(reg, nil)
+ if err != nil {
+ t.Fatalf("New: %v", err)
+ }
+ h := e.httpFallbackHandler()
+
+ serve := func(t *testing.T, requestLine string) *httptest.ResponseRecorder {
+ t.Helper()
+ req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(
+ requestLine + "\r\nHost: " + host + "\r\n\r\n")))
+ if err != nil {
+ t.Fatalf("ReadRequest(%q): %v", requestLine, err)
+ }
+ rec := httptest.NewRecorder()
+ h.ServeHTTP(rec, req)
+ return rec
+ }
+
+ for _, tc := range []struct {
+ name, requestLine, want string
+ }{
+ {"rootless", "GET x:@evil.example/p HTTP/1.1", "https://" + host + "/"},
+ {"asterisk", "GET * HTTP/1.1", "https://" + host + "/"},
+ // The absolute form's authority replaces the Host header (RFC 9112
+ // §3.2.2, and net/http does the same), so naming this box's own host
+ // there is an ordinary redirect.
+ {"absolute form, own host", "GET http://" + host + "/p?q=1 HTTP/1.1", "https://" + host + "/p?q=1"},
+ {"origin form", "GET /a/b?c=1 HTTP/1.1", "https://" + host + "/a/b?c=1"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ rec := serve(t, tc.requestLine)
+ if rec.Code != http.StatusFound {
+ t.Fatalf("status = %d, want 302", rec.Code)
+ }
+ if loc := rec.Header().Get("Location"); loc != tc.want {
+ t.Fatalf("Location = %q, want %q", loc, tc.want)
+ }
+ })
+ }
+
+ // An absolute-form target naming a host this box does not serve is that
+ // host's request, not shop's: it gets unknownHost (404, no fallback URL
+ // configured), never a Location pointing at the foreign authority.
+ t.Run("absolute form, foreign host", func(t *testing.T) {
+ rec := serve(t, "GET http://evil.example/p HTTP/1.1")
+ if rec.Code != http.StatusNotFound {
+ t.Fatalf("status = %d, want 404 (unknownHost)", rec.Code)
+ }
+ if loc := rec.Header().Get("Location"); loc != "" {
+ t.Fatalf("Location = %q, want none", loc)
+ }
+ })
+}