Skip to content

feat: Let a Snap's backend requests carry the caller's trace headers - #4183

Draft
MajorLift wants to merge 3 commits into
mainfrom
jongsun/feat/snap-network-trace-headers
Draft

MajorLift wants to merge 3 commits into
mainfrom
jongsun/feat/snap-network-trace-headers

Conversation

@MajorLift

Copy link
Copy Markdown
Contributor

Changelog

  • @metamask/snaps-utils: Add optional traceContext to SnapRpcHookArgs, with TraceContextStruct and TraceContext (a W3C traceparent, optional baggage, and a required non-empty list of exact origins)
  • @metamask/snaps-execution-environments: Attach a request's traceparent and baggage to the Snap's fetch requests to the listed origins while it is the Snap's only request in flight; allow URL for @metamask/snaps-utils in the LavaMoat policies
  • @metamask/snaps-controllers: Add optional traceContext to SnapController:handleRequest and ExecutionService:handleRpcRequest

Summary

A Snap's backend requests carry no trace headers, so the backend's spans join no client trace and the operation that called the Snap cannot be followed to the backend. With this change the caller can pass its trace context with a request, and the Snap's fetch attaches it.

  • Only origins the caller lists get the headers. traceparent and baggage are not CORS-safelisted, so sending them to any other server would add a preflight it may reject.
  • Only while the Snap has one request in flight. One network endowment serves every request, so a fetch made while requests overlap cannot be tied to one of them, and nothing is attached.
  • Headers the Snap sets itself are kept. No trace id or sampling decision is made here; both come from the caller.
  • Mixed versions: an executor without this change rejects a request carrying traceContext, since its request struct rejects unknown keys. The controller sends the field only when a caller passes one, so callers that pass none are unaffected. A caller that does must upgrade @metamask/snaps-controllers and @metamask/snaps-execution-environments together.

Related: MWPE-816 (Make backend requests from snaps carry trace headers)

Test plan

  • yarn workspace @metamask/snaps-execution-environments test: the network endowment tests cover a listed origin, an unlisted origin, overlapping requests and Snap-set headers; the unlisted-origin cases fail with the origin check removed
  • yarn workspace @metamask/snaps-controllers test: end to end through the Node.js thread executor to a local HTTP server
  • yarn workspace @metamask/snaps-utils test

@codecov

codecov Bot commented Nov 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.71%. Comparing base (20c55c4) to head (6ac75a3).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4183      +/-   ##
==========================================
+ Coverage   98.69%   98.71%   +0.02%     
==========================================
  Files         429      430       +1     
  Lines       12533    12583      +50     
  Branches     1986     2001      +15     
==========================================
+ Hits        12369    12421      +52     
+ Misses        164      162       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

1 participant