forked from rpetrich/deciduous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
852 lines (839 loc) · 33.3 KB
/
index.html
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width", initial-scale=1, maximum-scale=1>
<meta name="description" property="og:description" content="Deciduous is a security decision tree generator that serves as a threat modeling tool for engineering teams who want to create attack trees in the spirit of Security Chaos Engineering">
<meta name="author" content="Ryan Petrich, Kelly Shortridge">
<meta property="og:description" content="Deciduous is a security decision tree generator that serves as a threat modeling tool for engineering teams who want to create attack trees in the spirit of Security Chaos Engineering" />
<meta property="og:title" content="Deciduous - Security Decision Tree Generator" />
<meta property="og:image" content="./deciduous-logo-social.jpg" />
<meta property="og:url" content="https://swagitda.com/deciduous/" />
<meta name="twitter:card" content="summary_large_image"></meta>
<meta name="twitter:site" content="@swagitda_" />
<meta name="twitter:creator" content="@rpetrich" />
<meta name="twitter:image" content="https://swagitda.com/deciduous/deciduous-logo-social.jpg" />
<title>Deciduous - Security Decision Tree Generator</title>
<script src="https://cdn.jsdelivr.net/npm/@hpcc-js/wasm/dist/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js-yaml.min.js"></script>
<link rel="shortcut icon" href="./favicon.ico" type="image/x-icon" />
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Josefin+Sans&display=swap" rel="stylesheet">
<style type="text/css">
body, html {
padding: 0;
margin: 0;
overflow: hidden;
font-family: "Helvetica Neue", Helvetica, arial, sans-serif;
}
a {
color: #1B2CC1;
}
a:visited {
color: #FF499B;
}
#leftPane {
position: absolute;
width: 40%;
height: 100%;
}
.sectionHeader {
position: absolute;
width: 100%;
top: 0px;
left: 0px;
height: 40px;
font-family: 'Josefin Sans', sans-serif;
font-size: 16pt;
color: #0E1660;
margin: 0;
border-bottom: 1px solid silver;
padding-left: 20px;
padding-top: 9px;
box-sizing: border-box;
}
#inputSource, #highlighting {
position: absolute;
width: 100%;
height: 100%;
margin: 0;
font-size: 12pt;
padding: 20px;
line-height: 20px;
box-sizing: border-box;
overflow: auto;
white-space: pre;
text-size-adjust: none;
border: medium none;
border-top: 40px solid transparent;
}
#inputSource {
resize: none;
appearance: none;
-moz-appearance: none;
-webkit-appearance: none;
outline: 0;
border-right: 1px solid silver;
z-index: 1;
color: transparent;
background: transparent;
caret-color: black;
}
#highlighting {
z-index: 0;
}
#inputSource, code {
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
}
code {
padding: 0;
}
#errorTarget {
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
}
.contentArea {
position: absolute;
left: 40%;
width: 60%;
height: 100%;
overflow: auto;
font-family: 'Josefin Sans', sans-serif;
}
.right-header {
display: flex;
width: 100%;
height: auto;
}
.logo {
margin-right: 1em;
margin-bottom: 1em;
}
.downloadWrapper {
padding: 5px;
flex: 1;
}
#downloadLink, #downloadSvgLink {
float: right;
top: 0;
margin: 5px;
}
#renderTarget {
display: inline-block;
position: relative;
width: 100%;
padding-bottom: 100%;
vertical-align: top;
}
svg {
display: inline-block;
position: absolute;
top: 0;
left: 0;
}
.copyright {
text-align: center;
}
#highlighting {
color: grey;
}
#highlighting .punctuation {
color: grey;
}
#highlighting .attacks, #highlighting .attack {
color: #ff1c82;
}
#highlighting .mitigations, #highlighting .mitigation {
color: #2b56b5;
}
#highlighting .facts, #highlighting .fact {
color: #1b0533;
}
#highlighting .goals, #highlighting .goal {
color: #5f00c2;
}
#highlighting .comment {
}
#dragger {
cursor: col-resize;
position: absolute;
height: 100%;
width: 3px;
}
@media print {
.logo {
position: absolute;
left: 0;
top: 0;
}
.contentArea {
position: absolute;
width: 100%;
height: auto;
overflow: visible;
}
svg {
position:fixed;
top:0;
bottom:0;
left:0;
right:0;
width: 100%;
height: 100%;
}
#leftPane, #dragger {
display: none;
}
#inputSource {
display: none;
}
#inputHighlighted {
display: none;
}
.downloadWrapper {
display: none;
}
.copyright {
position: fixed;
bottom: 0;
}
</style>
</head>
<body>
<div id="leftPane">
<h2 class="sectionHeader">Text Editor</h2>
<textarea id="inputSource" spellcheck="false">
title: (Example) Attack Tree for S3 Bucket with Video Recordings
facts:
- wayback: API cache (e.g. Wayback Machine)
from:
- reality: '#yolosec'
- public_bucket: S3 bucket set to public
from:
- bucket_search: '#yolosec'
- subsystem_with_access: Subsystem with access to bucket data
from:
- compromise_user_creds
attacks:
- bucket_search: AWS public buckets search
from:
- disallow_crawling
- brute_force:
from:
- private_bucket
- phishing:
from:
- private_bucket
- internal_only_bucket:
backwards: true
- access_control_server_side:
backwards: true
- compromise_user_creds: Compromise user credentials
from:
- brute_force
- phishing
- analyze_web_client: Manually analyze web client for access control misconfig
from:
- lock_down_acls
- compromise_admin_creds: Compromise admin creds
from:
- phishing
- compromise_aws_creds: Compromise AWS admin creds
from:
- phishing
- intercept_2fa: Intercept 2FA
from:
- 2fa
- ssh_to_public_machine: SSH to an accessible machine
from:
- compromise_admin_creds: '#yolosec'
- compromise_aws_creds:
- intercept_2fa
- lateral_movement_to_machine_with_access: Lateral movement to machine with access to target bucket
from:
- ip_allowlist_for_ssh
- compromise_presigned: Compromise presigned URLs
from:
- phishing
- compromise_quickly: Compromise URL within N time period
from:
- short_lived_presigning
- recon_on_s3: Recon on S3 buckets
from:
- private_bucket
- disallow_bucket_urls:
backwards: true
- 2fa:
backwards: true
- find_systems_with_access: Find systems with R/W access to target bucket
from:
- recon_on_s3: '#yolosec'
- exploit_known_vulns: Exploit known 3rd party library vulns
from:
- find_systems_with_access
- buy_0day:
from:
- vuln_scanning
- discover_0day: Manual discovery of 0day
from:
- vuln_scanning
- exploit_vulns: Exploit vulns
from:
- buy_0day
- discover_0day
- aws_0day: 0day in AWS multitenant systems
from:
- ips
- supply_chain_backdoor: Supply chain compromise (backdoor)
from:
- single_tenant_hsm
mitigations:
- disallow_crawling: Disallow crawling on site maps
from:
- reality
- private_bucket: Auth required / ACLs (private bucket)
from:
- reality
- lock_down_acls: Lock down web client with creds / ACLs
from:
- subsystem_with_access
- access_control_server_side: Perform all access control server side
from:
- analyze_web_client
- 2fa: 2FA
from:
- compromise_admin_creds: '#yolosec'
- compromise_aws_creds
- ip_allowlist_for_ssh: IP allowlist for SSH
from:
- ssh_to_public_machine
- short_lived_presigning: Make URL short lived
from:
- compromise_presigned
- disallow_bucket_urls: Disallow the use of URLs to access buckets
from:
- compromise_quickly
- vuln_scanning: 3rd party library checking / vuln scanning
from:
- exploit_known_vulns
- ips: Exploit prevention / detection
from:
- exploit_vulns
- single_tenant_hsm: Use single tenant AWS HSM
from:
- aws_0day:
implemented: false
- internal_only_bucket: No public system has R/W access (internal only)
from:
- find_systems_with_access
goals:
- s3_asset: Access video recordings in S3 bucket (attackers win)
from:
- wayback: '#yolosec'
- public_bucket
- subsystem_with_access
- analyze_web_client
- lateral_movement_to_machine_with_access
- compromise_presigned
- compromise_quickly
- exploit_vulns
- aws_0day
- supply_chain_backdoor
- company_bank_account: Access company bank account
from:
- intercept_2fa
# filter can be used to show only paths that flow through specific nodes
filter:
- s3_asset
</textarea>
<pre id="highlighting" aria-hidden="true"><code id="inputHighlighted"></code></pre>
</div>
<div class="contentArea" id="rightPane">
<div class="right-header">
<div class="logo">
<img src="./deciduous-logo.png" alt="Deciduous logo" height="75" width="198">
</div>
<div class="downloadWrapper">
<p>Inspired by and with example taken from Kelly Shortridge's <a href="https://swagitda.com/blog/posts/security-decision-trees-with-graphviz/">Creating Security Decision Trees With Graphviz</a></p>
<a id="downloadLink" download="decision-tree.dot">Download .dot</a>
<a id="downloadSvgLink" download="decision-tree.svg">Download .svg</a>
</div>
</div>
<div id="errorTarget">JavaScript is required</div>
<div id="renderTarget"></div>
<p class="copyright">© Copyright 2021 Ryan Petrich & Kelly Shortridge</p>
</div>
<div id="dragger"></div>
<script>
function wordwrap(text, limit) {
text = String(text);
if (text.indexOf("\n") != -1) {
return text;
}
const split = text.split(" ");
let all = [];
let current = [];
let currentLength = 0;
for (let i = 0; i < split.length; i++) {
const line = split[i];
if (currentLength == 0 || (currentLength + line.length < limit && line[0] != "(")) {
current.push(line);
currentLength += line.length;
} else {
all.push(current.join(" "));
current = [line];
currentLength = line.length;
}
}
all.push(current.join(" "));
return all.join("\n");
}
function mangleName(name) {
if (/^[A-Za-z]\w*$/.test(name)) {
return name;
}
return JSON.stringify(name);
}
function line(name, properties) {
const entries = Object.entries(properties);
if (entries.length == 0) {
return name;
}
return name + " [ " + entries.map(([key, value]) => `${key}=${JSON.stringify(value)}`).join(" ") + " ]";
}
function parseFrom(raw) {
if (typeof raw == "object") {
const [fromName, label] = Object.entries(raw)[0];
return [fromName, label, raw]
}
return [String(raw), null, {}];
}
const themes = {
"classic": {
"edge": "#2B303A",
"edge-text": "#DB2955",
"backwards-edge": "#7692FF",
"reality-fill": "#2B303A",
"reality-text": "#FFFFFF",
"fact-fill": "#C6CCD2",
"attack-fill": "#ED96AC",
"mitigation-fill": "#ABD2FA",
"goal-fill": "#DB2955",
"goal-text": "#FFFFFF",
},
"default": {
"edge": "#2B303A",
"edge-text": "#010065",
"backwards-edge": "#7692FF",
"reality-fill": "#272727",
"reality-text": "#FFFFFF",
"fact-fill": "#D2D5DD",
"attack-fill": "#ffa6d5",
"mitigation-fill": "#B9D6F2",
"goal-fill": "#5f00c2",
"goal-text": "#FFFFFF",
},
}
function convertToDot(yaml) {
const parsed = jsyaml.load(yaml);
const font = 'Arial'
const theme = themes[Object.hasOwnProperty.call(themes, parsed.theme) ? parsed.theme : "default"];
const header = `// Generated from https://swagitda.com/deciduous/
digraph {
// base graph styling
rankdir="TB";
splines=true;
overlap=false;
nodesep="0.2";
ranksep="0.4";
label=${JSON.stringify(String(parsed.title))};
labelloc="t";
fontname=${JSON.stringify(font)};
node [ shape="plaintext" style="filled, rounded" fontname=${JSON.stringify(font)} margin=0.2 ]
edge [ fontname=${JSON.stringify(font)} fontsize=12 color="${theme["edge"]}" ]
// is reality a hologram?
reality [ label="Reality" fillcolor="${theme["reality-fill"]}" fontcolor="${theme["reality-text"]}" ]
`;
const goals = parsed.goals || [];
const facts = parsed.facts || [];
const attacks = parsed.attacks || [];
const mitigations = parsed.mitigations || [];
const filter = parsed.filter || [];
const subgraphs = [];
const forwards = {};
const forwardsAll = {};
const backwards = {};
const allNodes = [...facts, ...attacks, ...mitigations, ...goals];
const types = {};
for (const node of allNodes) {
const [toName] = Object.entries(node)[0];
const fromNames = backwards[toName] || (backwards[toName] = []);
if (node.from) {
for (const from of node.from) {
const [fromName, label, props] = parseFrom(from);
if (!from.backwards && !from.ungrouped) {
const toNames = forwards[fromName] || (forwards[fromName] = []);
toNames.push(toName);
fromNames.push(fromName);
}
const toNames = forwardsAll[fromName] || (forwardsAll[fromName] = []);
toNames.push(toName);
}
}
}
function anyDominates(forwards, d, n) {
// search to see if any nodes in d dominate n
// nodes dominate themselves
const search = [];
const added = {};
for (const other of d) {
added[other] = true;
search.push(other);
}
while ((d = search.shift()) !== undefined) {
if (d === n) {
return true;
}
const others = forwards[d];
if (others !== undefined) {
for (const other of others) {
if (!Object.hasOwnProperty.call(added, other)) {
added[other] = true;
search.push(other);
}
}
}
}
return false;
}
function shouldShow(n) {
if (filter.length == 0 || anyDominates(forwardsAll, filter, n)) {
return true;
}
const arrayN = [n];
return filter.find(other => anyDominates(forwardsAll, arrayN, other));
}
function defaultLabelForName(name) {
return name.replace(/_/g, " ").replace(/^[a-z]/, c => c.toUpperCase());
}
function nodes(type, values, properties) {
const result = [];
for (const value of values) {
const [name, label] = Object.entries(value)[0];
types[name] = type;
if (shouldShow(name)) {
result.push(line(mangleName(name), {
label: wordwrap(label === null ? defaultLabelForName(name) : label, 18),
...properties,
}));
}
}
return result;
}
const allNodeLines = [
`// facts`,
...nodes("fact", facts, {
fillcolor: theme["fact-fill"],
}),
`// attacks`,
...nodes("attack", attacks, {
fillcolor: theme["attack-fill"],
}),
`// mitigations`,
...nodes("mitigation", mitigations, {
fillcolor: theme["mitigation-fill"],
}),
`// goals`,
...nodes("goal", goals, {
fillcolor: theme["goal-fill"],
fontcolor: theme["goal-text"],
})
];
function edges(entries, properties) {
return entries.reduce((edges, value) => {
const [name] = Object.entries(value)[0];
if (!shouldShow(name)) {
return edges;
}
(value.from || []).forEach((from) => {
const [fromName, label, fromProps] = parseFrom(from);
if (!shouldShow(fromName)) {
return;
}
const props = {
...properties,
};
if (label !== null) {
props.xlabel = wordwrap(label, 20);
props.fontcolor = theme["edge-text"];
}
if (typeof fromProps.implemented == "boolean" && !fromProps.implemented) {
props.style = "dotted";
}
if (fromProps.backwards) {
props.style = "dotted";
props.color = theme["backwards-edge"];
props.weight = "0";
}
edges.push(line(`${mangleName(fromName)} -> ${mangleName(name)}`, props));
});
return edges;
}, []);
}
const allEdgeLines = [...edges(goals, {}), ...edges(attacks, {}), ...edges(mitigations, {}), ...edges(facts, {})];
const goalNames = goals.map((goal) => {
const [goalName] = Object.entries(goal)[0];
return goalName;
});
for (const [fromName, toNames] of Object.entries(forwards)) {
if (!shouldShow(fromName)) {
continue;
}
const copy = toNames.concat();
const filteredToNames = [];
for (let i = 0; i < toNames.length; i++) {
copy.splice(i, 1);
if (!anyDominates(forwards, copy, toNames[i]) && goalNames.indexOf(toNames[i]) == -1 && shouldShow(toNames[i])) {
filteredToNames.push(toNames[i]);
}
copy.splice(i, 0, toNames[i]);
}
if (filteredToNames.length > 1) {
subgraphs.push(` subgraph ${mangleName(fromName)}_order {
rank=same;
${filteredToNames.map(toName => mangleName(toName) + ";").join("\n ")}
}
${line(filteredToNames.map(mangleName).join(" -> "), { style: "invis" })}`);
}
}
const shownGoals = goalNames.filter(shouldShow);
if (shownGoals > 1) {
subgraphs.push(` subgraph goal_order {
rank=same;
${shownGoals.map(goalName => mangleName(goalName) + ";").join("\n ")}
}`);
subgraphs.push(" " + line(shownGoals.join(" -> "), { style: "invis" }));
}
subgraphs.push(` { rank=min; reality; }`);
for (const node of allNodes) {
const [toName] = Object.entries(node)[0];
if (shouldShow(toName) && !forwards[toName] && shownGoals.indexOf(toName) === -1) {
for (const goalName of shownGoals) {
subgraphs.push(" " + line(mangleName(toName) + " -> " + mangleName(goalName), { style: "invis", weight: 0 }));
}
}
}
subgraphs.push(` { rank=max; ${shownGoals.map(goalName => mangleName(goalName) + "; ").join("")}}`);
const footer = "\n\n}\n";
return [header + " " + allNodeLines.join("\n ") + "\n\n " + allEdgeLines.join("\n ") + "\n\n // subgraphs to give proper layout\n" + subgraphs.join("\n\n") + footer, parsed.title, types];
}
const renderTarget = document.getElementById("renderTarget");
const errorTarget = document.getElementById("errorTarget");
const inputSource = document.getElementById("inputSource");
const downloadLink = document.getElementById("downloadLink");
const downloadSvgLink = document.getElementById("downloadSvgLink");
const inputHighlighted = document.getElementById("inputHighlighted");
const highlighting = document.getElementById("highlighting");
function syncScroll() {
highlighting.scrollTop = inputSource.scrollTop;
highlighting.scrollLeft = inputSource.scrollLeft;
}
window["@hpcc-js/wasm"].graphvizSync().then(graphviz => {
let lastInput = "";
let lastObjectURL = "";
let lastSvgObjectURL = "";
let types = {};
function rerender() {
syncScroll();
const newInput = inputSource.value;
if (newInput != lastInput) {
lastInput = newInput;
if (window.localStorage) {
localStorage.setItem("deciduous-content", newInput);
}
function updateTextArea(){
types["attacks"] = "attacks";
types["mitigations"] = "mitigations";
types["goals"] = "goals";
types["facts"] = "facts";
types["reality"] = "facts";
let current = "";
let i = 0;
const childNodes = inputHighlighted.childNodes;
function insertTextNode(text) {
if (i < childNodes.length) {
const node = childNodes[i];
if (node.nodeType === Node.TEXT_NODE) {
if (node.textContent !== text) {
node.textContent = text;
}
} else {
inputHighlighted.replaceChild(document.createTextNode(text), node);
}
} else {
inputHighlighted.appendChild(document.createTextNode(text));
}
i++;
}
function insertSpan(text, className) {
if (i < childNodes.length) {
const node = childNodes[i];
if (node.nodeType === Node.ELEMENT_NODE) {
if (node.textContent !== text) {
node.textContent = text;
}
if (node.className !== className) {
node.className = className
}
} else {
const span = document.createElement("span");
span.className = className;
span.textContent = text;
inputHighlighted.replaceChild(span, node);
}
} else {
const span = document.createElement("span");
span.className = className;
span.textContent = text;
inputHighlighted.appendChild(span);
}
i++;
}
for (const token of (newInput[newInput.length-1] == "\n" ? newInput + " " : newInput).split(/\b/)) {
if (Object.hasOwnProperty.call(types, token)) {
insertTextNode(current);
current = "";
insertSpan(token, types[token]);
} else {
current += token;
}
}
insertTextNode(current);
while (i < childNodes.length) {
inputHighlighted.removeChild(childNodes[i]);
}
// Resync line height
const highlightedHeight = inputHighlighted.getBoundingClientRect().height;
const inputHeight = inputSource.scrollHeight;
if (highlightedHeight > inputHeight) {
// line height hack for Firefox+Windows
inputSource.style.lineHeight = highlightedHeight*20.01/(inputHeight-40) + "px";
}
}
try {
let dot, title;
[dot, title, types] = convertToDot(newInput);
// Syntax highlighting
updateTextArea();
// console.log(dot);
// Render SVG into the document
document.title = `Deciduous - Security Decision Tree Generator (${title})`;
const svg = graphviz.layout(dot, "svg", "dot");
renderTarget.innerHTML = svg;
const svgElement = renderTarget.querySelector("svg");
if (svgElement) {
const scale = 0.75;
svgElement.setAttribute("width", parseInt(svgElement.getAttribute("width"), 10) * scale + "pt");
svgElement.setAttribute("height", parseInt(svgElement.getAttribute("height"), 10) * scale + "pt");
}
// Create a download link
if (window.File && URL.createObjectURL) {
const file = new File([dot], "graph.dot", {
"type": "text/vnd.graphviz",
});
downloadLink.download = title + ".dot";
const newObjectURL = URL.createObjectURL(file);
downloadLink.href = newObjectURL;
if (lastObjectURL != "") {
URL.revokeObjectURL(lastObjectURL);
}
lastObjectURL = newObjectURL;
const svgFile = new File([svg], "graph.svg", {
"type": "image/svg+xml",
});
downloadSvgLink.download = title + ".svg";
const newSvgObjectURL = URL.createObjectURL(svgFile);
downloadSvgLink.href = newSvgObjectURL;
if (lastSvgObjectURL != "") {
URL.revokeObjectURL(lastSvgObjectURL);
}
lastSvgObjectURL = newSvgObjectURL;
}
// Add quick linky links
for (const title of renderTarget.querySelectorAll("title")) {
title.parentNode.style.cursor = "pointer";
title.parentNode.addEventListener("click", () => {
const node = title.textContent;
const index = lastInput.indexOf("\n- " + node);
if (index != -1) {
inputSource.blur();
inputSource.selectionEnd = inputSource.selectionStart = index + 3;
inputSource.focus();
inputSource.selectionEnd = index + 3 + node.length;
syncScroll();
setTimeout(syncScroll, 0);
}
}, false);
}
// Clear any error text
errorTarget.innerText = "";
} catch (e) {
errorTarget.innerText = String(e);
updateTextArea();
}
}
}
inputSource.addEventListener("change", rerender, false);
inputSource.addEventListener("input", rerender, false);
inputSource.addEventListener("scroll", syncScroll, false);
inputSource.addEventListener("select", syncScroll, false);
inputSource.addEventListener("wheel", syncScroll, false);
inputSource.addEventListener("focus", syncScroll, false);
inputSource.addEventListener("blur", syncScroll, false);
inputSource.addEventListener("keydown", syncScroll, false);
inputSource.addEventListener("keyup", syncScroll, false);
if (window.localStorage) {
const content = localStorage.getItem("deciduous-content");
if (content !== null && content !== "") {
inputSource.value = content;
}
}
rerender();
// Focus the textarea so that users know it's editable
inputSource.selectionStart = inputSource.selectionEnd = 0;
inputSource.focus();
// Make the dragger draggable
const dragger = document.getElementById("dragger");
dragger.style.left = "40%";
const leftPane = document.getElementById("leftPane");
let currentPercentage = 40;
if (window.localStorage) {
currentPercentage = localStorage.getItem("deciduous-divider") || 40;
}
function updateDivider(percent) {
dragger.style.left = percent + "%";
leftPane.style.width = percent + "%";
rightPane.style.left = percent + "%";
rightPane.style.width = (100 - percent) + "%";
currentPercentage = percent;
}
function mouseMove(e) {
const clientWidth = document.body.clientWidth;
updateDivider(Math.max(Math.min(e.clientX, clientWidth - 200), 200) * 100 / clientWidth);
}
function mouseUp(e) {
document.body.removeEventListener("mousemove", mouseMove);
document.body.removeEventListener("mouseup", mouseUp);
if (window.localStorage) {
localStorage.setItem("deciduous-divider", currentPercentage);
}
}
dragger.addEventListener("mousedown", e => {
document.body.addEventListener("mousemove", mouseMove, false);
document.body.addEventListener("mouseup", mouseUp, false);
}, false);
updateDivider(currentPercentage);
});
</script>
</body>
</html>