MetaMask / rpc-cap

A module for adding an object-capabilities system to any JSON-RPC API as middleware for json-rpc-engine
ISC License
31 stars 32 forks source link

Fix pending permission metadata.id generation #101

Closed rekmarks closed 4 years ago

rekmarks commented 4 years ago

Summary

Our generation of id values for pending permissions requests in requestPermissionsMiddleware was incorrect. This PR fixes it.

This is a prerequisite for solving: https://github.com/MetaMask/metamask-extension/issues/8919

Details

The middleware functions of this package expect their first argument to be an IOriginMetadata object. The objects intended usage is made clear by these lines in providerMiddlewareFunction. Specifically, it provides metadata about the external domain that the middleware is bound to.

We can see that our usage was incorrect, because we defaulted to the IOriginMetadata object's id property as the metadata.id property of the IPermissionsRequest object, which represents a pending permission. If the IOriginMetadata object did not have an id, it was set. Since the IOriginMetadata object is bound to a middleware, once this id was set, it forced the same metadata.id value for all pending permissions request for that domain. This breaks both methods internal to rpc-cap, like removePermissionsRequest, and functionality in the MetaMask Extension.

Changes

json-rpc-engine IDs

json-rpc-engine generates its IDs in this file. We use its ID remap middleware in @metamask/inpage-provider to ensure that every request has a unique ID. I mention this just to note that the ID can take value 0, and that the probability of collision is sufficiently low for our purposes.