-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateContext.js
412 lines (393 loc) · 15.3 KB
/
generateContext.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
const createEndPoints = ["searchContext.create", "maskContext.create", "files/fileSearchContext.create", "files/fileMaskContext.create", "files/fileSearchContext.mask"]
const destroyEndPoints = ["searchContext.destroy", "maskContext.destroy", "files/fileSearchContext.destroy", "files/fileMaskContext.destroy"]
var counter;
var point;
let contextNames = [];
function generateUniqueID() {
let id = () => {
return crypto.randomUUID();
}
contextNames.push("SearchContext_"+id());
contextNames.push("MaskContext_"+id());
contextNames.push("FileSearchContext_"+id());
contextNames.push("FileMaskContext_"+id());
}
function updatePayload() {
var firstPage = document.getElementById("first-page");
var secondPage = document.getElementById("second-page");
firstPage.style.display = "none";
secondPage.style.display = "block";
counter = 0;
document.getElementById("payloadText").value = JSON.stringify(generateContext(createEndPoints[counter]));
var par = document.getElementById("valueofendpoint");
par.innerHTML = createEndPoints[counter];
}
function parseMultipartResponse(bytes) {
lastSplit = 0;
sections = [];
addThis = false;
for (i = 0; i < bytes.length - 3; i++) {
if (addThis && bytes[i] == 13 && bytes[i + 1] == 10 && bytes[i + 2] == 45 && bytes[i + 3] == 45) {
sections.push(Uint8Array.from(bytes.slice(lastSplit + 4, i)));
lastSplit = i;
addThis = false;
continue;
}
if (bytes[i] == 13 && bytes[i + 1] == 10 && !addThis) {
blocal = bytes.slice(lastSplit, i);
sLocal = String.fromCharCode(...blocal);
if (sLocal.startsWith("\r\ncontent-transfer-encoding: binary")) {
addThis = true;
}
lastSplit = i;
}
}
return sections;
}
function generateMaskRule(type, i) {
var json = {}
switch (type) {
case "FPE AES256 Encryption":
json['name'] = type + "Rule" + i.toString();
json['type'] = "cosort";
json['expression'] = "enc_fp_aes256_alphanum(${F})";
return json;
case "Hash":
json['name'] = type + "Rule" + i.toString();
json['type'] = "cosort";
json['expression'] = "hash_sha2(${F})";
return json;
case "Redaction":
json['name'] = type + "Rule" + i.toString();
json['type'] = "cosort";
json['expression'] = "replace_chars(${F})";
return json;
case "AES256 Encryption":
json['name'] = type + "Rule" + i.toString();
json['type'] = "cosort";
json['expression'] = "enc_aes256(${F})";
return json;
default:
return json;
}
}
function generateMaskRuleMatchers(maskRule, searchMatcher, i) {
var json = {};
json['name'] = maskRule + "RuleMatcher";
json['type'] = "name";
json['rule'] = searchMatcher + "Rule" + i.toString();
json['pattern'] = maskRule;
return json;
}
function generateSearchMatcher(type) {
var json = {}
switch (type) {
case "US SSN":
json['name'] = "US SSN";
json['type'] = "pattern";
json['pattern'] = "\\b(\\d{3}[-]?\\d{2}[-]?\\d{4})\\b";
break;
case "Emails":
json['name'] = "Emails";
json['type'] = "pattern";
json['pattern'] = "\\b[\\w._%+-]+@[\\w.-]+\\.[A-Za-z]{2,4}\\b";
break;
case "Credit Cards":
json['name'] = "Credit Cards";
json['type'] = "pattern";
json['pattern'] = "\\b(4\\d{12}(\\d{3})?)|(5[1-5]\\d{14})|(3[47]\\d{13})|(3(0[0-5]|[68]\\d)\\d{11})|(6(011|5\\d{2})\\d{12})|((2131|1800|35\\d{3})\\d{11})|(8\\d{15})\\b";
break;
case "US Phone Numbers":
json['name'] = "US Phone Numbers";
json['type'] = "pattern";
json['pattern'] = "\\b(\\+?1?([ .-]?)?)?(\\(?([2-9]\\d{2})\\)?([ .-]?)?)([2-9]\\d{2})([ .-]?)(\\d{4})(?: #?[eE][xX][tT]\.? \\d{2,6})?\\b";
break;
case "International Phone Numbers":
json['name'] = "International Phone Numbers";
json['type'] = "pattern";
json['pattern'] = "\\b\\+?(1|2[1-689]\\d|2[07]|3[0-469]|3[578]\\d|4[0-13-9]|42\\d|5[09]\\d|5[1-8]|6[0-6]|6[7-9]\\d|7|8[035789]\\d|8[1246]|9[0-58]|9[679]\\d)(\\d+)\\b";
break;
case "American_Dates":
json['name'] = "American_Dates";
json['type'] = "pattern";
json['pattern'] = "\\b([0]\\d|[1][012])[-/.]?([012]\\d|[3][01])[-/.]?(\\d{4})\\b"
break;
case "European_Dates":
json['name'] = "European_Dates";
json['type'] = "pattern";
json['pattern'] = "\\b([012]\\d|[3][01])[-/.]?([0]\\d|[1][012])[-/.]?(\\d{4})\\b"
break;
case "URL":
json['name'] = "URL";
json['type'] = "pattern";
json['pattern'] = "\\b(\\w+):\\/\\/([\\w\\.-@]+)\.([A-Za-z\\.]{2,6})([\\/\w \\(\\)\\.-]*)*\\/?\\b"
break;
case "VIN":
json['name'] = "VIN";
json['type'] = "pattern";
json['pattern'] = "\\b([A-HJ-NPR-Z\\d]{3})([A-HJ-NPR-Z\\d]{5})([\\dX])(([A-HJ-NPR-Z\\d])([A-HJ-NPR-Z\\d])([A-HJ-NPR-Z\\d]{6}))\\b"
break;
case "US_Postal_Code":
json['name'] = "US_Postal_Code";
json['type'] = "pattern";
json['pattern'] = "\\b\\d{5}(?:-\\d{4})?\\b"
break;
case "IP_Address":
json['name'] = "IP_Address";
json['type'] = "pattern";
json['pattern'] = "\\b((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\b"
break;
case "SIN_Canada":
json['name'] = "SIN_Canada";
json['type'] = "pattern";
json['pattern'] = "\\b(\\d{3}[-]?\\d{3}[-]?\\d{3})\\b"
break;
case "Spain DNI":
json['name'] = "Spain DNI";
json['type'] = "pattern";
json['pattern'] = "\\b(\\d{8}-?[A-Z])\\b"
break;
case "Spain NIE":
json['name'] = "Spain NIE";
json['type'] = "pattern";
json['pattern'] = "\\b[XY]-?\\d{7,8}-?[A-Z]\\b"
break;
case "PIN_MEXICO":
json['name'] = "PIN_MEXICO";
json['type'] = "pattern";
json['pattern'] = "\\b([A-Z][AEIOUX][A-Z]{2}\\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\\d|3[01])[HM](?:AS|B[CS]|C[CLMSH]|D[FG]|G[TR]|HG|JC|M[CNS]|N[ETL]|OC|PL|Q[TR]|S[PLR]|T[CSL]|VZ|YN|ZS)[B-DF-HJ-NP-TV-Z]{3}[A-Z\\d])(\\d)\\b"
break;
case "PIN_AUSTRALIA":
json['name'] = "PIN_AUSTRALIA";
json['type'] = "pattern";
json['pattern'] = "\\b\\d{8,9}\\b"
break;
case "PIN_JAPAN":
json['name'] = "PIN_JAPAN";
json['type'] = "pattern";
json['pattern'] = "\\b\\d{4}-?\\d{4}-?\\d{4}\\b"
break;
case "PIN_FRANCE":
json['name'] = "PIN_FRANCE";
json['type'] = "pattern";
json['pattern'] = "\\b\\d{12}\\s?\\d{2}\\b"
break;
case "PIN_UK_NHS":
json['name'] = "PIN_UK_NHS";
json['type'] = "pattern";
json['pattern'] = "\\b\\d{3}[-\\s]?\\d{3}[-\\s]?\\d{4}\\b"
break;
case "PIN_UK_NINO":
json['name'] = "PIN_UK_NINO";
json['type'] = "pattern";
json['pattern'] = "\\b([A-Z]{2}\\d{6}[A-Z]{1})\\b"
break;
case "PIN_SINGAPORE":
json['name'] = "PIN_SINGAPORE";
json['type'] = "pattern";
json['pattern'] = "\\b[STFG]\\d{7}[A-Z]\\b"
break;
case "US Names NER Model":
json['name'] = "US Names NER Model";
json['type'] = "ner";
json['pattern'] = "\\b(\\+?1?([ .-]?)?)?(\\(?([2-9]\\d{2})\\)?([ .-]?)?)([2-9]\\d{2})([ .-]?)(\\d{4})(?: #?[eE][xX][tT]\.? \\d{2,6})?\\b";
json['modelUrl'] = "http://opennlp.sourceforge.net/models-1.5/en-ner-person.bin";
json['sentenceDetectorUrl'] = "http://opennlp.sourceforge.net/models-1.5/en-sent.bin";
json['tokenizerUrl'] = "http://opennlp.sourceforge.net/models-1.5/en-token.bin";
break;
case "US First Names Set File":
json['name'] = "US First Names Set File";
json['type'] = "set";
json['url'] = "https://raw.githubusercontent.com/DevonKoz/sets/main/sets/names/names_first.set"
json["ignoreCase"] = true,
json["matchWholeWords"] = true
break;
case "US Last Names Set File":
json['name'] = "US Last Names Set File";
json['type'] = "set";
json['url'] = "https://raw.githubusercontent.com/DevonKoz/sets/main/sets/names/names_last.set"
json["ignoreCase"] = true,
json["matchWholeWords"] = true
default:
break;
}
return json;
}
function generateContext(endPoint) {
//endPoint = getElementValue("endpoint");
switch (endPoint) {
case "searchContext.create":
var searchContext = {
name: contextNames[0],
matchers: []
};
for (const [key, value] of map.entries()) {
searchContext.matchers.push(generateSearchMatcher(key));
}
return searchContext;
case "searchContext.destroy":
var deleteContext = { name: contextNames[0] };
return deleteContext;
case "maskContext.create":
var maskContext = {
name: contextNames[1],
rules: [],
ruleMatchers: []
};
var i = 0;
for (const [key, value] of map.entries()) {
i++;
maskContext.rules.push(generateMaskRule(value, i));
maskContext.ruleMatchers.push(generateMaskRuleMatchers(key, value, i));
}
return maskContext;
case "maskContext.destroy":
var deleteContext = { name: contextNames[1] };
return deleteContext;
case "searchContext.search":
var context = {
name: contextNames[0],
text: document.getElementById("textForBaseAPI").value
}
return context;
case "searchContext.mask":
var context = {
searchContextName: contextNames[0],
maskContextName: contextNames[1],
text: document.getElementById("textForBaseAPI").value
}
return context;
case "maskContext.mask":
var context = {
maskContextName: contextNames[1],
text: document.getElementById("textForBaseAPI").value
}
return context;
case "files/fileSearchContext.create":
var context = {
"name": contextNames[2],
"matchers": [
{
"name": contextNames[0],
"type": "searchContext"
}
]
}
return context;
case "files/fileMaskContext.create":
var context = {
"name": contextNames[3],
"rules": [
{
"name": contextNames[1],
"type": "maskContext"
}
]
}
return context;
case "files/fileSearchContext.destroy":
var context = { name: contextNames[2] };
return context;
case "files/fileMaskContext.destroy":
var context = { name: contextNames[3] };
return context;
case "files/fileSearchContext.search":
case "files/fileMaskContext.mask":
case "files/fileSearchContext.mask":
var context =
{
"fileSearchContextName": contextNames[2],
"fileMaskContextName": contextNames[3]
}
return context;
default:
return "";
}
}
function next() {
if (window.sessionStorage.getItem('api_type') === 'darkshield-base') {
nextText();
} else if (window.sessionStorage.getItem('api_type') === 'darkshield-files') {
nextFile();
}
}
function nextFile() {
var btn = document.getElementById("process-payload");
var fileUpload = document.getElementsByClassName("file-upload");
var par = document.getElementById("valueofendpoint");
sendRequest(createEndPoints[counter]);
if (counter < 4) {
counter++;
}
if (counter === 4) {
for (var i = 0; i < fileUpload.length; i++) {
fileUpload[i].style.display = "block";
}
btn.innerHTML = "Search and Mask";
}
par.innerHTML = createEndPoints[counter];
document.getElementById("payloadText").value = JSON.stringify(generateContext(createEndPoints[counter]));
}
function nextText() {
var textUpload = document.getElementsByClassName("text-upload");
var par = document.getElementById("valueofendpoint");
var btn = document.getElementById("process-payload");
var payload = document.getElementById("payloadText");
if (counter === 2) {
sendRequest("searchContext.mask");
}
if (counter < 2) {
sendRequest(createEndPoints[counter]);
counter++;
}
if (counter === 2) {
for (var i = 0; i < textUpload.length; i++) {
textUpload[i].style.display = "block";
}
btn.innerHTML = "Search and Mask";
par.innerHTML = "searchContext.mask";
payload.value = JSON.stringify(generateContext("searchContext.mask"));
btn.disabled = true;
document.getElementById("btn-set-text").style.display = "block";
} else {
par.innerHTML = createEndPoints[counter];
payload.value = JSON.stringify(generateContext(createEndPoints[counter]));
}
}
function reset() {
counter--;
while (counter >= 0) {
document.getElementById("payloadText").value = JSON.stringify(generateContext(destroyEndPoints[counter]));
sendRequest(destroyEndPoints[counter]);
counter--;
}
document.getElementById("payloadText").value = '';
document.getElementById("textForBaseAPI").value = '';
clearRulePair();
toggleOff();
var firstPage = document.getElementById("first-page");
var secondPage = document.getElementById("second-page");
firstPage.style.display = "block";
secondPage.style.display = "none";
var fileUpload = document.getElementsByClassName("file-upload");
for (var i = 0; i < fileUpload.length; i++) {
fileUpload[i].style.display = "none";
}
var btn = document.getElementById("process-payload");
btn.innerHTML = "Next";
var textUpload = document.getElementsByClassName("text-upload");
for (var i = 0; i < textUpload.length; i++) {
textUpload[i].style.display = "none";
}
btn.disabled = false;
}
function setText() {
let textContent = document.getElementById("textForBaseAPI").value;
let context = {
searchContextName: contextNames[0],
maskContextName: contextNames[1],
text: textContent
}
document.getElementById("payloadText").value = JSON.stringify(context);
document.getElementById("process-payload").disabled = false;
}