Skip to content

Agent Mesh

router, node: bound the ingress dial and treat a backslash as a path separator

Antonio Ojea1fc0fbfa8e1f
diff --git a/api/ingress.go b/api/ingress.go
index 84bb4451..71a0fc64 100644
--- a/api/ingress.go
+++ b/api/ingress.go
@@ -155,6 +155,21 @@ func ValidatePeerRecordIngress(ing *ServiceIngress) error {
return nil
}
+// HasDotSegment reports whether a URL path carries a "." or ".." segment.
+// Policy is decided on the /<type>/<name> prefix of a path, so a dot segment
+// in what follows could resolve to a sibling service or file on the backend.
+// A backslash counts as a separator too: it reaches the backend
+// percent-encoded, and a backend on a platform that treats "\" as "/" would
+// otherwise see a traversal this check did not.
+func HasDotSegment(p string) bool {
+ for _, seg := range strings.Split(strings.ReplaceAll(p, "\\", "/"), "/") {
+ if seg == "." || seg == ".." {
+ return true
+ }
+ }
+ return false
+}
+
// SanitizeExternalIngressHeaders removes ambient cookies, spoofable X-Mesh-*
// and X-Peer-Id attribution headers, and (when stripAuthorization is true) the
// external caller's Authorization header before forwarding an external ingress
diff --git a/internal/node/a2a_service.go b/internal/node/a2a_service.go
index 14c1101a..359a7ebd 100644
--- a/internal/node/a2a_service.go
+++ b/internal/node/a2a_service.go
@@ -289,7 +289,7 @@ func regenerateAgentCardForMesh(card *a2a.AgentCard, base string) error {
func meshInterfaceURL(base, raw string) string {
ref, err := url.Parse(raw)
if err != nil || ref.Scheme != "" || ref.Host != "" || ref.User != nil || ref.Opaque != "" ||
- strings.HasPrefix(ref.Path, "/") || hasDotSegment(ref.Path) {
+ strings.HasPrefix(ref.Path, "/") || api.HasDotSegment(ref.Path) {
return base
}
out := base
@@ -411,7 +411,7 @@ func relativeToRoot(raw, root string) (string, bool) {
default:
return "", false
}
- if hasDotSegment(rest) {
+ if api.HasDotSegment(rest) {
return "", false
}
if u.RawQuery != "" {
diff --git a/internal/node/egress.go b/internal/node/egress.go
index 4a437986..bfa7460b 100644
--- a/internal/node/egress.go
+++ b/internal/node/egress.go
@@ -474,7 +474,7 @@ func (n *AgentMeshNode) applyPendingEgress(ctx context.Context) {
// path and the destination's host and port, as on the mesh datapath. The
// client's Authorization was for the node and does not travel further.
func handleLocalEgress(node *AgentMeshNode, w http.ResponseWriter, r *http.Request) {
- if hasDotSegment(r.URL.Path) {
+ if api.HasDotSegment(r.URL.Path) {
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}
diff --git a/internal/node/ingress.go b/internal/node/ingress.go
index bdeb9946..0fcc59a7 100644
--- a/internal/node/ingress.go
+++ b/internal/node/ingress.go
@@ -177,7 +177,7 @@ func (n *AgentMeshNode) externalIngressHandler() http.Handler {
}()
path := r.URL.Path
- if hasDotSegment(path) {
+ if api.HasDotSegment(path) {
externalIngressRequestsTotal.WithLabelValues(modeMetric, externalIngressOutcomeInvalid).Inc()
http.Error(w, "Invalid path", http.StatusBadRequest)
return
diff --git a/internal/node/node.go b/internal/node/node.go
index d1e1a61c..918f4d2a 100644
--- a/internal/node/node.go
+++ b/internal/node/node.go
@@ -2530,7 +2530,7 @@ func (n *AgentMeshNode) StartIngressServer(ctx context.Context) error {
path := r.URL.Path
// Policy is decided on the /{type}/{name} prefix; a ".." segment in
// what follows could resolve to a sibling service on a shared backend.
- if hasDotSegment(path) {
+ if api.HasDotSegment(path) {
http.Error(w, "Invalid path", http.StatusBadRequest)
return
}
diff --git a/internal/node/sidecar.go b/internal/node/sidecar.go
index 00ba4b40..0bca8f55 100644
--- a/internal/node/sidecar.go
+++ b/internal/node/sidecar.go
@@ -490,19 +490,6 @@ func stripSidecarTokenFromAuthorization(r *http.Request, token string) {
}
}
-// hasDotSegment reports whether any path segment is "." or "..". Go's
-// ServeMux canonicalizes these with a redirect, but the mesh proxies pass
-// paths through to backends that may resolve them against a different
-// service prefix than the one authorization was decided on.
-func hasDotSegment(p string) bool {
- for _, seg := range strings.Split(p, "/") {
- if seg == "." || seg == ".." {
- return true
- }
- }
- return false
-}
-
// constantTimeEqual compares two secrets without leaking their contents through
// timing. Hashing first keeps the comparison length-independent, so a mismatched
// length is no more distinguishable than a mismatched byte. This mirrors how the
@@ -776,7 +763,7 @@ func createEgressProxy(node *AgentMeshNode) http.Handler {
}
// The remote's policy is decided on the /{type}/{name} prefix this
// proxy forwards verbatim; a ".." in the rest is not ours to send.
- if hasDotSegment(r.URL.Path) {
+ if api.HasDotSegment(r.URL.Path) {
http.Error(w, "Bad Request: path must not contain dot segments", http.StatusBadRequest)
return
}
diff --git a/internal/node/sidecar_hygiene_test.go b/internal/node/sidecar_hygiene_test.go
index 155df0d3..47467225 100644
--- a/internal/node/sidecar_hygiene_test.go
+++ b/internal/node/sidecar_hygiene_test.go
@@ -34,9 +34,14 @@ func TestHasDotSegment(t *testing.T) {
"/mcp/a/x..y/tools": false, // ".." inside a segment is just a name
"/mcp/a/tools/...": false,
"/mesh/p/mcp/a/../b/x": true,
+ // A backslash reaches the backend percent-encoded; a backend that
+ // treats it as a separator would see a traversal, so it is one here.
+ "/mcp/a/..\\b/tools": true,
+ "/mcp/a\\..\\b/tools": true,
+ "/mcp/a/x\\y/tools": false,
} {
- if got := hasDotSegment(path); got != want {
- t.Errorf("hasDotSegment(%q) = %v, want %v", path, got, want)
+ if got := api.HasDotSegment(path); got != want {
+ t.Errorf("HasDotSegment(%q) = %v, want %v", path, got, want)
}
}
}
diff --git a/internal/router/ingress.go b/internal/router/ingress.go
index 4a551138..3c46d9b4 100644
--- a/internal/router/ingress.go
+++ b/internal/router/ingress.go
@@ -146,7 +146,13 @@ func (r *Router) IngressHandler() http.Handler {
if r.gated == nil {
return nil, errors.New("router host is not initialized")
}
- return gostream.Dial(network.WithAllowLimitedConn(ctx, "ingress"), r.gated, pid, api.IngressProtocolID)
+ // The inbound request carries no deadline, so an unresponsive
+ // provider or a hung stream negotiation would hold this
+ // goroutine for as long as the client waits. The stream, once
+ // open, outlives dialCtx.
+ dialCtx, cancel := context.WithTimeout(ctx, ingressDialTimeout)
+ defer cancel()
+ return gostream.Dial(network.WithAllowLimitedConn(dialCtx, "ingress"), r.gated, pid, api.IngressProtocolID)
},
MaxIdleConnsPerHost: 16,
IdleConnTimeout: 60 * time.Second,
@@ -299,7 +305,7 @@ func (r *Router) handleIngressRequest(w http.ResponseWriter, req *http.Request,
clientIP := clientIPFromRemoteAddr(req.RemoteAddr)
req.Body = http.MaxBytesReader(w, req.Body, maxIngressBodyBytes)
- if hasDotSegment(req.URL.Path) {
+ if api.HasDotSegment(req.URL.Path) {
routerIngressRequestsTotal.WithLabelValues(ingressModeUnknown, ingressOutcomeInvalid).Inc()
logger.Debugf("[Ingress] Rejecting dot-segment path from %s", clientIP)
http.Error(w, "Invalid path", http.StatusBadRequest)
@@ -949,15 +955,6 @@ func clientIPFromRemoteAddr(remoteAddr string) string {
return "unknown"
}
-func hasDotSegment(p string) bool {
- for _, seg := range strings.Split(p, "/") {
- if seg == "." || seg == ".." {
- return true
- }
- }
- return false
-}
-
func isHTTPTransportBinding(binding a2a.TransportProtocol) bool {
switch strings.ToUpper(string(binding)) {
case string(a2a.TransportProtocolJSONRPC), string(a2a.TransportProtocolHTTPJSON):
@@ -969,7 +966,7 @@ func isHTTPTransportBinding(binding a2a.TransportProtocol) bool {
func resolveIngressInterfaceURL(base, raw string) string {
ref, err := url.Parse(raw)
if err != nil || ref.Scheme != "" || ref.Host != "" || ref.User != nil || ref.Opaque != "" ||
- strings.HasPrefix(ref.Path, "/") || hasDotSegment(ref.Path) {
+ strings.HasPrefix(ref.Path, "/") || api.HasDotSegment(ref.Path) {
return base
}
out := base