High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
refactor(httpx): give the request-id header one spelling and a census to keep it
Luigi Agostidb3d93d17361
diff --git a/internal/activity/context.go b/internal/activity/context.goindex 5754b390..3ae74452 100644--- a/internal/activity/context.go+++ b/internal/activity/context.go@@ -170,7 +170,7 @@ func PropagateHeaders(ctx context.Context, req *http.Request) {evt := state.snapshot()if evt.RequestID != "" {- req.Header.Set("X-Request-Id", evt.RequestID)+ req.Header.Set(httpx.RequestIDHeader, evt.RequestID)}if evt.AgentID != "" {req.Header.Set(HeaderAgentID, evt.AgentID)@@ -197,11 +197,11 @@ func PropagateHeaders(ctx context.Context, req *http.Request) {func requestIDFor(r *http.Request, w http.ResponseWriter) string {if w != nil {- if rid := strings.TrimSpace(w.Header().Get("X-Request-Id")); rid != "" {+ if rid := strings.TrimSpace(w.Header().Get(httpx.RequestIDHeader)); rid != "" {return rid}}- return strings.TrimSpace(r.Header.Get("X-Request-Id"))+ return strings.TrimSpace(r.Header.Get(httpx.RequestIDHeader))}func agentIDFor(r *http.Request) string {diff --git a/internal/authn/audit.go b/internal/authn/audit.goindex 3d4d4603..21fc22a9 100644--- a/internal/authn/audit.go+++ b/internal/authn/audit.go@@ -4,6 +4,8 @@ import ("log/slog""net/http""strings"++ "github.com/pinchtab/pinchtab/internal/httpx")// AuditLog records security-sensitive actions without logging raw credentials@@ -23,7 +25,7 @@ func auditAttrs(r *http.Request, event string) []any {return attrs}attrs = append(attrs,- "requestId", strings.TrimSpace(r.Header.Get("X-Request-Id")),+ "requestId", strings.TrimSpace(r.Header.Get(httpx.RequestIDHeader)),"method", r.Method,"path", r.URL.Path,"clientIP", ClientIP(r),diff --git a/internal/handlers/middleware.go b/internal/handlers/middleware.goindex bc13d1de..8b07d4b1 100644--- a/internal/handlers/middleware.go+++ b/internal/handlers/middleware.go@@ -59,7 +59,7 @@ func LoggingMiddleware(next http.Handler) http.Handler {atomic.AddUint64(&metricRequestsFailed, 1)recordFailureEvent(FailureEvent{Time: time.Now(),- RequestID: w.Header().Get("X-Request-Id"),+ RequestID: w.Header().Get(httpx.RequestIDHeader),Method: r.Method,Path: r.URL.Path,Status: sw.Code,@@ -69,7 +69,7 @@ func LoggingMiddleware(next http.Handler) http.Handler {})}attrs := []any{- "requestId", w.Header().Get("X-Request-Id"),+ "requestId", w.Header().Get(httpx.RequestIDHeader),"method", r.Method,"path", r.URL.Path,"status", sw.Code,@@ -232,14 +232,14 @@ func StripInternalHeadersMiddleware(next http.Handler) http.Handler {func RequestIDMiddleware(next http.Handler) http.Handler {return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- rid := r.Header.Get("X-Request-Id")+ rid := r.Header.Get(httpx.RequestIDHeader)if !usableRequestID(rid) {b := make([]byte, 8)_, _ = rand.Read(b)rid = hex.EncodeToString(b)}- w.Header().Set("X-Request-Id", rid)- r.Header.Set("X-Request-Id", rid)+ w.Header().Set(httpx.RequestIDHeader, rid)+ r.Header.Set(httpx.RequestIDHeader, rid)next.ServeHTTP(w, r)})}diff --git a/internal/handlers/middleware_owned_headers_test.go b/internal/handlers/middleware_owned_headers_test.goindex 28b79595..27280be1 100644--- a/internal/handlers/middleware_owned_headers_test.go+++ b/internal/handlers/middleware_owned_headers_test.go@@ -84,10 +84,10 @@ func TestRequestIDMiddlewareStampsTheSameIDOnRequestAndResponse(t *testing.T) {var seen stringRequestIDMiddleware(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {- seen = r.Header.Get(httpx.HeaderRequestID)+ seen = r.Header.Get(httpx.RequestIDHeader)})).ServeHTTP(rec, req)- response := rec.Header().Get(httpx.HeaderRequestID)+ response := rec.Header().Get(httpx.RequestIDHeader)if response == "" {t.Fatal("no request id on the response, so a caller has nothing to quote")}diff --git a/internal/httpx/httpx.go b/internal/httpx/httpx.goindex dad13a25..dec6d717 100644--- a/internal/httpx/httpx.go+++ b/internal/httpx/httpx.go@@ -217,6 +217,13 @@ const (// RequestIDHeader is the id the request-id middleware stamps on every response and// the activity recorder copies into its event, so it is the join key between an// access-log line and the cause logged for it.+//+// It is the ONLY spelling of this header in the module, and the census beside it+// keeps it that way. The name was written out twice here and as a bare literal in+// four more packages, so renaming it — to lower-case it, or to move to a+// standard trace header — would have changed some readers and not others, and the+// halves fail quietly: a stamp nobody reads logs an empty id, and a reader whose+// writer moved on lets the outer chain's id through twice.const RequestIDHeader = "X-Request-Id"// logFailureCause writes the UNREDACTED reason a failure was returned. The messagediff --git a/internal/httpx/proxyheaders.go b/internal/httpx/proxyheaders.goindex 02e853d3..248e69dd 100644--- a/internal/httpx/proxyheaders.go+++ b/internal/httpx/proxyheaders.go@@ -6,7 +6,6 @@ import ()const (- HeaderRequestID = "X-Request-Id"HeaderContentSecurityPolicy = "Content-Security-Policy"HeaderFrameOptions = "X-Frame-Options"HeaderContentTypeOptions = "X-Content-Type-Options"@@ -14,7 +13,7 @@ const ()var outerChainResponseHeaders = []string{- HeaderRequestID,+ RequestIDHeader,HeaderContentSecurityPolicy,HeaderFrameOptions,HeaderContentTypeOptions,@@ -76,8 +75,8 @@ func containsFold(names []string, key string) bool {// the two are different permissions, since a copy forwards whatever arrived under that// name while this forwards the one value the outer chain resolved for this request.func ForwardRequestID(dst, src http.Header) {- if rid := strings.TrimSpace(src.Get(HeaderRequestID)); rid != "" {- dst.Set(HeaderRequestID, rid)+ if rid := strings.TrimSpace(src.Get(RequestIDHeader)); rid != "" {+ dst.Set(RequestIDHeader, rid)}}diff --git a/internal/httpx/proxyheaders_test.go b/internal/httpx/proxyheaders_test.goindex 989d2160..a0a523d5 100644--- a/internal/httpx/proxyheaders_test.go+++ b/internal/httpx/proxyheaders_test.go@@ -43,15 +43,15 @@ func TestCopyProxiedResponseHeadersLeavesTheOuterChainsHeadersSingleValued(t *tefunc TestCopyProxiedResponseHeadersMatchesOwnedHeadersCaseInsensitively(t *testing.T) {dst := http.Header{}- dst.Set(HeaderRequestID, "outer")+ dst.Set(RequestIDHeader, "outer")src := http.Header{}src["x-request-id"] = []string{"upstream"}CopyProxiedResponseHeaders(dst, src)- if got := dst.Values(HeaderRequestID); len(got) != 1 || got[0] != "outer" {- t.Errorf("%s = %v, want the outer value alone — an upstream spelling the canonicaliser did not touch must still be filtered", HeaderRequestID, got)+ if got := dst.Values(RequestIDHeader); len(got) != 1 || got[0] != "outer" {+ t.Errorf("%s = %v, want the outer value alone — an upstream spelling the canonicaliser did not touch must still be filtered", RequestIDHeader, got)}}@@ -93,8 +93,8 @@ func TestOuterChainResponseHeadersCannotBeMutatedByCallers(t *testing.T) {got := OuterChainResponseHeaders()got[0] = "X-Not-Owned"- if !OuterChainOwnsResponseHeader(HeaderRequestID) {- t.Fatalf("a caller editing the returned slice changed the owned set, so one proxy hop could stop filtering %s", HeaderRequestID)+ if !OuterChainOwnsResponseHeader(RequestIDHeader) {+ t.Fatalf("a caller editing the returned slice changed the owned set, so one proxy hop could stop filtering %s", RequestIDHeader)}}diff --git a/internal/httpx/request_id_spelling_census_test.go b/internal/httpx/request_id_spelling_census_test.gonew file mode 100644index 00000000..d0b392d2--- /dev/null+++ b/internal/httpx/request_id_spelling_census_test.go@@ -0,0 +1,70 @@+package httpx++import (+ "path/filepath"+ "strings"+ "testing"++ "github.com/pinchtab/pinchtab/internal/srccensus"+)++// The request id is a join key: the middleware stamps it, the proxy forwards it,+// the failure logger keys the cause on it, the activity recorder copies it into+// its event, and the audit log records it. Those five readers only agree while+// they spell the header the same way, and the name was written out twice in this+// package and as a bare literal in three more — so a rename would have moved some+// and not others, and every half fails quietly. A stamp nobody reads logs an empty+// id; a reader whose writer moved on lets the outer chain's id through twice and a+// caller sees two.+//+// So the spelling has one home, and this walks the module rather than a list of+// the packages that happened to have it when the rule was written.+func TestTheRequestIDHeaderIsSpelledInOnePlace(t *testing.T) {+ // The declaration itself is the one site that must contain the literal —+ // checked below rather than exempted, so deleting the constant fails too.+ const declaration = "internal/httpx/httpx.go"++ // A site that legitimately writes the name out, with the reason at the entry.+ // Checked in both directions: an entry naming a file the walk no longer finds,+ // or one that no longer contains the literal, fails as a stale exemption.+ exempt := map[string]string{}++ files := srccensus.Tree(t, filepath.Join("..", ".."), 200)++ declared := false+ var offenders []string+ for _, file := range files {+ if !strings.Contains(file.Text, `"`+RequestIDHeader+`"`) {+ continue+ }+ if file.Name == declaration {+ declared = true+ continue+ }+ if reason, ok := exempt[file.Name]; ok {+ if reason == "" {+ t.Errorf("%s is exempt with no reason recorded", file.Name)+ }+ continue+ }+ offenders = append(offenders, file.Name)+ }++ if !declared {+ t.Fatalf("%s no longer contains the literal %q; if the constant moved, point this census at its new home rather than deleting it", declaration, RequestIDHeader)+ }+ for _, name := range offenders {+ t.Errorf("%s spells %q instead of using httpx.RequestIDHeader; a rename would move this reader and not the others", name, RequestIDHeader)+ }+ for name, reason := range exempt {+ found := false+ for _, file := range files {+ if file.Name == name && strings.Contains(file.Text, `"`+RequestIDHeader+`"`) {+ found = true+ }+ }+ if !found {+ t.Errorf("%s is exempt (%s) but no longer spells the literal, so the exemption is stale", name, reason)+ }+ }+}diff --git a/internal/instance/bridge_client_proxy_headers_test.go b/internal/instance/bridge_client_proxy_headers_test.goindex 34a4991c..5c153d9d 100644--- a/internal/instance/bridge_client_proxy_headers_test.go+++ b/internal/instance/bridge_client_proxy_headers_test.go@@ -149,14 +149,14 @@ func TestBothBridgeHopsTellTheInstanceTheOuterChainsRequestID(t *testing.T) {}req := httptest.NewRequest("POST", "/find", strings.NewReader(`{"text":"Buy"}`))- req.Header.Set(httpx.HeaderRequestID, "outer-trace-key")+ req.Header.Set(httpx.RequestIDHeader, "outer-trace-key")tc.call(NewBridgeClient(), httptest.NewRecorder(), req, parsed.Port())if seen == nil {t.Fatal("the instance received no request; this test is measuring nothing")}- if got := seen.Get(httpx.HeaderRequestID); got != "outer-trace-key" {+ if got := seen.Get(httpx.RequestIDHeader); got != "outer-trace-key" {t.Errorf("instance was told %q, want the outer chain's id — without it the instance mints its own and one request needs two ids to trace", got)}})@@ -179,14 +179,14 @@ func TestProxyWithTabIDForwardsOnlyTheRequestID(t *testing.T) {}req := httptest.NewRequest("POST", "/find", strings.NewReader(`{"text":"Buy"}`))- req.Header.Set(httpx.HeaderRequestID, "outer-trace-key")+ req.Header.Set(httpx.RequestIDHeader, "outer-trace-key")req.Header.Set("Cookie", "pinchtab_auth_token=session-secret")req.Header.Set("X-Forwarded-For", "203.0.113.10")req.Header.Set("Authorization", "Bearer user-token")NewBridgeClient().ProxyWithTabID(httptest.NewRecorder(), req, parsed.Port(), "tab-1", "/find")- if got := seen.Get(httpx.HeaderRequestID); got != "outer-trace-key" {+ if got := seen.Get(httpx.RequestIDHeader); got != "outer-trace-key" {t.Fatalf("request id = %q, want it forwarded — the rest of this test cannot tell a narrow forward from no forward without it", got)}for _, name := range []string{"Cookie", "X-Forwarded-For", "Authorization"} {diff --git a/internal/proxy/request_id_hop_test.go b/internal/proxy/request_id_hop_test.goindex d3ab299b..255a28eb 100644--- a/internal/proxy/request_id_hop_test.go+++ b/internal/proxy/request_id_hop_test.go@@ -99,7 +99,7 @@ func instanceEchoingItsOwnRequestID(t *testing.T) *httptest.Server {t.Helper()srv := httptest.NewServer(handlers.RequestIDMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- _, _ = w.Write([]byte(r.Header.Get(httpx.HeaderRequestID)))+ _, _ = w.Write([]byte(r.Header.Get(httpx.RequestIDHeader)))})))t.Cleanup(srv.Close)return srv@@ -117,13 +117,13 @@ func proxyThroughOuterChain(t *testing.T, upstream *httptest.Server, inbound strfront := handlers.RequestIDMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {// What the outer chain resolved for this request: RequestIDMiddleware stamps it// onto the request, and it is the value msg=request logs.- outerID = r.Header.Get(httpx.HeaderRequestID)+ outerID = r.Header.Get(httpx.RequestIDHeader)Forward(w, r, targetURL, Options{})}))req := httptest.NewRequest("GET", "/tabs", nil)if inbound != "" {- req.Header.Set(httpx.HeaderRequestID, inbound)+ req.Header.Set(httpx.RequestIDHeader, inbound)}front.ServeHTTP(rec, req)return outerID, rec.Body.String(), rec@@ -162,9 +162,9 @@ func TestOneProxiedRequestIsTraceableByOneIDInBothLogs(t *testing.T) {}// The doubling half, on the same response.- values := rec.Header().Values(httpx.HeaderRequestID)+ values := rec.Header().Values(httpx.RequestIDHeader)if len(values) != 1 {- t.Fatalf("%s = %v, want exactly one value; a caller cannot tell which of two ids their HTTP library will show them", httpx.HeaderRequestID, values)+ t.Fatalf("%s = %v, want exactly one value; a caller cannot tell which of two ids their HTTP library will show them", httpx.RequestIDHeader, values)}if values[0] != outerID {t.Errorf("response carries %q but the outer chain logged %q, so the value a caller reads is not the one on disk", values[0], outerID)@@ -199,7 +199,7 @@ func TestForwardingTheRequestIDDoesNotWidenIntoAWholesaleCopy(t *testing.T) {} {req.Header.Set(name, value)}- req.Header.Set(httpx.HeaderRequestID, "traced")+ req.Header.Set(httpx.RequestIDHeader, "traced")front := handlers.RequestIDMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {Forward(w, r, targetURL, Options{})@@ -209,7 +209,7 @@ func TestForwardingTheRequestIDDoesNotWidenIntoAWholesaleCopy(t *testing.T) {if received == nil {t.Fatal("upstream received no request; this test is measuring nothing")}- if got := received.Get(httpx.HeaderRequestID); got != "traced" {+ if got := received.Get(httpx.RequestIDHeader); got != "traced" {t.Fatalf("request id = %q, want it forwarded — without that the rest of this test cannot distinguish a narrow forward from no forward at all", got)}for _, name := range []string{"Cookie", "Forwarded", "X-Forwarded-For", "X-Forwarded-Host", "X-Forwarded-Proto", "X-Real-Ip"} {@@ -225,13 +225,13 @@ func TestForwardingTheRequestIDDoesNotWidenIntoAWholesaleCopy(t *testing.T) {// Deleting it from the list would also forward it, and every behavioural test above would// stay green — this is the only check that tells the two implementations apart.func TestTheRequestIDIsForwardedByReAddingItRatherThanByLeavingTheStripList(t *testing.T) {- if _, stripped := strippedProxyRequestHeaders[strings.ToLower(httpx.HeaderRequestID)]; !stripped {+ if _, stripped := strippedProxyRequestHeaders[strings.ToLower(httpx.RequestIDHeader)]; !stripped {t.Error("x-request-id left strippedProxyRequestHeaders; it must stay stripped from the blind copy and be re-added by httpx.ForwardRequestID, or an arbitrary inbound value passes through on any path that skipped the outer middleware")}copied := http.Header{}- copyRequestHeaders(copied, http.Header{httpx.HeaderRequestID: {"from-the-blind-copy"}})- if got := copied.Get(httpx.HeaderRequestID); got != "" {+ copyRequestHeaders(copied, http.Header{httpx.RequestIDHeader: {"from-the-blind-copy"}})+ if got := copied.Get(httpx.RequestIDHeader); got != "" {t.Errorf("the blind copy carried the request id as %q; the re-add is meant to be the only way it travels", got)}}