Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
fix(fleet): stream exposed service bodies (#7129)
r33drichards86650c3d6462
diff --git a/libs/fleet/backend/handlers/svc.go b/libs/fleet/backend/handlers/svc.goindex 8878a2f7b5..ab7ea6a0b8 100644--- a/libs/fleet/backend/handlers/svc.go+++ b/libs/fleet/backend/handlers/svc.go@@ -46,6 +46,26 @@ func rewriteLocation(loc, basePath string) string {return basePath + "/" + loc}+func newSvcReverseProxy(target *url.URL, upstreamPath, basePath string) *httputil.ReverseProxy {+ rp := httputil.NewSingleHostReverseProxy(target)+ origDirector := rp.Director+ rp.Director = func(req *http.Request) {+ origDirector(req)+ req.URL.Path = upstreamPath+ req.URL.RawPath = ""+ req.Header.Del("Authorization")+ req.Host = target.Host+ }+ rp.ModifyResponse = func(resp *http.Response) error {+ loc := resp.Header.Get("Location")+ if rewritten := rewriteLocation(loc, basePath); rewritten != loc {+ resp.Header.Set("Location", rewritten)+ }+ return nil+ }+ return rp+}+// Svc godoc//// @Summary Authenticated reverse proxy to a K8s Service in a namespace the caller owns@@ -106,23 +126,11 @@ func (h Handlers) Svc(w http.ResponseWriter, r *http.Request) {// prepend this prefix so the browser stays within /api/svc.basePath := fmt.Sprintf("/api/svc/%s/%s", ns, service)- rp := httputil.NewSingleHostReverseProxy(target)+ rp := newSvcReverseProxy(target, upstreamPath, basePath)origDirector := rp.Directorrp.Director = func(req *http.Request) {origDirector(req)- req.URL.Path = upstreamPath- req.URL.RawPath = ""req.URL.RawQuery = r.URL.RawQuery- req.Header.Del("Authorization")- req.Host = target.Host- }- // Rewrite upstream Location headers to include the base path.- rp.ModifyResponse = func(resp *http.Response) error {- loc := resp.Header.Get("Location")- if rewritten := rewriteLocation(loc, basePath); rewritten != loc {- resp.Header.Set("Location", rewritten)- }- return nil}rw := &statusCapture{ResponseWriter: w}diff --git a/libs/fleet/backend/handlers/svc_test.go b/libs/fleet/backend/handlers/svc_test.goindex 8fb704c7fa..12accdfc59 100644--- a/libs/fleet/backend/handlers/svc_test.go+++ b/libs/fleet/backend/handlers/svc_test.go@@ -1,9 +1,11 @@package handlersimport (+ "bytes""context""errors""fmt"+ "io""net""net/http""net/http/httptest"@@ -65,6 +67,154 @@ func TestSvc_Unauthenticated(t *testing.T) {}}+func TestSvcReverseProxyPreservesLargeBodies(t *testing.T) {+ payload := bytes.Repeat([]byte{0, 1, 2, 255}, 300*1024)+ upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ body, err := io.ReadAll(r.Body)+ if err != nil {+ t.Errorf("read request: %v", err)+ return+ }+ if !bytes.Equal(body, payload) {+ t.Errorf("request changed: got %d want %d", len(body), len(payload))+ }+ _, _ = w.Write(body)+ }))+ defer upstream.Close()+ target, err := url.Parse(upstream.URL)+ if err != nil {+ t.Fatalf("parse upstream URL: %v", err)+ }+ req := httptest.NewRequest(http.MethodPost, "/upload", bytes.NewReader(payload))+ res := httptest.NewRecorder()+ newSvcReverseProxy(target, "/upload", "/api/svc/ns/svc").ServeHTTP(res, req)+ if res.Code != http.StatusOK {+ t.Fatalf("status=%d body=%s", res.Code, res.Body.String())+ }+ if !bytes.Equal(res.Body.Bytes(), payload) {+ t.Fatalf("response changed: got %d want %d", res.Body.Len(), len(payload))+ }+}++func TestSvcReverseProxyStreamsRequestWithBackpressure(t *testing.T) {+ prefixRead := make(chan struct{})+ release := make(chan struct{})+ upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ prefix := make([]byte, 5)+ if _, err := io.ReadFull(r.Body, prefix); err != nil {+ t.Errorf("read prefix: %v", err)+ return+ }+ if string(prefix) != "first" {+ t.Errorf("prefix=%q", prefix)+ }+ close(prefixRead)+ <-release+ rest, err := io.ReadAll(r.Body)+ if err != nil {+ t.Errorf("read rest: %v", err)+ return+ }+ _, _ = w.Write(rest)+ }))+ defer upstream.Close()+ target, err := url.Parse(upstream.URL)+ if err != nil {+ t.Fatalf("parse upstream URL: %v", err)+ }+ proxy := httptest.NewServer(newSvcReverseProxy(target, "/upload", "/api/svc/ns/svc"))+ defer proxy.Close()+ reader, writer := io.Pipe()+ req, err := http.NewRequest(http.MethodPost, proxy.URL, reader)+ if err != nil {+ t.Fatalf("build request: %v", err)+ }+ result := make(chan struct {+ resp *http.Response+ err error+ }, 1)+ go func() {+ resp, err := http.DefaultClient.Do(req)+ result <- struct {+ resp *http.Response+ err error+ }{resp, err}+ }()+ if _, err := writer.Write([]byte("first")); err != nil {+ t.Fatal(err)+ }+ select {+ case <-prefixRead:+ case <-time.After(time.Second):+ t.Fatal("request prefix was buffered")+ }+ close(release)+ if _, err := writer.Write([]byte("second")); err != nil {+ t.Fatal(err)+ }+ _ = writer.Close()+ select {+ case got := <-result:+ if got.err != nil {+ t.Fatal(got.err)+ }+ defer got.resp.Body.Close()+ body, err := io.ReadAll(got.resp.Body)+ if err != nil {+ t.Fatalf("read response: %v", err)+ }+ if string(body) != "second" {+ t.Fatalf("body=%q", body)+ }+ case <-time.After(time.Second):+ t.Fatal("proxy response timed out")+ }+}++func TestSvcReverseProxyFlushesStreamingResponse(t *testing.T) {+ release := make(chan struct{})+ upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ _, _ = w.Write([]byte("first\n"))+ w.(http.Flusher).Flush()+ <-release+ _, _ = w.Write([]byte("second\n"))+ }))+ defer upstream.Close()+ target, err := url.Parse(upstream.URL)+ if err != nil {+ t.Fatalf("parse upstream URL: %v", err)+ }+ proxy := httptest.NewServer(newSvcReverseProxy(target, "/stream", "/api/svc/ns/svc"))+ defer proxy.Close()+ resp, err := http.Get(proxy.URL)+ if err != nil {+ t.Fatal(err)+ }+ defer resp.Body.Close()+ first := make([]byte, 6)+ read := make(chan error, 1)+ go func() { _, err := io.ReadFull(resp.Body, first); read <- err }()+ select {+ case err := <-read:+ if err != nil {+ t.Fatal(err)+ }+ if string(first) != "first\n" {+ t.Fatalf("first=%q", first)+ }+ case <-time.After(time.Second):+ t.Fatal("first chunk not flushed")+ }+ close(release)+ rest, err := io.ReadAll(resp.Body)+ if err != nil {+ t.Fatalf("read response remainder: %v", err)+ }+ if string(rest) != "second\n" {+ t.Fatalf("rest=%q", rest)+ }+}+func TestSvcProxyErrorStatus(t *testing.T) {tests := []struct {name stringdiff --git a/libs/fleet/nginx.conf b/libs/fleet/nginx.confindex e792b178ef..f43d4ea88d 100644--- a/libs/fleet/nginx.conf+++ b/libs/fleet/nginx.conf@@ -103,7 +103,11 @@ server {proxy_set_header X-Forwarded-Proto $scheme;proxy_set_header X-Auth-Request-User $auth_user;proxy_set_header X-Auth-Request-Email $auth_email;- proxy_buffering off;+ # Arbitrary exposed-service bodies stream through without nginx's+ # default 1 MiB request limit or request-body disk buffering.+ client_max_body_size 0;+ proxy_request_buffering off;+ proxy_buffering off;proxy_read_timeout 1h;proxy_send_timeout 1h;}diff --git a/libs/fleet/tests/nginx-config.test.mjs b/libs/fleet/tests/nginx-config.test.mjsindex 0223aaeb9d..90f3735455 100644--- a/libs/fleet/tests/nginx-config.test.mjs+++ b/libs/fleet/tests/nginx-config.test.mjs@@ -50,3 +50,14 @@ test("oauth2-proxy accepts Fleets GitHub WIF bearer tokens", async () => {"expected oauth2-proxy to verify GitHub OIDC tokens with the Fleets audience",)})++test("sandbox service proxy streams bodies without a size cap", async () => {+ const conf = await readFile(nginxConf, "utf8")+ const route = conf.match(/location ~ \^\/api\/svc\(\/\|\$\) \{([\s\S]*?)\n \}/)+ assert.ok(route)+ assert.match(route[1], /client_max_body_size\s+0\s*;/)+ assert.match(route[1], /proxy_request_buffering\s+off\s*;/)+ assert.match(route[1], /proxy_buffering\s+off\s*;/)+ assert.match(route[1], /proxy_read_timeout\s+1h\s*;/)+ assert.match(route[1], /proxy_send_timeout\s+1h\s*;/)+})