High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix(handlers): adopt an inbound request id only when it is shaped like one
Luigi Agosti39705a2c60ce
diff --git a/internal/handlers/middleware.go b/internal/handlers/middleware.goindex 7b60da5a..d5b0184d 100644--- a/internal/handlers/middleware.go+++ b/internal/handlers/middleware.go@@ -220,7 +220,7 @@ 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")- if rid == "" {+ if !usableRequestID(rid) {b := make([]byte, 8)_, _ = rand.Read(b)rid = hex.EncodeToString(b)@@ -230,3 +230,33 @@ func RequestIDMiddleware(next http.Handler) http.Handler {next.ServeHTTP(w, r)})}++// maxRequestIDLen bounds an inbound correlation id. A UUID is 36 characters and+// a W3C traceparent 55, so this leaves room for every id a real client sends+// while keeping the header out of the "arbitrary payload" class — the server+// accepts 256 KiB of headers, and this value is copied into every audit and+// access log line for the request, into the dashboard activity stream, and onto+// the proxied hop.+const maxRequestIDLen = 128++// usableRequestID reports whether an inbound X-Request-Id may be adopted as this+// request's correlation id. Honouring a client's id is deliberate — it is what+// makes one request findable in the outer log and the instance log alike — but+// the id is adopted only when it is shaped like one. Anything else is replaced+// by a generated id rather than refused, because the correlation id is not+// something a request should fail on.+func usableRequestID(rid string) bool {+ if rid == "" || len(rid) > maxRequestIDLen {+ return false+ }+ for i := 0; i < len(rid); i++ {+ c := rid[i]+ switch {+ case c >= 'a' && c <= 'z', c >= 'A' && c <= 'Z', c >= '0' && c <= '9':+ case c == '-', c == '_', c == '.', c == ':':+ default:+ return false+ }+ }+ return true+}diff --git a/internal/handlers/request_id_shape_test.go b/internal/handlers/request_id_shape_test.gonew file mode 100644index 00000000..16b02dcc--- /dev/null+++ b/internal/handlers/request_id_shape_test.go@@ -0,0 +1,92 @@+package handlers++import (+ "net/http"+ "net/http/httptest"+ "strings"+ "testing"+)++func servedRequestID(t *testing.T, inbound string) (onRequest, onResponse string) {+ t.Helper()+ handler := RequestIDMiddleware(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {+ onRequest = r.Header.Get("X-Request-Id")+ }))+ req := httptest.NewRequest("GET", "/test", nil)+ if inbound != "" {+ req.Header.Set("X-Request-Id", inbound)+ }+ w := httptest.NewRecorder()+ handler.ServeHTTP(w, req)+ return onRequest, w.Header().Get("X-Request-Id")+}++// Honouring an inbound id is the point: it is what makes one request findable in+// the outer log and in the proxied instance's log. Every shape a real client+// sends must survive.+func TestRequestIDMiddlewareKeepsAWellFormedInboundID(t *testing.T) {+ for _, id := range []string{+ "req-123",+ "3f2504e0-4f89-11d3-9a0c-0305e82c3301",+ "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",+ "01ARZ3NDEKTSV4RRFFQ69G5FAV",+ "trace.id_with:separators",+ } {+ onRequest, onResponse := servedRequestID(t, id)+ if onRequest != id || onResponse != id {+ t.Errorf("inbound %q became request=%q response=%q; a well-formed id must be forwarded verbatim", id, onRequest, onResponse)+ }+ }+}++// The adopted id is copied into every audit and access log line for the request,+// into the dashboard activity stream, and onto the proxied hop. The server+// accepts 256 KiB of headers, so an unconstrained id is an arbitrary payload+// with a guaranteed path into all of them.+func TestRequestIDMiddlewareReplacesAnIDThatIsNotShapedLikeOne(t *testing.T) {+ cases := map[string]string{+ "over length": strings.Repeat("a", maxRequestIDLen+1),+ "newline": "req\r\n123",+ "space": "req 123",+ "control character": "req\x00123",+ "log field syntax": `req" level=INFO msg="forged`,+ "non-ascii": "req-123-ü",+ }+ for name, inbound := range cases {+ t.Run(name, func(t *testing.T) {+ onRequest, onResponse := servedRequestID(t, inbound)+ if onRequest == inbound {+ t.Errorf("the request carries the supplied id verbatim; it reaches the audit log, the activity stream and the proxied hop unchanged")+ }+ if onRequest != onResponse {+ t.Errorf("request id %q and response id %q disagree; one request must have one id", onRequest, onResponse)+ }+ if onRequest == "" {+ t.Error("no id was generated to replace the rejected one")+ }+ if !usableRequestID(onRequest) {+ t.Errorf("the generated replacement %q is itself not a usable id", onRequest)+ }+ })+ }+}++// The length bound has to be a bound, not a coincidence of the fixtures.+func TestRequestIDLengthBoundIsInclusive(t *testing.T) {+ atLimit := strings.Repeat("a", maxRequestIDLen)+ if !usableRequestID(atLimit) {+ t.Errorf("an id of exactly maxRequestIDLen was rejected; the bound must be inclusive")+ }+ if usableRequestID(atLimit + "a") {+ t.Error("an id one character over the bound was accepted")+ }+}++// Absent stays generated, which is the behaviour every other test in this+// package relies on.+func TestRequestIDMiddlewareGeneratesWhenNoneIsSupplied(t *testing.T) {+ onRequest, onResponse := servedRequestID(t, "")+ if onRequest == "" || onRequest != onResponse {+ t.Errorf("request=%q response=%q, want one generated id on both", onRequest, onResponse)+ }+}