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

feat(snaps-utils): Allow overriding allowed protocols in validateLink #2411

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ export class SnapInterfaceController extends BaseController<
);

await this.#triggerPhishingListUpdate();
// TODO: Expose configuration for allowedProtocols in validateJsxLinks
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change isn't very impactful until this is done, since all UI is converted to JSX as of recently.

validateJsxLinks(element, this.#checkPhishingList.bind(this));
}
}
22 changes: 15 additions & 7 deletions packages/snaps-rpc-methods/src/restricted/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,19 @@ export const notifyBuilder = Object.freeze({
* @param hooks.showInAppNotification - A function that shows a notification in the MetaMask UI.
* @param hooks.isOnPhishingList - A function that checks for links against the phishing list.
* @param hooks.maybeUpdatePhishingList - A function that updates the phishing list if needed.
* @param allowedProtocols - Allowed protocols for links (example: ['https:']).
* @returns The method implementation which returns `null` on success.
* @throws If the params are invalid.
*/
export function getImplementation({
showNativeNotification,
showInAppNotification,
isOnPhishingList,
maybeUpdatePhishingList,
}: NotifyMethodHooks) {
export function getImplementation(
{
showNativeNotification,
showInAppNotification,
isOnPhishingList,
maybeUpdatePhishingList,
}: NotifyMethodHooks,
allowedProtocols?: string[],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the PermissionController support this?

) {
return async function implementation(
args: RestrictedMethodOptions<NotifyParams>,
): Promise<NotifyResult> {
Expand All @@ -132,7 +136,11 @@ export function getImplementation({

await maybeUpdatePhishingList();

validateTextLinks(validatedParams.message, isOnPhishingList);
validateTextLinks(
validatedParams.message,
isOnPhishingList,
allowedProtocols,
);

switch (validatedParams.type) {
case NotificationType.Native:
Expand Down
2 changes: 1 addition & 1 deletion packages/snaps-utils/coverage.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"branches": 96.7,
"branches": 96.72,
"functions": 98.72,
"lines": 98.81,
"statements": 94.79
Expand Down
25 changes: 25 additions & 0 deletions packages/snaps-utils/src/ui.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,31 @@ describe('validateTextLinks', () => {
'Invalid URL: Unable to parse URL.',
);
});

it('handles custom protocols', () => {
const allowedProtocols = ['http:', 'file:'];
expect(() =>
validateTextLinks(
'[test](http://foo.bar)',
() => false,
allowedProtocols,
),
).not.toThrow();
expect(() =>
validateTextLinks(
'[test](https://foo.bar)',
() => false,
allowedProtocols,
),
).toThrow('Invalid URL: Protocol must be one of: http:, file:.');
expect(() =>
validateTextLinks(
'<file:///var/www/index.html>',
() => false,
allowedProtocols,
),
).not.toThrow();
});
});

describe('validateJsxLinks', () => {
Expand Down
16 changes: 11 additions & 5 deletions packages/snaps-utils/src/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { lexer, walkTokens } from 'marked';
import type { Token, Tokens } from 'marked';

const MAX_TEXT_LENGTH = 50_000; // 50 kb
const ALLOWED_PROTOCOLS = ['https:', 'mailto:'];
const DEFAULT_ALLOWED_PROTOCOLS = ['https:', 'mailto:'];

/**
* Get the button variant from a legacy button component variant.
Expand Down Expand Up @@ -320,16 +320,18 @@ function getMarkdownLinks(text: string) {
* @param link - The link to validate.
* @param isOnPhishingList - The function that checks the link against the
* phishing list.
* @param allowedProtocols - Allowed protocols (example: ['https:']).
*/
function validateLink(
link: string,
isOnPhishingList: (url: string) => boolean,
allowedProtocols: string[],
) {
try {
const url = new URL(link);
assert(
ALLOWED_PROTOCOLS.includes(url.protocol),
`Protocol must be one of: ${ALLOWED_PROTOCOLS.join(', ')}.`,
allowedProtocols.includes(url.protocol),
`Protocol must be one of: ${allowedProtocols.join(', ')}.`,
);

const hostname =
Expand All @@ -352,16 +354,18 @@ function validateLink(
* @param text - The text to verify.
* @param isOnPhishingList - The function that checks the link against the
* phishing list.
* @param allowedProtocols - Allowed protocols (example: ['https:']).
* @throws If the text contains a link that is not allowed.
*/
export function validateTextLinks(
text: string,
isOnPhishingList: (url: string) => boolean,
allowedProtocols: string[] = DEFAULT_ALLOWED_PROTOCOLS,
) {
const links = getMarkdownLinks(text);

for (const link of links) {
validateLink(link.href, isOnPhishingList);
validateLink(link.href, isOnPhishingList, allowedProtocols);
}
}

Expand All @@ -372,17 +376,19 @@ export function validateTextLinks(
* @param node - The JSX node to walk.
* @param isOnPhishingList - The function that checks the link against the
* phishing list.
* @param allowedProtocols - Allowed protocols (example: ['https:']).
*/
export function validateJsxLinks(
node: JSXElement,
isOnPhishingList: (url: string) => boolean,
allowedProtocols: string[] = DEFAULT_ALLOWED_PROTOCOLS,
) {
walkJsx(node, (childNode) => {
if (childNode.type !== 'Link') {
return;
}

validateLink(childNode.props.href, isOnPhishingList);
validateLink(childNode.props.href, isOnPhishingList, allowedProtocols);
});
}

Expand Down
Loading