Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(ingress): proxy request paths with "//", "." or ".." segments
Maohaoe06e92997834
diff --git a/components/ingress/main.go b/components/ingress/main.goindex 3ea30dc19..c2e39f874 100644--- a/components/ingress/main.go+++ b/components/ingress/main.go@@ -162,9 +162,9 @@ func main() {scopeVerifier,proxyOptions...,)- mux := newIngressMux(reverseProxy, networkReadiness)+ handler := newIngressHandler(reverseProxy, networkReadiness)- if err := http.ListenAndServe(fmt.Sprintf(":%v", flag.Port), mux); err != nil {+ if err := http.ListenAndServe(fmt.Sprintf(":%v", flag.Port), handler); err != nil {log.Panicf("Error starting http server: %v", err)}}@@ -252,12 +252,21 @@ func newNetworkReadiness(config connectivity.TrackerConfig) (connectivity.Observreturn observer, connectivity.NewReadinessHandler(tracker), nil}-func newIngressMux(reverseProxy, networkReadiness http.Handler) *http.ServeMux {- mux := http.NewServeMux()- mux.Handle("/", reverseProxy)- mux.Handle("/status.ok/network-readiness", networkReadiness)- mux.HandleFunc("/status.ok", proxy.Healthz)- return mux+// newIngressHandler serves the status endpoints and proxies everything else.+// It does not use http.ServeMux, which answers paths with "//", "." or ".."+// segments with a redirect to the cleaned path instead of passing them on;+// the sandbox application owns its paths.+func newIngressHandler(reverseProxy, networkReadiness http.Handler) http.Handler {+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ switch r.URL.Path {+ case "/status.ok":+ proxy.Healthz(w, r)+ case "/status.ok/network-readiness":+ networkReadiness.ServeHTTP(w, r)+ default:+ reverseProxy.ServeHTTP(w, r)+ }+ })}func withLogger(ctx context.Context, logLevel string) context.Context {diff --git a/components/ingress/main_test.go b/components/ingress/main_test.gonew file mode 100644index 000000000..c0742a4c6--- /dev/null+++ b/components/ingress/main_test.go@@ -0,0 +1,71 @@+// Copyright 2026 The OpenSandbox Authors+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package main++import (+ "net/http"+ "net/http/httptest"+ "testing"+)++func pathRecorder(name string, got *string) http.Handler {+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ *got = name + " " + r.Method + " " + r.URL.Path+ w.WriteHeader(http.StatusTeapot)+ })+}++func TestIngressHandlerSendsUncleanPathsToTheProxy(t *testing.T) {+ var got string+ handler := newIngressHandler(pathRecorder("proxy", &got), pathRecorder("readiness", &got))++ for _, target := range []string{+ "/api//v1/items",+ "/sbx/8080/proxy/http://example.com/x",+ "/sbx/8080/a/../b",+ "/sbx/8080/./c",+ } {+ got = ""+ rec := httptest.NewRecorder()+ handler.ServeHTTP(rec, httptest.NewRequest(http.MethodPost, target, nil))+ if rec.Code != http.StatusTeapot || got != "proxy POST "+target {+ t.Errorf("POST %s: status %d, handled as %q; want it proxied unchanged", target, rec.Code, got)+ }+ }+}++func TestIngressHandlerServesStatusPaths(t *testing.T) {+ var got string+ handler := newIngressHandler(pathRecorder("proxy", &got), pathRecorder("readiness", &got))++ rec := httptest.NewRecorder()+ handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/status.ok/network-readiness", nil))+ if got != "readiness GET /status.ok/network-readiness" {+ t.Errorf("network readiness handled as %q", got)+ }++ got = ""+ rec = httptest.NewRecorder()+ handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/status.ok", nil))+ if rec.Code != http.StatusOK || got != "" {+ t.Errorf("/status.ok: status %d, handled as %q; want the health check", rec.Code, got)+ }++ rec = httptest.NewRecorder()+ handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/status.ok/other", nil))+ if got != "proxy GET /status.ok/other" {+ t.Errorf("/status.ok/other handled as %q; want it proxied", got)+ }+}