Skip to content

Commit e8fa8e0

Browse files
authored
Merge pull request #3961 from Countly/SER-464-notes-widget-content-sanitize
[SER-464] sanitize html when creating updating widget
2 parents 796212b + 5648c1a commit e8fa8e0

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

api/utils/common.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2788,7 +2788,6 @@ common.sanitizeFilename = (filename, replacement = "") => {
27882788
* @returns {string} sanitizedHTML - sanitized html content
27892789
*/
27902790
common.sanitizeHTML = (html) => {
2791-
27922791
const whiteList = {
27932792
a: ["target", "title"],
27942793
abbr: ["title"],
@@ -2887,20 +2886,34 @@ common.sanitizeHTML = (html) => {
28872886
}
28882887

28892888
const attributesRegex = /\b(\w+)=["']([^"']*)["']/g;
2890-
2889+
var doubleQuote = '"',
2890+
singleQuote = "'";
28912891
let matches;
28922892
let filteredAttributes = [];
28932893
let allowedAttributes = Object.getOwnPropertyDescriptor(whiteList, tagName).value;
28942894
let tagHasAttributes = false;
28952895
while ((matches = attributesRegex.exec(tag)) !== null) {
28962896
tagHasAttributes = true;
2897+
let fullAttribute = matches[0];
28972898
let attributeName = matches[1];
28982899
let attributeValue = matches[2];
28992900
if (allowedAttributes.indexOf(attributeName) > -1) {
2900-
filteredAttributes.push(`${attributeName}="${attributeValue}"`);
2901+
2902+
var attributeValueStart = fullAttribute.indexOf(attributeValue);
2903+
if (attributeValueStart >= 1) {
2904+
var attributeWithQuote = fullAttribute.substring(attributeValueStart - 1);
2905+
if (attributeWithQuote.indexOf(doubleQuote) === 0) {
2906+
filteredAttributes.push(`${attributeName}=${doubleQuote}${attributeValue}${doubleQuote}`);
2907+
}
2908+
else if ((attributeWithQuote.indexOf(singleQuote) === 0)) {
2909+
filteredAttributes.push(`${attributeName}=${singleQuote}${attributeValue}${singleQuote}`);
2910+
}
2911+
else { //no quote
2912+
filteredAttributes.push(`${attributeName}=${attributeValue}`);
2913+
}
2914+
}
29012915
}
29022916
}
2903-
console.log("attributes", filteredAttributes);
29042917
if (!tagHasAttributes) { //closing tag or tag without any attributes
29052918
return tag;
29062919
}

plugins/dashboards/api/api.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ plugins.setConfigs("dashboards", {
10781078
widget = params.qstring.widget || {};
10791079

10801080
try {
1081-
widget = JSON.parse(widget);
1081+
widget = JSON.parse(common.sanitizeHTML(widget));
10821082
}
10831083
catch (SyntaxError) {
10841084
log.d('Parse widget failed', widget);
@@ -1157,7 +1157,7 @@ plugins.setConfigs("dashboards", {
11571157
widget = params.qstring.widget || {};
11581158

11591159
try {
1160-
widget = JSON.parse(widget);
1160+
widget = JSON.parse(common.sanitizeHTML(widget));
11611161
}
11621162
catch (SyntaxError) {
11631163
log.d('Parse widget failed', widget);

0 commit comments

Comments
 (0)