Repository navigation
fix: propagate verified identity headers to remote MCP backends - #986
roonakyadav wants to merge 3 commits into
Conversation
Propagate verified caller identity headers (X-Mesh-Actor-Node, X-Mesh-Principal, X-Mesh-Roles, X-Mesh-Claims, X-Mesh-Labels, X-Mesh-Task, X-Peer-Id, and X-Mesh-Origin) to HTTP target_url backends consistently across both /mesh/<peer>/mcp/<service> and call_remote_tool (/mesh/mcp/1.0.0). Enforce safe-default privacy boundary: - Loopback destinations (127.0.0.1, localhost, ::1) receive verified identity headers automatically by default. - Non-loopback (external) destinations strip X-Mesh-* and X-Peer-Id headers by default unless forward_context: true is configured on the service. - Inbound caller-supplied forged headers are stripped before stamping verified attributes. - Stdio/command backends are unaffected. Closes google#971
There was a problem hiding this comment.
Code Review
This pull request introduces verified identity header forwarding (such as X-Mesh-Principal, X-Mesh-Roles, etc.) to published service backends (target_url) for both HTTP proxy and MCP stream pass-through paths. To protect privacy, headers are stripped by default for non-loopback destinations unless forward_context is explicitly enabled. The changes include new configuration options, helper functions, documentation, and extensive test coverage. The review feedback suggests strengthening the test assertions in mcp_identity_headers_test.go by explicitly verifying the expected HTTP client and transport types instead of using loose conditional checks, ensuring that the tests do not pass silently if the underlying implementation changes.
| st := transport.(*mcp.StreamableClientTransport) | ||
| // Point transport to mock roundtripper | ||
| client := &http.Client{Transport: mockTransport} | ||
| if st.HTTPClient != nil && st.HTTPClient.Transport != nil { | ||
| if ht, ok := st.HTTPClient.Transport.(mcpHeaderTransport); ok { | ||
| ht.base = mockTransport | ||
| client.Transport = ht | ||
| } | ||
| } |
There was a problem hiding this comment.
Section 3: Test coverage must follow the pyramid
The test currently uses a loose conditional check to mock the transport. Since forward_context is false for an external backend, we expect st.HTTPClient to be http.DefaultClient (with a nil transport). We should explicitly assert this expectation to avoid silent test passes if the implementation changes.
st := transport.(*mcp.StreamableClientTransport)
if st.HTTPClient != http.DefaultClient {
t.Fatalf("expected HTTPClient to be http.DefaultClient, got %v", st.HTTPClient)
}
client := &http.Client{Transport: mockTransport}References
- Section 3: Test coverage must follow the pyramid. Tests MUST NOT shallow errors, those need to be handled and fail the test. Flag a test that logs an error and continues, or that ignores a returned error on the path under test: the error must fail the test. (link)
| st := transport.(*mcp.StreamableClientTransport) | ||
| client := &http.Client{Transport: mockTransport} | ||
| if st.HTTPClient != nil && st.HTTPClient.Transport != nil { | ||
| if ht, ok := st.HTTPClient.Transport.(mcpHeaderTransport); ok { | ||
| ht.base = mockTransport | ||
| client.Transport = ht | ||
| } | ||
| } |
There was a problem hiding this comment.
Section 3: Test coverage must follow the pyramid
The test currently uses a loose conditional check to mock the transport. Since forward_context is true for this external backend, we expect st.HTTPClient.Transport to be of type mcpHeaderTransport. We should explicitly assert this expectation to avoid silent test passes if the implementation changes.
| st := transport.(*mcp.StreamableClientTransport) | |
| client := &http.Client{Transport: mockTransport} | |
| if st.HTTPClient != nil && st.HTTPClient.Transport != nil { | |
| if ht, ok := st.HTTPClient.Transport.(mcpHeaderTransport); ok { | |
| ht.base = mockTransport | |
| client.Transport = ht | |
| } | |
| } | |
| st := transport.(*mcp.StreamableClientTransport) | |
| if st.HTTPClient == nil || st.HTTPClient.Transport == nil { | |
| t.Fatalf("expected non-nil HTTPClient and Transport") | |
| } | |
| ht, ok := st.HTTPClient.Transport.(mcpHeaderTransport) | |
| if !ok { | |
| t.Fatalf("expected Transport to be mcpHeaderTransport, got %T", st.HTTPClient.Transport) | |
| } | |
| ht.base = mockTransport | |
| client := &http.Client{Transport: ht} |
References
- Section 3: Test coverage must follow the pyramid. Tests MUST NOT shallow errors, those need to be handled and fail the test. Flag a test that logs an error and continues, or that ignores a returned error on the path under test: the error must fail the test. (link)
Closes #971
Problem & Background
As reported in #971, there was an unintended mismatch between two invocation paths targeting an MCP service with an HTTP
target_url:/mesh/<peer>/mcp/<service>: Delivered over/libp2p-http, where the provider node stripped any caller-suppliedX-Mesh-*headers and stamped verified identity headers (X-Mesh-Actor-Node,X-Mesh-Principal,X-Mesh-Roles,X-Mesh-Claims,X-Mesh-Labels,X-Mesh-Task,X-Peer-Id, andX-Mesh-Origin).call_remote_toolon/mcp: Delivered over/mesh/mcp/1.0.0as a pass-through JSON-RPC stream, where the provider node dialedtarget_urlusing a fresh HTTP client without attaching verified identity headers.Because of this mismatch, backend authors observing calls made via
call_remote_toolsaw no identity context, which could lead them to believe caller identity is unavailable and incentivize self-declared sender names.Implementation & Architecture
This change establishes behavioral parity across both invocation paths while maintaining strict Zero Trust and anti-spoofing guarantees:
populateVerifiedIdentityHeaders): Extracted the header stamping logic into a shared, reusable helper ininternal/node/service.go. Stamped headers include:X-Peer-Id: Verified libp2p peer ID of the immediate caller.X-Mesh-Origin: Stamped asmesh-remote.X-Mesh-Actor-Node: Verified actor node peer ID (falling back to node peer ID or caller peer ID).X-Mesh-Principal: Verified primary principal (email, user, or peer ID).X-Mesh-Roles: Comma-separated verified mesh roles.X-Mesh-Claims: Verified custom claims formatted as key=value pairs.X-Mesh-Labels: Verified node labels formatted as key=value pairs.X-Mesh-Task: Innermost task rule name for task-attenuated tokens.WithBiscuitAuth(internal/node/middleware.go), once the caller's Biscuit is authenticated and verified,CallerClaims(*identity.VerifiedBiscuitClaims) andCallerBiscuitare populated onRequestContext.backendTransportForStream): UpdatedMCPService.HandleStreamPassThroughto callbackendTransportForStream(s, reqCtx)instead ofbackendTransport(). For HTTPtarget_urlbackends,target.clientWithHeaders(headers)is utilized to attach verified headers viamcpHeaderTransport.X-Mesh-*orX-Peer-Idheaders are strictly stripped on inbound requests and inmcpHeaderTransportbefore stamping verified attributes.command) run as local subprocesses and are completely unaffected.Privacy Model: Loopback vs External Destinations
In line with the privacy boundary discussed in #971 and established for
egress://:127.0.0.1,localhost,::1): Automatically receive verified identity headers by default on both paths, as local services co-located on the node are part of the local trust domain.X-Mesh-*andX-Peer-Idheaders by default on both paths to prevent accidental leakage of internal roles, claims, or node metadata to third-party endpoints.forward_contextConfiguration: Addedforward_context(bool) toServiceInfoinapi/agentmesh.proto,ServiceConfiginapi/policy.go, and static service configuration inagentmesh-node.yaml. Settingforward_context: trueexplicitly opts in to forwarding verified identity headers to remotetarget_urlbackends.Testing
Added comprehensive unit and regression tests in
internal/node/mcp_identity_headers_test.goandapi/trust_test.go:TestPopulateVerifiedIdentityHeaders: Validates header population with full claims, missing actorNode fallback, and safe handling of nil claims.TestStripMeshIdentityHeaders: Validates case-insensitive stripping of allX-Mesh-*andX-Peer-Idheaders while preserving unrelated headers.TestReverseProxy_PrivacyBoundary: Verifies loopback forwarding by default, external backend stripping by default, andforward_context: truebehavior.TestMCPService_BackendTransportForStream_PrivacyBoundary: Verifies transport header attachment for loopback backends, external backends (forward_context: falsevstrue), and probe calls.TestMCPService_ParityBetweenLibp2pHTTPAndPassThrough: Verifies 1:1 header parity between/mesh/<peer>/mcp/<service>andcall_remote_toolpass-through streams.TestIsLoopbackHostAndURL: Verifies accurate loopback detection for IPv4, IPv6, and localhost.make(builds cleanly),golangci-lint run -v(0 issues), deadcode check (clean), and fullmake testtest suite (all tests passing).Documentation
Updated
site/content/docs/reference/node-api.mdandsite/content/docs/reference/node-config.md:/mesh/<peer>/mcp/<service>andcall_remote_tooldeliver verified identity headers to HTTPtarget_urlbackends.forward_contextconfiguration.