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: add Network URL non-ascii -> punycode warning #12813

Merged
merged 14 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -295,7 +295,7 @@ const NetworkVerificationInfo = ({
};

const renderBannerNetworkUrlNonAsciiDetected = () => {
if (isValidASCIIURL(customRpcUrl)) { return null; }
if (!customRpcUrl || isValidASCIIURL(customRpcUrl)) { return null; }
const punycodeUrl = toPunycodeURL(customRpcUrl);

return (
Expand Down
7 changes: 5 additions & 2 deletions app/util/url/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export function isBridgeUrl(url: string) {

export const isValidASCIIURL = (urlString?: string) => {
try {
return urlString?.includes(punycode.toASCII(new URL(urlString).host));
if (!urlString) { return false; }

const urlPunycodeString = punycode.toASCII(new URL(urlString).href);
return urlPunycodeString?.includes(urlString);
digiwand marked this conversation as resolved.
Show resolved Hide resolved
} catch (exp: unknown) {
console.error(exp);
digiwand marked this conversation as resolved.
Show resolved Hide resolved
return false;
Expand All @@ -39,7 +42,7 @@ function removePathTrailingSlash(path: string) {
return path.endsWith('/') ? path.slice(0, -1) : path;
}

/**
/**
* Note: We use the punycode library here because the url library in react native doesn't support punycode encoding.
* It is supported in node.js which allows tests to pass, but behavior in react-native does not match the
* behavior in the tests. This differs from the toPunycodeURL util method in metamask-extension.
Expand Down
14 changes: 13 additions & 1 deletion app/util/url/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,21 @@ describe('URL Check Functions', () => {
expect(isValidASCIIURL('https://www.google.com')).toEqual(true);
});

it('returns false for URL containing special character', () => {
it('returns true for URL with a path containing ASCII characters', () => {
expect(
isValidASCIIURL('https://infura.io/gnosis?x=xn--ifura-dig.io'),
).toStrictEqual(true);
});

it('returns false for URL containing non-ASCII characters', () => {
expect(isValidASCIIURL('https://iոfura.io/gnosis')).toStrictEqual(false);
});

it('returns false for URL containing non-ASCII characters in its path', () => {
expect(
isValidASCIIURL('https://infura.io/gnosis?x=iոfura.io'),
).toStrictEqual(false);
});
});

describe('toPunycodeURL', () => {
Expand Down
Loading