-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
164 lines (134 loc) · 5.87 KB
/
index.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
'use strict';
var doc = document.implementation.createHTMLDocument('');
var div = doc.createElement('div');
function removeHTML(htmlString) {
div.innerHTML = htmlString;
var elements = div.querySelectorAll('*')
while (elements.length) {
div.innerHTML = div.innerText;
elements = div.querySelectorAll('*');
}
return div.innerText;
}
function escapeHTML(htmlString) {
try {
return htmlString.toString().replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
}
catch(e) {
return '';
}
}
function safeHTML(htmlString) {
div.innerHTML = htmlString;
for (var elements = div.querySelectorAll('*'), i = elements.length - 1; i >= 0; i--) {
var element = elements[i],
tagName = element.localName;
if (tagName == 'script' || tagName == 'noscript' || tagName == 'noembed' || !(element.attributes instanceof NamedNodeMap)) {
try {
element.parentNode.removeChild(element);
}
catch(e) {
element.outerHTML = '';
}
continue;
}
if (!element.hasAttributes())
continue;
for (var attributes = element.attributes, j = attributes.length - 1; j >= 0; j--) {
var attribute = attributes[j],
attributeName = attribute.localName,
attributeValue = attribute.value.replace(/[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205f\u3000]/g, '').toLowerCase().trim();
// Remove insecure attribute starting "on*" (example: <img src='' onerror=alert(1)>)
if (attributeName.indexOf('on') == 0)
element.removeAttribute(attributeName);
// Remove insecure src/href attribute with value starting "javascript:*" (example: href="javascript:alert(1)")
else if ((attributeName == 'src' || attributeName == 'href') && attributeValue.indexOf('javascript:') == 0)
element.removeAttribute(attributeName);
// For non-specific tags remove insecure src/data attribute with value starting "data:*" (example: <embed src="data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==">)
else if (['audio', 'image', 'img', 'source', 'video'].indexOf(tagName) == -1 && (attributeName == 'src' || attributeName == 'data') && attributeValue.indexOf('data:') == 0)
element.removeAttribute(attributeName);
}
}
return div.innerHTML;
}
// Export as VUE.js plugin with directives v-html-remove, v-html-escape, v-html-safe
// Usage example:
// import VueSecureHTML from 'VueSecureHTML.js';
// Vue.use(VueSecureHTML);
// <teplate><div v-html-remove="message"></div><div v-html-escape="message"></div><div v-html-safe="message"></div></teplate>
// See directive hooks difference between Vue 2.x and Vue 3.x: https://v3-migration.vuejs.org/breaking-changes/custom-directives.html
module.exports = {
install : function (Vue, options) {
Vue.directive('html-remove', {
// For Vue 2.x
inserted : function (el, binding) {
el.innerHTML = removeHTML(binding.value);
},
// For Vue 3.x
mounted : function (el, binding) {
el.innerHTML = removeHTML(binding.value);
},
// For Vue 2.x
update : function (el, binding) {
if (binding.value !== binding.oldValue)
el.innerHTML = removeHTML(binding.value);
},
// For Vue 3.x
updated : function (el, binding) {
if (binding.value !== binding.oldValue)
el.innerHTML = removeHTML(binding.value);
},
});
Vue.directive('html-escape', {
// For Vue 2.x
inserted : function (el, binding) {
el.innerHTML = escapeHTML(binding.value);
},
// For Vue 3.x
mounted : function (el, binding) {
el.innerHTML = escapeHTML(binding.value);
},
// For Vue 2.x
update : function (el, binding) {
if (binding.value !== binding.oldValue)
el.innerHTML = escapeHTML(binding.value);
},
// For Vue 3.x
updated : function (el, binding) {
if (binding.value !== binding.oldValue)
el.innerHTML = escapeHTML(binding.value);
},
});
Vue.directive('html-safe', {
// For Vue 2.x
inserted : function (el, binding) {
el.innerHTML = safeHTML(binding.value);
},
// For Vue 3.x
mounted : function (el, binding) {
el.innerHTML = safeHTML(binding.value);
},
// For Vue 2.x
update : function (el, binding) {
if (binding.value !== binding.oldValue)
el.innerHTML = safeHTML(binding.value);
},
// For Vue 3.x
updated : function (el, binding) {
if (binding.value !== binding.oldValue)
el.innerHTML = safeHTML(binding.value);
},
});
}
}
// Also export separate functions
// Usage example:
// import VueSecureHTML from 'VueSecureHTML.js';
// Vue.prototype.$removeHTML = VueSecureHTML.removeHTML;
// Vue.prototype.$escapeHTML = VueSecureHTML.escapeHTML;
// Vue.prototype.$safeHTML = VueSecureHTML.safeHTML;
// A) <teplate><div>{{ $removeHTML(message) }}</div><div>{{ $escapeHTML(message) }}</div><div>{{ $safeHTML(message) }}</div></teplate>
// B) <script>var message = 'My <b>secure part</b> and user-provided insecure part: ' + $removeHTML("<img src='' onerror=alert('XSS!')>");</script> + <teplate><div v-html="message"></div></teplate>
module.exports.removeHTML = removeHTML;
module.exports.escapeHTML = escapeHTML;
module.exports.safeHTML = safeHTML;