forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityCheck.js
More file actions
102 lines (102 loc) · 2.7 KB
/
Copy pathSecurityCheck.js
File metadata and controls
102 lines (102 loc) · 2.7 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
import logger from './logger';
class SecurityCheck {
constructor(data) {
const { group, title, warning, check, failed, success } = data;
try {
if (!group || !title || !warning) {
throw 'Security checks must have a group, title, and a warning.';
}
if (typeof group !== 'string') {
throw '"group" of the security check must be a string, e.g SecurityCheck.Category.Database';
}
if (typeof success !== 'string') {
throw '"success" message of the security check must be a string.';
}
if (typeof title !== 'string') {
throw '"title" of the security check must be a string.';
}
if (typeof warning !== 'string') {
throw '"warning" message of the security check must be a string.';
}
if (check && typeof check !== 'function') {
throw '"check" of the security check must be a function.';
}
this.group = group;
this.title = title;
this.warning = warning;
this.check = check;
this.failed = failed;
this.success = success;
} catch (e) {
logger.error(e);
return;
}
_registerCheck(this);
}
async run() {
try {
if (this.failed) {
throw 'Check failed.';
}
if (!this.check) {
return {
result: 'success',
};
}
const result = await this.check();
if (result != null && result === false) {
throw 'Check failed.';
}
return {
result: 'success',
};
} catch (error) {
return {
result: 'fail',
error,
};
}
}
setFailed() {
this.failed = true;
}
}
SecurityCheck.Category = {
Database: 'Database',
CLP: 'CLP',
ServerConfiguration: 'ServerConfiguration',
};
SecurityCheck.getChecks = async () => {
const resultsByGroup = {};
let total = 0;
const resolveSecurityCheck = async check => {
const { group, title, warning, success } = check;
const { result, error } = await check.run();
const category = resultsByGroup[group] || [];
category.push({
title,
warning,
error,
result,
success,
});
resultsByGroup[group] = category;
if (result !== 'success') {
total++;
}
};
await Promise.all(securityCheckStore.map(check => resolveSecurityCheck(check)));
resultsByGroup.Total = total;
return resultsByGroup;
};
const securityCheckStore = [];
function _registerCheck(securityCheck) {
for (const [i, check] of securityCheckStore.entries()) {
if (check.title == securityCheck.title && check.warning == securityCheck.warning) {
securityCheckStore[i] = securityCheck;
return;
}
}
securityCheckStore.push(securityCheck);
}
module.exports = SecurityCheck;