Commit f941345
refactor(permission-controller)!: decouple permission middleware via messenger actions (MetaMask#8532)
Advances MetaMask#4238
Reverts MetaMask#8502
- Rewrites `permission-middleware.ts` as a standalone
`createPermissionMiddleware({ messenger, subject })` factory that
dispatches through the `PermissionController:executeRestrictedMethod`
and `PermissionController:hasUnrestrictedMethod` messenger actions
instead of bound controller hooks. Removes the
`createPermissionMiddleware` property from `PermissionController`.
- Adds `createPermissionMiddlewareV2`, a `JsonRpcEngineV2` variant of
the same factory. Consumers using the legacy `JsonRpcEngine` can
continue to use the now-deprecated `createPermissionMiddleware`; new
integrations should prefer V2.
- Exposes `hasUnrestrictedMethod` as a public method / messenger action,
and makes `getRestrictedMethod` `#`-private (it has no remaining
external consumers now that the middleware goes through the messenger).
- When a restricted method returns `undefined`, the middleware now
propagates the plain `Error` thrown by `executeRestrictedMethod`; the
JSON-RPC engine serializes it as a standard internal error response
instead of a custom `internalError` with a `request` data payload.
- If properly typed, it's impossible for restricted methods to return
`undefined`. We nevertheless retain this check to minimize changes.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **High Risk**
> High risk because it introduces a breaking API change by removing
`PermissionController.createPermissionMiddleware`/`getRestrictedMethod`
and rerouting enforcement through new messenger actions and a new
`JsonRpcEngineV2` middleware, which could affect all RPC permissioning
integrations.
>
> **Overview**
> **Decouples permission enforcement middleware from
`PermissionController`.** The `json-rpc-engine` permission middleware is
removed from the controller and replaced with standalone exports
`createPermissionMiddleware` (legacy, *deprecated*) and
`createPermissionMiddlewareV2` (for `JsonRpcEngineV2`) that dispatch
permission checks/execution via messenger actions.
>
> **Expands and reshapes the controller’s public surface via
messenger.** Adds `PermissionController:hasUnrestrictedMethod` and
`PermissionController:executeRestrictedMethod` action types/handlers,
makes `getRestrictedMethod` private, and updates error behavior when
restricted methods return no result (now a plain `Error`, which engines
serialize as internal errors). Documentation and tests are updated to
use the new middleware factories and the `JsonRpcEngineV2` path.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
257feaa. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>1 parent 5801bc0 commit f941345
7 files changed
Lines changed: 622 additions & 219 deletions
File tree
- packages/permission-controller
- src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
326 | 326 | | |
327 | 327 | | |
328 | 328 | | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
329 | 336 | | |
330 | 337 | | |
331 | 338 | | |
332 | 339 | | |
333 | 340 | | |
334 | 341 | | |
335 | 342 | | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
341 | 352 | | |
342 | 353 | | |
343 | 354 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
16 | 20 | | |
17 | 21 | | |
18 | 22 | | |
19 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
20 | 28 | | |
21 | 29 | | |
22 | 30 | | |
| |||
186 | 194 | | |
187 | 195 | | |
188 | 196 | | |
189 | | - | |
| 197 | + | |
190 | 198 | | |
191 | 199 | | |
192 | 200 | | |
| |||
Lines changed: 48 additions & 27 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
10 | 16 | | |
11 | | - | |
12 | | - | |
13 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
14 | 20 | | |
15 | 21 | | |
16 | 22 | | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
| 23 | + | |
34 | 24 | | |
35 | | - | |
36 | | - | |
37 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
38 | 28 | | |
39 | 29 | | |
40 | 30 | | |
| |||
86 | 76 | | |
87 | 77 | | |
88 | 78 | | |
89 | | - | |
| 79 | + | |
90 | 80 | | |
91 | 81 | | |
92 | 82 | | |
| |||
293 | 283 | | |
294 | 284 | | |
295 | 285 | | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
296 | 316 | | |
297 | 317 | | |
298 | 318 | | |
299 | 319 | | |
| 320 | + | |
300 | 321 | | |
301 | | - | |
302 | 322 | | |
303 | 323 | | |
304 | 324 | | |
| |||
312 | 332 | | |
313 | 333 | | |
314 | 334 | | |
315 | | - | |
| 335 | + | |
| 336 | + | |
0 commit comments