-
Notifications
You must be signed in to change notification settings - Fork 7
/
loadThreatsPropertiesforThreats.js
146 lines (141 loc) · 4.75 KB
/
loadThreatsPropertiesforThreats.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var threatData;
var mitigationsData;
//Load the json file that contains the threat and device properties mapping
window.onload = () => {
fetch("../_data/properties_threat_mappings.json")
.then((res) => {
if (!res.ok) {
throw new Error(`HTTP error! Status: ${res.status}`);
}
return res.json();
})
.then((json) => {
threatData = json;
var threatTitle = document
.getElementById("threattitle")
.innerHTML.replaceAll("\n", "")
.replaceAll(" ", "");
getRelatedThreats(threatTitle);
});
//Load the json file that contains the threat, device properties and mitigation mappings
fetch("../_data/threats_properties_mitigations_mappings.json")
.then((res) => {
if (!res.ok) {
throw new Error(`HTTP error! Status: ${res.status}`);
}
return res.json();
})
.then((json) => {
mitigationsData = json;
var threatTitle = document
.getElementById("threattitle")
.innerHTML.replaceAll("\n", "")
.replaceAll(" ", "");
getRelatedMitigations(threatTitle);
});
};
// Get device properties and threats related to the current one and display as a list on the threat description page
function getRelatedThreats(threatTitle) {
var foundRelated = false;
var devpropsID = [];
var devpropsDesc = [];
var selectedIDs = [];
for (let i = 0; i < threatData.properties.length; i++) {
for (let j = 0; j < threatData.properties[i].threats.length; j++) {
if (threatTitle == threatData.properties[i].threats[j].id) {
for (let k = 0; k < threatData.properties[i].threats.length; k++) {
if (threatTitle == threatData.properties[i].threats[k].id) {
devpropsID.push(threatData.properties[i].id);
devpropsDesc.push(threatData.properties[i].text);
continue;
}
selectedIDs.push(threatData.properties[i].threats[k].id);
foundRelated = true;
}
}
}
}
var threatsDiv = document.getElementById("relatedthreats");
var propertiesDiv = document.getElementById("devprops");
if (selectedIDs.length != 0) {
var threatsDiv_inner = "<h2>Related Threats:</h2>";
for (var i = 0; i < selectedIDs.length - 1; i++) {
threatsDiv_inner +=
"<a href='/threats/" +
selectedIDs[i] +
".html'>" +
selectedIDs[i] +
"</a>, ";
}
threatsDiv_inner +=
"<a href='/threats/" +
selectedIDs[selectedIDs.length - 1] +
".html'>" +
selectedIDs[selectedIDs.length - 1] +
"</a>";
threatsDiv.innerHTML = threatsDiv_inner;
}
var propertiesDiv_inner = "";
for (var i = 0; i < devpropsID.length; i++) {
propertiesDiv_inner +=
"<a href='/properties-mapper/?id=" +
devpropsID[i] +
"'>" +
devpropsID[i] +
"</a>" +
" - " +
devpropsDesc[i] +
"<br>";
}
propertiesDiv.innerHTML = propertiesDiv_inner;
}
// Get mitigations for the current threat
function getRelatedMitigations(threatTitle) {
var mitigations = [];
for (let i = 0; i < mitigationsData.threats.length; i++) {
if (threatTitle == mitigationsData.threats[i].id) {
for (let j = 0; j < mitigationsData.threats[i].mitigations.length; j++) {
mitigations.push({
id: mitigationsData.threats[i].mitigations[j].id,
desc: mitigationsData.threats[i].mitigations[j].text,
level: mitigationsData.threats[i].mitigations[j].level
});
}
}
}
var mitigationsDiv = document.getElementById("mitigations");
if (mitigations.length != 0) {
var foundational = [];
var intermediate = [];
var leading = [];
for (var i = 0; i < mitigations.length; i++) {
var mitigationText =
"<a href='/mitigations/" +
mitigations[i].id +
".html'>" +
mitigations[i].id +
"</a>" +
" - " +
mitigations[i].desc;
if (mitigations[i].level === "foundational") {
foundational.push(mitigationText);
} else if (mitigations[i].level === "intermediate") {
intermediate.push(mitigationText);
} else if (mitigations[i].level === "leading") {
leading.push(mitigationText);
}
}
var mitigationsDiv_inner = "<h2>Mitigations:</h2>";
mitigationsDiv_inner +=
'<table class="mitigationsTableOnThreat"><tr class="mitigationTableoOnThreatRow"><th>Foundational</th><th>Intermediate</th><th>Leading</th></tr>';
mitigationsDiv_inner += "<tr>";
mitigationsDiv_inner +=
'<td>' + foundational.join("<br>") + "</td>";
mitigationsDiv_inner +=
'<td>' + intermediate.join("<br>") + "</td>";
mitigationsDiv_inner +=
'<td>' + leading.join("<br>") + "</td>";
mitigationsDiv_inner += "</tr></table>";
mitigationsDiv.innerHTML = mitigationsDiv_inner;
}
}