-
Notifications
You must be signed in to change notification settings - Fork 0
/
validators.ts
67 lines (60 loc) · 1.76 KB
/
validators.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import {
type UncPath,
type UncPathOptions,
type UncPathOptionsWithCredentials,
uncPathPrefix
} from './types.js'
function stringHasForbiddenCharacters(stringToCheck?: string): boolean {
return (stringToCheck ?? '').includes('"')
}
/**
* Checks if the operating system is Windows.
* @returns True if the operating system is Windows.
*/
export function isWindows(): boolean {
return process.platform === 'win32'
}
/**
* Checks if the options include credentials.
* @param uncPathOptions - UNC path options.
* @returns True when the UNC path options include credentials.
*/
export function uncPathOptionsHaveCredentials(
uncPathOptions: UncPathOptions
): uncPathOptions is UncPathOptionsWithCredentials {
return (
((uncPathOptions as Partial<UncPathOptionsWithCredentials>).userName ??
'') !== '' &&
((uncPathOptions as Partial<UncPathOptionsWithCredentials>).password ??
'') !== ''
)
}
/**
* Ensures a UNC path is safe.
* @param uncPath - UNC path.
* @returns True if the UNC path is safe for use.
*/
export function uncPathIsSafe(uncPath: string): uncPath is UncPath {
return (
uncPath.startsWith(uncPathPrefix) && !stringHasForbiddenCharacters(uncPath)
)
}
/**
* Ensures the options are safe to use.
* @param uncPathOptions - UNC path options.
* @returns True if the options are safe to use.
*/
export function uncPathOptionsAreSafe(uncPathOptions: UncPathOptions): boolean {
if (!uncPathIsSafe(uncPathOptions.uncPath)) {
return false
}
// eslint-disable-next-line sonarjs/prefer-single-boolean-return
if (
uncPathOptionsHaveCredentials(uncPathOptions) &&
(stringHasForbiddenCharacters(uncPathOptions.userName) ||
stringHasForbiddenCharacters(uncPathOptions.password))
) {
return false
}
return true
}