Skip to content

Commit a163469

Browse files
authored
Merge pull request #174 from FREAC/alert-autofix-5
Potential fix for code scanning alert no. 5: DOM text reinterpreted as HTML
2 parents ba44bcc + bde46e8 commit a163469

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

  • leaflet_app/plugin/jQuery-Plugin-For-Filterable-Multiple-Select-with-Checkboxes-fSelect

leaflet_app/plugin/jQuery-Plugin-For-Filterable-Multiple-Select-with-Checkboxes-fSelect/fSelect.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@
4949
this.create();
5050
}
5151

52+
// Helper to safely convert any value to string
53+
function encodeHtml(str) {
54+
return String(str == null ? '' : str);
55+
}
56+
57+
// Escape for safe use inside HTML attribute or inner HTML
58+
function escapeAttr(str) {
59+
return $('<div>').text(encodeHtml(str)).html();
60+
}
61+
62+
function escapeHtmlText(str) {
63+
return $('<div>').text(encodeHtml(str)).html();
64+
}
5265

5366
/**
5467
* Prototype class
@@ -105,7 +118,7 @@
105118
var $el = $(el);
106119

107120
if ('optgroup' == $el.prop('nodeName').toLowerCase()) {
108-
choices += '<div class="fs-optgroup-label" data-group="' + $this.optgroup + '">' + $el.prop('label') + '</div>';
121+
choices += '<div class="fs-optgroup-label" data-group="' + $this.optgroup + '">' + escapeHtmlText($el.prop('label')) + '</div>';
109122
choices += $this.buildOptions($el);
110123
$this.optgroup++;
111124
}
@@ -119,7 +132,9 @@
119132
var disabled = $el.is(':disabled') ? ' disabled' : '';
120133
var selected = -1 < $.inArray(val, $this.selected) ? ' selected' : '';
121134
var group = ' g' + $this.optgroup;
122-
var row = '<div class="fs-option' + selected + disabled + group + classes + '" data-value="' + val + '" data-index="' + $this.idx + '"><span class="fs-checkbox"><i></i></span><div class="fs-option-label">' + $el.html() + '</div></div>';
135+
var safeVal = escapeAttr(val);
136+
var labelText = escapeHtmlText($el.text());
137+
var row = '<div class="fs-option' + selected + disabled + group + classes + '" data-value="' + safeVal + '" data-index="' + $this.idx + '"><span class="fs-checkbox"><i></i></span><div class="fs-option-label">' + labelText + '</div></div>';
123138

124139
if ('function' === typeof $this.settings.optionFormatter) {
125140
row = $this.settings.optionFormatter(row);
@@ -139,7 +154,7 @@
139154
var labelText = [];
140155

141156
this.$wrap.find('.fs-option.selected').each(function(i, el) {
142-
labelText.push($(el).find('.fs-option-label').html());
157+
labelText.push($(el).find('.fs-option-label').text());
143158
});
144159

145160
if (labelText.length < 1) {
@@ -152,7 +167,7 @@
152167
labelText = labelText.join(', ');
153168
}
154169

155-
this.$wrap.find('.fs-label').html(labelText);
170+
this.$wrap.find('.fs-label').text(labelText);
156171
this.$wrap.toggleClass('fs-default', labelText === settings.placeholder);
157172
}
158173
}

0 commit comments

Comments
 (0)