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: account for caveats in permissions difference calculation #2345

Merged
merged 2 commits into from Apr 22, 2024

Conversation

FrederikBolding
Copy link
Member

The permission diff calculation was incorrect when considering caveats.

Because of this, changes to caveats were not always correctly applied when updating a Snap.

One specific instance of this was updating a Snap with:

'endowment:rpc': { dapps: true },

to

'endowment:rpc': { allowedOrigins: ['https://metamask.io'] },

This problem was fixed by introducing permissionsDiff which follows the implementation of setDiff except for the additional condition that when the permission is included in both permission sets, it is subtracted if the caveats differ.

@FrederikBolding FrederikBolding requested a review from a team as a code owner April 19, 2024 10:01
@FrederikBolding FrederikBolding changed the title fix: fix permissions difference calculation with caveats fix: account for caveats in permissions difference calculation Apr 19, 2024
Copy link
Member

@rekmarks rekmarks left a comment

Choose a reason for hiding this comment

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

LGTM! Just a couple of notes for future consideration.

return Object.entries(permissionsA).reduce<
Record<string, Pick<PermissionConstraint, 'caveats'>>
>((acc, [key, value]) => {
const isIncluded = key in permissionsB;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const isIncluded = key in permissionsB;
const isIncluded = hasProperty(permissionsB, key);

Copy link
Member

Choose a reason for hiding this comment

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

I just realized: Ditto for setDiff. Maybe best to ignore in this PR.

if (
!isIncluded ||
(isIncluded &&
!deepEqual(value.caveats ?? [], permissionsB[key].caveats ?? []))
Copy link
Member

Choose a reason for hiding this comment

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

Just a note here, for my own benefit as much as anyone else:

fast-deep-equal is sensitive to array element order:

> const deepEqual = require('fast-deep-equal')
undefined
> deepEqual([1, 2, 3], [1, 2, 3])
true
> deepEqual([1, 2, 3], [3, 2, 1])
false

I don't believe that this is documented or enforced anywhere, but I believe that element order should not be a consideration for caveat identity. In practice, I think it probably is, just like it will be here.

This problem should probably be solved in the PermissionController itself, where we'd want to add some kind of logic that always treats caveat value arrays as sets.

For present purposes, I don't think it matters, because we'll just revoke "identical" permissions only to immediately add them back.

@FrederikBolding FrederikBolding merged commit 3c11ec4 into main Apr 22, 2024
148 checks passed
@FrederikBolding FrederikBolding deleted the fb/fix-permissions-diff branch April 22, 2024 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants