| 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) |
| + } |
| + }) |
| +} |