-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
160 lines (139 loc) · 4.62 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
<!DOCTYPE html>
<html>
<head>
<title>DOM XSS Example with Prevention</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 20px auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
}
h2 {
color: #333;
border-bottom: 2px solid #eee;
padding-bottom: 10px;
}
h3 {
color: #666;
margin-top: 0;
}
.warning {
color: #721c24;
background-color: #f8d7da;
border: 1px solid #f5c6cb;
padding: 10px;
border-radius: 4px;
margin-bottom: 15px;
}
.input-group {
margin-bottom: 15px;
}
textarea {
width: 100%;
padding: 8px;
margin: 8px 0;
border: 1px solid #ddd;
border-radius: 4px;
resize: vertical;
min-height: 100px;
}
button {
background-color: #007bff;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.2s;
}
button:hover {
background-color: #0056b3;
}
.result-box {
border: 1px solid #ddd;
padding: 15px;
border-radius: 4px;
background-color: #fff;
min-height: 100px;
margin-top: 10px;
}
</style>
</head>
<body>
<h2>Content Editor with XSS Prevention</h2>
<!-- Vulnerable Implementation -->
<div class="container" id="vulnerable-example">
<div class="warning">
Vulnerable Implementation (For Educational Purposes Only - Do Not Use in Production)
</div>
<div class="input-group">
<h3>Unsafe Editor</h3>
<textarea id="unsafeInput" placeholder="Enter your content here...">Hello <b>World</b>!</textarea>
<button onclick="previewVulnerable()">Preview Content</button>
</div>
<div class="result-box" id="unsafeResult"></div>
</div>
<!-- Safe Implementation -->
<div class="container" id="safe-example">
<div class="input-group">
<h3>Safe Editor</h3>
<textarea id="safeInput" placeholder="Enter your content here...">Hello <b>World</b>!</textarea>
<button onclick="previewSafe()">Preview Content (Safe)</button>
</div>
<div class="result-box" id="safeResult"></div>
</div>
<script>
// Vulnerable implementation - DO NOT USE
function previewVulnerable() {
const userInput = document.getElementById('unsafeInput').value;
// UNSAFE: Direct innerHTML assignment
document.getElementById('unsafeResult').innerHTML = userInput;
}
// Safe implementation
function previewSafe() {
const userInput = document.getElementById('safeInput').value;
const safeResult = document.getElementById('safeResult');
// Clear previous content
safeResult.textContent = '';
// Safely escape HTML and create text node
const escapedContent = escapeHTML(userInput);
const textNode = document.createTextNode(escapedContent);
safeResult.appendChild(textNode);
}
// HTML Escaping function
function escapeHTML(str) {
return str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
// Initialize with example content
previewSafe();
previewVulnerable();
function setAdminCookie(secretval) {
document.cookie = 'adminsecret=' + secretval;
alert('Admin cookie set successfully with value: ' + secretval);
}
</script>
<!-- Security Headers (to be added server-side) -->
<!--
Content-Security-Policy: default-src 'self';
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
-->
<!-- set secret -->
<input type="text" id="secret" value="secretvalue">
<button onclick="setAdminCookie(document.getElementById('secret').value)">Set Admin Cookie</button>
</body>
</html>