Skip to content

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

fix: scheduler sends the internal token so its activity records as 'scheduler', MCP drops its stripped Source header, with a census flagging stripped public-client request headers (PIN-384)

Luigi Agosti7bd4b27cfa13
diff --git a/internal/api/types/headers_census_test.go b/internal/api/types/headers_census_test.go
index 12b332ff..8f5d96d5 100644
--- a/internal/api/types/headers_census_test.go
+++ b/internal/api/types/headers_census_test.go
@@ -5,6 +5,7 @@ import (
"go/ast"
"go/parser"
"go/token"
+ "path"
"sort"
"strconv"
"strings"
@@ -152,3 +153,168 @@ func TestCensusIgnoresHeadersNamedInComments(t *testing.T) {
t.Fatalf("a header named only in a comment was counted as a spelling: %v", got)
}
}
+
+const internalTokenHeaderValue = "X-PinchTab-Internal-Token"
+
+// knownHeaderConsts maps the X-PinchTab-* header constants (by their identifier name) to the
+// wire value, so a Set call using a constant is resolved the same as one using a literal.
+var knownHeaderConsts = map[string]string{
+ "HeaderVocab": "X-PinchTab-Vocab",
+ "HeaderTabID": "X-PinchTab-Tab-Id",
+ "HeaderSource": "X-PinchTab-Source",
+ "HeaderPTSessionID": "X-PinchTab-Session-Id",
+ "HeaderPTSource": "X-PinchTab-Source",
+ "HeaderPTInstance": "X-PinchTab-Instance-Id",
+ "HeaderPTProfileID": "X-PinchTab-Profile-Id",
+ "HeaderPTProfile": "X-PinchTab-Profile-Name",
+ "HeaderPTTabID": "X-PinchTab-Tab-Id",
+ "HeaderPTTabCreated": "X-PinchTab-Tab-Created",
+ "InternalTokenHeader": internalTokenHeaderValue,
+}
+
+// publicClientPackages originate outbound requests to a public PinchTab listener (bearer or
+// session auth, not orchestrator-proxied). An X-PinchTab-* request header they set is dropped
+// by the ingress strip layer unless the request also carries the internal token, so setting
+// one is a silent no-op — the failure PIN-376 and PIN-384 both hit. The server, the identity
+// helper (activity) and the trusted orchestrator hops are deliberately absent.
+var publicClientPackages = map[string]string{
+ "internal/cli/apiclient": "the CLI's HTTP transport",
+ "internal/mcp": "the MCP server's client to the front door",
+ "cmd/pinchtab": "CLI commands that build their own requests",
+ "internal/scheduler": "the scheduler's action executor posting to an instance",
+}
+
+// publicClientExemptHeaders are the (package, header) pairs a public client may still set:
+// the CLI's Source is harmless because its bearer credential fallback records the same
+// 'client' label, so a stripped Source changes nothing. MCP is deliberately NOT here — its
+// Source set was removed (PIN-384), and the census forbids re-adding it.
+var publicClientExemptHeaders = map[string]map[string]string{
+ "internal/cli/apiclient": {"x-pinchtab-source": "harmless: bearer credential fallback records the same 'client' label"},
+ "cmd/pinchtab": {"x-pinchtab-source": "harmless: same as the CLI apiclient — bearer fallback yields 'client'"},
+}
+
+func resolveHeaderName(arg ast.Expr) (string, bool) {
+ switch a := arg.(type) {
+ case *ast.BasicLit:
+ if a.Kind == token.STRING {
+ if v, err := strconv.Unquote(a.Value); err == nil {
+ return v, true
+ }
+ }
+ case *ast.Ident:
+ if v, ok := knownHeaderConsts[a.Name]; ok {
+ return v, true
+ }
+ case *ast.SelectorExpr:
+ if v, ok := knownHeaderConsts[a.Sel.Name]; ok {
+ return v, true
+ }
+ }
+ return "", false
+}
+
+type headerSetSite struct{ pkg, header, site string }
+
+// pinchtabRequestHeaderSets finds request-side `x.Header.Set(<X-PinchTab-* header>, …)` calls
+// (field-access .Header, so `w.Header().Set` response writes are not counted) and, separately,
+// the packages that set the internal token — a trusted hop whose headers survive ingress.
+func pinchtabRequestHeaderSets(t *testing.T, files []srccensus.SourceFile) (sets []headerSetSite, tokenPkgs map[string]bool) {
+ t.Helper()
+ tokenPkgs = map[string]bool{}
+ for _, f := range files {
+ parsed, err := parser.ParseFile(token.NewFileSet(), f.Name, f.Text, 0)
+ if err != nil {
+ t.Fatalf("parse %s: %v", f.Name, err)
+ }
+ pkg := path.Dir(f.Name)
+ ast.Inspect(parsed, func(n ast.Node) bool {
+ call, ok := n.(*ast.CallExpr)
+ if !ok {
+ return true
+ }
+ sel, ok := call.Fun.(*ast.SelectorExpr)
+ if !ok || sel.Sel.Name != "Set" {
+ return true
+ }
+ inner, ok := sel.X.(*ast.SelectorExpr)
+ if !ok || inner.Sel.Name != "Header" {
+ return true
+ }
+ if len(call.Args) == 0 {
+ return true
+ }
+ name, ok := resolveHeaderName(call.Args[0])
+ if !ok {
+ return true
+ }
+ key := headerKey(name)
+ if key == headerKey(internalTokenHeaderValue) {
+ tokenPkgs[pkg] = true
+ return true
+ }
+ if strings.HasPrefix(key, "x-pinchtab-") {
+ sets = append(sets, headerSetSite{pkg: pkg, header: key, site: f.Name})
+ }
+ return true
+ })
+ }
+ return sets, tokenPkgs
+}
+
+func strippedHeaderViolations(sets []headerSetSite, tokenPkgs map[string]bool) []string {
+ var violations []string
+ for _, s := range sets {
+ if _, isClient := publicClientPackages[s.pkg]; !isClient {
+ continue
+ }
+ if tokenPkgs[s.pkg] {
+ continue
+ }
+ if hdrs, ok := publicClientExemptHeaders[s.pkg]; ok {
+ if _, ok := hdrs[s.header]; ok {
+ continue
+ }
+ }
+ violations = append(violations, fmt.Sprintf("%s: %s sets request header %s, but the package sends no internal token, so ingress strips it (a silent no-op); carry the value in the body, send the internal token on a trusted hop, or exempt it with a reason", s.site, s.pkg, s.header))
+ }
+ sort.Strings(violations)
+ return violations
+}
+
+func TestPublicClientsDoNotSetStrippedRequestHeaders(t *testing.T) {
+ sets, tokenPkgs := pinchtabRequestHeaderSets(t, srccensus.Tree(t, moduleRoot, minModuleFiles))
+ if v := strippedHeaderViolations(sets, tokenPkgs); len(v) > 0 {
+ t.Errorf("public-client packages set X-PinchTab-* request headers the ingress strip layer drops:\n%s", strings.Join(v, "\n"))
+ }
+}
+
+func TestStrippedHeaderCensusFlagsAPublicClientSet(t *testing.T) {
+ planted := []srccensus.SourceFile{
+ {Name: "internal/mcp/evil.go", Text: "package mcp\nimport \"net/http\"\nfunc f(req *http.Request) { req.Header.Set(\"X-PinchTab-Source\", \"mcp\") }\n"},
+ }
+ sets, tokenPkgs := pinchtabRequestHeaderSets(t, planted)
+ if len(strippedHeaderViolations(sets, tokenPkgs)) == 0 {
+ t.Fatal("a public client setting a stripped X-PinchTab-* request header passed the census")
+ }
+}
+
+func TestStrippedHeaderCensusAllowsATrustedHopThatSendsTheToken(t *testing.T) {
+ planted := []srccensus.SourceFile{
+ {Name: "internal/scheduler/x.go", Text: "package scheduler\nimport \"net/http\"\nfunc f(req *http.Request) { req.Header.Set(\"X-PinchTab-Internal-Token\", \"s\"); req.Header.Set(\"X-PinchTab-Source\", \"scheduler\") }\n"},
+ }
+ sets, tokenPkgs := pinchtabRequestHeaderSets(t, planted)
+ if len(strippedHeaderViolations(sets, tokenPkgs)) != 0 {
+ t.Fatal("a package that sends the internal token was flagged; its headers survive ingress")
+ }
+}
+
+// A response-side write (w.Header().Set) is not a request header the strip layer can drop.
+func TestStrippedHeaderCensusIgnoresResponseWrites(t *testing.T) {
+ planted := []srccensus.SourceFile{
+ {Name: "internal/mcp/resp.go", Text: "package mcp\nimport \"net/http\"\nfunc f(w http.ResponseWriter) { w.Header().Set(\"X-PinchTab-Source\", \"mcp\") }\n"},
+ }
+ sets, _ := pinchtabRequestHeaderSets(t, planted)
+ if len(sets) != 0 {
+ t.Fatalf("a response-side w.Header().Set was counted as a request header: %v", sets)
+ }
+}
diff --git a/internal/mcp/client.go b/internal/mcp/client.go
index a6b8fc2e..b5d4ab7d 100644
--- a/internal/mcp/client.go
+++ b/internal/mcp/client.go
@@ -98,7 +98,10 @@ func (c *Client) doWithHeaders(req *http.Request) ([]byte, int, http.Header, err
req.Header.Set("Authorization", "Bearer "+c.Token)
}
req.Header.Set(activity.HeaderAgentID, "mcp")
- req.Header.Set(activity.HeaderPTSource, "mcp")
+ // No X-PinchTab-Source: MCP calls the public front door with a bearer token, so
+ // the ingress strip layer drops inbound X-PinchTab-* headers. Setting a source
+ // here would be a silent no-op; MCP correctly falls back to "client" until a
+ // per-client MCP credential exists.
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, 0, nil, fmt.Errorf("request %s %s: %w", req.Method, req.URL.Path, err)
diff --git a/internal/mcp/client_test.go b/internal/mcp/client_test.go
index 218602e1..12431534 100644
--- a/internal/mcp/client_test.go
+++ b/internal/mcp/client_test.go
@@ -144,6 +144,32 @@ func TestClientAuthHeaderAbsentWhenNoToken(t *testing.T) {
}
}
+// Option 3: MCP calls the public front door with a bearer token, so the ingress strip layer
+// drops inbound X-PinchTab-* headers. It must not set X-PinchTab-Source (a silent no-op that
+// falsely claimed attribution) and correctly falls back to "client"; X-Agent-Id is not
+// stripped and must still identify the client. On HEAD this sent X-PinchTab-Source=mcp.
+func TestMCPDoesNotSetTheStrippedSourceHeader(t *testing.T) {
+ var gotSource, gotAgent string
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ gotSource = r.Header.Get("X-PinchTab-Source")
+ gotAgent = r.Header.Get("X-Agent-Id")
+ w.WriteHeader(200)
+ _, _ = io.WriteString(w, `{}`)
+ }))
+ defer srv.Close()
+
+ c := NewClient(srv.URL, "tok")
+ if _, _, err := c.Post(context.Background(), "/action", map[string]any{"kind": "click"}); err != nil {
+ t.Fatal(err)
+ }
+ if gotSource != "" {
+ t.Errorf("MCP set X-PinchTab-Source=%q; ingress strips it, so setting it is a silent no-op that must be removed", gotSource)
+ }
+ if gotAgent != "mcp" {
+ t.Errorf("X-Agent-Id = %q, want mcp (it is not stripped and must still identify the client)", gotAgent)
+ }
+}
+
func TestClientProfileInstancePath(t *testing.T) {
c := NewClient("http://localhost:9867", "")
got := c.profileInstancePath("work profile")
diff --git a/internal/scheduler/executor.go b/internal/scheduler/executor.go
index 625954d9..db36db12 100644
--- a/internal/scheduler/executor.go
+++ b/internal/scheduler/executor.go
@@ -9,8 +9,10 @@ import (
"net"
"net/http"
"net/url"
+ "os"
"github.com/pinchtab/pinchtab/internal/activity"
+ "github.com/pinchtab/pinchtab/internal/handlers"
)
// TaskExecutor runs a single task and returns its result, decoupling dispatch
@@ -84,6 +86,15 @@ func (e *actionEndpointExecutor) Execute(ctx context.Context, t *Task) (any, err
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
+ // The instance strips inbound X-PinchTab-* headers unless the request carries the
+ // trusted internal token, so the source and tab-id below would be a silent no-op
+ // without it. The scheduler runs in the instance process, which holds the same
+ // PINCHTAB_INTERNAL_TOKEN the ingress checks; when it is unset (single-instance,
+ // no trust configured) the strip is total by design and the action records as
+ // "client".
+ if tok := os.Getenv("PINCHTAB_INTERNAL_TOKEN"); tok != "" {
+ req.Header.Set(handlers.InternalTokenHeader, tok)
+ }
req.Header.Set(activity.HeaderPTSource, "scheduler")
req.Header.Set(activity.HeaderPTTabID, t.TabID)
if t.AgentID != "" {
diff --git a/internal/scheduler/executor_test.go b/internal/scheduler/executor_test.go
index 988994cd..87da92ac 100644
--- a/internal/scheduler/executor_test.go
+++ b/internal/scheduler/executor_test.go
@@ -1,6 +1,90 @@
package scheduler
-import "testing"
+import (
+ "context"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "sync"
+ "testing"
+
+ "github.com/pinchtab/pinchtab/internal/activity"
+ "github.com/pinchtab/pinchtab/internal/handlers"
+)
+
+type captureRecorder struct {
+ mu sync.Mutex
+ events []activity.Event
+}
+
+func (c *captureRecorder) Enabled() bool { return true }
+func (c *captureRecorder) Record(e activity.Event) error {
+ c.mu.Lock()
+ c.events = append(c.events, e)
+ c.mu.Unlock()
+ return nil
+}
+func (c *captureRecorder) Query(activity.Filter) ([]activity.Event, error) { return nil, nil }
+func (c *captureRecorder) last() (activity.Event, bool) {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ if len(c.events) == 0 {
+ return activity.Event{}, false
+ }
+ return c.events[len(c.events)-1], true
+}
+
+type fixedResolver struct{ port string }
+
+func (f fixedResolver) ResolveTabInstance(string) (string, error) { return f.port, nil }
+
+// The scheduler must record its actions as "scheduler", not "client": it now carries the
+// trusted internal token so its X-PinchTab-Source and X-PinchTab-Tab-Id survive the ingress
+// strip layer. Driven through the real TrustedInternalProxyStripMiddleware (the layer the
+// bridge mounts) plus activity.Middleware (the recorder). On HEAD the executor sends no
+// token, the strip drops both headers, and the source falls back to "client".
+func TestSchedulerActionSurvivesIngressAndRecordsAsScheduler(t *testing.T) {
+ const secret = "test-internal-token"
+ t.Setenv("PINCHTAB_INTERNAL_TOKEN", secret)
+
+ var gotSource, gotTabID string
+ final := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ gotSource = r.Header.Get(activity.HeaderPTSource)
+ gotTabID = r.Header.Get(activity.HeaderPTTabID)
+ w.WriteHeader(http.StatusOK)
+ _, _ = w.Write([]byte(`{"ok":true}`))
+ })
+ rec := &captureRecorder{}
+ handler := handlers.TrustedInternalProxyStripMiddleware(secret)(activity.Middleware(rec, "fallback", final))
+ srv := httptest.NewServer(handler)
+ defer srv.Close()
+
+ parsed, err := url.Parse(srv.URL)
+ if err != nil {
+ t.Fatal(err)
+ }
+ exec := &actionEndpointExecutor{resolver: fixedResolver{port: parsed.Port()}, client: srv.Client()}
+ if _, err := exec.Execute(context.Background(), &Task{Action: "click", Ref: "e5", TabID: "tab-42"}); err != nil {
+ t.Fatalf("Execute: %v", err)
+ }
+
+ if gotSource != "scheduler" {
+ t.Errorf("inbound X-PinchTab-Source = %q, want scheduler (the internal token must let it survive the strip layer)", gotSource)
+ }
+ if gotTabID != "tab-42" {
+ t.Errorf("inbound X-PinchTab-Tab-Id = %q, want tab-42 (the tab id header must survive, not fall back to the path)", gotTabID)
+ }
+ evt, ok := rec.last()
+ if !ok {
+ t.Fatal("no activity event recorded")
+ }
+ if evt.Source != "scheduler" {
+ t.Errorf("recorded activity source = %q, want scheduler", evt.Source)
+ }
+ if evt.TabID != "tab-42" {
+ t.Errorf("recorded activity tab id = %q, want tab-42", evt.TabID)
+ }
+}
func TestBuildActionBodyEnvelope(t *testing.T) {
task := &Task{