From 2edcf95e6cf390e4511e3bfd61cdab6057cd6adc Mon Sep 17 00:00:00 2001 From: Vijay Aravamudhan Date: Tue, 26 Oct 2021 08:57:50 +0530 Subject: feature: allow 'file' urls for services (fixes #2118) (#2130) --- src/helpers/url-helpers.ts | 6 +++++- src/helpers/validation-helpers.ts | 21 +++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) (limited to 'src/helpers') diff --git a/src/helpers/url-helpers.ts b/src/helpers/url-helpers.ts index 135f06cbf..c1ca3ab25 100644 --- a/src/helpers/url-helpers.ts +++ b/src/helpers/url-helpers.ts @@ -1,7 +1,7 @@ // This is taken from: https://benjamin-altpeter.de/shell-openexternal-dangers/ import { URL } from 'url'; -import { ensureDirSync } from 'fs-extra'; +import { ensureDirSync, existsSync } from 'fs-extra'; import { shell } from 'electron'; import { ALLOWED_PROTOCOLS } from '../config'; @@ -23,6 +23,10 @@ export function isValidExternalURL(url: string | URL) { return isAllowed; } +export function isValidFileUrl(path: string) { + return path.startsWith('file') && existsSync(new URL(path)); +} + export async function openPath(folderName: string) { ensureDirSync(folderName); shell.openPath(folderName); diff --git a/src/helpers/validation-helpers.ts b/src/helpers/validation-helpers.ts index 23c297443..dfaf199ee 100644 --- a/src/helpers/validation-helpers.ts +++ b/src/helpers/validation-helpers.ts @@ -1,5 +1,6 @@ import { defineMessages } from 'react-intl'; import isEmail from 'validator/lib/isEmail'; +import { isValidExternalURL, isValidFileUrl } from './url-helpers'; const messages = defineMessages({ required: { @@ -47,17 +48,17 @@ export function email({ field }) { export function url({ field }) { const value = field.value.trim(); - let isValid = false; + let isValid = true; - isValid = - value !== '' - ? Boolean( - // eslint-disable-next-line unicorn/better-regex - /(^|[\s.:;?\-\]<(])(https?:\/\/[-\w;/?:@&=+$|_.!~*|'()[\]%#,☺]+[\w/#](\(\))?)(?=$|[\s',|().:;?\-[\]>)])/i.test( - value, - ), - ) - : true; + if (value !== '') { + if (value.startsWith('http')) { + isValid = isValidExternalURL(value); + } else if (value.startsWith('file')) { + isValid = isValidFileUrl(value); + } else { + isValid = false; + } + } return [ isValid, -- cgit v1.2.3-70-g09d2