Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Place permission middleware ahead of all method implementations #24472

Merged
merged 5 commits into from
May 31, 2024

Conversation

rekmarks
Copy link
Member

@rekmarks rekmarks commented May 10, 2024

Description

Adds all unrestricted RPC methods to the unrestrictedMethods array passed to the permission controller, and moves the permission middleware ahead of all RPC method implementations in setupProviderEngine. This forces us to add all methods that aren't permission to the unrestrictedMethods array, as a "method not found" error will be thrown otherwise. It's safer this way.

Due the requirement that it swallows authorization errors, the implementation of eth_accounts is moved to a new "legacy method middleware", which we by necessity place ahead of the permission middleware. In addition, the responsibility of rejecting intentionally unsupported RPC methods has been moved to a dedicated middleware in front of the legacy method middleware.

Open in GitHub Codespaces

Related issues

Fixes: #24331

Manual testing steps

  1. Go the test dapp
  2. Ensure that everything is working normally
  3. Repeat these steps for Snaps and MMI

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • I've completed the PR template to the best of my ability
  • I’ve included tests if applicable
  • I’ve documented my code using JSDoc format if applicable
  • I’ve applied the right labels on the PR (see labeling guidelines). Not required for external contributors.

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

@rekmarks rekmarks added team-snaps DEPRECATED: Use "team-snaps-platform" or "team-snaps-ecosystem" instead team-extension-platform team-mmi PRs from the MMI team labels May 10, 2024
@rekmarks rekmarks requested a review from a team as a code owner May 10, 2024 02:34
@metamaskbot metamaskbot added the INVALID-PR-TEMPLATE PR's body doesn't match template label May 10, 2024
@rekmarks rekmarks requested review from zone-live and a team and removed request for FrederikBolding and zone-live May 10, 2024 02:36
@rekmarks
Copy link
Member Author

@MetaMask/snaps-devs @MetaMask/mmi tagging you guys to confirm that your RPC methods continue to work as expected.

@rekmarks rekmarks force-pushed the rekmarks/reorder-permission-middleware branch 3 times, most recently from 760c9a6 to 9fb45c4 Compare May 10, 2024 18:38
@rekmarks rekmarks marked this pull request as draft May 10, 2024 19:20
@rekmarks

This comment was marked as resolved.

@metamaskbot
Copy link
Collaborator

Builds ready [653e514]
Page Load Metrics (2225 ± 753 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint804521417938
domContentLoaded9302366431
load65392422251569753
domInteractive9302356431
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 1.45 KiB (0.04%)
  • ui: 0 Bytes (0.00%)
  • common: 189 Bytes (0.00%)

Copy link
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@metamaskbot
Copy link
Collaborator

Builds ready [611dac9]
Page Load Metrics (1833 ± 699 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint621801022813
domContentLoaded9331673
load51357118331456699
domInteractive9321673
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 1.45 KiB (0.04%)
  • ui: 0 Bytes (0.00%)
  • common: 189 Bytes (0.00%)

Copy link

codecov bot commented May 14, 2024

Codecov Report

Attention: Patch coverage is 88.88889% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 67.48%. Comparing base (d27a233) to head (611dac9).
Report is 59 commits behind head on develop.

❗ Current head 611dac9 differs from pull request most recent head 9c346cd. Consider uploading reports for the commit 9c346cd to get more accurate results

Files Patch % Lines
app/scripts/metamask-controller.js 80.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #24472      +/-   ##
===========================================
+ Coverage    67.37%   67.48%   +0.11%     
===========================================
  Files         1278     1289      +11     
  Lines        49881    50168     +287     
  Branches     12944    13015      +71     
===========================================
+ Hits         33605    33853     +248     
- Misses       16276    16315      +39     

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

@rekmarks rekmarks marked this pull request as ready for review May 14, 2024 14:41
@rekmarks rekmarks marked this pull request as draft May 14, 2024 15:16
@rekmarks rekmarks force-pushed the rekmarks/reorder-permission-middleware branch from 9c346cd to d3f1a0a Compare May 14, 2024 16:12
Comment on lines +5041 to +5096
// If the origin is not in the selectedNetworkController's `domains` state
// when the provider engine is created, the selectedNetworkController will
// fetch the globally selected networkClient from the networkController and wrap
// it in a proxy which can be switched to use its own state if/when the origin
// is added to the `domains` state
const proxyClient =
this.selectedNetworkController.getProviderAndBlockTracker(origin);
Copy link
Member Author

@rekmarks rekmarks May 24, 2024

Choose a reason for hiding this comment

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

I moved this declaration closer to where it is used. I do not see how it could result in a behavioral change.

Copy link
Contributor

Choose a reason for hiding this comment

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

The thing one could imagine would be if something further up has been relying on specific error being thrown from getProviderAndBlockTracker under certain conditions, but sounds far-fetched and move makes sense to me.

@metamaskbot
Copy link
Collaborator

Builds ready [91dc4ea]
Page Load Metrics (991 ± 545 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint61174932813
domContentLoaded95316115
load5025869911135545
domInteractive95316115
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 1.17 KiB (0.03%)
  • ui: 0 Bytes (0.00%)
  • common: 460 Bytes (0.01%)

@rekmarks

This comment was marked as resolved.

@danjm

This comment was marked as resolved.

@mcmire mcmire self-requested a review May 28, 2024 15:55
@metamaskbot
Copy link
Collaborator

Builds ready [8f971bf]
Page Load Metrics (1231 ± 610 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint762031173416
domContentLoaded107019168
load64289012311270610
domInteractive107019168
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 1.17 KiB (0.03%)
  • ui: 0 Bytes (0.00%)
  • common: 460 Bytes (0.01%)

@rekmarks rekmarks force-pushed the rekmarks/reorder-permission-middleware branch 2 times, most recently from 58a0b80 to 3175d64 Compare May 29, 2024 03:55
@metamaskbot
Copy link
Collaborator

Builds ready [3175d64]
Page Load Metrics (663 ± 491 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint76218993115
domContentLoaded9191131
load6627876631023491
domInteractive9191131
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 1.17 KiB (0.03%)
  • ui: 0 Bytes (0.00%)
  • common: 460 Bytes (0.01%)

@rekmarks rekmarks requested a review from legobeat May 29, 2024 16:56
@metamaskbot
Copy link
Collaborator

Builds ready [782af6c]
Page Load Metrics (745 ± 493 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint65137952110
domContentLoaded94919147
load5724197451026493
domInteractive94919147
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 1.17 KiB (0.04%)
  • ui: 0 Bytes (0.00%)
  • common: 460 Bytes (0.01%)

@@ -35,4 +34,4 @@ const handlers = [
///: END:ONLY_INCLUDE_IF
];

export default handlers;
export const legacyHandlers = [ethAccounts];
Copy link
Contributor

@adonesky1 adonesky1 May 30, 2024

Choose a reason for hiding this comment

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

not a huge deal but wondering if "legacy" is the right framing here? It seems like the main reason this is separated out is that you need to be able to call this method and receive an empty array if there is no authorization? Perhaps that is a legacy pattern in that we don't want to it to be reused but legacyHandlers implies to me that this method or handler is set for deprecation

Copy link
Contributor

@adonesky1 adonesky1 May 30, 2024

Choose a reason for hiding this comment

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

Perhaps that is a legacy pattern in that we don't want to it to be reused

on second thought, maybe this is the proper use of legacy... 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, well, our implementation of eth_accounts is using a legacy pattern that we should never have cause to return to. However, it falls under a more general category of "middleware that do stuff to requests before the permission system". We could be establishing a pattern for that, but that also includes almost every other middleware in front of the permission middleware...

No, I think legacy is our best bet. We shouldn't do this!

@rekmarks rekmarks force-pushed the rekmarks/reorder-permission-middleware branch from 782af6c to 03017e7 Compare May 30, 2024 18:42
@rekmarks rekmarks requested a review from adonesky1 May 30, 2024 18:42
Copy link
Contributor

@adonesky1 adonesky1 left a comment

Choose a reason for hiding this comment

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

Looks good to me!

Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Description of problem makes sense as does the solution. LGTM 👍🏻

@rekmarks rekmarks merged commit 46356f6 into develop May 31, 2024
75 of 77 checks passed
@rekmarks rekmarks deleted the rekmarks/reorder-permission-middleware branch May 31, 2024 05:42
@github-actions github-actions bot locked and limited conversation to collaborators May 31, 2024
@metamaskbot
Copy link
Collaborator

Builds ready [7506f83]
Page Load Metrics (294 ± 347 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint712181022914
domContentLoaded10371463
load472949294722347
domInteractive10371463

@gauthierpetetin gauthierpetetin added the release-12.0.0 Issue or pull request that will be included in release 12.0.0 label Jun 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
INVALID-PR-TEMPLATE PR's body doesn't match template release-12.0.0 Issue or pull request that will be included in release 12.0.0 team-extension-platform team-mmi PRs from the MMI team team-snaps DEPRECATED: Use "team-snaps-platform" or "team-snaps-ecosystem" instead
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Stop implementing RPC methods ahead of the permission middleware
7 participants