Skip to content

🛡️ Warracker is an open source, self-hostable warranty tracker to monitor expirations, store receipts, files. You own the data, your rules!

Security: escape tag names and colors at every render site, quote-safe escapeHtml (XSS-05)

Omega (QA)b5a3a2fd4edc
diff --git a/frontend/js/components/tagManager.js b/frontend/js/components/tagManager.js
index f227e64..24315f6 100644
--- a/frontend/js/components/tagManager.js
+++ b/frontend/js/components/tagManager.js
@@ -20,6 +20,21 @@ import {
import { showToast, showLoadingSpinner, hideLoadingSpinner } from './ui.js';
import { renderWarrantiesList } from './warrantyRenderer.js';
+// Escape user-controlled strings before injecting into innerHTML templates
+// (stored-XSS defense; safe for both element content and attribute
+// contexts, so tag names and colors cannot break out of markup).
+function escapeHtml(text) {
+ if (text === null || text === undefined) return '';
+ const map = {
+ '&': '&',
+ '<': '&lt;',
+ '>': '&gt;',
+ '"': '&quot;',
+ "'": '&#039;',
+ };
+ return String(text).replace(/[&<>"']/g, (m) => map[m]);
+}
+
// Safe translation helper that works even if i18next isn't fully loaded
function t(key, options = {}) {
// Try window.i18next first (the actual i18next library)
@@ -116,7 +131,7 @@ function renderDropdownList(container, tags, selected, onSelect, searchTerm = ''
createOption.className = 'tag-option create-tag';
// Use i18next directly with fallback
const createText = window.i18next?.t('tags.create_with_name', { name: searchTerm, defaultValue: `Create "${searchTerm}"` }) || `Create "${searchTerm}"`;
- createOption.innerHTML = `<i class="fas fa-plus"></i> ${createText}`;
+ createOption.innerHTML = `<i class="fas fa-plus"></i> ${escapeHtml(createText)}`;
createOption.addEventListener('click', async () => {
try {
const created = await createTag(searchTerm);
@@ -135,8 +150,8 @@ function renderDropdownList(container, tags, selected, onSelect, searchTerm = ''
option.className = 'tag-option';
const isSelected = selected.some((selectedTag) => selectedTag.id === tag.id);
option.innerHTML = `
- <span class="tag-color" style="background-color: ${tag.color}"></span>
- ${tag.name}
+ <span class="tag-color" style="background-color: ${escapeHtml(tag.color)}"></span>
+ ${escapeHtml(tag.name)}
<span class="tag-status">${isSelected ? '<i class="fas fa-check"></i>' : ''}</span>
`;
option.addEventListener('click', () => onSelect(tag, isSelected));
@@ -204,7 +219,7 @@ export function renderSelectedTags() {
tagElement.style.backgroundColor = tag.color;
tagElement.style.color = getContrastColor(tag.color);
tagElement.innerHTML = `
- ${tag.name}
+ ${escapeHtml(tag.name)}
<span class="remove-tag" data-id="${tag.id}">&times;</span>
`;
tagElement.querySelector('.remove-tag').addEventListener('click', (event) => {
@@ -235,7 +250,7 @@ export function renderEditSelectedTags() {
tagElement.style.backgroundColor = tag.color;
tagElement.style.color = getContrastColor(tag.color);
tagElement.innerHTML = `
- ${tag.name}
+ ${escapeHtml(tag.name)}
<span class="remove-tag" data-id="${tag.id}">&times;</span>
`;
tagElement.querySelector('.remove-tag').addEventListener('click', (event) => {
@@ -361,13 +376,13 @@ export function renderExistingTags() {
row.innerHTML = `
<td>
<div class="tag-color-swatch-container">
- <div class="tag-color-swatch" style="background-color: ${tag.color}" aria-label="${tag.name}"></div>
- <input type="color" class="tag-color-input-hidden" value="${tag.color}" aria-label="${t('tags.pick_color', { defaultValue: 'Pick color' })}" />
+ <div class="tag-color-swatch" style="background-color: ${escapeHtml(tag.color)}" aria-label="${escapeHtml(tag.name)}"></div>
+ <input type="color" class="tag-color-input-hidden" value="${escapeHtml(tag.color)}" aria-label="${t('tags.pick_color', { defaultValue: 'Pick color' })}" />
</div>
</td>
<td>
- <span class="tag-name-display">${tag.name}</span>
- <input type="text" class="form-control tag-name-input" value="${tag.name}" />
+ <span class="tag-name-display">${escapeHtml(tag.name)}</span>
+ <input type="text" class="form-control tag-name-input" value="${escapeHtml(tag.name)}" />
</td>
<td class="tag-actions">
<div class="view-actions">
diff --git a/frontend/script.js b/frontend/script.js
index cede61f..30e164e 100644
--- a/frontend/script.js
+++ b/frontend/script.js
@@ -1763,8 +1763,8 @@ async function renderWarranties(warrantiesToRender) {
const tagsHtml = warranty.tags && warranty.tags.length > 0
? `<div class="tags-row">
${warranty.tags.map(tag =>
- `<span class="tag" style="background-color: ${tag.color}; color: ${getContrastColor(tag.color)}">
- ${tag.name}
+ `<span class="tag" style="background-color: ${escapeHtml(tag.color)}; color: ${getContrastColor(tag.color)}">
+ ${escapeHtml(tag.name)}
</span>`
).join('')}
</div>`
@@ -3852,13 +3852,20 @@ function closeClaimFormModal() {
}
/**
- * Escape HTML to prevent XSS
+ * Escape HTML to prevent XSS. This definition shadows the earlier one
+ * (later function declarations win), so it must escape quotes too:
+ * attribute contexts such as input value="..." depend on it.
*/
function escapeHtml(text) {
if (!text) return '';
- const div = document.createElement('div');
- div.textContent = text;
- return div.innerHTML;
+ const map = {
+ '&': '&amp;',
+ '<': '&lt;',
+ '>': '&gt;',
+ '"': '&quot;',
+ "'": '&#039;'
+ };
+ return String(text).replace(/[&<>"']/g, function(m) { return map[m]; });
}
// Add this function to handle edit tab functionality
@@ -4333,8 +4340,8 @@ function renderTagsList(searchTerm = '') {
const isSelected = selectedTags.some(selected => selected.id === tag.id);
option.innerHTML = `
- <span class="tag-color" style="background-color: ${tag.color}"></span>
- ${tag.name}
+ <span class="tag-color" style="background-color: ${escapeHtml(tag.color)}"></span>
+ ${escapeHtml(tag.name)}
<span class="tag-status">${isSelected ? '<i class="fas fa-check"></i>' : ''}</span>
`;
@@ -4399,8 +4406,8 @@ function renderEditTagsList(searchTerm = '') {
const isSelected = editSelectedTags.some(selected => selected.id === tag.id);
option.innerHTML = `
- <span class="tag-color" style="background-color: ${tag.color}"></span>
- ${tag.name}
+ <span class="tag-color" style="background-color: ${escapeHtml(tag.color)}"></span>
+ ${escapeHtml(tag.name)}
<span class="tag-status">${isSelected ? '<i class="fas fa-check"></i>' : ''}</span>
`;
@@ -4454,7 +4461,7 @@ function renderSelectedTags() {
tagElement.style.color = getContrastColor(tag.color);
tagElement.innerHTML = `
- ${tag.name}
+ ${escapeHtml(tag.name)}
<span class="remove-tag" data-id="${tag.id}">&times;</span>
`;
@@ -4492,7 +4499,7 @@ function renderEditSelectedTags() {
tagElement.style.color = getContrastColor(tag.color);
tagElement.innerHTML = `
- ${tag.name}
+ ${escapeHtml(tag.name)}
<span class="remove-tag" data-id="${tag.id}">&times;</span>
`;
@@ -4681,8 +4688,8 @@ function renderExistingTags() {
row.innerHTML = `
<td>
<div class="tag-color-swatch-container">
- <div class="tag-color-swatch" style="background-color: ${tag.color}" aria-label="${escapeHtml(tag.name)}"></div>
- <input type="color" class="tag-color-input-hidden" value="${tag.color}" aria-label="Pick color" />
+ <div class="tag-color-swatch" style="background-color: ${escapeHtml(tag.color)}" aria-label="${escapeHtml(tag.name)}"></div>
+ <input type="color" class="tag-color-input-hidden" value="${escapeHtml(tag.color)}" aria-label="Pick color" />
</div>
</td>
<td>
diff --git a/tests/test_xss_regressions.py b/tests/test_xss_regressions.py
index 0f06723..6dbd85c 100644
--- a/tests/test_xss_regressions.py
+++ b/tests/test_xss_regressions.py
@@ -1,4 +1,4 @@
-"""Regression pins for the 1.0.5 stored-XSS fixes (audit XSS-01/02/04A).
+"""Regression pins for the stored-XSS fixes (audit XSS-01/02/04A/05).
The vulnerable sinks are frontend rendering paths, so these tests pin
the safe rendering patterns in the shipped sources: if anyone puts the
@@ -14,9 +14,17 @@ raw interpolation back, the suite fails before the payload does.
escapeHtml(sanitizeUrl(...)) path, like the product link already did.
- XSS-04A: the legacy edit modal (the only edit modal on the status
page) interpolated serial numbers into an input's value attribute.
- Serials must be assigned through the input's value property. Note
- script.js's global escapeHtml is shadowed by a quote-less variant,
- so escaping alone is not sufficient protection in attribute context.
+ Serials must be assigned through the input's value property. (At the
+ time, script.js's global escapeHtml was shadowed by a quote-less
+ variant, so escaping alone was not sufficient protection in
+ attribute context; the XSS-05 fix made both definitions escape
+ quotes, and the value-property assignment stays as defence in depth.)
+- XSS-05: tag names and colors were interpolated raw into innerHTML in
+ the module tag manager (picker, selected chips, Manage Tags) and the
+ legacy tag renders, and script.js's effective escapeHtml did not
+ escape quotes, so a quote-bearing tag name broke out of input value
+ attributes. Every tag name and color interpolation must go through
+ an escapeHtml that escapes quotes.
"""
import os
@@ -49,3 +57,35 @@ def test_xss04a_edit_modal_serials_assigned_via_value_property():
assert ".value = validSerialNumbers[0]" in src
assert ".value = validSerialNumbers[i]" in src
+
+def test_xss05_tag_manager_escapes_names_and_colors():
+ src = _read("frontend/js/components/tagManager.js")
+ assert "${tag.name}" not in src
+ assert "${tag.color}" not in src
+ assert "${escapeHtml(tag.name)}" in src
+ assert "${escapeHtml(tag.color)}" in src
+ # The "Create ..." option interpolates the typed search term.
+ assert "${escapeHtml(createText)}" in src
+ # The module helper must escape quotes (attribute contexts).
+ assert "'\"': '&quot;'" in src
+
+
+def test_xss05_script_escapehtml_definitions_escape_quotes():
+ src = _read("frontend/script.js")
+ # Both function declarations must escape quotes: the later one
+ # shadows the earlier at every call site.
+ parts = src.split("function escapeHtml")
+ assert len(parts) == 3, "expected exactly two escapeHtml definitions"
+ for body in parts[1:]:
+ assert "&quot;" in body[:500]
+ # The old quote-less div.textContent implementation must be gone.
+ assert "div.textContent = text" not in src
+
+
+def test_xss05_legacy_tag_sites_escaped():
+ src = _read("frontend/script.js")
+ assert "${tag.name}" not in src
+ assert "${tag.color}" not in src
+ assert "${escapeHtml(tag.name)}" in src
+ assert "${escapeHtml(tag.color)}" in src
+