Skip to content

fix: propagate verified identity headers to remote MCP backends - #986

Open
roonakyadav wants to merge 3 commits into
google:mainfrom
roonakyadav:fix/971-mcp-identity-header-parity
Open

roonakyadav wants to merge 3 commits into
google:mainfrom
roonakyadav:fix/971-mcp-identity-header-parity

Conversation

@roonakyadav

Copy link
Copy Markdown
Contributor

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:

  1. /mesh/<peer>/mcp/<service>: Delivered over /libp2p-http, where the provider node stripped any caller-supplied X-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, and X-Mesh-Origin).
  2. call_remote_tool on /mcp: Delivered over /mesh/mcp/1.0.0 as a pass-through JSON-RPC stream, where the provider node dialed target_url using a fresh HTTP client without attaching verified identity headers.

Because of this mismatch, backend authors observing calls made via call_remote_tool saw 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:

  • Shared Identity Header Stamping (populateVerifiedIdentityHeaders): Extracted the header stamping logic into a shared, reusable helper in internal/node/service.go. Stamped headers include:
    • X-Peer-Id: Verified libp2p peer ID of the immediate caller.
    • X-Mesh-Origin: Stamped as mesh-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.
  • Caller Context Propagation: In WithBiscuitAuth (internal/node/middleware.go), once the caller's Biscuit is authenticated and verified, CallerClaims (*identity.VerifiedBiscuitClaims) and CallerBiscuit are populated on RequestContext.
  • Pass-Through Transport Context (backendTransportForStream): Updated MCPService.HandleStreamPassThrough to call backendTransportForStream(s, reqCtx) instead of backendTransport(). For HTTP target_url backends, target.clientWithHeaders(headers) is utilized to attach verified headers via mcpHeaderTransport.
  • Anti-Spoofing & Inbound Header Stripping: Any caller-supplied or existing X-Mesh-* or X-Peer-Id headers are strictly stripped on inbound requests and in mcpHeaderTransport before stamping verified attributes.
  • Non-HTTP Backends: Command/stdio backends (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://:

  • Loopback Destinations (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.
  • External / Remote Destinations: Strip X-Mesh-* and X-Peer-Id headers by default on both paths to prevent accidental leakage of internal roles, claims, or node metadata to third-party endpoints.
  • forward_context Configuration: Added forward_context (bool) to ServiceInfo in api/agentmesh.proto, ServiceConfig in api/policy.go, and static service configuration in agentmesh-node.yaml. Setting forward_context: true explicitly opts in to forwarding verified identity headers to remote target_url backends.

Testing

Added comprehensive unit and regression tests in internal/node/mcp_identity_headers_test.go and api/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 all X-Mesh-* and X-Peer-Id headers while preserving unrelated headers.
  • TestReverseProxy_PrivacyBoundary: Verifies loopback forwarding by default, external backend stripping by default, and forward_context: true behavior.
  • TestMCPService_BackendTransportForStream_PrivacyBoundary: Verifies transport header attachment for loopback backends, external backends (forward_context: false vs true), and probe calls.
  • TestMCPService_ParityBetweenLibp2pHTTPAndPassThrough: Verifies 1:1 header parity between /mesh/<peer>/mcp/<service> and call_remote_tool pass-through streams.
  • TestIsLoopbackHostAndURL: Verifies accurate loopback detection for IPv4, IPv6, and localhost.
  • Validated with make (builds cleanly), golangci-lint run -v (0 issues), deadcode check (clean), and full make test test suite (all tests passing).

Documentation

Updated site/content/docs/reference/node-api.md and site/content/docs/reference/node-config.md:

  • Documented that both /mesh/<peer>/mcp/<service> and call_remote_tool deliver verified identity headers to HTTP target_url backends.
  • Documented available identity headers, their formats, and how backend authors should verify them.
  • Documented safe privacy defaults, loopback behavior, and forward_context configuration.

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
@roonakyadav
roonakyadav requested a review from aojea as a code owner November 28, 2026 20:45

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +358 to +366
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
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
  1. 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)

Comment on lines +407 to +414
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
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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
  1. 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)

This branch has not been deployed

No deployments
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.

Backend receives X-Mesh-Actor-Node via /mesh/<peer>/mcp/<svc> but not via call_remote_tool (undocumented)

1 participant