Web: Etiketten-CSV gegen Formel-Injektion härten

Zellen, die mit = + - @ (oder Tab/CR) beginnen, werden mit einem vorangestellten
Apostroph zu Text entschaerft. So fuehrt ein Produktname wie "=HYPERLINK(...)"
beim Oeffnen der Etiketten-CSV in Excel/P-touch nicht mehr als Formel aus.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Scarriffle
2026-07-26 15:58:54 +02:00
parent b49546b297
commit f88a459e12

View File

@@ -11,7 +11,11 @@ function buildLabelCsv(rows, origin, delim = ",") {
const delimRe = delim === "\t" ? "\\t" : delim; const delimRe = delim === "\t" ? "\\t" : delim;
const needsQuote = new RegExp(`[${delimRe}"\\n\\r]`); const needsQuote = new RegExp(`[${delimRe}"\\n\\r]`);
const esc = (v) => { const esc = (v) => {
const s = String(v ?? ""); let s = String(v ?? "");
// Formel-Injektion verhindern: Beginnt eine Zelle mit = + - @ (oder Tab/CR),
// koennte Excel/P-touch sie als Formel ausfuehren. Ein vorangestelltes
// Apostroph macht sie zu reinem Text.
if (/^[=+\-@\t\r]/.test(s)) s = `'${s}`;
return needsQuote.test(s) ? `"${s.replace(/"/g, '""')}"` : s; return needsQuote.test(s) ? `"${s.replace(/"/g, '""')}"` : s;
}; };
const lines = [head.map(esc).join(delim)]; const lines = [head.map(esc).join(delim)];