@metamask/snaps-controllers 3.1.0 → 3.2.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/CHANGELOG.md CHANGED
@@ -6,6 +6,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [3.2.0]
10
+ ### Added
11
+ - Add support for links in custom UI and notifications ([#1814](https://github.com/MetaMask/snaps/pull/1814))
12
+
13
+ ### Fixed
14
+ - Fix an issue where snaps throwing a `SnapError` would be allowed to run for longer than expected ([#1897](https://github.com/MetaMask/snaps/pull/1897))
15
+
16
+ ## [3.1.1]
17
+ ### Fixed
18
+ - Fix a few issues with allowlist version resolving ([#1888](https://github.com/MetaMask/snaps/pull/1888))
19
+
9
20
  ## [3.1.0]
10
21
  ### Added
11
22
  - Add static file API ([#1836](https://github.com/MetaMask/snaps/pull/1836))
@@ -90,7 +101,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
90
101
  - The version of the package no longer needs to match the version of all other
91
102
  MetaMask Snaps packages.
92
103
 
93
- [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@3.1.0...HEAD
104
+ [Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@3.2.0...HEAD
105
+ [3.2.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@3.1.1...@metamask/snaps-controllers@3.2.0
106
+ [3.1.1]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@3.1.0...@metamask/snaps-controllers@3.1.1
94
107
  [3.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@3.0.0...@metamask/snaps-controllers@3.1.0
95
108
  [3.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@2.0.2...@metamask/snaps-controllers@3.0.0
96
109
  [2.0.2]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-controllers@2.0.1...@metamask/snaps-controllers@2.0.2
@@ -29,6 +29,7 @@ const _basecontroller = require("@metamask/base-controller");
29
29
  const _permissioncontroller = require("@metamask/permission-controller");
30
30
  const _rpcerrors = require("@metamask/rpc-errors");
31
31
  const _snapsrpcmethods = require("@metamask/snaps-rpc-methods");
32
+ const _snapsui = require("@metamask/snaps-ui");
32
33
  const _snapsutils = require("@metamask/snaps-utils");
33
34
  const _utils = require("@metamask/utils");
34
35
  const _fsm = require("@xstate/fsm");
@@ -192,7 +193,7 @@ _initializeStateMachine = /*#__PURE__*/ new WeakSet(), /**
192
193
  *
193
194
  * @param snapId - The id of the Snap whose message handler to get.
194
195
  * @returns The RPC handler for the given snap.
195
- */ _getRpcRequestHandler = /*#__PURE__*/ new WeakSet(), _executeWithTimeout = /*#__PURE__*/ new WeakSet(), _recordSnapRpcRequestStart = /*#__PURE__*/ new WeakSet(), _recordSnapRpcRequestFinish = /*#__PURE__*/ new WeakSet(), /**
196
+ */ _getRpcRequestHandler = /*#__PURE__*/ new WeakSet(), _assertSnapRpcRequestResult = /*#__PURE__*/ new WeakSet(), _executeWithTimeout = /*#__PURE__*/ new WeakSet(), _recordSnapRpcRequestStart = /*#__PURE__*/ new WeakSet(), _recordSnapRpcRequestFinish = /*#__PURE__*/ new WeakSet(), /**
196
197
  * Retrieves the rollback snapshot of a snap.
197
198
  *
198
199
  * @param snapId - The snap id.
@@ -597,16 +598,15 @@ class SnapController extends _basecontroller.BaseControllerV2 {
597
598
  try {
598
599
  for (const [snapId, { version: rawVersion }] of Object.entries(requestedSnaps)){
599
600
  (0, _snapsutils.assertIsValidSnapId)(snapId);
600
- const [error, resolvedVersion] = (0, _snapsutils.resolveVersionRange)(rawVersion);
601
+ const [error, version] = (0, _snapsutils.resolveVersionRange)(rawVersion);
601
602
  if (error) {
602
603
  throw _rpcerrors.rpcErrors.invalidParams(`The "version" field must be a valid SemVer version range if specified. Received: "${rawVersion}".`);
603
604
  }
604
- // If we are running in allowlist mode, try to match the version with an allowlist version.
605
- const version = _class_private_field_get(this, _featureFlags).requireAllowlist ? await _class_private_method_get(this, _resolveAllowlistVersion, resolveAllowlistVersion).call(this, snapId, resolvedVersion) : resolvedVersion;
606
605
  const location = _class_private_field_get(this, _detectSnapLocation).call(this, snapId, {
607
606
  versionRange: version,
608
607
  fetch: _class_private_field_get(this, _fetchFunction),
609
- allowLocal: _class_private_field_get(this, _featureFlags).allowLocalSnaps
608
+ allowLocal: _class_private_field_get(this, _featureFlags).allowLocalSnaps,
609
+ resolveVersion: async (range)=>_class_private_field_get(this, _featureFlags).requireAllowlist ? await _class_private_method_get(this, _resolveAllowlistVersion, resolveAllowlistVersion).call(this, snapId, range) : range
610
610
  });
611
611
  // Existing snaps may need to be updated, unless they should be re-installed (e.g. local snaps)
612
612
  // Everything else is treated as an install
@@ -1022,6 +1022,12 @@ class SnapController extends _basecontroller.BaseControllerV2 {
1022
1022
  _class_private_method_init(this, _validateSnapPermissions);
1023
1023
  _class_private_method_init(this, _getRpcRequestHandler);
1024
1024
  /**
1025
+ * Asserts that the returned result of a Snap RPC call is the expected shape.
1026
+ *
1027
+ * @param handlerType - The handler type of the RPC Request.
1028
+ * @param result - The result of the RPC request.
1029
+ */ _class_private_method_init(this, _assertSnapRpcRequestResult);
1030
+ /**
1025
1031
  * Awaits the specified promise and rejects if the promise doesn't resolve
1026
1032
  * before the timeout.
1027
1033
  *
@@ -1562,7 +1568,7 @@ function getRpcRequestHandler(snapId) {
1562
1568
  // This will either get the result or reject due to the timeout.
1563
1569
  try {
1564
1570
  const result = await _class_private_method_get(this, _executeWithTimeout, executeWithTimeout).call(this, handleRpcRequestPromise, timer);
1565
- _class_private_method_get(this, _recordSnapRpcRequestFinish, recordSnapRpcRequestFinish).call(this, snapId, request.id);
1571
+ await _class_private_method_get(this, _assertSnapRpcRequestResult, assertSnapRpcRequestResult).call(this, handlerType, result);
1566
1572
  return result;
1567
1573
  } catch (error) {
1568
1574
  const [jsonRpcError, handled] = (0, _snapsutils.unwrapError)(error);
@@ -1570,11 +1576,24 @@ function getRpcRequestHandler(snapId) {
1570
1576
  await this.stopSnap(snapId, _snapsutils.SnapStatusEvents.Crash);
1571
1577
  }
1572
1578
  throw jsonRpcError;
1579
+ } finally{
1580
+ _class_private_method_get(this, _recordSnapRpcRequestFinish, recordSnapRpcRequestFinish).call(this, snapId, request.id);
1573
1581
  }
1574
1582
  };
1575
1583
  runtime.rpcHandler = rpcHandler;
1576
1584
  return rpcHandler;
1577
1585
  }
1586
+ async function assertSnapRpcRequestResult(handlerType, result) {
1587
+ switch(handlerType){
1588
+ case _snapsutils.HandlerType.OnTransaction:
1589
+ (0, _utils.assertStruct)(result, _snapsutils.OnTransactionResponseStruct);
1590
+ await this.messagingSystem.call('PhishingController:maybeUpdateState');
1591
+ await (0, _snapsui.assertUILinksAreSafe)(result.content, (url)=>this.messagingSystem.call('PhishingController:testOrigin', url).result);
1592
+ break;
1593
+ default:
1594
+ break;
1595
+ }
1596
+ }
1578
1597
  async function executeWithTimeout(promise, timer) {
1579
1598
  const result = await (0, _utils1.withTimeout)(promise, timer ?? this.maxRequestTime);
1580
1599
  if (result === _utils1.hasTimedOut) {