Skip to content

Commit

Permalink
fix: extension freezing when validating a specifically malformed netw…
Browse files Browse the repository at this point in the history
…ork URL (#1459)

## Context
With the previous regular expression, trying to parse a specifically
malformed string would block the V8 thread (good catch @LuizAsFight!).
More info on the issue can be read about
[here](https://issues.chromium.org/issues/365066528).

## Changes
- Optimized expression
- Added named matching groups
- Moved regular expression to be a global, avoiding instantiating it at
the method's execution time

Closes #1460
  • Loading branch information
arthurgeron authored Sep 9, 2024
1 parent 6f57be0 commit 00234c0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-ties-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels-wallet": patch
---

Fixed extension freezing when validating a specifically malformed network URL
8 changes: 8 additions & 0 deletions packages/app/src/systems/Network/utils/url.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ describe('Network URL Utilities', () => {

// URLs with illegal characters in the domain
expect(isValidNetworkUrl('http://exa$mple.com')).toBe(false);

//Tests if authentication URL without ":" should fail without hanging the V8 Thread
// https://issues.chromium.org/issues/365066528
expect(
isValidNetworkUrl(
'https://fffffff10abbxyFfffFFOfFffF0Fff@supernet.ffff.network/v1/graphql'
)
).toBe(false);
});
});

Expand Down
34 changes: 21 additions & 13 deletions packages/app/src/systems/Network/utils/url.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import { DEVNET_NETWORK_URL, TESTNET_NETWORK_URL } from 'fuels';

const URL_PATTERN = new RegExp(
'^(?<protocol>https?:\\/\\/)' +
'(?:(?<auth>[\\w%.~+-]+:[\\w%.~+-]+)@)?' +
'(?<host>' +
'(?:' +
'(?:[\\w-]+\\.)*[a-z]{2,}|' + // Domain name
'(?:\\d{1,3}\\.){3}\\d{1,3}' + // IP address
')' +
')' +
'(?::(?<port>\\d+))?' +
'(?<path>\\/[\\w%.~+-]*)*?' +
'(?<query>\\?[\\w%.~+=&-]*)?' +
'(?<fragment>#[\\w%.-]*)?' +
'$',
'gi'
);

export function isValidNetworkUrl(url?: string) {
if (!url) return false;

// ^https?:\/\/(?:[a-z\d%_.~+-]+:[a-z\d%_.~+-]+@)?((([a-z\d]([a-z\d-]*[a-z\d])*)\.{0,1})+[a-z]{2,}|((\d{1,3}\.){3}\d{1,3}))(\:\d+)?(\/[-a-z\d%_.-]*)*?(\?[&a-z\d%_.~+=-]*)?([a-zA-Z\d]*)$
const pattern = new RegExp(
'^https?:\\/\\/' + // Matches the protocol (http or https)
'(?:[a-z\\d%_.~+-]+:[a-z\\d%_.~+-]+@)?' + // Optional basic auth (user:password@)
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.{0,1})+[a-z]{2,}|' + // Matches domain name
'((\\d{1,3}\\.){3}\\d{1,3}))' + // Or matches IP address
'(\\:\\d+)?' + // Optional port number
'(\\/[-a-z\\d%_.-]*)*?' + // Makes any path optional, including no path, and multiple paths
'(\\?[&a-z\\d%_.~+=-]*)?' + // Optional query parameters
'([a-zA-Z\\d]*)$', // mnake sure it blocks any extra special character in the URL
'i' // Case-insensitive
);
return pattern.test(url);
// Reset lastIndex to 0 before each test
URL_PATTERN.lastIndex = 0;
return URL_PATTERN.test(url);
}

export function isNetworkTestnet(url: string) {
return url.includes(TESTNET_NETWORK_URL);
}
Expand Down

0 comments on commit 00234c0

Please sign in to comment.