Bläddra i källkod

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 månader sedan
förälder
incheckning
4eac54aa18
No account linked to committer's email
1 ändrade filer med 12 tillägg och 1 borttagningar
  1. 12
    1
      sortable.js

+ 12
- 1
sortable.js Visa fil

@@ -17,6 +17,17 @@ var europeandate = true;
17 17
 var alternate_row_colors = true;
18 18
 
19 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 31
 addEvent(window, "load", sortables_init);
21 32
 
22 33
 var SORT_COLUMN_INDEX;
@@ -50,7 +61,7 @@ function ts_makeSortable(t) {
50 61
 		var cell = firstRow.cells[i];
51 62
 		var txt = ts_getInnerText(cell);
52 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 67
 	if (alternate_row_colors) {