Skip to content

Commit 835dfc6

Browse files
committed
Fix unused permission detection for endowments with multiple handlers
1 parent 08bb538 commit 835dfc6

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

packages/snaps-utils/src/manifest/validators/unused-exports.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,36 @@ describe('unusedExports', () => {
9191
),
9292
);
9393
});
94+
95+
it('does not report if the Snap exports a handler and requests permission for it in the manifest', async () => {
96+
const report = jest.fn();
97+
assert(unusedExports.semanticCheck);
98+
99+
const files = getMockSnapFiles({
100+
manifest: getSnapManifest({
101+
initialPermissions: {
102+
'endowment:page-home': {},
103+
'endowment:rpc': {},
104+
'endowment:lifecycle-hooks': {},
105+
},
106+
}),
107+
manifestPath: __filename,
108+
});
109+
110+
await unusedExports.semanticCheck(files, {
111+
report,
112+
options: {
113+
exports: ['onRpcRequest', 'onHomePage', 'onInstall'],
114+
handlerEndowments: {
115+
onRpcRequest: 'endowment:rpc',
116+
onHomePage: 'endowment:page-home',
117+
onInstall: 'endowment:lifecycle-hooks',
118+
onUpdate: 'endowment:lifecycle-hooks',
119+
onUserInput: null,
120+
},
121+
},
122+
});
123+
124+
expect(report).toHaveBeenCalledTimes(0);
125+
});
94126
});

packages/snaps-utils/src/manifest/validators/unused-exports.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ export const unusedExports: ValidatorMeta = {
1717
return;
1818
}
1919

20+
// Endowments used based on the exports from the Snap. This is used to
21+
// filter endowments that are used by multiple handlers, e.g., the lifecycle
22+
// handlers.
23+
const usedEndowments = Object.entries(handlerEndowments)
24+
.filter(
25+
([handler, endowment]) =>
26+
endowment === null || exports.includes(handler),
27+
)
28+
.map(([, endowment]) => endowment);
29+
2030
const unusedHandlers = Object.entries(handlerEndowments)
2131
.filter(([handler, endowment]) => {
2232
if (endowment === null) {
@@ -39,9 +49,11 @@ export const unusedExports: ValidatorMeta = {
3949
}
4050

4151
return (
52+
!usedEndowments.includes(endowment) &&
4253
files.manifest.result.initialPermissions[
4354
endowment as keyof InitialPermissions
44-
] && !exports.includes(handler)
55+
] &&
56+
!exports.includes(handler)
4557
);
4658
},
4759
);

0 commit comments

Comments
 (0)