diff --git a/docs/cli/dev.md b/docs/cli/dev.md index 5291565..8ab7b7c 100644 --- a/docs/cli/dev.md +++ b/docs/cli/dev.md @@ -151,6 +151,10 @@ explicitly exposed service. This is development access control, not a production tenant isolation or network-policy equivalence claim. Host-IP allowlisting is coarser than per-port isolation. +Local bindings keep the capability in root-relative and path-relative +redirects. Path-relative redirects use the application request path, without +the capability prefix. External and protocol-relative redirects do not change. + Fresh-per-request is the default for HTTP and direct CloudEvents. Explicit `execution.mode: reuse` retains revision-scoped live state until `execution.idleTimeout`. Streams keep their Fiber until completion or client diff --git a/internal/fiberdintegration/local.go b/internal/fiberdintegration/local.go index 3a54306..e1c0575 100644 --- a/internal/fiberdintegration/local.go +++ b/internal/fiberdintegration/local.go @@ -14,6 +14,7 @@ import ( "log" "net" "net/http" + "net/url" "os" "os/exec" "path/filepath" @@ -621,6 +622,12 @@ func localHandler(router *Router, name, capability string) http.Handler { localForward(router, name, &localBindingResponseWriter{ ResponseWriter: w, capability: capability, + requestURL: url.URL{ + Path: request.URL.Path, + RawPath: request.URL.RawPath, + RawQuery: request.URL.RawQuery, + ForceQuery: request.URL.ForceQuery, + }, }, request) }) } @@ -628,6 +635,7 @@ func localHandler(router *Router, name, capability string) http.Handler { type localBindingResponseWriter struct { http.ResponseWriter capability string + requestURL url.URL rewritten bool } @@ -653,9 +661,19 @@ func (writer *localBindingResponseWriter) rewriteLocation() { } writer.rewritten = true location := writer.Header().Get("Location") - if strings.HasPrefix(location, "/") && !strings.HasPrefix(location, "//") { + if location == "" || strings.HasPrefix(location, "//") { + return + } + if strings.HasPrefix(location, "/") { writer.Header().Set("Location", writer.capability+location) + return + } + reference, err := url.Parse(location) + if err != nil || reference.IsAbs() || reference.Host != "" { + return } + resolved := writer.requestURL.ResolveReference(reference) + writer.Header().Set("Location", writer.capability+resolved.String()) } func localIngressHandler(router *Router, exposed map[string]string, port string) http.Handler { diff --git a/internal/fiberdintegration/local_test.go b/internal/fiberdintegration/local_test.go index 29de331..fd28f10 100644 --- a/internal/fiberdintegration/local_test.go +++ b/internal/fiberdintegration/local_test.go @@ -71,6 +71,93 @@ func TestLocalBindingRequiresCapabilityBeforeAcquiringFiber(t *testing.T) { } } +func TestLocalBindingFollowsRelativeRedirects(t *testing.T) { + const capability = "/_lampyr/private" + for _, test := range []struct { + name string + path string + location string + target string + }{ + {name: "exact binding URL", path: "", location: "next", target: "/next"}, + {name: "nested binding path", path: "/nested", location: "../target", target: "/target"}, + } { + t.Run(test.name, func(t *testing.T) { + router, client, route := routerFixture(t, project.ExecutionFresh) + client.handler = http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) { + if request.URL.Path == test.target { + fmt.Fprint(w, "reached "+request.URL.Path) + return + } + start := test.path + if start == "" { + start = "/" + } + if request.URL.Path != start { + http.NotFound(w, request) + return + } + w.Header().Set("Location", test.location) + w.WriteHeader(http.StatusFound) + }) + server := httptest.NewServer(localHandler(router, route.Revision, capability)) + defer server.Close() + httpClient := server.Client() + httpClient.Timeout = 5 * time.Second + response, err := httpClient.Get(server.URL + capability + test.path) + if err != nil { + t.Fatal(err) + } + defer response.Body.Close() + body, err := io.ReadAll(response.Body) + if err != nil { + t.Fatal(err) + } + if response.StatusCode != http.StatusOK || string(body) != "reached "+test.target || + response.Request.URL.Path != capability+test.target { + t.Fatalf("followed %q redirect: status=%d URL=%s body=%q", + test.location, response.StatusCode, response.Request.URL, body) + } + }) + } +} + +func TestLocalBindingRedirectReferences(t *testing.T) { + for _, test := range []struct { + name string + path string + location string + want string + }{ + {name: "escaped path", path: "/a%2Fb/start", location: "next%2Fpart?q=hello%20world#section", want: "/_lampyr/private/a%2Fb/next%2Fpart?q=hello%20world#section"}, + {name: "query only", path: "/nested/start?old=value", location: "?new=value", want: "/_lampyr/private/nested/start?new=value"}, + {name: "fragment only", path: "/nested/start?old=value", location: "#section", want: "/_lampyr/private/nested/start?old=value#section"}, + {name: "traverse above root", path: "/nested/start", location: "../../../target", want: "/_lampyr/private/target"}, + {name: "root relative", path: "/start", location: "/target?q=value#section", want: "/_lampyr/private/target?q=value#section"}, + {name: "external", path: "/start", location: "https://example.com/target", want: "https://example.com/target"}, + {name: "protocol relative", path: "/start", location: "//example.com/target", want: "//example.com/target"}, + {name: "non HTTP scheme", path: "/start", location: "mailto:test@example.com", want: "mailto:test@example.com"}, + {name: "invalid reference", path: "/start", location: "%invalid", want: "%invalid"}, + {name: "no location", path: "/start"}, + } { + t.Run(test.name, func(t *testing.T) { + router, client, route := routerFixture(t, project.ExecutionFresh) + client.handler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + if test.location != "" { + w.Header().Set("Location", test.location) + } + w.WriteHeader(http.StatusFound) + }) + response := httptest.NewRecorder() + request := httptest.NewRequest("GET", "http://unknown/_lampyr/private"+test.path, nil) + localHandler(router, route.Revision, "/_lampyr/private").ServeHTTP(response, request) + if got := response.Result().Header.Get("Location"); got != test.want { + t.Fatalf("Location = %q, want %q", got, test.want) + } + }) + } +} + func TestLocalIngressOnlyExposesDeclaredHostnames(t *testing.T) { router, client, route := routerFixture(t, project.ExecutionFresh) handler := localIngressHandler(router, map[string]string{"demo.default.localhost": route.Revision}, "8080") diff --git a/internal/fiberdintegration/router_lifecycle_test.go b/internal/fiberdintegration/router_lifecycle_test.go index 8e04cc3..e6e8712 100644 --- a/internal/fiberdintegration/router_lifecycle_test.go +++ b/internal/fiberdintegration/router_lifecycle_test.go @@ -230,8 +230,17 @@ func TestFreshReleaseOnApplicationAndTransportErrors(t *testing.T) { } func TestFreshStreamRetainsFiberUntilCompletionOrCancellation(t *testing.T) { - for _, cancelStream := range []bool{false, true} { - t.Run(fmt.Sprint(cancelStream), func(t *testing.T) { + for _, test := range []struct { + name string + binding bool + cancelStream bool + }{ + {name: "router completion"}, + {name: "router cancellation", cancelStream: true}, + {name: "binding completion", binding: true}, + {name: "binding cancellation", binding: true, cancelStream: true}, + } { + t.Run(test.name, func(t *testing.T) { router, client, route := routerFixture(t, project.ExecutionFresh) end := make(chan struct{}) client.handler = http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) { @@ -243,15 +252,19 @@ func TestFreshStreamRetainsFiberUntilCompletionOrCancellation(t *testing.T) { fmt.Fprint(w, "end\n") } }) - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) { + var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) { request.Header.Set("Knative-Serving-Namespace", route.Namespace) request.Header.Set("Knative-Serving-Revision", route.Revision) router.ServeHTTP(w, request) - })) + }) + if test.binding { + handler = localHandler(router, route.Revision, "/_lampyr/private") + } + server := httptest.NewServer(handler) defer server.Close() ctx, cancel := context.WithCancel(t.Context()) defer cancel() - request, _ := http.NewRequestWithContext(ctx, "GET", server.URL, nil) + request, _ := http.NewRequestWithContext(ctx, "GET", server.URL+"/_lampyr/private", nil) response, err := http.DefaultClient.Do(request) if err != nil { t.Fatal(err) @@ -266,7 +279,7 @@ func TestFreshStreamRetainsFiberUntilCompletionOrCancellation(t *testing.T) { if released != 0 { t.Fatal("stream released before completion") } - if cancelStream { + if test.cancelStream { cancel() } else { close(end)