Skip to content

Commit f6addee

Browse files
authored
Merge pull request #2293 from synonymdev/fix/electrum-url
fix: Slow RegExp when validating electrum server URL
2 parents c2c297c + d5427d7 commit f6addee

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

src/screens/Settings/ElectrumConfig/index.tsx

+29-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { memo, ReactElement, useEffect, useState } from 'react';
22
import { StyleSheet } from 'react-native';
33
import { err, ok, Result } from '@synonymdev/result';
4-
import Url from 'url-parse';
4+
import parseUrl from 'url-parse';
55
import { useTranslation } from 'react-i18next';
66
import isEqual from 'lodash/isEqual';
77
import { EProtocol } from 'beignet';
@@ -42,20 +42,35 @@ const radioButtons: RadioButtonItem[] = [
4242
];
4343

4444
const isValidURL = (data: string): boolean => {
45-
const pattern = new RegExp(
46-
'^(https?:\\/\\/)?' + // protocol
47-
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
48-
'((\\d{1,3}\\.){3}\\d{1,3}))' + // IP (v4) address
49-
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*', // port and path
50-
'i',
51-
);
52-
53-
// wave through everything 'localhost' for development
54-
if (__DEV__ && data.includes('localhost')) {
55-
return true;
45+
// Add 'http://' if the protocol is missing to enable URL parsing
46+
let normalizedData = data;
47+
if (!/^https?:\/\//i.test(data)) {
48+
normalizedData = 'http://' + data;
5649
}
5750

58-
return !!pattern.test(data);
51+
try {
52+
const url = parseUrl(normalizedData);
53+
54+
// Allow standard domains, custom TLDs like .local, and IPv4 addresses
55+
const isValidDomainOrIP = !!url.hostname.match(
56+
/^([a-z\d]([a-z\d-]*[a-z\d])*\.[a-z\d-]+|(\d{1,3}\.){3}\d{1,3})$/i,
57+
);
58+
59+
// Always allow .local domains
60+
if (url.hostname.endsWith('.local')) {
61+
return true;
62+
}
63+
64+
// Allow localhost in development mode
65+
if (__DEV__ && data.includes('localhost')) {
66+
return true;
67+
}
68+
69+
return isValidDomainOrIP;
70+
} catch (e) {
71+
// If URL constructor fails, it's not a valid URL
72+
return false;
73+
}
5974
};
6075

6176
const validateInput = (
@@ -218,7 +233,7 @@ const ElectrumConfig = ({
218233
protocol: _protocol,
219234
};
220235
} else {
221-
const url = new Url(data);
236+
const url = parseUrl(data);
222237

223238
connectData = {
224239
host: url.hostname,

src/utils/lightning/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ export const addPeer = async ({
10751075
}
10761076

10771077
if (!isValidLightningNodePublicKey(parsedUri.value.publicKey)) {
1078-
return err(i18n.t('lightning:error_add_msg'));
1078+
return err(i18n.t('lightning:error_add'));
10791079
}
10801080

10811081
const res = await lm.addPeer({
@@ -1086,7 +1086,7 @@ export const addPeer = async ({
10861086
});
10871087

10881088
if (res.isErr()) {
1089-
res.error.message = i18n.t('lightning:error_add_msg', {
1089+
res.error.message = i18n.t('lightning:error_add', {
10901090
raw: res.error.message,
10911091
});
10921092
}

0 commit comments

Comments
 (0)