Skip to content

server: forward non-prefixed /broker/ paths without a redirect - #203

Merged
shawnburke merged 2 commits into
mainfrom
fix/broker-dispatch-unclean-paths
Nov 22, 2026
Merged

shawnburke merged 2 commits into
mainfrom
fix/broker-dispatch-unclean-paths

Conversation

@shawnburke

@shawnburke shawnburke commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

After a tenant moved from snyk-broker to grpc-tunnel, the relay dispatcher logged this warning for some GitHub requests:

Dispatch failed for token *** but broker relay-grpc-tunnel-sts-0:8080 is responsive:
IllegalArgumentException: Expected URL scheme 'http' or 'https' but no scheme was found for /broke...

The cause is the http.ServeMux in HttpAdapter. ServeMux cleans the request path. If a path has //, /./ or /../ (for example /broker/<token>/repos/o/r/contents//Jenkinsfile), ServeMux does not call the handler. It sends a 301 with a relative Location that holds the cleaned path. The dispatcher's redirect code cannot parse a relative URL, so the call fails. The broker is healthy, so the dispatcher tries every client again, and each request makes about 20 attempts before it fails.

snyk-broker forwards the path as it is, so this did not happen before the switch.

Fix

HttpAdapter.ServeHTTP now sends a /broker/ path that is not clean directly to handleBrokerDispatch. The agent gets the path as the caller sent it, the same as with snyk-broker. isCleanPath uses the same rule as ServeMux (path.Clean, with a trailing slash kept). Clean paths still go through the mux, so connection-status routing does not change.

Tests

http_test.go checks that clean, //, /./, /../ and query paths get to the agent unchanged. It reuses failureHarness. Before this change, no test covered HttpAdapter path forwarding.

CI on this branch runs make -C server test.

A separate change in the relay-dispatcher (brain-backend) will make it resolve relative Location headers, as defense in depth.

🤖 Generated with Claude Code

shawnburke and others added 2 commits November 22, 2026 08:01
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
@shawnburke
shawnburke requested a review from ashiramin November 22, 2026 08:13
@shawnburke shawnburke changed the title server: forward unclean /broker/ paths without a redirect server: forward non-prefixed /broker/ paths without a redirect Nov 22, 2026
@shawnburke
shawnburke enabled auto-merge (squash) November 22, 2026 08:17
@shawnburke
shawnburke merged commit 283e525 into main Nov 22, 2026
26 checks passed
@shawnburke
shawnburke deleted the fix/broker-dispatch-unclean-paths branch November 22, 2026 18:24
engrams-agent Bot pushed a commit that referenced this pull request Nov 23, 2026
HttpAdapter.ServeHTTP had its own copy of the unclean-path check from
#203. It now uses Wrap in front of its own ServeMux, the same as
cmd/main.go does for the outer one, so the check is in one place. Wrap
calls handleBrokerDispatch directly, so ServeHTTP and Wrap do not call
each other.

The test now sends each path through both entry points: the adapter
directly, and an outer ServeMux as in cmd/main.go.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
shawnburke added a commit that referenced this pull request Nov 23, 2026
* server: bypass path cleaning in the outer HTTP mux too

#203 kept the adapter's own ServeMux from redirecting unclean /broker/
paths. But cmd/main.go mounts the adapter on a second ServeMux, which
cleans the path and sends the relative 301 first, so the adapter never
saw these requests and the dispatcher still failed on them.

Add HttpAdapter.Wrap, which sends unclean /broker/ paths directly to
the adapter, and use it in front of the outer mux. The test now goes
through an outer mux, the same as production.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: relay paths that are not clean, end to end

Add a shared relay scenario that requests //file and /./file through the
real tunnel server image, agent and origin. On main the outer ServeMux in
cmd/main.go sends a relative 301 and the scenario fails. With HttpAdapter.Wrap
the origin serves the file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: expect the agent to reject unclean paths, not the origin to serve them

Both agents' accept filters reject a path that is not normalized: the
grpc-tunnel agent with 400 "path is not normalized", snyk-broker with 401
"blocked". The scenario now checks that such a path reaches the agent and
gets that 4xx, and that the ingress does not answer with a redirect.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* server: route the adapter's own mux through Wrap too

HttpAdapter.ServeHTTP had its own copy of the unclean-path check from
#203. It now uses Wrap in front of its own ServeMux, the same as
cmd/main.go does for the outer one, so the check is in one place. Wrap
calls handleBrokerDispatch directly, so ServeHTTP and Wrap do not call
each other.

The test now sends each path through both entry points: the adapter
directly, and an outer ServeMux as in cmd/main.go.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* server: serve all adapter routes in Wrap, without a ServeMux

Wrap had to find paths that are not clean and send them past each
ServeMux that routed to the adapter. Now no ServeMux routes the adapter's
paths. Wrap matches connection-status and /broker/ dispatch on the path
as it was sent, and sends all other requests to next. ServeHTTP is Wrap
in front of a 404, and cmd/main.go no longer registers the adapter's
paths on its mux. isCleanPath is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* server: explain why ServeHTTP wraps a 404 handler

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* server: match connection-status paths with one regex

connectionStatusToken now uses a regex for /connection-status/{token}
and /broker/connection-status/{token}, instead of hand-written prefix
cuts. Wrap checks the method. TestConnectionStatusToken covers both
forms and the paths that must not match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: shawnburke <shawnburke@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants