Skip to content

Commit 2ce8487

Browse files
Add test and fix some linting issues
1 parent 0c8a77e commit 2ce8487

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

packages/snaps-controllers/src/interface/SnapInterfaceController.test.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,39 @@ describe('SnapInterfaceController', () => {
524524
).toThrow('A Snap interface context may not be larger than 5 MB');
525525
});
526526

527+
it('throws if the Snap attempts to use external images without permission', async () => {
528+
const rootMessenger = getRootSnapInterfaceControllerMessenger();
529+
const controllerMessenger =
530+
getRestrictedSnapInterfaceControllerMessenger(rootMessenger);
531+
532+
rootMessenger.registerActionHandler(
533+
'PermissionController:hasPermission',
534+
() => false,
535+
);
536+
537+
// eslint-disable-next-line no-new
538+
new SnapInterfaceController({
539+
messenger: controllerMessenger,
540+
});
541+
542+
const element = (
543+
<Box>
544+
<Image src="https://metamask.io/foo.png" />
545+
</Box>
546+
);
547+
548+
expect(() =>
549+
rootMessenger.call(
550+
'SnapInterfaceController:createInterface',
551+
MOCK_SNAP_ID,
552+
element,
553+
{},
554+
),
555+
).toThrow(
556+
'Using external images is only permitted with the network access endowment',
557+
);
558+
});
559+
527560
it('throws if a link is on the phishing list', async () => {
528561
const rootMessenger = getRootSnapInterfaceControllerMessenger();
529562
const controllerMessenger = getRestrictedSnapInterfaceControllerMessenger(

packages/snaps-controllers/src/interface/SnapInterfaceController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
} from '@metamask/base-controller';
99
import { BaseController } from '@metamask/base-controller';
1010
import type { Messenger } from '@metamask/messenger';
11+
import type { HasPermission } from '@metamask/permission-controller';
1112
import type { TestOrigin } from '@metamask/phishing-controller';
1213
import type {
1314
InterfaceState,
@@ -41,7 +42,6 @@ import {
4142
validateInterfaceContext,
4243
} from './utils';
4344
import type { GetSnap } from '../snaps';
44-
import { HasPermission } from '@metamask/permission-controller';
4545

4646
const MAX_UI_CONTENT_SIZE = 10_000_000; // 10 mb
4747

packages/snaps-controllers/src/test-utils/controller.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,7 @@ export const getRestrictedSnapInterfaceControllerMessenger = (
768768
'SnapController:get',
769769
'AccountsController:getSelectedMultichainAccount',
770770
'AccountsController:listMultichainAccounts',
771+
'PermissionController:hasPermission',
771772
],
772773
events: ['NotificationServicesController:notificationsListUpdated'],
773774
messenger: snapInterfaceControllerMessenger,

packages/snaps-utils/src/ui.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { NodeType } from '@metamask/snaps-sdk';
33
import type {
44
BoldChildren,
55
GenericSnapElement,
6-
ImageElement,
76
ItalicChildren,
87
JSXElement,
98
LinkElement,
@@ -42,8 +41,8 @@ import type { Token, Tokens } from 'marked';
4241

4342
import type { InternalAccount } from './account';
4443
import type { Snap } from './snaps';
45-
import { parseMetaMaskUrl } from './url';
4644
import { isValidUrl } from './types';
45+
import { parseMetaMaskUrl } from './url';
4746

4847
const MAX_TEXT_LENGTH = 50_000; // 50 kb
4948
const ALLOWED_PROTOCOLS = ['https:', 'mailto:', 'metamask:'];
@@ -467,7 +466,7 @@ export function validateJsxElements(
467466
);
468467
break;
469468
case 'Image': {
470-
const { src } = (childNode as ImageElement).props;
469+
const { src } = childNode.props;
471470
const isUrl = isValidUrl(src);
472471
assert(
473472
!isUrl || (isUrl && hasPermission('endowment:network-access')),

0 commit comments

Comments
 (0)