aboutsummaryrefslogtreecommitdiffstats
path: root/src/helpers/url-helpers.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/helpers/url-helpers.ts')
-rw-r--r--src/helpers/url-helpers.ts36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/helpers/url-helpers.ts b/src/helpers/url-helpers.ts
new file mode 100644
index 000000000..23e8fab29
--- /dev/null
+++ b/src/helpers/url-helpers.ts
@@ -0,0 +1,36 @@
1// This is taken from: https://benjamin-altpeter.de/shell-openexternal-dangers/
2
3import { URL } from 'url';
4import { ensureDirSync } from 'fs-extra';
5import { shell } from 'electron';
6
7import { ALLOWED_PROTOCOLS } from '../config';
8
9const debug = require('debug')('Ferdi:Helpers:url');
10
11export function isValidExternalURL(url: string) {
12 let parsedUrl: URL;
13 try {
14 parsedUrl = new URL(url);
15 } catch (_) {
16 return false;
17 }
18
19 const isAllowed = ALLOWED_PROTOCOLS.includes(parsedUrl.protocol);
20
21 debug('protocol check is', isAllowed, 'for:', url);
22
23 return isAllowed;
24}
25
26export async function openPath(folderName: string) {
27 ensureDirSync(folderName);
28 shell.openPath(folderName);
29}
30
31// TODO: Need to verify and fix/remove the skipping logic. Ideally, we should never skip this check
32export function openExternalUrl(url: string, skipValidityCheck: boolean = false) {
33 if (skipValidityCheck || isValidExternalURL(url)) {
34 shell.openExternal(url);
35 }
36}