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

Move snap_manageAccounts to a gated permitted method #2869

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions packages/snaps-controllers/src/snaps/SnapController.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,59 @@
snapController.destroy();
});

it('filters out removed permissions', async () => {
const messenger = getSnapControllerMessenger();
const initialPermissions: SnapPermissions = {
[handlerEndowments.onRpcRequest as string]: { snaps: false, dapps: true },
// eslint-disable-next-line @typescript-eslint/naming-convention
snap_manageAccounts: {},
};

const { manifest } = await getMockSnapFilesWithUpdatedChecksum({
manifest: getSnapManifest({
initialPermissions,
}),
});

const snapController = getSnapController(
getSnapControllerOptions({
messenger,
detectSnapLocation: loopbackDetect({
manifest: manifest.result,
}),
}),
);

await snapController.installSnaps(MOCK_ORIGIN, {
[MOCK_SNAP_ID]: {},
});

expect(messenger.call).toHaveBeenNthCalledWith(
5,
'PermissionController:grantPermissions',
{
approvedPermissions: {
[SnapEndowments.Rpc]: {
caveats: [
{ type: 'rpcOrigin', value: { dapps: true, snaps: false } },
],
},
},
subject: { origin: MOCK_SNAP_ID },
requestData: {
metadata: {
dappOrigin: MOCK_ORIGIN,
id: expect.any(String),
origin: MOCK_SNAP_ID,
},
snapId: MOCK_SNAP_ID,
},
},
);

snapController.destroy();
});

it('throws an error if the installation is disabled during installSnaps', async () => {
const controller = getSnapController(
getSnapControllerOptions({
Expand Down Expand Up @@ -1851,7 +1904,7 @@
});

// This isn't stable in CI unfortunately
it.skip('throws if the Snap is terminated while executing', async () => {

Check warning on line 1907 in packages/snaps-controllers/src/snaps/SnapController.test.tsx

View workflow job for this annotation

GitHub Actions / Build, lint, and test / Lint (@metamask/snaps-controllers)

Disabled test
const { manifest, sourceCode, svgIcon } =
await getMockSnapFilesWithUpdatedChecksum({
sourceCode: `
Expand Down
8 changes: 4 additions & 4 deletions packages/snaps-rpc-methods/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ module.exports = deepmerge(baseConfig, {
],
coverageThreshold: {
global: {
branches: 92.88,
functions: 97.26,
lines: 97.84,
statements: 97.36,
branches: 94.11,
functions: 98.37,
lines: 98.58,
statements: 98.07,
},
},
});
110 changes: 101 additions & 9 deletions packages/snaps-rpc-methods/src/permissions.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,109 @@
import type { Bip32Entropy } from '@metamask/snaps-sdk';

import {
buildSnapEndowmentSpecifications,
buildSnapRestrictedMethodSpecifications,
filterRemovedPermissions,
processSnapPermissions,
} from './permissions';

describe('filterRemovedPermissions', () => {
it('returns true for a permission that is not removed', () => {
const result = filterRemovedPermissions(['snap_dialog', {}]);
expect(result).toBe(true);
});

it('returns false for a permission that is removed', () => {
const result = filterRemovedPermissions(['snap_manageAccounts', {}]);
expect(result).toBe(false);
});
});

/* eslint-disable @typescript-eslint/naming-convention */
describe('processSnapPermissions', () => {
it('returns the expected object', () => {
const permissions = {
snap_dialog: {},

snap_manageAccounts: {},
};
const result = processSnapPermissions(permissions);
expect(result).toStrictEqual({
snap_dialog: {},
});
});

it('returns the expected object when the permission is not a snap permission', () => {
const permissions = {
snap_dialog: {},
snap_manageAccounts: {},
wallet_foobar: {},
};
const result = processSnapPermissions(permissions);
expect(result).toStrictEqual({
snap_dialog: {},
wallet_foobar: {},
});
});

it('returns the expected object when the permission is a snap endowment with a mapper', () => {
const permissions = {
snap_dialog: {},
snap_manageAccounts: {},
'endowment:rpc': {
dapps: true,
snaps: true,
},
};
const result = processSnapPermissions(permissions);
expect(result).toStrictEqual({
snap_dialog: {},
'endowment:rpc': {
caveats: [
{
type: 'rpcOrigin',
value: {
dapps: true,
snaps: true,
},
},
],
},
});
});

it('returns the expected object when the permission is a snap permission with a mapper', () => {
const permissions = {
snap_dialog: {},
snap_manageAccounts: {},
snap_getBip32Entropy: [
{
path: ['m', "44'", "3'"],
curve: 'secp256k1',
} as Bip32Entropy,
],
};
const result = processSnapPermissions(permissions);
expect(result).toStrictEqual({
snap_dialog: {},
snap_getBip32Entropy: {
caveats: [
{
type: 'permittedDerivationPaths',
value: [
{
path: ['m', "44'", "3'"],
curve: 'secp256k1',
},
],
},
],
},
});
});
});
/* eslint-enable @typescript-eslint/naming-convention */

describe('buildSnapEndowmentSpecifications', () => {
it('returns the expected object', () => {
const specifications = buildSnapEndowmentSpecifications([]);
Expand Down Expand Up @@ -211,15 +312,6 @@ describe('buildSnapRestrictedMethodSpecifications', () => {
],
"targetName": "snap_getPreferences",
},
"snap_manageAccounts": {
"allowedCaveats": null,
"methodImplementation": [Function],
"permissionType": "RestrictedMethod",
"subjectTypes": [
"snap",
],
"targetName": "snap_manageAccounts",
},
"snap_manageState": {
"allowedCaveats": null,
"methodImplementation": [Function],
Expand Down
43 changes: 30 additions & 13 deletions packages/snaps-rpc-methods/src/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ import {
} from './restricted';
import { selectHooks } from './utils';

const REMOVED_PERMISSIONS = Object.freeze(['snap_manageAccounts']);

/**
* Filters out permissions that have been removed from the Snap API.
*
* @param initialPermission - The initial permission to filter.
* @returns Whether the permission has been removed.
*/
export const filterRemovedPermissions = (
initialPermission: [string, unknown],
) => {
const [value] = initialPermission;
return !REMOVED_PERMISSIONS.some((permission) => permission === value);
};

/**
* Map initial permissions as defined in a Snap manifest to something that can
* be processed by the PermissionsController. Each caveat mapping function
Expand All @@ -30,22 +45,24 @@ export function processSnapPermissions(
initialPermissions: SnapPermissions,
): Record<string, Pick<PermissionConstraint, 'caveats'>> {
return Object.fromEntries(
Object.entries(initialPermissions).map(([initialPermission, value]) => {
if (hasProperty(caveatMappers, initialPermission)) {
return [initialPermission, caveatMappers[initialPermission](value)];
} else if (hasProperty(endowmentCaveatMappers, initialPermission)) {
Object.entries(initialPermissions)
.filter(filterRemovedPermissions)
.map(([initialPermission, value]) => {
if (hasProperty(caveatMappers, initialPermission)) {
return [initialPermission, caveatMappers[initialPermission](value)];
} else if (hasProperty(endowmentCaveatMappers, initialPermission)) {
return [
initialPermission,
endowmentCaveatMappers[initialPermission](value),
];
}

// If we have no mapping, this may be a non-snap permission, return as-is
return [
initialPermission,
endowmentCaveatMappers[initialPermission](value),
value as Pick<PermissionConstraint, 'caveats'>,
];
}

// If we have no mapping, this may be a non-snap permission, return as-is
return [
initialPermission,
value as Pick<PermissionConstraint, 'caveats'>,
];
}),
}),
);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/snaps-rpc-methods/src/permitted/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getInterfaceStateHandler } from './getInterfaceState';
import { getSnapsHandler } from './getSnaps';
import { invokeKeyringHandler } from './invokeKeyring';
import { invokeSnapSugarHandler } from './invokeSnapSugar';
import { manageAccountsHandler } from './manageAccounts';
import { requestSnapsHandler } from './requestSnaps';
import { resolveInterfaceHandler } from './resolveInterface';
import { updateInterfaceHandler } from './updateInterface';
Expand All @@ -29,6 +30,7 @@ export const methodHandlers = {
snap_resolveInterface: resolveInterfaceHandler,
snap_getCurrencyRate: getCurrencyRateHandler,
snap_experimentalProviderRequest: providerRequestHandler,
snap_manageAccounts: manageAccountsHandler,
};
/* eslint-enable @typescript-eslint/naming-convention */

Expand Down
4 changes: 3 additions & 1 deletion packages/snaps-rpc-methods/src/permitted/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { GetClientStatusHooks } from './getClientStatus';
import type { GetCurrencyRateMethodHooks } from './getCurrencyRate';
import type { GetInterfaceStateMethodHooks } from './getInterfaceState';
import type { GetSnapsHooks } from './getSnaps';
import type { ManageAccountsMethodHooks } from './manageAccounts';
import type { RequestSnapsHooks } from './requestSnaps';
import type { ResolveInterfaceMethodHooks } from './resolveInterface';
import type { UpdateInterfaceMethodHooks } from './updateInterface';
Expand All @@ -18,7 +19,8 @@ export type PermittedRpcMethodHooks = GetAllSnapsHooks &
GetInterfaceStateMethodHooks &
ResolveInterfaceMethodHooks &
GetCurrencyRateMethodHooks &
ProviderRequestMethodHooks;
ProviderRequestMethodHooks &
ManageAccountsMethodHooks;

export * from './handlers';
export * from './middleware';
Loading
Loading