Skip to content

Commit 5067e42

Browse files
authored
fix: escape html in property editor (#278)
Problem: - Unescaped HTML in property values Solution: - Escape HTML special characters - Remove duplicate CSS classes - Preserve clickable-value during processing
1 parent 1d72e51 commit 5067e42

File tree

4 files changed

+1524
-3128
lines changed

4 files changed

+1524
-3128
lines changed

app/scripts/modules/ui/DataView.js

+4
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ DataView.prototype._generateHTMLForKeyValuePair = function (key, currentView) {
228228
};
229229
}
230230

231+
if (typeof vValue === 'string') {
232+
vValue = DVHelper.formatHTMLString(vValue);
233+
}
234+
231235
if (vValue && typeof vValue === 'object') {
232236
valueHTML = JSONFormatter.formatJSONtoHTML(vValue);
233237
} else if (typeof type === 'object') {

app/scripts/modules/ui/helpers/DataViewHelper.js

+43-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
var _ = require('lodash');
4+
35
/**
46
* Generates attributes in HTML.
57
* @param {Object} attributes
@@ -403,6 +405,45 @@ function _getObjectProperty(sourceObject, path) {
403405
}, sourceObject);
404406
}
405407

408+
/**
409+
* Clean up duplicate classes in HTML string.
410+
* @param {string} value - HTML string that might contain class attributes
411+
* @returns {string} - HTML string with unique classes
412+
* @private
413+
*/
414+
function _cleanDuplicateClasses(value) {
415+
if (!value.includes('class="')) {
416+
return value;
417+
}
418+
return value.replace(/class="([^"]*)"/g, function(match, classString) {
419+
var uniqueClasses = [...new Set(classString.split(/\s+/).filter(Boolean))];
420+
return 'class="' + uniqueClasses.join(' ') + '"';
421+
});
422+
}
423+
424+
/**
425+
* Escape HTML special characters.
426+
* @param {string} value - String to escape
427+
* @returns {string} - Escaped string
428+
* @private
429+
*/
430+
function _escapeHTML(value) {
431+
return _.escape(value);
432+
}
433+
434+
/**
435+
* Format HTML string value by cleaning classes and escaping
436+
* @param {string} value - String to process
437+
* @returns {string} - Processed string
438+
* @private
439+
*/
440+
function _formatHTMLString(value) {
441+
if (value.includes('clickable-value')) {
442+
return value;
443+
}
444+
return _escapeHTML(_cleanDuplicateClasses(value));
445+
}
446+
406447
module.exports = {
407448
addArrow: _addArrow,
408449
addToolsButtons: _addToolsButtons,
@@ -426,5 +467,6 @@ module.exports = {
426467
wrapInSelectTag: _wrapInSelectTag,
427468
wrapInCheckBox: _wrapInCheckBox,
428469
valueNeedsQuotes: _valueNeedsQuotes,
429-
createDefaultSpan: _createDefaultSpan
470+
createDefaultSpan: _createDefaultSpan,
471+
formatHTMLString: _formatHTMLString
430472
};

0 commit comments

Comments
 (0)