Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix: carry the vocab tab tag in the request body (vocabTab), not a stripped X-PinchTab-* header, so the server can ignore a token from a moved-off tab (PIN-376)

Luigi Agostid805578e9e31
diff --git a/internal/bridge/action_request.go b/internal/bridge/action_request.go
index 3a5f7d69..a640d2dd 100644
--- a/internal/bridge/action_request.go
+++ b/internal/bridge/action_request.go
@@ -153,6 +153,13 @@ type ActionRequest struct {
Browser string `json:"browser,omitempty"`
Vocab string `json:"vocab,omitempty"`
+
+ // VocabTab names the tab the echoed Vocab token belongs to. The server enforces
+ // the epoch check only when it is empty (legacy clients) or equals the resolved
+ // tab, so a token left over from a tab the caller has since moved off is ignored
+ // rather than refused. It rides in the body (and the GET query) because the
+ // X-PinchTab-* request headers are stripped from public clients.
+ VocabTab string `json:"vocabTab,omitempty"`
}
type actionRequestAlias ActionRequest
diff --git a/internal/cli/actions/actions_element.go b/internal/cli/actions/actions_element.go
index 5a9c9378..3ce97f09 100644
--- a/internal/cli/actions/actions_element.go
+++ b/internal/cli/actions/actions_element.go
@@ -124,10 +124,7 @@ func postActionWithHeaders(client *http.Client, base, token string, cmd *cobra.C
if _, ok := body["vocab"]; !ok {
if vocabTab, tok := apiclient.VocabForAction(base, tabID); tok != "" {
body["vocab"] = tok
- if headers == nil {
- headers = map[string]string{}
- }
- headers[apiclient.VocabTabHeader] = vocabTab
+ body["vocabTab"] = vocabTab
}
}
diff --git a/internal/cli/apiclient/client.go b/internal/cli/apiclient/client.go
index d5ae2c2f..7a64d1db 100644
--- a/internal/cli/apiclient/client.go
+++ b/internal/cli/apiclient/client.go
@@ -14,11 +14,7 @@ import (
const (
vocabHeader = "X-PinchTab-Vocab"
vocabTabIDHeader = "X-PinchTab-Tab-Id"
- // VocabTabHeader tells the server which tab the echoed token belongs to, so it
- // enforces the epoch check only when the action resolves that same tab and
- // ignores a token left over from a tab the current pointer has since moved off.
- VocabTabHeader = "X-PinchTab-Vocab-Tab"
- vocabStoreLimit = 16
+ vocabStoreLimit = 16
)
// vocabEntry pairs a snapshot's vocabulary token with the tab the server
diff --git a/internal/cli/apiclient/vocab_test.go b/internal/cli/apiclient/vocab_test.go
index a5961c19..7c0769e7 100644
--- a/internal/cli/apiclient/vocab_test.go
+++ b/internal/cli/apiclient/vocab_test.go
@@ -74,7 +74,7 @@ func (s *vocabServer) handle(w http.ResponseWriter, r *http.Request) {
if v, ok := decoded["vocab"].(string); ok {
s.lastVocab, s.hadVocab = v, true
}
- s.lastVocabTab = r.Header.Get("X-PinchTab-Vocab-Tab")
+ s.lastVocabTab, _ = decoded["vocabTab"].(string)
_, _ = w.Write([]byte(`{"success":true}`))
}
diff --git a/internal/handlers/action_vocab_test.go b/internal/handlers/action_vocab_test.go
index 523ef1f9..d1855f2e 100644
--- a/internal/handlers/action_vocab_test.go
+++ b/internal/handlers/action_vocab_test.go
@@ -6,6 +6,7 @@ import (
"go/ast"
"go/parser"
"go/token"
+ "net/http"
"net/http/httptest"
"strings"
"testing"
@@ -124,39 +125,36 @@ func TestAStaleTokenDoesNotBlockANonRefAction(t *testing.T) {
}
}
-// A token tagged (X-PinchTab-Vocab-Tab) for the tab the action resolves is enforced: a
-// superseded token on the resolved tab is still refused, so the implicit snap-then-click
-// flow keeps its supersession protection.
-func TestATokenTaggedForTheResolvedTabIsEnforced(t *testing.T) {
- h, mb := newVocabHandlers("current")
- w := httptest.NewRecorder()
- req := httptest.NewRequest("POST", "/action", strings.NewReader(`{"kind":"click","ref":"e1","tabId":"tab1","vocab":"stale"}`))
- req.Header.Set(vocabTabHeader, "tab1")
- h.HandleAction(w, req)
- var body map[string]any
- _ = json.Unmarshal(w.Body.Bytes(), &body)
- if w.Code != 409 || body["code"] != vocabSupersededCode {
- t.Fatalf("a superseded token tagged for the resolved tab was not refused: status %d body %s", w.Code, w.Body.String())
+// The tag travels in the body, not a header: the front door strips every inbound
+// X-PinchTab-* header from public clients, so a header tag would never reach the guard.
+// Driven through TrustedInternalProxyStripMiddleware (the middleware that strips) to prove
+// the body field survives where a header would not.
+//
+// Row A — the tag equals the resolved tab: a superseded token is refused, so the implicit
+// snap-then-click flow keeps its supersession protection.
+// Row B — the tag names a different tab than the one resolved (the current pointer moved
+// off the snapshotted tab): the token is ignored, not refused, killing the mirror-case
+// false 409. Pre-fix (header tag, or no tag) this answered 409.
+func TestVocabTabTravelsInTheBodyThroughTheStripMiddleware(t *testing.T) {
+ serve := func(h *Handlers, body string) (int, map[string]any) {
+ w := httptest.NewRecorder()
+ req := httptest.NewRequest("POST", "/action", strings.NewReader(body))
+ TrustedInternalProxyStripMiddleware("")(http.HandlerFunc(h.HandleAction)).ServeHTTP(w, req)
+ var out map[string]any
+ _ = json.Unmarshal(w.Body.Bytes(), &out)
+ return w.Code, out
}
- if mb.executeCalled {
- t.Error("the refusal must land before ExecuteAction")
+
+ hA, _ := newVocabHandlers("current")
+ code, body := serve(hA, `{"kind":"click","ref":"e1","tabId":"tab1","vocab":"stale","vocabTab":"tab1"}`)
+ if code != 409 || body["code"] != vocabSupersededCode {
+ t.Fatalf("row A: a superseded token tagged for the resolved tab was not refused through the strip chain: status %d body %v", code, body)
}
-}
-// A token tagged for a DIFFERENT tab than the action resolves is ignored, not refused: the
-// current pointer moved off the snapshotted tab, and a false supersession there is the
-// mirror-case regression the tag exists to kill. Pre-fix the server ignored the tag and
-// refused this with a 409.
-func TestATokenTaggedForADifferentTabIsIgnored(t *testing.T) {
- h, _ := newVocabHandlers("current")
- w := httptest.NewRecorder()
- req := httptest.NewRequest("POST", "/action", strings.NewReader(`{"kind":"click","ref":"e1","tabId":"tab1","vocab":"stale"}`))
- req.Header.Set(vocabTabHeader, "other-tab")
- h.HandleAction(w, req)
- var body map[string]any
- _ = json.Unmarshal(w.Body.Bytes(), &body)
+ hB, _ := newVocabHandlers("current")
+ _, body = serve(hB, `{"kind":"click","ref":"e1","tabId":"tab1","vocab":"stale","vocabTab":"other-tab"}`)
if body["code"] == vocabSupersededCode {
- t.Errorf("a token tagged for another tab produced a false supersession: %v", body)
+ t.Fatalf("row B: a token tagged for a moved-off tab produced a false supersession through the strip chain: %v", body)
}
}
diff --git a/internal/handlers/actions.go b/internal/handlers/actions.go
index e1d6787c..cf4bc28b 100644
--- a/internal/handlers/actions.go
+++ b/internal/handlers/actions.go
@@ -400,6 +400,7 @@ func decodeActionRequest(w http.ResponseWriter, r *http.Request) (bridge.ActionR
req.HasDelta = req.HasDelta || hasDeltaParam
req.Browser = q.Get("browser")
req.Vocab = q.Get("vocab")
+ req.VocabTab = q.Get("vocabTab")
if err := d.Err(); err != nil {
httpx.Error(w, 400, err)
return bridge.ActionRequest{}, false
@@ -417,12 +418,6 @@ func decodeActionRequest(w http.ResponseWriter, r *http.Request) (bridge.ActionR
const vocabHeader = "X-PinchTab-Vocab"
-// vocabTabHeader names the tab the echoed token belongs to. The epoch check runs
-// only when it is empty (a legacy client that cannot tag) or equals the resolved
-// tab; a token tagged for a different tab is ignored, so a stale current pointer
-// after the tab moved proceeds instead of a false supersession.
-const vocabTabHeader = "X-PinchTab-Vocab-Tab"
-
const vocabSupersededCode = "vocab_superseded"
func vocabTabMatches(vocabTab, resolvedTabID string) bool {
@@ -520,7 +515,7 @@ func (h *Handlers) HandleAction(w http.ResponseWriter, r *http.Request) {
if req.Vocab == "" {
req.Vocab = r.Header.Get(vocabHeader)
}
- if req.Vocab != "" && actionTargetsRef(req) && vocabTabMatches(r.Header.Get(vocabTabHeader), resolvedTabID) {
+ if req.Vocab != "" && actionTargetsRef(req) && vocabTabMatches(req.VocabTab, resolvedTabID) {
if cache := h.Bridge.GetRefCache(resolvedTabID); cache != nil && cache.DomEpoch != "" && cache.DomEpoch != req.Vocab {
writeVocabSuperseded(w, resolvedTabID)
return