🛡️ Warracker is an open source, self-hostable warranty tracker to monitor expirations, store receipts, files. You own the data, your rules!
Security: neutralize formula-leading values in CSV exports (CSV-02)
Omega (QA)db09e9e3e507
diff --git a/frontend/js/components/importExport.js b/frontend/js/components/importExport.jsindex f634a96..a5f76a1 100644--- a/frontend/js/components/importExport.js+++ b/frontend/js/components/importExport.js@@ -4,6 +4,21 @@ import { loadWarranties, applyFilters } from '../controllers/warrantyListControlimport { loadTags } from './tagManager.js';import { formatDateYYYYMMDD } from '../lib/dates.js';+// CSV-02: neutralize formula injection. A value whose first non-space+// character is =, +, -, @, tab, or carriage return would be evaluated+// as a formula by spreadsheet apps on import. Prefixing a tab keeps+// the value inert, and the importer strips field values, so a+// re-imported export round-trips to the stored value.+export function neutralizeCsvFormula(value) {+ const text = String(value);+ const first = text.trimStart()[0];+ if (first === '=' || first === '+' || first === '-' || first === '@'+ || first === '\t' || first === '\r') {+ return '\t' + text;+ }+ return text;+}+export async function exportWarranties() {const warranties = getWarranties();if (!warranties.length) {@@ -90,7 +105,7 @@ export async function exportWarranties() {warranty.model_number || '',warranty.warranty_type || '',];- csv += `${row.map((field) => `"${String(field).replace(/"/g, '""')}"`).join(',')}\n`;+ csv += `${row.map((field) => `"${neutralizeCsvFormula(String(field)).replace(/"/g, '""')}"`).join(',')}\n`;});const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });diff --git a/frontend/status.js b/frontend/status.jsindex 08ab0cd..3a6110d 100644--- a/frontend/status.js+++ b/frontend/status.js@@ -793,6 +793,21 @@return;}+ // CSV-02: neutralize formula injection. A value whose first+ // non-space character is =, +, -, @, tab, or carriage return+ // would be evaluated as a formula by spreadsheet apps on+ // import. Prefixing a tab keeps the value inert, and the+ // importer strips field values, so a re-imported export+ // round-trips to the stored value.+ const neutralizeCsvFormula = (value) => {+ const text = String(value);+ const first = text.trimStart()[0];+ if (first === '=' || first === '+' || first === '-' || first === '@'+ || first === '\t' || first === '\r') {+ return '\t' + text;+ }+ return text;+ };const headers = ['ProductName','PurchaseDate',@@ -812,7 +827,7 @@'ModelNumber','WarrantyType',];- const escapeField = (field) => `"${String(field).replace(/"/g, '""')}"`;+ const escapeField = (field) => `"${neutralizeCsvFormula(String(field)).replace(/"/g, '""')}"`;let csv = headers.join(',') + '\n';displayWarranties.forEach((warranty) => {// JSON-encoded so values containing commas round-trip intactdiff --git a/tests/test_csv_formula_neutralization.py b/tests/test_csv_formula_neutralization.pynew file mode 100644index 0000000..fb36c09--- /dev/null+++ b/tests/test_csv_formula_neutralization.py@@ -0,0 +1,28 @@+"""CSV-02: exported CSV must not carry live formulas.++A value starting with =, +, -, @, tab, or carriage return is+evaluated as a formula by spreadsheet apps on import. Both exporters+neutralize such values with a tab prefix, which the importer strips+on re-import, so an export round-trips to the stored value. These+pins fail if an exporter quotes a field without neutralizing it.+"""+from pathlib import Path++REPO = Path(__file__).resolve().parent.parent+++def test_both_exporters_neutralize_formulas():+ main = (REPO / "frontend" / "js" / "components"+ / "importExport.js").read_text()+ status = (REPO / "frontend" / "status.js").read_text()+ for name, text in (("importExport.js", main), ("status.js", status)):+ assert "neutralizeCsvFormula" in text, name+ # Every quoting site passes through the neutralizer.+ assert 'neutralizeCsvFormula(String(field))' in text, name+ # No quoting site escapes a field without neutralizing it first.+ assert '"${String(field).replace' not in main, "importExport.js"+ assert '"${String(field).replace' not in status, "status.js"+ # The trigger set covers the OWASP formula starters.+ for trigger in ("'='", "'+'", "'-'", "'@'", "'\\t'", "'\\r'"):+ assert trigger in main, trigger+ assert trigger in status, trigger