Skip to content

Commit

Permalink
Merge pull request #2293 from synonymdev/fix/electrum-url
Browse files Browse the repository at this point in the history
fix: Slow RegExp when validating electrum server URL
  • Loading branch information
pwltr authored Oct 7, 2024
2 parents c2c297c + d5427d7 commit f6addee
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
43 changes: 29 additions & 14 deletions src/screens/Settings/ElectrumConfig/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { memo, ReactElement, useEffect, useState } from 'react';
import { StyleSheet } from 'react-native';
import { err, ok, Result } from '@synonymdev/result';
import Url from 'url-parse';
import parseUrl from 'url-parse';
import { useTranslation } from 'react-i18next';
import isEqual from 'lodash/isEqual';
import { EProtocol } from 'beignet';
Expand Down Expand Up @@ -42,20 +42,35 @@ const radioButtons: RadioButtonItem[] = [
];

const isValidURL = (data: string): boolean => {
const pattern = new RegExp(
'^(https?:\\/\\/)?' + // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))' + // IP (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*', // port and path
'i',
);

// wave through everything 'localhost' for development
if (__DEV__ && data.includes('localhost')) {
return true;
// Add 'http://' if the protocol is missing to enable URL parsing
let normalizedData = data;
if (!/^https?:\/\//i.test(data)) {
normalizedData = 'http://' + data;
}

return !!pattern.test(data);
try {
const url = parseUrl(normalizedData);

// Allow standard domains, custom TLDs like .local, and IPv4 addresses
const isValidDomainOrIP = !!url.hostname.match(
/^([a-z\d]([a-z\d-]*[a-z\d])*\.[a-z\d-]+|(\d{1,3}\.){3}\d{1,3})$/i,
);

// Always allow .local domains
if (url.hostname.endsWith('.local')) {
return true;
}

// Allow localhost in development mode
if (__DEV__ && data.includes('localhost')) {
return true;
}

return isValidDomainOrIP;
} catch (e) {
// If URL constructor fails, it's not a valid URL
return false;
}
};

const validateInput = (
Expand Down Expand Up @@ -218,7 +233,7 @@ const ElectrumConfig = ({
protocol: _protocol,
};
} else {
const url = new Url(data);
const url = parseUrl(data);

connectData = {
host: url.hostname,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/lightning/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ export const addPeer = async ({
}

if (!isValidLightningNodePublicKey(parsedUri.value.publicKey)) {
return err(i18n.t('lightning:error_add_msg'));
return err(i18n.t('lightning:error_add'));
}

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

if (res.isErr()) {
res.error.message = i18n.t('lightning:error_add_msg', {
res.error.message = i18n.t('lightning:error_add', {
raw: res.error.message,
});
}
Expand Down

0 comments on commit f6addee

Please sign in to comment.