Skip to content

Commit 1fa3717

Browse files
authored
Merge pull request #212 from fortunatomaldonado/LPD-33910
LPD-33910 Add sanitization for codeMirror editor
2 parents 88be248 + 799b5af commit 1fa3717

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

plugins/codemirror/dialogs/codemirrordialog.js

+23-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ CKEDITOR.dialog.add('codemirrordialog', function (editor) {
55
var scaley = 0.7;
66
var height = size.height * scaley;
77
var width = size.width * scalex;
8+
var ALERT_REGEX = /alert\((.*?)\)/;
9+
var INNER_HTML_REGEX = /innerHTML\s*=\s*.*?/;
10+
var PHP_CODE_REGEX = /<\?[\s\S]*?\?>/g;
11+
var ASP_CODE_REGEX = /<%[\s\S]*?%>/g;
12+
var ASP_NET_CODE_REGEX = /(<asp:[^]+>[\s|\S]*?<\/asp:[^]+>)|(<asp:[^]+\/>)/gi;
13+
var HTML_TAG_WITH_ON_ATTRIBUTE_REGEX = /<[^>]+?(\s+\bon\w+=(?:'[^']*'|"[^"]*"|[^'"\s>]+))*\s*\/?>/gi;
14+
var ON_ATTRIBUTE_REGEX = /(\s+\bon\w+=(?:'[^']*'|"[^"]*"|[^'"\s>]+))/gi;
815

916
if (!editor.window) {
1017
editor.window = editorWindow;
@@ -87,6 +94,9 @@ CKEDITOR.dialog.add('codemirrordialog', function (editor) {
8794

8895
_handleCodeMirrorChange: function () {
8996
var newData = this.codeMirrorEditor.getValue();
97+
98+
var sanitizedData = this._sanitizeHTML(newData);
99+
90100
var preview = this.dialog
91101
.getContentElement('main', 'preview')
92102
.getElement();
@@ -95,7 +105,7 @@ CKEDITOR.dialog.add('codemirrordialog', function (editor) {
95105
if (iframe && iframe.$) {
96106
var iframeDocument = iframe.$.contentDocument;
97107
var iframeBody = iframeDocument.body;
98-
iframeBody.innerHTML = newData;
108+
iframeBody.innerHTML = sanitizedData;
99109
}
100110
},
101111

@@ -183,18 +193,18 @@ CKEDITOR.dialog.add('codemirrordialog', function (editor) {
183193
};
184194
},
185195

186-
_handleCodeMirrorChange: function () {
187-
var newData = this.codeMirrorEditor.getValue();
188-
var preview = this.dialog
189-
.getContentElement('main', 'preview')
190-
.getElement();
191-
192-
var iframe = preview.findOne('iframe');
193-
if (iframe && iframe.$) {
194-
var iframeDocument = iframe.$.contentDocument;
195-
var iframeBody = iframeDocument.body;
196-
iframeBody.innerHTML = newData;
197-
}
196+
_sanitizeHTML: function (html) {
197+
var sanitizedHtml = html
198+
.replace(HTML_TAG_WITH_ON_ATTRIBUTE_REGEX, function (match) {
199+
return match.replace(ON_ATTRIBUTE_REGEX, '');
200+
})
201+
.replace(ALERT_REGEX, '')
202+
.replace(INNER_HTML_REGEX, '')
203+
.replace(PHP_CODE_REGEX, '')
204+
.replace(ASP_CODE_REGEX, '')
205+
.replace(ASP_NET_CODE_REGEX, '');
206+
207+
return sanitizedHtml;
198208
},
199209

200210
contents: [

0 commit comments

Comments
 (0)