Skip to content
Merged
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
4 changes: 2 additions & 2 deletions packages/snaps-rpc-methods/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module.exports = deepmerge(baseConfig, {
],
coverageThreshold: {
global: {
branches: 97.28,
functions: 98.84,
branches: 97.29,
functions: 98.85,
lines: 99.14,
statements: 98.81,
},
Expand Down
26 changes: 25 additions & 1 deletion packages/snaps-rpc-methods/src/permissions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,14 @@ describe('buildSnapRestrictedMethodSpecifications', () => {
it('returns the expected object', () => {
const specifications = buildSnapRestrictedMethodSpecifications(
[],
{},
{
getUnlockPromise: jest.fn(),
getClientCryptography: jest.fn(),
isOnPhishingList: jest.fn(),
maybeUpdatePhishingList: jest.fn(),
getSnapKeyring: jest.fn(),
getPreferences: jest.fn(),
},
new Messenger({ namespace: 'SnapsRestrictedMethods' }),
);
expect(specifications).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -316,4 +323,21 @@ describe('buildSnapRestrictedMethodSpecifications', () => {
}
`);
});

it('excludes the specified permissions', () => {
const specifications = buildSnapRestrictedMethodSpecifications(
['snap_dialog'],
{
getUnlockPromise: jest.fn(),
getClientCryptography: jest.fn(),
isOnPhishingList: jest.fn(),
maybeUpdatePhishingList: jest.fn(),
getSnapKeyring: jest.fn(),
getPreferences: jest.fn(),
},
new Messenger({ namespace: 'SnapsRestrictedMethods' }),
);

expect(specifications.snap_dialog).toBeUndefined();
});
});
43 changes: 28 additions & 15 deletions packages/snaps-rpc-methods/src/permissions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { selectHooks } from '@metamask/json-rpc-engine/v2';
import { selectHooks, assertExpectedHooks } from '@metamask/json-rpc-engine/v2';
import {
createRestrictedMethodMessenger,
type PermissionConstraint,
Expand Down Expand Up @@ -70,27 +70,40 @@ export const buildSnapRestrictedMethodSpecifications = (
excludedPermissions: string[],
hooks: Record<string, unknown>,
messenger: RestrictedMethodMessenger,
) =>
Object.values(restrictedMethodPermissionBuilders).reduce<
) => {
const permissionBuilders = Object.values(
restrictedMethodPermissionBuilders,
).filter((builder) => !excludedPermissions.includes(builder.targetName));

const expectedHookNames = new Set(
permissionBuilders.flatMap((builder) =>
builder.methodHooks
? Object.getOwnPropertyNames(builder.methodHooks)
: [],
),
);

assertExpectedHooks(hooks, expectedHookNames);
Comment thread
cursor[bot] marked this conversation as resolved.

return permissionBuilders.reduce<
Record<string, PermissionSpecificationConstraint>
>(
(
specifications,
{ targetName, specificationBuilder, methodHooks, actionNames },
) => {
if (!excludedPermissions.includes(targetName)) {
specifications[targetName] = specificationBuilder({
methodHooks: selectHooks(hooks, methodHooks),
messenger: createRestrictedMethodMessenger({
namespace: targetName,
rootMessenger: messenger,
actionNames: actionNames as readonly [
RestrictedMethodActions['type'],
],
}),
});
}
specifications[targetName] = specificationBuilder({
methodHooks: selectHooks(hooks, methodHooks),
messenger: createRestrictedMethodMessenger({
namespace: targetName,
rootMessenger: messenger,
actionNames: actionNames as readonly [
RestrictedMethodActions['type'],
],
}),
});
return specifications;
},
{},
);
};
4 changes: 3 additions & 1 deletion packages/snaps-simulation/src/methods/specifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export function getPermissionSpecifications({
hooks,
options,
}: GetPermissionSpecificationsOptions): PermissionSpecificationMap<PermissionSpecificationConstraint> {
const { getClientCryptography } = hooks;
return {
[caip25EndowmentBuilder.targetName]:
caip25EndowmentBuilder.specificationBuilder({}),
Expand All @@ -81,11 +82,12 @@ export function getPermissionSpecifications({
EXCLUDED_SNAP_PERMISSIONS,
{
// Shared hooks.
...hooks,
getClientCryptography,

// Snaps-specific hooks.
getPreferences: getGetPreferencesMethodImplementation(options),
getUnlockPromise: asyncResolve(true),
getSnapKeyring: asyncResolve(null),

// TODO: Allow the user to specify the result of this function.
isOnPhishingList: resolve(false),
Expand Down
Loading