Skip to content

Commit

Permalink
Datasets: Add html sanitation in data table
Browse files Browse the repository at this point in the history
  • Loading branch information
mgbaybay committed Aug 12, 2024
1 parent 80b2761 commit 33c7eb1
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion views/dataset-dataentry-page.twig
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,33 @@
var multiSelectTitleTrans = "{% trans "Multi Select Mode" %}";
var editModeHelpTrans = "{% trans "Click on any row to edit" %}";
var multiSelectHelpTrans = "{% trans "Select one or more rows to delete" %}";
const entityMap = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
'/': '&#x2F;',
'`': '&#x60;',
'=': '&#x3D;'
};
function sanitizeHtml(string) {
return String(string).replace(/[&<>"'`=\/]/g, function (s) {
return entityMap[s];
});
}
function validateHTMLData(str) {
let doc = new DOMParser().parseFromString(str, "text/html");
// If valid html, sanitize and format as a code
if (Array.from(doc.body.childNodes).some(node => node.nodeType === 1)) {
return `<code>${sanitizeHtml(str)}</code>`;
}
return str;
}
cols.push({ "name": "id", "data": "id" });
{% for col in dataSet.getColumn() %}
Expand All @@ -104,7 +130,13 @@
}
});
{% else %}
cols.push({ "data": "{{ col.heading }}", "orderable": {% if col.showSort == 1 %}true{% else %}false{% endif %} });
cols.push({
"data": "{{ col.heading }}",
"orderable": {% if col.showSort == 1 %}true{% else %}false{% endif %},
"render": function(data) {
return validateHTMLData(data);
}
});
{% endif %}
{% endfor %}
Expand Down

0 comments on commit 33c7eb1

Please sign in to comment.