diff --git a/budget.js b/budget.js index 440e054..6845cf2 100644 --- a/budget.js +++ b/budget.js @@ -240,9 +240,21 @@ function updateUI() { localStorage.setItem("entry_list", JSON.stringify(ENTRY_LIST)); } +// HELPER FUNC: Escape special HTML characters to prevent XSS attacks +function escapeHTML(str) { + return String(str) + .replace(/&/g, "&") + .replace(//g, ">") + .replace(/"/g, """) + .replace(/'/g, "'"); +} + function showEntry(list, type, title, amount, id) { + const safeTitle = escapeHTML(title); + const safeAmount = escapeHTML(amount); const entry = `
  • -
    ${title} : $${amount}
    +
    ${safeTitle} : $${safeAmount}
  • `; @@ -298,6 +310,7 @@ function inactive(elements) { element.classList.remove("focus"); }); } + // ── Exports for testing ── if (typeof module !== "undefined") { module.exports = { @@ -312,6 +325,9 @@ if (typeof module !== "undefined") { showEntry, deleteEntry, editEntry, + escapeHTML, + get ENTRY_LIST() { return ENTRY_LIST; }, + set ENTRY_LIST(v) { ENTRY_LIST = v; }, get ENTRY_LIST() { return ENTRY_LIST; }, @@ -319,4 +335,4 @@ if (typeof module !== "undefined") { ENTRY_LIST = v; }, }; -} +} \ No newline at end of file