Skip to content

refactor(snaps-rpc-methods)!: Use messenger for permitted methods#3987

Open
FrederikBolding wants to merge 17 commits intomainfrom
fb/refactor-permitted-methods
Open

refactor(snaps-rpc-methods)!: Use messenger for permitted methods#3987
FrederikBolding wants to merge 17 commits intomainfrom
fb/refactor-permitted-methods

Conversation

@FrederikBolding
Copy link
Copy Markdown
Member

@FrederikBolding FrederikBolding commented May 6, 2026

Migrate the permitted method handlers to use the messenger where applicable.

https://consensyssoftware.atlassian.net/browse/WPC-995


Note

Medium Risk
Refactors many permitted RPC method handlers to route permission checks and side effects through controller messenger.call actions, which could subtly change authorization, origin handling, or return types if any action wiring is incorrect.

Overview
Migrates permitted RPC method handlers in snaps-rpc-methods from hook-based implementations to messenger-driven controller actions, adding explicit actionNames and using messenger.call(...) for permission checks and operations like state access, cronjob/websocket management, UI interface operations, and Snap invocation.

Updates the permitted middleware/schema plumbing to source handlers from src/permitted/middleware.ts, adjusts public exports (dropping permittedMethods/selectHooks), and rewrites unit tests to inject origin via createOriginMiddleware plus MockControllerMessenger. Also includes small follow-ups: tighten snap_getFile example snap parsing with an assert, refresh the example manifest shasum, and bump Jest coverage thresholds.

Reviewed by Cursor Bugbot for commit 7bb3579. Bugbot is set up for automated code reviews on this repo. Configure here.

/**
* @returns All installed Snaps.
*/
getAllSnaps: () => Promise<GetSnapsResult>;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Turns out the types here have always been wrong

* file does not exist.
*/
export type GetFileResult = string;
export type GetFileResult = string | null;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This has always been possible, but the type was wrong.

@FrederikBolding FrederikBolding changed the base branch from main to fb/restricted-methods-messenger May 6, 2026 14:51
@FrederikBolding FrederikBolding force-pushed the fb/refactor-permitted-methods branch from 41732f0 to 84d1229 Compare May 6, 2026 14:53
@FrederikBolding FrederikBolding changed the title refactor(snaps-rpc-methods): Use messenger for permitted methods refactor(snaps-rpc-methods)!: Use messenger for permitted methods May 7, 2026
import { updateInterfaceHandler } from './updateInterface';

/* eslint-disable @typescript-eslint/naming-convention */
const methodHandlers = {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

TypeScript failed to generate declarations for this, but we don't actually need to export it, so I moved it here and stopped exporting.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.58%. Comparing base (0044932) to head (7bb3579).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3987   +/-   ##
=======================================
  Coverage   98.58%   98.58%           
=======================================
  Files         427      425    -2     
  Lines       12400    12360   -40     
  Branches     1948     1948           
=======================================
- Hits        12224    12185   -39     
+ Misses        176      175    -1     

☔ View full report in Codecov by Sentry.
📢 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.

@FrederikBolding FrederikBolding marked this pull request as ready for review May 8, 2026 07:59
@FrederikBolding FrederikBolding requested a review from a team as a code owner May 8, 2026 07:59
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 644d708. Configure here.

Comment thread packages/snaps-rpc-methods/src/permitted/listEntropySources.ts
Base automatically changed from fb/restricted-methods-messenger to main May 8, 2026 11:21
@FrederikBolding FrederikBolding force-pushed the fb/refactor-permitted-methods branch 2 times, most recently from 8d8aceb to 0e1747b Compare May 8, 2026 11:46
Comment thread packages/examples/packages/get-file/src/index.ts Outdated
Comment thread packages/snaps-rpc-methods/src/permitted/cancelBackgroundEvent.ts Outdated
Comment thread packages/snaps-rpc-methods/src/permitted/createInterface.test.tsx Outdated
const response = await engine.handle({
jsonrpc: '2.0',
id: 1,
method: 'snap_updateInterface',
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Some of the tests were not correctly nested under the describe() which results in some whitespace changes. Likely easier to review with whitespace off.

origin,
params.path,
params.encoding ?? AuxiliaryFileEncoding.Base64,
(params.encoding as AuxiliaryFileEncoding) ??
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How come this isn't typed properly anymore? Or was the hook not as strict?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The hook used InferredGetFileParams, which makes the enum a union: https://github.com/MetaMask/snaps-skunkworks/blob/fb/refactor-permitted-methods/packages/snaps-sdk/src/types/methods/get-file.ts#L25

The controller expects the union type.

hooks: PermittedRpcMethodHooks,
messenger: Messenger<string, PermittedRpcMethodActions>,
): JsonRpcMiddleware<JsonRpcParams, Json> {
// This is not actually a misused promise, the type is just wrong
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This comment still seems accurate, no?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Maybe? I don't think I understand why the linter thinks this is a misused promise.

Comment thread packages/snaps-rpc-methods/src/permitted/middleware.ts
Comment thread packages/snaps-rpc-methods/src/permitted/requestSnaps.ts Outdated
Comment thread packages/snaps-rpc-methods/src/permitted/requestSnaps.ts Outdated
* @param origin - The origin to set on the request.
* @returns The middleware.
*/
export function createOriginMiddleware(origin: string) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we use the one from json-rpc-engine?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, I added this before going back to add it to core. I have yet to release core though.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@FrederikBolding FrederikBolding force-pushed the fb/refactor-permitted-methods branch from 7bdac91 to 3d46203 Compare May 8, 2026 13:44
@FrederikBolding FrederikBolding requested a review from Mrtenz May 8, 2026 13:46
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.

2 participants