Skip to content

Commit a848a7e

Browse files
committed
Filter removed permissions
1 parent 709b347 commit a848a7e

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

packages/snaps-rpc-methods/src/permissions.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ import {
1515
} from './restricted';
1616
import { selectHooks } from './utils';
1717

18+
const REMOVED_PERMISSIONS = Object.freeze(['snap_manageAccounts']);
19+
20+
/**
21+
* Filters out permissions that have been removed from the Snap API.
22+
*
23+
* @param initialPermission - The initial permission to filter.
24+
* @returns Whether the permission has been removed.
25+
*/
26+
export const filterRemovedPermissions = (
27+
initialPermission: [string, unknown],
28+
) => {
29+
const [value] = initialPermission;
30+
return REMOVED_PERMISSIONS.some((permission) => permission === value);
31+
};
32+
1833
/**
1934
* Map initial permissions as defined in a Snap manifest to something that can
2035
* be processed by the PermissionsController. Each caveat mapping function
@@ -30,22 +45,32 @@ export function processSnapPermissions(
3045
initialPermissions: SnapPermissions,
3146
): Record<string, Pick<PermissionConstraint, 'caveats'>> {
3247
return Object.fromEntries(
33-
Object.entries(initialPermissions).map(([initialPermission, value]) => {
34-
if (hasProperty(caveatMappers, initialPermission)) {
35-
return [initialPermission, caveatMappers[initialPermission](value)];
36-
} else if (hasProperty(endowmentCaveatMappers, initialPermission)) {
48+
Object.entries(initialPermissions)
49+
.filter(filterRemovedPermissions)
50+
.map(([initialPermission, value]) => {
51+
if (
52+
REMOVED_PERMISSIONS.some(
53+
(permission) => permission === initialPermission,
54+
)
55+
) {
56+
return [];
57+
}
58+
59+
if (hasProperty(caveatMappers, initialPermission)) {
60+
return [initialPermission, caveatMappers[initialPermission](value)];
61+
} else if (hasProperty(endowmentCaveatMappers, initialPermission)) {
62+
return [
63+
initialPermission,
64+
endowmentCaveatMappers[initialPermission](value),
65+
];
66+
}
67+
68+
// If we have no mapping, this may be a non-snap permission, return as-is
3769
return [
3870
initialPermission,
39-
endowmentCaveatMappers[initialPermission](value),
71+
value as Pick<PermissionConstraint, 'caveats'>,
4072
];
41-
}
42-
43-
// If we have no mapping, this may be a non-snap permission, return as-is
44-
return [
45-
initialPermission,
46-
value as Pick<PermissionConstraint, 'caveats'>,
47-
];
48-
}),
73+
}),
4974
);
5075
}
5176

0 commit comments

Comments
 (0)