Add escapeHtml utility and apply to all user-controllable SweetAlert2 html

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-06 07:52:26 +08:00
parent 954b41b555
commit 5be29ddd51
3 changed files with 49 additions and 5 deletions

13
src/utils/escapeHtml.js Normal file
View File

@@ -0,0 +1,13 @@
/**
* Escapes HTML special characters to prevent XSS.
* @param {string} str The string to escape.
* @returns {string} The escaped string.
*/
export function escapeHtml(str) {
return str
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');
}