Repository navigation
identity: one authorizer assembly for every policy enforcement point - #839
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces external HTTP ingress capabilities to the Agent Mesh router and node, allowing services to be exposed externally via edge (zero-trust, Biscuit-verified) or passthrough modes. It implements the /mesh/ingress/1.0.0 protocol, adds ingress routing and rate-limiting to the router, and integrates these features into the standalone server, database storage, and Helm charts. Feedback on the changes highlights two high-severity security issues: a potential Denial of Service vulnerability due to a missing timeout in the router's stream dialer, and a potential Path Traversal vulnerability on Windows hosts in the path normalization logic.
| DialContext: func(ctx context.Context, _, addr string) (net.Conn, error) { | ||
| peerStr := addr | ||
| if h, _, err := net.SplitHostPort(addr); err == nil { | ||
| peerStr = h | ||
| } | ||
| pid, err := peer.Decode(peerStr) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid target peer %q: %w", peerStr, err) | ||
| } | ||
| 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) | ||
| }, |
There was a problem hiding this comment.
The DialContext function in http.Transport does not enforce a timeout when calling gostream.Dial. Since standard incoming HTTP requests do not have a default context deadline, an unresponsive peer or hung stream negotiation could cause the connection attempt to block indefinitely. This can lead to goroutine leaks and potential denial of service. Enforce a timeout using context.WithTimeout with ingressDialTimeout.
| DialContext: func(ctx context.Context, _, addr string) (net.Conn, error) { | |
| peerStr := addr | |
| if h, _, err := net.SplitHostPort(addr); err == nil { | |
| peerStr = h | |
| } | |
| pid, err := peer.Decode(peerStr) | |
| if err != nil { | |
| return nil, fmt.Errorf("invalid target peer %q: %w", peerStr, err) | |
| } | |
| 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) | |
| }, | |
| DialContext: func(ctx context.Context, _, addr string) (net.Conn, error) { | |
| peerStr := addr | |
| if h, _, err := net.SplitHostPort(addr); err == nil { | |
| peerStr = h | |
| } | |
| pid, err := peer.Decode(peerStr) | |
| if err != nil { | |
| return nil, fmt.Errorf("invalid target peer %q: %w", peerStr, err) | |
| } | |
| if r.gated == nil { | |
| return nil, errors.New("router host is not initialized") | |
| } | |
| dialCtx, cancel := context.WithTimeout(ctx, ingressDialTimeout) | |
| defer cancel() | |
| return gostream.Dial(network.WithAllowLimitedConn(dialCtx, "ingress"), r.gated, pid, api.IngressProtocolID) | |
| }, |
There was a problem hiding this comment.
Done in ae660d4: the dial is bounded by ingressDialTimeout (same bound the provider handshake before it already had); the stream outlives the dial context.
| func hasDotSegment(p string) bool { | ||
| for _, seg := range strings.Split(p, "/") { | ||
| if seg == "." || seg == ".." { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } |
There was a problem hiding this comment.
The hasDotSegment function only splits the path on forward slashes (/). If the backend service is running on a Windows host, backslashes (\) may be interpreted as path separators. An attacker could bypass the dot-segment check by sending percent-encoded backslashes (e.g., %5C..%5C), potentially leading to directory traversal. Normalize backslashes to forward slashes before checking for dot segments.
| func hasDotSegment(p string) bool { | |
| for _, seg := range strings.Split(p, "/") { | |
| if seg == "." || seg == ".." { | |
| return true | |
| } | |
| } | |
| return false | |
| } | |
| func hasDotSegment(p string) bool { | |
| normalized := strings.ReplaceAll(p, "\\", "/") | |
| for _, seg := range strings.Split(normalized, "/") { | |
| if seg == "." || seg == ".." { | |
| return true | |
| } | |
| } | |
| return false | |
| } |
There was a problem hiding this comment.
Done in ae660d4, and consolidated: hasDotSegment existed in both the router and the node; api.HasDotSegment is now the single copy, normalizes \ before splitting, and both binaries call it. TestHasDotSegment gained the backslash cases.
The node's in-mesh verifier, the node's external ingress handler and the control plane's /sts/token each assembled their Datalog authorizer by hand: the service fact, the HTTP and egress facts, the replay binding, the target facts and check, the local attenuation, the baseline policies and rules and the mesh rules, in three copies. They matched, but nothing made them match: a rule added to one path reached the others only if someone remembered. One difference had already crept in. The ingress path took an EnforceTargetCheck knob and left BaselineTargetCheck out when the node had no identity, where the in-mesh path always added it and admitted only an unrestricted grant. identity.AuthorizeMeshRequest is now the only place the authorizer is assembled. MeshRequest says what the request is: the service, the HTTP line when there is one, the egress destination when there is one, the connection peer when there is a libp2p stream to bind client_peer_id to, and whether targets are unrestricted (a local caller, or the control plane with no provider credential). MeshPolicy says what the enforcement point adds: its own target facts, its local attenuation and the synced mesh rules. BaselineTargetCheck, the baseline policies and rules and expiration() are always added, so a path cannot leave them out, and every absent field means not present, never unrestricted. MeshRequest.TaskRequest derives the TaskAuthorizationRule context from the same value. The node builds MeshPolicy once (meshPolicy) for both its verifier and its ingress handler; injectIdentityFacts goes, the target facts come from TargetFactsFromBiscuit as on the router. IngressEvalParams keeps its fields and loses EnforceTargetCheck. The STS egress check keeps its destination rules as the PEP's local rules and the policy rules as mesh rules. TestAuthorizeMeshRequest fixes what each MeshRequest field contributes and that each fails closed when absent: an HTTP-narrowed grant on a bare stream, a node-scoped grant on a provider without target facts, a connection peer other than client_peer_id. TestIngressEvalParamsMatchMeshRequest runs the ingress evaluator and the shared assembly on the same credential, service and policy and requires the same decision. The guide now says that an agent on the mesh calls peers through its sidecar and that the ingress URL is neither a second route for it nor one that takes its node credential; the security architecture describes the single assembly.
0bbf7ef to
4a9e80f
Compare
…separator The router's ingress transport dialed the provider on the inbound request's context, which carries no deadline: a provider that accepted the connection and then never finished stream negotiation held the goroutine for as long as the client waited. The dial is now bounded by ingressDialTimeout, as the provider handshake before it already was; the stream outlives that context. hasDotSegment existed twice, in the router and in the node, and both split on "/" only. A path segment reaches the backend percent-encoded, so a backend on a platform that treats "\" as "/" would resolve "..\" as a traversal the check never saw. api.HasDotSegment is now the one copy, it normalizes backslashes before splitting, and both binaries call it.
Test identities used a real domain and real first names. They are now the alice, bob, carol and dave personas at example.com, like the rest of the suite. No assertion changes meaning: the one negative case keyed on a different local part, not on the domain.
Follow-up to #838 (merged). Rebased onto
mainatc5562280. Three commits: the authorizer consolidation, the two review items on the merged ingress code (bounded ingress dial;api.HasDotSegmentas the single dot-segment check, backslash-aware), and a scrub of test identities toexample.compersonas.Summary
The node's in-mesh verifier (
verifyBiscuitTokenWithRules), the node's external ingress handler, and the control plane's/sts/tokeneach assembled their Datalog authorizer by hand: service fact, HTTP and egress facts, replay binding, target facts and check, local attenuation, baseline policies and rules, mesh rules — three copies. They matched by discipline, not by construction, and one divergence had already crept in: the ingress path took anEnforceTargetCheckknob and skippedBaselineTargetCheckwhen the node had no identity, where the in-mesh path always added it.identity.AuthorizeMeshRequest(authorizer, MeshRequest, MeshPolicy)is now the only place a mesh authorizer is assembled.MeshRequestsays what the request is:service();method()/path()only whenHasHTTP;host()/port()only whenHasEgress;connection_peer_id()+BaselineReplayCheckonly whenConnectionPeeris set;target_unrestrictedonly whenUnrestricted. Every absent field means "not present" and fails closed.MeshPolicysays what the PEP adds: its own target facts, local attenuation, synced mesh rules.BaselineTargetCheck, baseline policies/rules/HTTP rules,expiration().MeshRequest.TaskRequest()derives theTaskAuthorizationRulecontext from the same value.Callers
internal/node/middleware.go)injectIdentityFactsreplaced byMeshRequest{ConnectionPeer, Unrestricted: Local, …}+n.meshPolicy()internal/node/ingress.go)n.meshPolicy();EnforceTargetCheckremoved, target check now unconditionalinternal/router/ingress.go)IngressEvalParamskeeps its fields and delegates to the shared assembly/sts/token(internal/controlplane/sts.go)MeshRequest{egress, Unrestricted, ConnectionPeer: clientPeer};stsDestinationRulesasLocalRulesProduction code is net +27 lines (
+305/−278); the rest is tests and docs.Tests
TestAuthorizeMeshRequest— 11 table cases pinning what eachMeshRequestfield contributes and that each fails closed when absent (HTTP-narrowed grant on a bare stream, node-scoped grant without provider target facts, replay from a different connection peer, border bearer without connection peer, local deny).TestIngressEvalParamsMatchMeshRequest— runs the ingress evaluator and the shared assembly on identical inputs and requires the same decision: the structural "no leak between paths" guard.Docs
external-ingress.md: new section "Ingress is for callers without a sidecar" — an agent on the mesh calls peers through its sidecar; the ingress URL is neither a second route for it nor one that accepts its node credential; same policy on both paths, no privilege by path choice.security-architecture.md: "One authorizer assembly for every PEP".Validation
On the rebased branch:
make,go vet,make lint(0 findings),-raceunit tests forapi,identity,node,router,controlplane,envoy,standalone, and the fulltests/integrationsuite serially (all pass).