Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datasets: Fixed HTML sanitation in datasets table #2698

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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