Skip to content

Commit

Permalink
Escape HTML output
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelTallet committed Oct 26, 2021
1 parent 65abe62 commit d0ad84d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
9 changes: 6 additions & 3 deletions static/js/mpg.database.query.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,10 @@ MPG.eventListeners.addUpdate = function() {

var documentField = event.currentTarget;

var documentFieldNewValue = window.prompt('New value', documentField.innerHTML);
var documentFieldNewValue = window.prompt(
'New value',
MPG.helpers.unescapeHTML(documentField.innerHTML)
);

if ( documentFieldNewValue === null ) {
return;
Expand Down Expand Up @@ -518,8 +521,8 @@ MPG.eventListeners.addUpdate = function() {
function(response) {

if ( JSON.parse(response) === 1 ) {
documentField.innerHTML = MPG.helpers.convertAnyToString(
documentFieldNewValue
documentField.innerHTML = MPG.helpers.escapeHTML(
MPG.helpers.convertAnyToString(documentFieldNewValue)
);
}

Expand Down
27 changes: 27 additions & 0 deletions static/js/mpg.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,33 @@ MPG.helpers.completeNavLinks = function(urlFragment) {

};

/**
* Escapes HTML tags and entities.
* This prevents HTML stored in MongoDB documents to be interpreted by browser.
*
* @param {string} html
*
* @returns {string}
*/
MPG.helpers.escapeHTML = function(html) {

return html.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');

};

/**
* Unescapes HTML tags and entities.
*
* @param {string} html
*
* @returns {string}
*/
MPG.helpers.unescapeHTML = function(html) {

return html.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>');

};

/**
* Reloads collections of a specific database.
*
Expand Down

0 comments on commit d0ad84d

Please sign in to comment.