Skip to content

Commit d0ad84d

Browse files
committed
Escape HTML output
1 parent 65abe62 commit d0ad84d

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

static/js/mpg.database.query.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,10 @@ MPG.eventListeners.addUpdate = function() {
483483

484484
var documentField = event.currentTarget;
485485

486-
var documentFieldNewValue = window.prompt('New value', documentField.innerHTML);
486+
var documentFieldNewValue = window.prompt(
487+
'New value',
488+
MPG.helpers.unescapeHTML(documentField.innerHTML)
489+
);
487490

488491
if ( documentFieldNewValue === null ) {
489492
return;
@@ -518,8 +521,8 @@ MPG.eventListeners.addUpdate = function() {
518521
function(response) {
519522

520523
if ( JSON.parse(response) === 1 ) {
521-
documentField.innerHTML = MPG.helpers.convertAnyToString(
522-
documentFieldNewValue
524+
documentField.innerHTML = MPG.helpers.escapeHTML(
525+
MPG.helpers.convertAnyToString(documentFieldNewValue)
523526
);
524527
}
525528

static/js/mpg.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,33 @@ MPG.helpers.completeNavLinks = function(urlFragment) {
140140

141141
};
142142

143+
/**
144+
* Escapes HTML tags and entities.
145+
* This prevents HTML stored in MongoDB documents to be interpreted by browser.
146+
*
147+
* @param {string} html
148+
*
149+
* @returns {string}
150+
*/
151+
MPG.helpers.escapeHTML = function(html) {
152+
153+
return html.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
154+
155+
};
156+
157+
/**
158+
* Unescapes HTML tags and entities.
159+
*
160+
* @param {string} html
161+
*
162+
* @returns {string}
163+
*/
164+
MPG.helpers.unescapeHTML = function(html) {
165+
166+
return html.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>');
167+
168+
};
169+
143170
/**
144171
* Reloads collections of a specific database.
145172
*

0 commit comments

Comments
 (0)