Browse Source

Potential fix for code scanning alert no. 2: DOM text reinterpreted as HTML

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
lechibang-1512 2 months ago
parent
commit
4eac54aa18
No account linked to committer's email
1 changed files with 12 additions and 1 deletions
  1. 12
    1
      sortable.js

+ 12
- 1
sortable.js View File

17
 var alternate_row_colors = true;
17
 var alternate_row_colors = true;
18
 
18
 
19
 /* Don't change anything below this unless you know what you're doing */
19
 /* Don't change anything below this unless you know what you're doing */
20
+
21
+// Escape HTML special characters in a string for safe insertion into innerHTML
22
+function escapeHtml(str) {
23
+	return String(str)
24
+		.replace(/&/g, '&amp;')
25
+		.replace(/</g, '&lt;')
26
+		.replace(/>/g, '&gt;')
27
+		.replace(/"/g, '&quot;')
28
+		.replace(/'/g, '&#39;');
29
+}
30
+
20
 addEvent(window, "load", sortables_init);
31
 addEvent(window, "load", sortables_init);
21
 
32
 
22
 var SORT_COLUMN_INDEX;
33
 var SORT_COLUMN_INDEX;
50
 		var cell = firstRow.cells[i];
61
 		var cell = firstRow.cells[i];
51
 		var txt = ts_getInnerText(cell);
62
 		var txt = ts_getInnerText(cell);
52
 		if (cell.className != "unsortable" && cell.className.indexOf("unsortable") == -1) {
63
 		if (cell.className != "unsortable" && cell.className.indexOf("unsortable") == -1) {
53
-			cell.innerHTML = '<a href="#" class="sortheader" onclick="ts_resortTable(this, '+i+');return false;">'+txt+'<span class="sortarrow">&nbsp;&nbsp;<img src="'+ image_path + image_none + '" alt="&darr;"/></span></a>';
64
+			cell.innerHTML = '<a href="#" class="sortheader" onclick="ts_resortTable(this, '+i+');return false;">'+escapeHtml(txt)+'<span class="sortarrow">&nbsp;&nbsp;<img src="'+ image_path + image_none + '" alt="&darr;"/></span></a>';
54
 		}
65
 		}
55
 	}
66
 	}
56
 	if (alternate_row_colors) {
67
 	if (alternate_row_colors) {