From 40042da4b3d4e33c3cb918d9ccfe84a109f3bcf8 Mon Sep 17 00:00:00 2001 From: Shawn Burke Date: Sun, 22 Nov 2026 08:01:30 +0000 Subject: [PATCH 1/2] server: forward unclean /broker/ paths without a redirect ServeMux answers a path with "//", "/./" or "/../" with a 301 to the cleaned path. The Location is relative, and the relay dispatcher cannot follow a relative redirect, so each such call failed after the dispatcher retried every client. snyk-broker forwards the path as it is, so the failures started only after a tenant moved to grpc-tunnel. Send these paths directly to broker dispatch so the agent gets the path as the caller sent it. Co-Authored-By: Claude Opus 5.5 --- server/adapters/http.go | 21 ++++++++ server/adapters/unclean_path_test.go | 72 ++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 server/adapters/unclean_path_test.go diff --git a/server/adapters/http.go b/server/adapters/http.go index 3fbf5fc6..2a45bbc3 100644 --- a/server/adapters/http.go +++ b/server/adapters/http.go @@ -14,6 +14,7 @@ import ( "fmt" "io" "net/http" + "path" "strconv" "strings" "time" @@ -76,10 +77,30 @@ func NewHttpAdapter( // ServeHTTP delegates to the internal mux which routes between // connection-status lookups and broker dispatch. +// +// A /broker/ path that is not clean (it has "//", "/./" or "/../") goes +// directly to dispatch. ServeMux answers such a path with a 301 to the +// cleaned path, and that Location is relative. The dispatcher cannot follow +// a relative redirect, so the call failed. snyk-broker forwards the path as +// it is, and the upstream decides what the path means. We do the same. func (h *HttpAdapter) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if strings.HasPrefix(r.URL.Path, "/broker/") && !isCleanPath(r.URL.Path) { + h.handleBrokerDispatch(w, r) + return + } h.mux.ServeHTTP(w, r) } +// isCleanPath reports whether ServeMux would route p without a redirect. It +// uses the same rule as ServeMux: path.Clean, with a trailing slash kept. +func isCleanPath(p string) bool { + cleaned := path.Clean(p) + if strings.HasSuffix(p, "/") && cleaned != "/" { + cleaned += "/" + } + return cleaned == p +} + // brokerOKFalse and brokerOKTrue are snyk-broker's response bodies, byte for // byte. Their exact bytes matter: the dispatcher identifies "this server does // not have that token" by comparing the body, so a reformat here (a space diff --git a/server/adapters/unclean_path_test.go b/server/adapters/unclean_path_test.go new file mode 100644 index 00000000..d2f46726 --- /dev/null +++ b/server/adapters/unclean_path_test.go @@ -0,0 +1,72 @@ +package adapters + +import ( + "errors" + "net/http" + "net/http/httptest" + "testing" + "time" + + pb "github.com/cortexapps/axon-server/.generated/proto/tunnelpb" + "github.com/cortexapps/axon-server/broker" + "github.com/cortexapps/axon-server/config" + "github.com/cortexapps/axon-server/dispatch" + "github.com/cortexapps/axon-server/metrics" + "github.com/cortexapps/axon-server/tunnel" + "github.com/stretchr/testify/require" + "go.uber.org/zap" +) + +// ServeMux answers a path that is not clean with a 301 and a relative +// Location. The dispatcher cannot follow a relative redirect, so each such +// call failed. The path must go to the agent as it is, as snyk-broker does. +func TestBrokerDispatch_UncleanPathIsForwardedAsIs(t *testing.T) { + cases := []struct { + name, reqPath, agentPath string + }{ + {"double slash", "/broker/tok/repos/o/r/contents//Jenkinsfile", "/repos/o/r/contents//Jenkinsfile"}, + {"dot segment", "/broker/tok/repos/o/r/contents/./Jenkinsfile", "/repos/o/r/contents/./Jenkinsfile"}, + {"query kept", "/broker/tok/a//b?ref=main", "/a//b?ref=main"}, + {"clean path", "/broker/tok/repos/o/r/contents/Jenkinsfile", "/repos/o/r/contents/Jenkinsfile"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + logger := zap.NewNop() + registry := tunnel.NewClientRegistry(logger) + cfg := config.Config{DispatchTimeout: 5 * time.Second} + m := metrics.New("test") + h := NewHttpAdapter(cfg, registry, dispatch.NewDispatcher(cfg, registry, m, logger), m, logger) + + // Record the path the agent gets, then fail the send so the + // call ends without a wait for a response. + var agentPath string + mustRegister(t, registry, broker.NewToken("tok"), tunnel.ClientIdentity{InstanceID: "agent-1"}, + &tunnel.StreamHandle{StreamID: "s1", Send: func(f *pb.ServerFrame) error { + if start := f.GetCall().GetStart(); start != nil { + agentPath = start.GetPseudoHeaders()[":path"] + } + return errors.New("stop after start") + }}) + + w := httptest.NewRecorder() + h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, tc.reqPath, nil)) + + require.NotEqual(t, http.StatusMovedPermanently, w.Code) + require.Empty(t, w.Header().Get("Location")) + require.Equal(t, tc.agentPath, agentPath) + }) + } +} + +func TestIsCleanPath(t *testing.T) { + for p, want := range map[string]bool{ + "/broker/tok/a/b": true, + "/broker/tok/a/b/": true, + "/broker/tok/a//b": false, + "/broker/tok/./a": false, + "/broker/tok/a/../": false, + "/broker/tok//": false, + } { + require.Equal(t, want, isCleanPath(p), p) + } +} From 5e5acb437a2f747b03ca8fda15798a6bd1d0d353 Mon Sep 17 00:00:00 2001 From: Shawn Burke Date: Sun, 22 Nov 2026 08:06:24 +0000 Subject: [PATCH 2/2] server: reuse failureHarness in the path-forwarding test No test covered HttpAdapter path forwarding. Put the new test in http_test.go and reuse the existing harness instead of new setup. Co-Authored-By: Claude Opus 5.5 --- server/adapters/http_test.go | 34 +++++++++++++ server/adapters/unclean_path_test.go | 72 ---------------------------- 2 files changed, 34 insertions(+), 72 deletions(-) create mode 100644 server/adapters/http_test.go delete mode 100644 server/adapters/unclean_path_test.go diff --git a/server/adapters/http_test.go b/server/adapters/http_test.go new file mode 100644 index 00000000..c227160a --- /dev/null +++ b/server/adapters/http_test.go @@ -0,0 +1,34 @@ +package adapters + +import ( + "context" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/require" +) + +// The agent must get the path as the caller sent it, as with snyk-broker. +// ServeMux cleans a path with "//", "/./" or "/../" and answers with a 301 +// to a relative Location, which the dispatcher cannot follow. +func TestBrokerDispatch_ForwardsPathAsSent(t *testing.T) { + for name, path := range map[string]string{ + "clean": "/repos/o/r/contents/Jenkinsfile", + "double slash": "/repos/o/r/contents//Jenkinsfile", + "dot": "/repos/o/r/contents/./Jenkinsfile", + "dot dot": "/repos/o/r/../r/contents/Jenkinsfile", + "query": "/repos/o/r/contents//Jenkinsfile?ref=main", + } { + t.Run(name, func(t *testing.T) { + fh := newFailureHarness(t, 0) + ctx, cancel := context.WithCancel(context.Background()) + done := make(chan *httptest.ResponseRecorder, 1) + go func() { done <- fh.serve(ctx, path) }() + + start := fh.next(t, isStart).GetStart() + require.Equal(t, path, start.GetPseudoHeaders()[":path"]) + cancel() + <-done + }) + } +} diff --git a/server/adapters/unclean_path_test.go b/server/adapters/unclean_path_test.go deleted file mode 100644 index d2f46726..00000000 --- a/server/adapters/unclean_path_test.go +++ /dev/null @@ -1,72 +0,0 @@ -package adapters - -import ( - "errors" - "net/http" - "net/http/httptest" - "testing" - "time" - - pb "github.com/cortexapps/axon-server/.generated/proto/tunnelpb" - "github.com/cortexapps/axon-server/broker" - "github.com/cortexapps/axon-server/config" - "github.com/cortexapps/axon-server/dispatch" - "github.com/cortexapps/axon-server/metrics" - "github.com/cortexapps/axon-server/tunnel" - "github.com/stretchr/testify/require" - "go.uber.org/zap" -) - -// ServeMux answers a path that is not clean with a 301 and a relative -// Location. The dispatcher cannot follow a relative redirect, so each such -// call failed. The path must go to the agent as it is, as snyk-broker does. -func TestBrokerDispatch_UncleanPathIsForwardedAsIs(t *testing.T) { - cases := []struct { - name, reqPath, agentPath string - }{ - {"double slash", "/broker/tok/repos/o/r/contents//Jenkinsfile", "/repos/o/r/contents//Jenkinsfile"}, - {"dot segment", "/broker/tok/repos/o/r/contents/./Jenkinsfile", "/repos/o/r/contents/./Jenkinsfile"}, - {"query kept", "/broker/tok/a//b?ref=main", "/a//b?ref=main"}, - {"clean path", "/broker/tok/repos/o/r/contents/Jenkinsfile", "/repos/o/r/contents/Jenkinsfile"}, - } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - logger := zap.NewNop() - registry := tunnel.NewClientRegistry(logger) - cfg := config.Config{DispatchTimeout: 5 * time.Second} - m := metrics.New("test") - h := NewHttpAdapter(cfg, registry, dispatch.NewDispatcher(cfg, registry, m, logger), m, logger) - - // Record the path the agent gets, then fail the send so the - // call ends without a wait for a response. - var agentPath string - mustRegister(t, registry, broker.NewToken("tok"), tunnel.ClientIdentity{InstanceID: "agent-1"}, - &tunnel.StreamHandle{StreamID: "s1", Send: func(f *pb.ServerFrame) error { - if start := f.GetCall().GetStart(); start != nil { - agentPath = start.GetPseudoHeaders()[":path"] - } - return errors.New("stop after start") - }}) - - w := httptest.NewRecorder() - h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, tc.reqPath, nil)) - - require.NotEqual(t, http.StatusMovedPermanently, w.Code) - require.Empty(t, w.Header().Get("Location")) - require.Equal(t, tc.agentPath, agentPath) - }) - } -} - -func TestIsCleanPath(t *testing.T) { - for p, want := range map[string]bool{ - "/broker/tok/a/b": true, - "/broker/tok/a/b/": true, - "/broker/tok/a//b": false, - "/broker/tok/./a": false, - "/broker/tok/a/../": false, - "/broker/tok//": false, - } { - require.Equal(t, want, isCleanPath(p), p) - } -}