Skip to content

Commit

Permalink
fix: use flexible URL validation (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
sereneblue committed Sep 8, 2024
1 parent 5968565 commit 777cd7e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ let isInternalIP = (host: string): boolean => {
);
};

let isValidURL = (url: string): boolean => {
try {
new URL(url);
return true; // The URL is valid
} catch (e) {
return false; // The URL is not valid
}
};

let isValidIP = (ip: string): boolean => {
return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(
ip
Expand Down Expand Up @@ -150,6 +159,7 @@ export default {
ipToString,
isInternalIP,
isValidIP,
isValidURL,
parseURL,
validateIPRange,
};
3 changes: 1 addition & 2 deletions src/options/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,6 @@ enum Modal {
@Component
export default class App extends Vue {
public REGEX_DOMAIN: any = /^(?:^|\s)((https?:\/\/)?(?:localhost|[\w-]+(?:\.[\w-]+)+)(:\d+)?(\/\S*)?)/;
public Modal = Modal;
public modalType: Modal = Modal.DEFAULT;
public currentTab: string = 'about';
Expand Down Expand Up @@ -964,7 +963,7 @@ export default class App extends Vue {
}
// test if valid domain
if (sites[i][0] === '' || !this.REGEX_DOMAIN.test(sites[i][0])) {
if (sites[i][0] === '' || !util.isValidURL(sites[i][0])) {
this.errors.wlRuleSites = true;
this.savingWLRule = false;
Expand Down

0 comments on commit 777cd7e

Please sign in to comment.