@metamask/permission-controller 14.1.0 → 15.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@ export { createRestrictedMethodMessenger } from './createRestrictedMethodMesseng
3
3
  export * from './errors.js';
4
4
  export * from './Permission.js';
5
5
  export * from './PermissionController.js';
6
- export type { PermissionControllerClearStateAction, PermissionControllerExecuteRestrictedMethodAction, PermissionControllerGetCaveatAction, PermissionControllerGetEndowmentsAction, PermissionControllerGetPermissionsAction, PermissionControllerGetSubjectNamesAction, PermissionControllerGrantPermissionsAction, PermissionControllerGrantPermissionsIncrementalAction, PermissionControllerHasPermissionAction, PermissionControllerHasPermissionsAction, PermissionControllerHasUnrestrictedMethodAction, PermissionControllerRequestPermissionsAction, PermissionControllerRequestPermissionsIncrementalAction, PermissionControllerRevokeAllPermissionsAction, PermissionControllerRevokePermissionForAllSubjectsAction, PermissionControllerRevokePermissionsAction, PermissionControllerUpdateCaveatAction, PermissionControllerGetPermissionAction, PermissionControllerRevokePermissionAction, PermissionControllerUpdatePermissionsByCaveatAction, PermissionControllerAcceptPermissionsRequestAction, PermissionControllerRejectPermissionsRequestAction, } from './PermissionController-method-action-types.js';
6
+ export type { PermissionControllerClearStateAction, PermissionControllerExecuteRestrictedMethodAction, PermissionControllerGetCaveatAction, PermissionControllerGetEndowmentsGlobalsAction, PermissionControllerGetPermissionsAction, PermissionControllerGetSubjectNamesAction, PermissionControllerGrantPermissionsAction, PermissionControllerGrantPermissionsIncrementalAction, PermissionControllerHasPermissionAction, PermissionControllerHasPermissionsAction, PermissionControllerHasUnrestrictedMethodAction, PermissionControllerRequestPermissionsAction, PermissionControllerRequestPermissionsIncrementalAction, PermissionControllerRevokeAllPermissionsAction, PermissionControllerRevokePermissionForAllSubjectsAction, PermissionControllerRevokePermissionsAction, PermissionControllerUpdateCaveatAction, PermissionControllerGetPermissionAction, PermissionControllerRevokePermissionAction, PermissionControllerUpdatePermissionsByCaveatAction, PermissionControllerAcceptPermissionsRequestAction, PermissionControllerRejectPermissionsRequestAction, } from './PermissionController-method-action-types.js';
7
7
  export { createPermissionMiddleware, createPermissionMiddlewareV2, type PermissionMiddlewareActions, } from './permission-middleware.js';
8
8
  export type { ExtractSpecifications } from './utils.js';
9
9
  export { MethodNames } from './utils.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,OAAO,EAAE,+BAA+B,EAAE,MAAM,sCAAsC,CAAC;AACvF,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,2BAA2B,CAAC;AAC1C,YAAY,EACV,oCAAoC,EACpC,iDAAiD,EACjD,mCAAmC,EACnC,uCAAuC,EACvC,wCAAwC,EACxC,yCAAyC,EACzC,0CAA0C,EAC1C,qDAAqD,EACrD,uCAAuC,EACvC,wCAAwC,EACxC,+CAA+C,EAC/C,4CAA4C,EAC5C,uDAAuD,EACvD,8CAA8C,EAC9C,wDAAwD,EACxD,2CAA2C,EAC3C,sCAAsC,EACtC,uCAAuC,EACvC,0CAA0C,EAC1C,mDAAmD,EACnD,kDAAkD,EAClD,kDAAkD,GACnD,MAAM,+CAA+C,CAAC;AACvD,OAAO,EACL,0BAA0B,EAC1B,4BAA4B,EAC5B,KAAK,2BAA2B,GACjC,MAAM,4BAA4B,CAAC;AACpC,YAAY,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,cAAc,gCAAgC,CAAC;AAC/C,YAAY,EACV,yCAAyC,EACzC,iDAAiD,EACjD,iDAAiD,EACjD,gDAAgD,GACjD,MAAM,oDAAoD,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,OAAO,EAAE,+BAA+B,EAAE,MAAM,sCAAsC,CAAC;AACvF,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,2BAA2B,CAAC;AAC1C,YAAY,EACV,oCAAoC,EACpC,iDAAiD,EACjD,mCAAmC,EACnC,8CAA8C,EAC9C,wCAAwC,EACxC,yCAAyC,EACzC,0CAA0C,EAC1C,qDAAqD,EACrD,uCAAuC,EACvC,wCAAwC,EACxC,+CAA+C,EAC/C,4CAA4C,EAC5C,uDAAuD,EACvD,8CAA8C,EAC9C,wDAAwD,EACxD,2CAA2C,EAC3C,sCAAsC,EACtC,uCAAuC,EACvC,0CAA0C,EAC1C,mDAAmD,EACnD,kDAAkD,EAClD,kDAAkD,GACnD,MAAM,+CAA+C,CAAC;AACvD,OAAO,EACL,0BAA0B,EAC1B,4BAA4B,EAC5B,KAAK,2BAA2B,GACjC,MAAM,4BAA4B,CAAC;AACpC,YAAY,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,cAAc,gCAAgC,CAAC;AAC/C,YAAY,EACV,yCAAyC,EACzC,iDAAiD,EACjD,iDAAiD,EACjD,gDAAgD,GACjD,MAAM,oDAAoD,CAAC"}
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,OAAO,EAAE,+BAA+B,EAAE,MAAM,sCAAsC,CAAC;AACvF,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,2BAA2B,CAAC;AAyB1C,OAAO,EACL,0BAA0B,EAC1B,4BAA4B,GAE7B,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,cAAc,gCAAgC,CAAC","sourcesContent":["export * from './Caveat.js';\nexport { createRestrictedMethodMessenger } from './createRestrictedMethodMessenger.js';\nexport * from './errors.js';\nexport * from './Permission.js';\nexport * from './PermissionController.js';\nexport type {\n PermissionControllerClearStateAction,\n PermissionControllerExecuteRestrictedMethodAction,\n PermissionControllerGetCaveatAction,\n PermissionControllerGetEndowmentsAction,\n PermissionControllerGetPermissionsAction,\n PermissionControllerGetSubjectNamesAction,\n PermissionControllerGrantPermissionsAction,\n PermissionControllerGrantPermissionsIncrementalAction,\n PermissionControllerHasPermissionAction,\n PermissionControllerHasPermissionsAction,\n PermissionControllerHasUnrestrictedMethodAction,\n PermissionControllerRequestPermissionsAction,\n PermissionControllerRequestPermissionsIncrementalAction,\n PermissionControllerRevokeAllPermissionsAction,\n PermissionControllerRevokePermissionForAllSubjectsAction,\n PermissionControllerRevokePermissionsAction,\n PermissionControllerUpdateCaveatAction,\n PermissionControllerGetPermissionAction,\n PermissionControllerRevokePermissionAction,\n PermissionControllerUpdatePermissionsByCaveatAction,\n PermissionControllerAcceptPermissionsRequestAction,\n PermissionControllerRejectPermissionsRequestAction,\n} from './PermissionController-method-action-types.js';\nexport {\n createPermissionMiddleware,\n createPermissionMiddlewareV2,\n type PermissionMiddlewareActions,\n} from './permission-middleware.js';\nexport type { ExtractSpecifications } from './utils.js';\nexport { MethodNames } from './utils.js';\nexport * from './SubjectMetadataController.js';\nexport type {\n SubjectMetadataControllerClearStateAction,\n SubjectMetadataControllerAddSubjectMetadataAction,\n SubjectMetadataControllerGetSubjectMetadataAction,\n SubjectMetadataControllerTrimMetadataStateAction,\n} from './SubjectMetadataController-method-action-types.js';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,OAAO,EAAE,+BAA+B,EAAE,MAAM,sCAAsC,CAAC;AACvF,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,2BAA2B,CAAC;AAyB1C,OAAO,EACL,0BAA0B,EAC1B,4BAA4B,GAE7B,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,cAAc,gCAAgC,CAAC","sourcesContent":["export * from './Caveat.js';\nexport { createRestrictedMethodMessenger } from './createRestrictedMethodMessenger.js';\nexport * from './errors.js';\nexport * from './Permission.js';\nexport * from './PermissionController.js';\nexport type {\n PermissionControllerClearStateAction,\n PermissionControllerExecuteRestrictedMethodAction,\n PermissionControllerGetCaveatAction,\n PermissionControllerGetEndowmentsGlobalsAction,\n PermissionControllerGetPermissionsAction,\n PermissionControllerGetSubjectNamesAction,\n PermissionControllerGrantPermissionsAction,\n PermissionControllerGrantPermissionsIncrementalAction,\n PermissionControllerHasPermissionAction,\n PermissionControllerHasPermissionsAction,\n PermissionControllerHasUnrestrictedMethodAction,\n PermissionControllerRequestPermissionsAction,\n PermissionControllerRequestPermissionsIncrementalAction,\n PermissionControllerRevokeAllPermissionsAction,\n PermissionControllerRevokePermissionForAllSubjectsAction,\n PermissionControllerRevokePermissionsAction,\n PermissionControllerUpdateCaveatAction,\n PermissionControllerGetPermissionAction,\n PermissionControllerRevokePermissionAction,\n PermissionControllerUpdatePermissionsByCaveatAction,\n PermissionControllerAcceptPermissionsRequestAction,\n PermissionControllerRejectPermissionsRequestAction,\n} from './PermissionController-method-action-types.js';\nexport {\n createPermissionMiddleware,\n createPermissionMiddlewareV2,\n type PermissionMiddlewareActions,\n} from './permission-middleware.js';\nexport type { ExtractSpecifications } from './utils.js';\nexport { MethodNames } from './utils.js';\nexport * from './SubjectMetadataController.js';\nexport type {\n SubjectMetadataControllerClearStateAction,\n SubjectMetadataControllerAddSubjectMetadataAction,\n SubjectMetadataControllerGetSubjectMetadataAction,\n SubjectMetadataControllerTrimMetadataStateAction,\n} from './SubjectMetadataController-method-action-types.js';\n"]}
@@ -3,11 +3,11 @@ import type { JsonRpcMiddleware as JsonRpcMiddlewareV2 } from '@metamask/json-rp
3
3
  import type { Messenger } from '@metamask/messenger';
4
4
  import type { Json } from '@metamask/utils';
5
5
  import type { RestrictedMethodParameters } from './Permission.js';
6
- import type { PermissionControllerExecuteRestrictedMethodAction, PermissionControllerHasUnrestrictedMethodAction } from './PermissionController-method-action-types.js';
6
+ import type { PermissionControllerExecuteRestrictedMethodAction, PermissionControllerGetEndowmentsRpcMethodsAction, PermissionControllerHasUnrestrictedMethodAction, PermissionControllerIsEndowmentRpcMethodAction } from './PermissionController-method-action-types.js';
7
7
  /**
8
8
  * The set of messenger actions required by the permission middleware.
9
9
  */
10
- export type PermissionMiddlewareActions = PermissionControllerExecuteRestrictedMethodAction | PermissionControllerHasUnrestrictedMethodAction;
10
+ export type PermissionMiddlewareActions = PermissionControllerExecuteRestrictedMethodAction | PermissionControllerHasUnrestrictedMethodAction | PermissionControllerGetEndowmentsRpcMethodsAction | PermissionControllerIsEndowmentRpcMethodAction;
11
11
  export type CreatePermissionMiddlewareOptions = {
12
12
  messenger: Messenger<string, PermissionMiddlewareActions>;
13
13
  origin: string;
@@ -15,16 +15,14 @@ export type CreatePermissionMiddlewareOptions = {
15
15
  /**
16
16
  * Creates a JSON-RPC middleware that enforces permissions for a single subject.
17
17
  *
18
- * The middleware passes through unrestricted methods, and otherwise dispatches
19
- * restricted methods to the `PermissionController` via messenger actions. If
20
- * the subject lacks the required permission, or if the method does not exist,
18
+ * The middleware passes through methods gated by endowments, unrestricted methods,
19
+ * and otherwise dispatches restricted methods to the `PermissionController` via messenger actions.
20
+ * If the subject lacks the required permission, or if the method does not exist,
21
21
  * the corresponding error is propagated to the JSON-RPC response.
22
22
  *
23
23
  * @deprecated Use {@link createPermissionMiddlewareV2} with `JsonRpcEngineV2`.
24
24
  * @param options - Options bag.
25
- * @param options.messenger - A messenger with the
26
- * `PermissionController:executeRestrictedMethod` and
27
- * `PermissionController:hasUnrestrictedMethod` actions.
25
+ * @param options.messenger - A messenger with the required actions.
28
26
  * @param options.origin - The origin of the subject for which to create the middleware.
29
27
  * @returns A `json-rpc-engine` middleware.
30
28
  */
@@ -33,15 +31,13 @@ export declare function createPermissionMiddleware({ messenger, origin, }: Creat
33
31
  * Creates a `JsonRpcEngineV2` middleware that enforces permissions for a
34
32
  * single subject.
35
33
  *
36
- * The middleware passes through unrestricted methods, and otherwise dispatches
37
- * restricted methods to the `PermissionController` via messenger actions. If
38
- * the subject lacks the required permission, or if the method does not exist,
34
+ * The middleware passes through methods gated by endowments, unrestricted methods,
35
+ * and otherwise dispatches restricted methods to the `PermissionController` via messenger actions.
36
+ * If the subject lacks the required permission, or if the method does not exist,
39
37
  * the corresponding error is thrown.
40
38
  *
41
39
  * @param options - Options bag.
42
- * @param options.messenger - A messenger with the
43
- * `PermissionController:executeRestrictedMethod` and
44
- * `PermissionController:hasUnrestrictedMethod` actions.
40
+ * @param options.messenger - A messenger with the required actions.
45
41
  * @param options.origin - The origin of the subject for which to create the middleware.
46
42
  * @returns A `JsonRpcEngineV2` middleware.
47
43
  */
@@ -1 +1 @@
1
- {"version":3,"file":"permission-middleware.d.ts","sourceRoot":"","sources":["../src/permission-middleware.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAEV,iBAAiB,EAClB,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EAAE,iBAAiB,IAAI,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAC7F,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,iBAAiB,CAAC;AAClE,OAAO,KAAK,EACV,iDAAiD,EACjD,+CAA+C,EAChD,MAAM,+CAA+C,CAAC;AAEvD;;GAEG;AACH,MAAM,MAAM,2BAA2B,GACnC,iDAAiD,GACjD,+CAA+C,CAAC;AAEpD,MAAM,MAAM,iCAAiC,GAAG;IAC9C,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,2BAA2B,CAAC,CAAC;IAC1D,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,0BAA0B,CAAC,EACzC,SAAS,EACT,MAAM,GACP,EAAE,iCAAiC,GAAG,iBAAiB,CACtD,0BAA0B,EAC1B,IAAI,CACL,CAuBA;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,4BAA4B,CAAC,EAC3C,SAAS,EACT,MAAM,GACP,EAAE,iCAAiC,GAAG,mBAAmB,CAezD"}
1
+ {"version":3,"file":"permission-middleware.d.ts","sourceRoot":"","sources":["../src/permission-middleware.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAEV,iBAAiB,EAClB,MAAM,2BAA2B,CAAC;AACnC,OAAO,KAAK,EAAE,iBAAiB,IAAI,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAC7F,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAG5C,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,iBAAiB,CAAC;AAClE,OAAO,KAAK,EACV,iDAAiD,EACjD,iDAAiD,EACjD,+CAA+C,EAC/C,8CAA8C,EAC/C,MAAM,+CAA+C,CAAC;AAEvD;;GAEG;AACH,MAAM,MAAM,2BAA2B,GACnC,iDAAiD,GACjD,+CAA+C,GAC/C,iDAAiD,GACjD,8CAA8C,CAAC;AAEnD,MAAM,MAAM,iCAAiC,GAAG;IAC9C,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,2BAA2B,CAAC,CAAC;IAC1D,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AA6CF;;;;;;;;;;;;;GAaG;AACH,wBAAgB,0BAA0B,CAAC,EACzC,SAAS,EACT,MAAM,GACP,EAAE,iCAAiC,GAAG,iBAAiB,CACtD,0BAA0B,EAC1B,IAAI,CACL,CA2BA;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,4BAA4B,CAAC,EAC3C,SAAS,EACT,MAAM,GACP,EAAE,iCAAiC,GAAG,mBAAmB,CAmBzD"}
@@ -1,17 +1,40 @@
1
1
  import { createAsyncMiddleware } from '@metamask/json-rpc-engine';
2
+ import { unauthorized } from './errors.js';
3
+ /**
4
+ * Checks whether a method is an endowment RPC method, and if so, whether the
5
+ * origin is allowed to call it.
6
+ *
7
+ * @param options - Options bag.
8
+ * @param options.origin - The origin of the subject making the request.
9
+ * @param options.method - The requested JSON-RPC method.
10
+ * @param options.messenger - A messenger with the endowment-related actions.
11
+ * @returns True if the method is an RPC method gated by an endowment and permitted for the
12
+ * origin. False if the RPC method is not gated by an endowment.
13
+ * @throws If the method is an RPC method gated by an endowment that the origin has not
14
+ * been granted.
15
+ */
16
+ function isHandledEndowmentRpcMethod({ origin, method, messenger, }) {
17
+ const isEndowmentRpcMethod = messenger.call('PermissionController:isEndowmentRpcMethod', method);
18
+ if (!isEndowmentRpcMethod) {
19
+ return false;
20
+ }
21
+ const endowmentRpcMethods = messenger.call('PermissionController:getEndowmentsRpcMethods', origin);
22
+ if (endowmentRpcMethods.includes(method)) {
23
+ return true;
24
+ }
25
+ throw unauthorized({ data: { origin, method } });
26
+ }
2
27
  /**
3
28
  * Creates a JSON-RPC middleware that enforces permissions for a single subject.
4
29
  *
5
- * The middleware passes through unrestricted methods, and otherwise dispatches
6
- * restricted methods to the `PermissionController` via messenger actions. If
7
- * the subject lacks the required permission, or if the method does not exist,
30
+ * The middleware passes through methods gated by endowments, unrestricted methods,
31
+ * and otherwise dispatches restricted methods to the `PermissionController` via messenger actions.
32
+ * If the subject lacks the required permission, or if the method does not exist,
8
33
  * the corresponding error is propagated to the JSON-RPC response.
9
34
  *
10
35
  * @deprecated Use {@link createPermissionMiddlewareV2} with `JsonRpcEngineV2`.
11
36
  * @param options - Options bag.
12
- * @param options.messenger - A messenger with the
13
- * `PermissionController:executeRestrictedMethod` and
14
- * `PermissionController:hasUnrestrictedMethod` actions.
37
+ * @param options.messenger - A messenger with the required actions.
15
38
  * @param options.origin - The origin of the subject for which to create the middleware.
16
39
  * @returns A `json-rpc-engine` middleware.
17
40
  */
@@ -21,6 +44,9 @@ export function createPermissionMiddleware({ messenger, origin, }) {
21
44
  if (messenger.call('PermissionController:hasUnrestrictedMethod', method)) {
22
45
  return next();
23
46
  }
47
+ if (isHandledEndowmentRpcMethod({ origin, method, messenger })) {
48
+ return next();
49
+ }
24
50
  response.result = await messenger.call('PermissionController:executeRestrictedMethod', origin, method, params);
25
51
  return;
26
52
  };
@@ -30,15 +56,13 @@ export function createPermissionMiddleware({ messenger, origin, }) {
30
56
  * Creates a `JsonRpcEngineV2` middleware that enforces permissions for a
31
57
  * single subject.
32
58
  *
33
- * The middleware passes through unrestricted methods, and otherwise dispatches
34
- * restricted methods to the `PermissionController` via messenger actions. If
35
- * the subject lacks the required permission, or if the method does not exist,
59
+ * The middleware passes through methods gated by endowments, unrestricted methods,
60
+ * and otherwise dispatches restricted methods to the `PermissionController` via messenger actions.
61
+ * If the subject lacks the required permission, or if the method does not exist,
36
62
  * the corresponding error is thrown.
37
63
  *
38
64
  * @param options - Options bag.
39
- * @param options.messenger - A messenger with the
40
- * `PermissionController:executeRestrictedMethod` and
41
- * `PermissionController:hasUnrestrictedMethod` actions.
65
+ * @param options.messenger - A messenger with the required actions.
42
66
  * @param options.origin - The origin of the subject for which to create the middleware.
43
67
  * @returns A `JsonRpcEngineV2` middleware.
44
68
  */
@@ -48,6 +72,9 @@ export function createPermissionMiddlewareV2({ messenger, origin, }) {
48
72
  if (messenger.call('PermissionController:hasUnrestrictedMethod', method)) {
49
73
  return next();
50
74
  }
75
+ if (isHandledEndowmentRpcMethod({ origin, method, messenger })) {
76
+ return next();
77
+ }
51
78
  return messenger.call('PermissionController:executeRestrictedMethod', origin, method, params);
52
79
  };
53
80
  }
@@ -1 +1 @@
1
- {"version":3,"file":"permission-middleware.js","sourceRoot":"","sources":["../src/permission-middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AA2BlE;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,0BAA0B,CAAC,EACzC,SAAS,EACT,MAAM,GAC4B;IAIlC,MAAM,qBAAqB,GAGvB,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE;QACpC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAEnC,IAAI,SAAS,CAAC,IAAI,CAAC,4CAA4C,EAAE,MAAM,CAAC,EAAE,CAAC;YACzE,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,QAAQ,CAAC,MAAM,GAAG,MAAM,SAAS,CAAC,IAAI,CACpC,8CAA8C,EAC9C,MAAM,EACN,MAAM,EACN,MAAM,CACP,CAAC;QACF,OAAO;IACT,CAAC,CAAC;IAEF,OAAO,qBAAqB,CAC1B,qBAAqB,CACtB,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,4BAA4B,CAAC,EAC3C,SAAS,EACT,MAAM,GAC4B;IAClC,OAAO,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;QACjC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAEnC,IAAI,SAAS,CAAC,IAAI,CAAC,4CAA4C,EAAE,MAAM,CAAC,EAAE,CAAC;YACzE,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,OAAO,SAAS,CAAC,IAAI,CACnB,8CAA8C,EAC9C,MAAM,EACN,MAAM,EACN,MAAM,CACP,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { createAsyncMiddleware } from '@metamask/json-rpc-engine';\nimport type {\n AsyncJsonrpcMiddleware,\n JsonRpcMiddleware,\n} from '@metamask/json-rpc-engine';\nimport type { JsonRpcMiddleware as JsonRpcMiddlewareV2 } from '@metamask/json-rpc-engine/v2';\nimport type { Messenger } from '@metamask/messenger';\nimport type { Json } from '@metamask/utils';\n\nimport type { RestrictedMethodParameters } from './Permission.js';\nimport type {\n PermissionControllerExecuteRestrictedMethodAction,\n PermissionControllerHasUnrestrictedMethodAction,\n} from './PermissionController-method-action-types.js';\n\n/**\n * The set of messenger actions required by the permission middleware.\n */\nexport type PermissionMiddlewareActions =\n | PermissionControllerExecuteRestrictedMethodAction\n | PermissionControllerHasUnrestrictedMethodAction;\n\nexport type CreatePermissionMiddlewareOptions = {\n messenger: Messenger<string, PermissionMiddlewareActions>;\n origin: string;\n};\n\n/**\n * Creates a JSON-RPC middleware that enforces permissions for a single subject.\n *\n * The middleware passes through unrestricted methods, and otherwise dispatches\n * restricted methods to the `PermissionController` via messenger actions. If\n * the subject lacks the required permission, or if the method does not exist,\n * the corresponding error is propagated to the JSON-RPC response.\n *\n * @deprecated Use {@link createPermissionMiddlewareV2} with `JsonRpcEngineV2`.\n * @param options - Options bag.\n * @param options.messenger - A messenger with the\n * `PermissionController:executeRestrictedMethod` and\n * `PermissionController:hasUnrestrictedMethod` actions.\n * @param options.origin - The origin of the subject for which to create the middleware.\n * @returns A `json-rpc-engine` middleware.\n */\nexport function createPermissionMiddleware({\n messenger,\n origin,\n}: CreatePermissionMiddlewareOptions): JsonRpcMiddleware<\n RestrictedMethodParameters,\n Json\n> {\n const permissionsMiddleware: AsyncJsonrpcMiddleware<\n RestrictedMethodParameters,\n Json\n > = async (request, response, next) => {\n const { method, params } = request;\n\n if (messenger.call('PermissionController:hasUnrestrictedMethod', method)) {\n return next();\n }\n\n response.result = await messenger.call(\n 'PermissionController:executeRestrictedMethod',\n origin,\n method,\n params,\n );\n return;\n };\n\n return createAsyncMiddleware<RestrictedMethodParameters, Json>(\n permissionsMiddleware,\n );\n}\n\n/**\n * Creates a `JsonRpcEngineV2` middleware that enforces permissions for a\n * single subject.\n *\n * The middleware passes through unrestricted methods, and otherwise dispatches\n * restricted methods to the `PermissionController` via messenger actions. If\n * the subject lacks the required permission, or if the method does not exist,\n * the corresponding error is thrown.\n *\n * @param options - Options bag.\n * @param options.messenger - A messenger with the\n * `PermissionController:executeRestrictedMethod` and\n * `PermissionController:hasUnrestrictedMethod` actions.\n * @param options.origin - The origin of the subject for which to create the middleware.\n * @returns A `JsonRpcEngineV2` middleware.\n */\nexport function createPermissionMiddlewareV2({\n messenger,\n origin,\n}: CreatePermissionMiddlewareOptions): JsonRpcMiddlewareV2 {\n return async ({ request, next }) => {\n const { method, params } = request;\n\n if (messenger.call('PermissionController:hasUnrestrictedMethod', method)) {\n return next();\n }\n\n return messenger.call(\n 'PermissionController:executeRestrictedMethod',\n origin,\n method,\n params,\n );\n };\n}\n"]}
1
+ {"version":3,"file":"permission-middleware.js","sourceRoot":"","sources":["../src/permission-middleware.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AASlE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAuB3C;;;;;;;;;;;;GAYG;AACH,SAAS,2BAA2B,CAAC,EACnC,MAAM,EACN,MAAM,EACN,SAAS,GAKV;IACC,MAAM,oBAAoB,GAAG,SAAS,CAAC,IAAI,CACzC,2CAA2C,EAC3C,MAAM,CACP,CAAC;IAEF,IAAI,CAAC,oBAAoB,EAAE,CAAC;QAC1B,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,mBAAmB,GAAG,SAAS,CAAC,IAAI,CACxC,8CAA8C,EAC9C,MAAM,CACP,CAAC;IAEF,IAAI,mBAAmB,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;QACzC,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,YAAY,CAAC,EAAE,IAAI,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;AACnD,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,0BAA0B,CAAC,EACzC,SAAS,EACT,MAAM,GAC4B;IAIlC,MAAM,qBAAqB,GAGvB,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE;QACpC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAEnC,IAAI,SAAS,CAAC,IAAI,CAAC,4CAA4C,EAAE,MAAM,CAAC,EAAE,CAAC;YACzE,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,IAAI,2BAA2B,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,EAAE,CAAC;YAC/D,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,QAAQ,CAAC,MAAM,GAAG,MAAM,SAAS,CAAC,IAAI,CACpC,8CAA8C,EAC9C,MAAM,EACN,MAAM,EACN,MAAM,CACP,CAAC;QACF,OAAO;IACT,CAAC,CAAC;IAEF,OAAO,qBAAqB,CAC1B,qBAAqB,CACtB,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,4BAA4B,CAAC,EAC3C,SAAS,EACT,MAAM,GAC4B;IAClC,OAAO,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;QACjC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAEnC,IAAI,SAAS,CAAC,IAAI,CAAC,4CAA4C,EAAE,MAAM,CAAC,EAAE,CAAC;YACzE,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,IAAI,2BAA2B,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,EAAE,CAAC;YAC/D,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC;QAED,OAAO,SAAS,CAAC,IAAI,CACnB,8CAA8C,EAC9C,MAAM,EACN,MAAM,EACN,MAAM,CACP,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { createAsyncMiddleware } from '@metamask/json-rpc-engine';\nimport type {\n AsyncJsonrpcMiddleware,\n JsonRpcMiddleware,\n} from '@metamask/json-rpc-engine';\nimport type { JsonRpcMiddleware as JsonRpcMiddlewareV2 } from '@metamask/json-rpc-engine/v2';\nimport type { Messenger } from '@metamask/messenger';\nimport type { Json } from '@metamask/utils';\n\nimport { unauthorized } from './errors.js';\nimport type { RestrictedMethodParameters } from './Permission.js';\nimport type {\n PermissionControllerExecuteRestrictedMethodAction,\n PermissionControllerGetEndowmentsRpcMethodsAction,\n PermissionControllerHasUnrestrictedMethodAction,\n PermissionControllerIsEndowmentRpcMethodAction,\n} from './PermissionController-method-action-types.js';\n\n/**\n * The set of messenger actions required by the permission middleware.\n */\nexport type PermissionMiddlewareActions =\n | PermissionControllerExecuteRestrictedMethodAction\n | PermissionControllerHasUnrestrictedMethodAction\n | PermissionControllerGetEndowmentsRpcMethodsAction\n | PermissionControllerIsEndowmentRpcMethodAction;\n\nexport type CreatePermissionMiddlewareOptions = {\n messenger: Messenger<string, PermissionMiddlewareActions>;\n origin: string;\n};\n\n/**\n * Checks whether a method is an endowment RPC method, and if so, whether the\n * origin is allowed to call it.\n *\n * @param options - Options bag.\n * @param options.origin - The origin of the subject making the request.\n * @param options.method - The requested JSON-RPC method.\n * @param options.messenger - A messenger with the endowment-related actions.\n * @returns True if the method is an RPC method gated by an endowment and permitted for the\n * origin. False if the RPC method is not gated by an endowment.\n * @throws If the method is an RPC method gated by an endowment that the origin has not\n * been granted.\n */\nfunction isHandledEndowmentRpcMethod({\n origin,\n method,\n messenger,\n}: {\n origin: string;\n method: string;\n messenger: CreatePermissionMiddlewareOptions['messenger'];\n}): boolean {\n const isEndowmentRpcMethod = messenger.call(\n 'PermissionController:isEndowmentRpcMethod',\n method,\n );\n\n if (!isEndowmentRpcMethod) {\n return false;\n }\n\n const endowmentRpcMethods = messenger.call(\n 'PermissionController:getEndowmentsRpcMethods',\n origin,\n );\n\n if (endowmentRpcMethods.includes(method)) {\n return true;\n }\n\n throw unauthorized({ data: { origin, method } });\n}\n\n/**\n * Creates a JSON-RPC middleware that enforces permissions for a single subject.\n *\n * The middleware passes through methods gated by endowments, unrestricted methods,\n * and otherwise dispatches restricted methods to the `PermissionController` via messenger actions.\n * If the subject lacks the required permission, or if the method does not exist,\n * the corresponding error is propagated to the JSON-RPC response.\n *\n * @deprecated Use {@link createPermissionMiddlewareV2} with `JsonRpcEngineV2`.\n * @param options - Options bag.\n * @param options.messenger - A messenger with the required actions.\n * @param options.origin - The origin of the subject for which to create the middleware.\n * @returns A `json-rpc-engine` middleware.\n */\nexport function createPermissionMiddleware({\n messenger,\n origin,\n}: CreatePermissionMiddlewareOptions): JsonRpcMiddleware<\n RestrictedMethodParameters,\n Json\n> {\n const permissionsMiddleware: AsyncJsonrpcMiddleware<\n RestrictedMethodParameters,\n Json\n > = async (request, response, next) => {\n const { method, params } = request;\n\n if (messenger.call('PermissionController:hasUnrestrictedMethod', method)) {\n return next();\n }\n\n if (isHandledEndowmentRpcMethod({ origin, method, messenger })) {\n return next();\n }\n\n response.result = await messenger.call(\n 'PermissionController:executeRestrictedMethod',\n origin,\n method,\n params,\n );\n return;\n };\n\n return createAsyncMiddleware<RestrictedMethodParameters, Json>(\n permissionsMiddleware,\n );\n}\n\n/**\n * Creates a `JsonRpcEngineV2` middleware that enforces permissions for a\n * single subject.\n *\n * The middleware passes through methods gated by endowments, unrestricted methods,\n * and otherwise dispatches restricted methods to the `PermissionController` via messenger actions.\n * If the subject lacks the required permission, or if the method does not exist,\n * the corresponding error is thrown.\n *\n * @param options - Options bag.\n * @param options.messenger - A messenger with the required actions.\n * @param options.origin - The origin of the subject for which to create the middleware.\n * @returns A `JsonRpcEngineV2` middleware.\n */\nexport function createPermissionMiddlewareV2({\n messenger,\n origin,\n}: CreatePermissionMiddlewareOptions): JsonRpcMiddlewareV2 {\n return async ({ request, next }) => {\n const { method, params } = request;\n\n if (messenger.call('PermissionController:hasUnrestrictedMethod', method)) {\n return next();\n }\n\n if (isHandledEndowmentRpcMethod({ origin, method, messenger })) {\n return next();\n }\n\n return messenger.call(\n 'PermissionController:executeRestrictedMethod',\n origin,\n method,\n params,\n );\n };\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/permission-controller",
3
- "version": "14.1.0",
3
+ "version": "15.0.0",
4
4
  "description": "Mediates access to JSON-RPC methods, used to interact with pieces of the MetaMask stack, via middleware for json-rpc-engine",
5
5
  "keywords": [
6
6
  "Ethereum",
@@ -32,13 +32,14 @@
32
32
  "registry": "https://registry.npmjs.org/"
33
33
  },
34
34
  "scripts": {
35
+ "allow-scripts": "yarn workspace @metamask/core-monorepo run allow-scripts",
35
36
  "build": "tsc --project tsconfig.build.json",
36
37
  "build:all": "tsc --build tsconfig.build.json --verbose",
37
38
  "build:clean": "yarn build:only-clean && yarn build",
38
39
  "build:docs": "typedoc",
39
40
  "build:only-clean": "rimraf './dist' './tsconfig.build.tsbuildinfo'",
40
41
  "changelog:update": "../../scripts/update-changelog.sh @metamask/permission-controller",
41
- "changelog:validate": "../../scripts/validate-changelog.sh @metamask/permission-controller",
42
+ "changelog:validate": "../../scripts/validate-changelog.sh",
42
43
  "lint:tsconfigs": "node --import ../../scripts/resolver/register.ts ../../scripts/lint-tsconfigs/lint-tsconfigs.ts",
43
44
  "lint:tsconfigs:fix": "node --import ../../scripts/resolver/register.ts ../../scripts/lint-tsconfigs/lint-tsconfigs.ts --fix",
44
45
  "messenger-action-types:check": "node --import ../../scripts/resolver/register.ts ../../packages/messenger-cli/src/cli.ts --formatter oxfmt --esm --check",
@@ -46,6 +47,7 @@
46
47
  "since-latest-release": "../../scripts/since-latest-release.sh",
47
48
  "test": "vitest",
48
49
  "test:clean": "vitest --clearCache",
50
+ "test:coverage": "vitest --coverage",
49
51
  "test:verbose": "vitest --reporter=verbose",
50
52
  "test:watch": "vitest --watch"
51
53
  },
@@ -56,21 +58,22 @@
56
58
  "@metamask/json-rpc-engine": "^11.0.0",
57
59
  "@metamask/messenger": "^3.0.0",
58
60
  "@metamask/rpc-errors": "^7.0.3",
59
- "@metamask/utils": "^12.0.1",
61
+ "@metamask/utils": "^12.1.0",
60
62
  "@types/deep-freeze-strict": "^1.1.0",
61
63
  "deep-freeze-strict": "^1.1.1",
62
64
  "fast-deep-equal": "^3.1.3",
63
- "immer": "^11.1.18",
64
- "nanoid": "^3.3.19"
65
+ "immer": "^11.2.10",
66
+ "nanoid": "^6.0.2"
65
67
  },
66
68
  "devDependencies": {
67
- "@metamask/auto-changelog": "^6.2.1",
69
+ "@metamask/auto-changelog": "^7.1.0",
68
70
  "@typescript/native": "npm:typescript@^7.0.2",
69
- "rimraf": "^6.1.3",
71
+ "rimraf": "^6.1.4",
70
72
  "typedoc": "^0.28.20",
71
73
  "typedoc-plugin-missing-exports": "^4.1.4",
72
74
  "typescript": "npm:@typescript/typescript6@^6.0.2",
73
- "vitest": "^5.0.0"
75
+ "vite": "^8.3.8",
76
+ "vitest": "5.1.1"
74
77
  },
75
78
  "engines": {
76
79
  "node": "^22.14.0 || ^24 || ^26"