High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: behind the server front door a proxied body keeps its length, and body-reading handlers read a body of unknown length (PIN-367)
Luigi Agosti1db33d48f2eb
diff --git a/internal/activity/context.go b/internal/activity/context.goindex 9f6128e0..82e05156 100644--- a/internal/activity/context.go+++ b/internal/activity/context.go@@ -1,7 +1,6 @@package activityimport (- "bytes""context""encoding/json""io"@@ -272,15 +271,6 @@ func initialAction(r *http.Request) string {return ""}-// replayedBody hands the downstream handler the bytes already read followed by the-// rest of the stream, and closes the original. It is a ReadCloser rather than a-// NopCloser over a buffer because the request body is not fully in memory: only the-// peek is.-type replayedBody struct {- io.Reader- io.Closer-}-func initialURL(r *http.Request) string {if u := strings.TrimSpace(r.URL.Query().Get("url")); u != "" {return sanitizeActivityURL(u)@@ -314,10 +304,7 @@ func EnrichRouteActivity(r *http.Request) {original := r.Bodypeeked, err := io.ReadAll(io.LimitReader(original, activityPeekBytes))- r.Body = replayedBody{- Reader: io.MultiReader(bytes.NewReader(peeked), original),- Closer: original,- }+ r.Body = httpx.ReplayBody(peeked, original)if err != nil || len(peeked) == 0 {return}diff --git a/internal/handlers/network_route.go b/internal/handlers/network_route.goindex 32a66caa..39920195 100644--- a/internal/handlers/network_route.go+++ b/internal/handlers/network_route.go@@ -3,7 +3,9 @@ package handlersimport ("context""encoding/json"+ "errors""fmt"+ "io""net/http""github.com/pinchtab/pinchtab/internal/bridge"@@ -188,11 +190,11 @@ func (h *Handlers) handleNetworkUnrouteFor(w http.ResponseWriter, r *http.Reques}pattern := r.URL.Query().Get("pattern")- if pattern == "" && r.ContentLength > 0 {+ if pattern == "" && httpx.MayHaveBody(r) {var body struct {Pattern string `json:"pattern"`}- if err := json.NewDecoder(r.Body).Decode(&body); err != nil {+ if err := json.NewDecoder(r.Body).Decode(&body); err != nil && !errors.Is(err, io.EOF) {httpx.Error(w, 400, fmt.Errorf("decode body: %w", err))return}diff --git a/internal/handlers/pdf.go b/internal/handlers/pdf.goindex 9ba03761..ce28e9db 100644--- a/internal/handlers/pdf.go+++ b/internal/handlers/pdf.go@@ -4,7 +4,9 @@ import ("context""encoding/base64""encoding/json"+ "errors""fmt"+ "io""net/http""os""path/filepath"@@ -241,8 +243,8 @@ func (h *Handlers) HandleTabPDF(w http.ResponseWriter, r *http.Request) {provided := ""if r.Method == http.MethodPost {var body map[string]any- if r.ContentLength > 0 {- if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxBodySize)).Decode(&body); err != nil {+ if httpx.MayHaveBody(r) {+ if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxBodySize)).Decode(&body); err != nil && !errors.Is(err, io.EOF) {httpx.Error(w, 400, fmt.Errorf("decode: %w", err))return}diff --git a/internal/handlers/tab_lifecycle.go b/internal/handlers/tab_lifecycle.goindex d351d34f..b169b2ca 100644--- a/internal/handlers/tab_lifecycle.go+++ b/internal/handlers/tab_lifecycle.go@@ -131,7 +131,7 @@ func (h *Handlers) HandleTabClose(w http.ResponseWriter, r *http.Request) {var req struct {TabID string `json:"tabId"`}- if r.Body != nil && r.Body != http.NoBody && r.ContentLength != 0 {+ if httpx.MayHaveBody(r) {if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxBodySize)).Decode(&req); err != nil && !errors.Is(err, io.EOF) {httpx.Error(w, 400, fmt.Errorf("decode: %w", err))returndiff --git a/internal/handlers/unknown_length_body_test.go b/internal/handlers/unknown_length_body_test.gonew file mode 100644index 00000000..8a985b11--- /dev/null+++ b/internal/handlers/unknown_length_body_test.go@@ -0,0 +1,73 @@+package handlers++import (+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "strings"+ "testing"++ "github.com/pinchtab/pinchtab/internal/bridge"+ "github.com/pinchtab/pinchtab/internal/config"+)++func unknownLengthRequest(method, path, body string) *http.Request {+ req := httptest.NewRequest(method, path, strings.NewReader(body))+ req.ContentLength = -1+ req.SetPathValue("id", "tab1")+ return req+}++func TestAnUnknownLengthUnrouteBodyRemovesOnlyItsPattern(t *testing.T) {+ b := newRouteMockBridge()+ h := newRouteHandler(b)+ for _, pattern := range []string{"api", "img"} {+ if err := b.AddRouteRule("tab1", bridge.RouteRule{Pattern: pattern, Action: "abort"}); err != nil {+ t.Fatal(err)+ }+ }+ w := httptest.NewRecorder()+ h.HandleTabNetworkUnroute(w, unknownLengthRequest(http.MethodDelete, "/tabs/tab1/network/route", `{"pattern":"api"}`))+ var resp struct {+ Removed int `json:"removed"`+ }+ if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil || w.Code != http.StatusOK || resp.Removed != 1 {+ t.Fatalf("%d %s, want one rule removed", w.Code, w.Body.String())+ }+ if rules := b.rules["tab1"]; len(rules) != 1 || rules[0].Pattern != "img" {+ t.Fatalf("remaining rules = %+v, want only img", rules)+ }+}++func TestAnEmptyUnknownLengthUnrouteBodyIsAbsent(t *testing.T) {+ b := newRouteMockBridge()+ h := newRouteHandler(b)+ if err := b.AddRouteRule("tab1", bridge.RouteRule{Pattern: "api", Action: "abort"}); err != nil {+ t.Fatal(err)+ }+ w := httptest.NewRecorder()+ h.HandleTabNetworkUnroute(w, unknownLengthRequest(http.MethodDelete, "/tabs/tab1/network/route", ""))+ requireClearedRoutes(t, w, 1)+}++func TestAnUnknownLengthBodyTabIDIsCheckedAgainstThePath(t *testing.T) {+ for name, serve := range map[string]func(*Handlers, http.ResponseWriter, *http.Request){+ "pdf": (*Handlers).HandleTabPDF,+ "close": (*Handlers).HandleTabClose,+ } {+ t.Run(name, func(t *testing.T) {+ h := New(&mockBridge{failTab: true}, &config.RuntimeConfig{}, nil, nil, nil)+ w := httptest.NewRecorder()+ serve(h, w, unknownLengthRequest(http.MethodPost, "/tabs/tab1/"+name, `{"tabId":"OTHER"}`))+ if w.Code != http.StatusBadRequest || !strings.Contains(w.Body.String(), "does not match") {+ t.Fatalf("%d %s, want 400 for a body tabId that does not match the path", w.Code, w.Body.String())+ }++ w = httptest.NewRecorder()+ serve(h, w, unknownLengthRequest(http.MethodPost, "/tabs/tab1/"+name, ""))+ if w.Code == http.StatusBadRequest {+ t.Fatalf("an empty unknown-length body answered 400: %s", w.Body.String())+ }+ })+ }+}diff --git a/internal/httpx/body.go b/internal/httpx/body.gonew file mode 100644index 00000000..0abc6598--- /dev/null+++ b/internal/httpx/body.go@@ -0,0 +1,20 @@+package httpx++import (+ "bytes"+ "io"+ "net/http"+)++func MayHaveBody(r *http.Request) bool {+ return r.Body != nil && r.Body != http.NoBody && r.ContentLength != 0+}++type replayedBody struct {+ io.Reader+ io.Closer+}++func ReplayBody(peeked []byte, original io.ReadCloser) io.ReadCloser {+ return replayedBody{Reader: io.MultiReader(bytes.NewReader(peeked), original), Closer: original}+}diff --git a/internal/orchestrator/handlers_profiles.go b/internal/orchestrator/handlers_profiles.goindex 88d1fb20..f8f2f2b7 100644--- a/internal/orchestrator/handlers_profiles.go+++ b/internal/orchestrator/handlers_profiles.go@@ -1,7 +1,9 @@package orchestratorimport (+ "errors""fmt"+ "io""net/http""github.com/pinchtab/pinchtab/internal/authn"@@ -37,8 +39,8 @@ func (o *Orchestrator) handleStartByID(w http.ResponseWriter, r *http.Request) {Browser string `json:"browser,omitempty"`FallbackTargets []string `json:"fallbackTargets,omitempty"`}- if r.ContentLength > 0 {- if err := httpx.DecodeJSONBody(w, r, 0, &req); err != nil {+ if httpx.MayHaveBody(r) {+ if err := httpx.DecodeJSONBody(w, r, 0, &req); err != nil && !errors.Is(err, io.EOF) {httpx.Error(w, httpx.StatusForJSONDecodeError(err), err)return}diff --git a/internal/orchestrator/handlers_tabs.go b/internal/orchestrator/handlers_tabs.goindex 8633a0e0..5550cc74 100644--- a/internal/orchestrator/handlers_tabs.go+++ b/internal/orchestrator/handlers_tabs.go@@ -4,6 +4,7 @@ import ("bytes""context""encoding/json"+ "errors""fmt""io""net/http"@@ -33,8 +34,8 @@ func (o *Orchestrator) handleInstanceTabOpen(w http.ResponseWriter, r *http.Requvar req struct {URL string `json:"url,omitempty"`}- if r.ContentLength > 0 {- if err := httpx.DecodeJSONBody(w, r, 0, &req); err != nil {+ if httpx.MayHaveBody(r) {+ if err := httpx.DecodeJSONBody(w, r, 0, &req); err != nil && !errors.Is(err, io.EOF) {httpx.Error(w, httpx.StatusForJSONDecodeError(err), err)return}diff --git a/internal/orchestrator/tab_extract.go b/internal/orchestrator/tab_extract.goindex 134e4d4e..5abb5253 100644--- a/internal/orchestrator/tab_extract.go+++ b/internal/orchestrator/tab_extract.go@@ -1,11 +1,12 @@package orchestratorimport (- "bytes""encoding/json""io""net/http""strings"++ "github.com/pinchtab/pinchtab/internal/httpx")// maxBodyPeek caps how much of an inbound JSON request body the orchestrator@@ -62,10 +63,7 @@ func peekBodyTabID(r *http.Request) string {}func peekBodyStringField(r *http.Request, field string) string {- if r == nil || r.Body == nil || r.Body == http.NoBody {- return ""- }- if r.ContentLength <= 0 || r.ContentLength > maxBodyPeek {+ if r == nil || !httpx.MayHaveBody(r) || r.ContentLength > maxBodyPeek {return ""}ct := r.Header.Get("Content-Type")@@ -79,14 +77,10 @@ func peekBodyStringField(r *http.Request, field string) string {return ""}- buf, err := io.ReadAll(io.LimitReader(r.Body, maxBodyPeek+1))- if err != nil {- // Body may already be partially consumed; do not attempt to repair.- return ""- }- // Always restore the body so downstream handlers see the full payload.- r.Body = io.NopCloser(bytes.NewReader(buf))- if len(buf) > maxBodyPeek {+ original := r.Body+ buf, err := io.ReadAll(io.LimitReader(original, maxBodyPeek+1))+ r.Body = httpx.ReplayBody(buf, original)+ if err != nil || len(buf) > maxBodyPeek {return ""}diff --git a/internal/orchestrator/tab_extract_test.go b/internal/orchestrator/tab_extract_test.goindex 816e44c5..65e3b6d0 100644--- a/internal/orchestrator/tab_extract_test.go+++ b/internal/orchestrator/tab_extract_test.go@@ -81,14 +81,14 @@ func TestExtractExplicitTabID_BodySkippedWhenOversized(t *testing.T) {}}-func TestExtractExplicitTabID_BodySkippedWhenUnknownLength(t *testing.T) {+func TestExtractExplicitTabID_BodyReadWhenUnknownLength(t *testing.T) {r := httptest.NewRequest("POST", "/x", bytes.NewReader([]byte(`{"tabId":"streaming"}`)))r.Header.Set("Content-Type", "application/json")r.ContentLength = -1- got, _ := ExtractExplicitTabID(r)- if got != "" {- t.Fatalf("unknown-length body should be skipped, got %q", got)+ got, src := ExtractExplicitTabID(r)+ if got != "streaming" || src != TabIDSourceBody {+ t.Fatalf("an unknown-length body is a chunked client body and must route by its tabId; got %q/%q", got, src)}}diff --git a/internal/orchestrator/unknown_length_body_test.go b/internal/orchestrator/unknown_length_body_test.gonew file mode 100644index 00000000..b78b7896--- /dev/null+++ b/internal/orchestrator/unknown_length_body_test.go@@ -0,0 +1,100 @@+package orchestrator++import (+ "bytes"+ "encoding/json"+ "io"+ "net/http"+ "net/http/httptest"+ "strings"+ "testing"++ "github.com/pinchtab/pinchtab/internal/config"+ "github.com/pinchtab/pinchtab/internal/profiles"+)++func unknownLength(req *http.Request) *http.Request {+ req.ContentLength = -1+ return req+}++func TestAnEmptyUnknownLengthStartBodyIsAbsent(t *testing.T) {+ old := processAliveFunc+ processAliveFunc = func(pid int) bool { return pid > 0 }+ t.Cleanup(func() { processAliveFunc = old })+ stubPortAvailability(t, func(int) bool { return true })+ baseDir := t.TempDir()+ o := NewOrchestratorWithRunner(baseDir, &mockRunner{portAvail: true})+ pm := profiles.NewProfileManager(baseDir)+ if err := pm.CreateWithMeta("work", profiles.ProfileMeta{}); err != nil {+ t.Fatal(err)+ }+ o.profiles = pm++ req := unknownLength(httptest.NewRequest(http.MethodPost, "/profiles/work/start", strings.NewReader("")))+ req.SetPathValue("id", "work")+ w := httptest.NewRecorder()+ o.handleStartByID(w, req)+ if w.Code != http.StatusCreated {+ t.Fatalf("status = %d, want 201 for an empty body of unknown length: %s", w.Code, w.Body.String())+ }+}++func TestAnUnknownLengthStartBodyIsDecoded(t *testing.T) {+ baseDir := t.TempDir()+ o := NewOrchestratorWithRunner(baseDir, &mockRunner{portAvail: true})+ o.ApplyRuntimeConfig(&config.RuntimeConfig{+ DefaultTarget: "cloak-1",+ Targets: config.BrowserTargetsConfig{"cloak-1": {Provider: config.BrowserCloak}},+ })+ pm := profiles.NewProfileManager(baseDir)+ if err := pm.CreateWithMeta("work", profiles.ProfileMeta{}); err != nil {+ t.Fatal(err)+ }+ o.profiles = pm++ req := unknownLength(httptest.NewRequest(http.MethodPost, "/profiles/work/start", strings.NewReader(`{"browser":"ghost"}`)))+ req.SetPathValue("id", "work")+ w := httptest.NewRecorder()+ o.handleStartByID(w, req)+ if w.Code != http.StatusBadRequest {+ t.Fatalf("status = %d, want the 400 a decoded browser:ghost earns: %s", w.Code, w.Body.String())+ }+}++func TestAnUnknownLengthTabOpenBodyCarriesItsURL(t *testing.T) {+ var got map[string]any+ o, _ := orchestratorOverStubChild(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ _ = json.NewDecoder(r.Body).Decode(&got)+ _, _ = w.Write([]byte(`{"tabId":"t1"}`))+ }))+ for _, tc := range []struct{ body, wantURL string }{+ {`{"url":"https://example.com"}`, "https://example.com"},+ {"", ""},+ } {+ got = nil+ req := unknownLength(httptest.NewRequest(http.MethodPost, "/instances/"+stubInstanceID+"/tabs/open", strings.NewReader(tc.body)))+ req.SetPathValue("id", stubInstanceID)+ w := httptest.NewRecorder()+ o.handleInstanceTabOpen(w, req)+ if w.Code != http.StatusOK || got["url"] != tc.wantURL {+ t.Fatalf("body %q: %d %s, instance saw %v, want url %q", tc.body, w.Code, w.Body.String(), got, tc.wantURL)+ }+ }+}++func TestTheTabIDPeekReadsAnUnknownLengthBodyAndReplaysItWhole(t *testing.T) {+ small := `{"tabId":"TAB_A","kind":"click"}`+ large := `{"tabId":"TAB_B","pad":"` + strings.Repeat("x", maxBodyPeek) + `"}`+ for _, tc := range []struct{ body, wantTabID string }{{small, "TAB_A"}, {large, ""}} {+ req := unknownLength(httptest.NewRequest(http.MethodPost, "/action", strings.NewReader(tc.body)))+ req.Header.Set("Content-Type", "application/json")+ if got := peekBodyTabID(req); got != tc.wantTabID {+ t.Fatalf("peek of a %d-byte body = %q, want %q", len(tc.body), got, tc.wantTabID)+ }+ rest, err := io.ReadAll(req.Body)+ if err != nil || !bytes.Equal(rest, []byte(tc.body)) {+ t.Fatalf("downstream read %d bytes of %d after the peek (err %v)", len(rest), len(tc.body), err)+ }+ }+}diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.goindex 413c9e9e..2cc0a593 100644--- a/internal/proxy/proxy.go+++ b/internal/proxy/proxy.go@@ -114,6 +114,10 @@ func Forward(w http.ResponseWriter, r *http.Request, targetURL *url.URL, opts Op// Only a rewrite propagates a Host. Left alone, the transport derives the// Host header from the URL as before, which spells a default port the way// the wire expects rather than the way targetURL.Host holds it.+ outReq.ContentLength = proxyReq.ContentLength+ if proxyReq.ContentLength == 0 {+ outReq.Body = http.NoBody+ }if proxyReq.Host != hostBeforeRewrite {outReq.Host = proxyReq.Host}diff --git a/internal/proxy/proxy_test.go b/internal/proxy/proxy_test.goindex b66a7d31..0f9330dc 100644--- a/internal/proxy/proxy_test.go+++ b/internal/proxy/proxy_test.go@@ -2,8 +2,10 @@ package proxyimport ("encoding/json"+ "io""net/http""net/http/httptest"+ "strings""testing""github.com/pinchtab/pinchtab/internal/httpx"@@ -208,3 +210,44 @@ func TestHTTP_DoesNotDoubleTheOuterChainsResponseHeaders(t *testing.T) {}}}++type receivedBody struct {+ contentLength int64+ transferEncoding []string+ body string+}++func bodyEchoBridge(t *testing.T, got *receivedBody) *httptest.Server {+ t.Helper()+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ raw, _ := io.ReadAll(r.Body)+ *got = receivedBody{contentLength: r.ContentLength, transferEncoding: r.TransferEncoding, body: string(raw)}+ }))+ t.Cleanup(srv.Close)+ return srv+}++func TestHTTP_ForwardsABodyWithItsLength(t *testing.T) {+ var got receivedBody+ srv := bodyEchoBridge(t, &got)+ const body = `{"pattern":"api"}`++ req := httptest.NewRequest(http.MethodDelete, "/network/route", strings.NewReader(body))+ HTTP(httptest.NewRecorder(), req, srv.URL+"/network/route")++ if got.contentLength != int64(len(body)) || got.body != body || len(got.transferEncoding) != 0 {+ t.Fatalf("instance received length %d, encoding %v, body %q; want length %d, no chunking, %q", got.contentLength, got.transferEncoding, got.body, len(body), body)+ }+}++func TestHTTP_ForwardsAnEmptyBodyUnchunked(t *testing.T) {+ var got receivedBody+ srv := bodyEchoBridge(t, &got)++ req := httptest.NewRequest(http.MethodPost, "/close", strings.NewReader(""))+ HTTP(httptest.NewRecorder(), req, srv.URL+"/close")++ if got.contentLength != 0 || len(got.transferEncoding) != 0 || got.body != "" {+ t.Fatalf("instance received length %d, encoding %v, body %q; want an empty unchunked request", got.contentLength, got.transferEncoding, got.body)+ }+}diff --git a/tests/e2e/scenarios/api/files-basic.sh b/tests/e2e/scenarios/api/files-basic.shindex 9b4efe1a..ed628588 100644--- a/tests/e2e/scenarios/api/files-basic.sh+++ b/tests/e2e/scenarios/api/files-basic.sh@@ -57,6 +57,19 @@ assert_ok "tab pdf with options"end_test+# ─────────────────────────────────────────────────────────────────+start_test "pinchtab pdf --tab <id> refuses a body tabId naming another tab"++pt_post /navigate -d "{\"url\":\"${FIXTURES_URL}/index.html\"}"+assert_ok "navigate"+TAB_ID=$(echo "$RESULT" | jq -r '.tabId')++pt_post "/tabs/${TAB_ID}/pdf" -d '{"tabId":"OTHER"}'+assert_http_status 400 "body tabId that does not match the path"+assert_json_contains "$RESULT" '.error' 'does not match' "the refusal names the mismatch"++end_test+# ─────────────────────────────────────────────────────────────────start_test "screenshot: quality parameter"diff --git a/tests/e2e/scenarios/api/network-route-extended.sh b/tests/e2e/scenarios/api/network-route-extended.shindex 8588727e..14d549a5 100755--- a/tests/e2e/scenarios/api/network-route-extended.sh+++ b/tests/e2e/scenarios/api/network-route-extended.sh@@ -162,6 +162,24 @@ netroute_full_server_tests() {end_test+ # ─────────────────────────────────────────────────────────────────+ start_test "DELETE /tabs/{id}/network/route with a body pattern removes only that rule"++ pt_post "/tabs/${TAB_ID}/network/route" -d '{"pattern":"body-a.invalid","action":"abort"}'+ assert_ok "install first body-pattern rule"+ pt_post "/tabs/${TAB_ID}/network/route" -d '{"pattern":"body-b.invalid","action":"abort"}'+ assert_ok "install second body-pattern rule"++ pinchtab DELETE "/tabs/${TAB_ID}/network/route" -d '{"pattern":"body-a.invalid"}'+ assert_ok "remove by body pattern"+ assert_json_eq "$RESULT" '.removed' '1' "the body pattern removed exactly one rule"++ pt_get "/tabs/${TAB_ID}/network/route"+ assert_json_eq "$RESULT" '[.rules[].pattern | select(. == "body-a.invalid")] | length' '0' "the named rule is gone"+ assert_json_eq "$RESULT" '[.rules[].pattern | select(. == "body-b.invalid")] | length' '1' "the other rule survives"++ end_test+# ─────────────────────────────────────────────────────────────────start_test "DELETE /tabs/{id}/network/route (no pattern) clears remaining rules"