-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidation.ts
More file actions
103 lines (89 loc) · 2.62 KB
/
Copy pathvalidation.ts
File metadata and controls
103 lines (89 loc) · 2.62 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { log } from "./log.ts";
import { pipe } from "./utils/pipe.ts";
export function isValidPort(input: number | undefined): boolean {
return (
input != null && Number.isInteger(input) && input >= 1 && input <= 65535
);
}
export function isValidHost(input: string | undefined): boolean {
return (
input != null && /^[a-zA-Z0-9]+([-.](?![-.])[a-zA-Z0-9]+)*$/.test(input)
);
}
export function isValidScanRate(input: number | undefined): boolean {
return input != null && Number.isInteger(input) && input >= 1;
}
export function validate<T>(
input: T | undefined,
defaultValue: T,
validator: (input: T | undefined) => boolean,
symbolName: string,
): T {
if (input != null && validator(input)) {
return input;
} else {
if (input != null) {
log.info(
`${symbolName} with value "${input}" is not valid, using default "${defaultValue}"`,
);
}
return defaultValue;
}
}
export const validateCurry = <T>(
defaultValue: T,
validator: (input: T | undefined) => boolean,
symbolName: string,
) =>
(input: T | undefined) => validate(input, defaultValue, validator, symbolName);
export function makeNumberOrUndefined(
input: string | number | undefined,
): number | undefined {
return input == null ? undefined : Number(input);
}
export function validateHost(input: string | undefined) {
return validate(input, "0.0.0.0", isValidHost, "MANTLE_HOST");
}
export function validatePort(input: string | number | undefined) {
return validate(
makeNumberOrUndefined(input),
4001,
isValidPort,
"MANTLE_PORT",
);
}
const validateSslCaPath = (input: string | undefined) => {
if (input == null || input.length === 0) {
return false;
}
try {
const cert = Deno.readTextFileSync(input);
return validateSslCaCert(cert);
} catch (_error) {
return false;
}
};
export const validateSslCaCert = (input: string | undefined) => {
if (!input) return false;
// More robust certificate pattern
const caPattern =
/^-----BEGIN CERTIFICATE-----\s*([\s\S]*?)\s*-----END CERTIFICATE-----\s*$/;
const match = input.match(caPattern);
if (!match) return false;
// Basic content validation
const content = match[1].replace(/\s+/g, "");
return content.length > 0 && /^[A-Za-z0-9+/=]+$/.test(content);
};
export function validateSslCa(input: string | undefined) {
const defaultValue: string = "";
return pipe(
input,
validateCurry(defaultValue, validateSslCaPath, "MANTLE_DB_SSL_CA"),
(input) => input ? Deno.readTextFileSync(input) : undefined,
validateCurry(
defaultValue,
validateSslCaCert,
"MANTLE_DB_SSL_CA",
),
);
}