diff options
Diffstat (limited to 'src/helpers/url-helpers.ts')
-rw-r--r-- | src/helpers/url-helpers.ts | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/src/helpers/url-helpers.ts b/src/helpers/url-helpers.ts index 69a2cc4dc..9c5cf7752 100644 --- a/src/helpers/url-helpers.ts +++ b/src/helpers/url-helpers.ts | |||
@@ -1,14 +1,12 @@ | |||
1 | // This is taken from: https://benjamin-altpeter.de/shell-openexternal-dangers/ | 1 | // This is taken from: https://benjamin-altpeter.de/shell-openexternal-dangers/ |
2 | |||
3 | import { URL } from 'url'; | 2 | import { URL } from 'url'; |
4 | import { ensureDirSync, existsSync } from 'fs-extra'; | 3 | import { ensureDirSync, existsSync } from 'fs-extra'; |
5 | import { shell } from 'electron'; | 4 | import { shell } from 'electron'; |
6 | |||
7 | import { ALLOWED_PROTOCOLS } from '../config'; | 5 | import { ALLOWED_PROTOCOLS } from '../config'; |
8 | 6 | ||
9 | const debug = require('../preload-safe-debug')('Ferdium:Helpers:url'); | 7 | const debug = require('../preload-safe-debug')('Ferdium:Helpers:url'); |
10 | 8 | ||
11 | export function isValidExternalURL(url: string | URL) { | 9 | export function isValidExternalURL(url: string | URL): boolean { |
12 | let parsedUrl: URL; | 10 | let parsedUrl: URL; |
13 | try { | 11 | try { |
14 | parsedUrl = new URL(url.toString()); | 12 | parsedUrl = new URL(url.toString()); |
@@ -17,13 +15,12 @@ export function isValidExternalURL(url: string | URL) { | |||
17 | } | 15 | } |
18 | 16 | ||
19 | const isAllowed = ALLOWED_PROTOCOLS.includes(parsedUrl.protocol); | 17 | const isAllowed = ALLOWED_PROTOCOLS.includes(parsedUrl.protocol); |
20 | |||
21 | debug('protocol check is', isAllowed, 'for:', url); | 18 | debug('protocol check is', isAllowed, 'for:', url); |
22 | 19 | ||
23 | return isAllowed; | 20 | return isAllowed; |
24 | } | 21 | } |
25 | 22 | ||
26 | export function fixUrl(url: string | URL) { | 23 | export function fixUrl(url: string | URL): string { |
27 | return url | 24 | return url |
28 | .toString() | 25 | .toString() |
29 | .replaceAll('//', '/') | 26 | .replaceAll('//', '/') |
@@ -32,11 +29,11 @@ export function fixUrl(url: string | URL) { | |||
32 | .replaceAll('file:/', 'file://'); | 29 | .replaceAll('file:/', 'file://'); |
33 | } | 30 | } |
34 | 31 | ||
35 | export function isValidFileUrl(path: string) { | 32 | export function isValidFileUrl(path: string): boolean { |
36 | return path.startsWith('file') && existsSync(new URL(path)); | 33 | return path.startsWith('file') && existsSync(new URL(path)); |
37 | } | 34 | } |
38 | 35 | ||
39 | export async function openPath(folderName: string) { | 36 | export async function openPath(folderName: string): Promise<void> { |
40 | ensureDirSync(folderName); | 37 | ensureDirSync(folderName); |
41 | shell.openPath(folderName); | 38 | shell.openPath(folderName); |
42 | } | 39 | } |
@@ -45,7 +42,7 @@ export async function openPath(folderName: string) { | |||
45 | export function openExternalUrl( | 42 | export function openExternalUrl( |
46 | url: string | URL, | 43 | url: string | URL, |
47 | skipValidityCheck: boolean = false, | 44 | skipValidityCheck: boolean = false, |
48 | ) { | 45 | ): void { |
49 | const fixedUrl = fixUrl(url.toString()); | 46 | const fixedUrl = fixUrl(url.toString()); |
50 | debug('Open url:', fixedUrl, 'with skipValidityCheck:', skipValidityCheck); | 47 | debug('Open url:', fixedUrl, 'with skipValidityCheck:', skipValidityCheck); |
51 | if (skipValidityCheck || isValidExternalURL(fixedUrl)) { | 48 | if (skipValidityCheck || isValidExternalURL(fixedUrl)) { |