Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): quote a bare CR and guard tab/CR formulas in CSV

Yudhi Armyndharis32d7955685da
diff --git a/dashboard/src/utils/csv.test.ts b/dashboard/src/utils/csv.test.ts
index 027b99882..b0f180e67 100644
--- a/dashboard/src/utils/csv.test.ts
+++ b/dashboard/src/utils/csv.test.ts
@@ -35,3 +35,12 @@ test('structural quoting is unchanged for commas, quotes and newlines', () => {
assert.equal(escapeCsvCell('say "hi"'), '"say ""hi"""');
assert.equal(escapeCsvCell('line1\nline2'), '"line1\nline2"');
});
+
+test('a bare carriage return is quoted, like a newline', () => {
+ assert.equal(escapeCsvCell('a\rb'), '"a\rb"');
+});
+
+test('cells starting with a tab or carriage return are apostrophe-prefixed', () => {
+ assert.equal(escapeCsvCell('\t=1+1'), `'\t=1+1`);
+ assert.equal(escapeCsvCell('\r=1'), `"'\r=1"`);
+});
diff --git a/dashboard/src/utils/csv.ts b/dashboard/src/utils/csv.ts
index f0600288f..254270d09 100644
--- a/dashboard/src/utils/csv.ts
+++ b/dashboard/src/utils/csv.ts
@@ -2,17 +2,18 @@
* Escape one CSV cell for the audit-log export.
*
* Two concerns, in order:
- * 1. Formula injection: a cell whose text begins with `=`, `+`, `-`, or `@` is evaluated as a
- * formula when the export is opened in a spreadsheet (Excel/LibreOffice/Sheets). Audit rows
- * carry attacker-influenced strings (request paths, error messages, API-key names), so a
- * logged request like `GET /=HYPERLINK("https://evil…")` would become a live formula in the
- * operator's spreadsheet. Neutralize by prefixing an apostrophe — the spreadsheet then shows
- * the value as text. This mangles the export only (the dashboard UI shows the raw value).
- * 2. Structural quoting (pre-existing rule): a value containing `"`, `,` or a newline is
- * wrapped in double quotes with inner quotes doubled.
+ * 1. Formula injection: a cell whose text begins with `=`, `+`, `-`, `@`, a tab or a carriage
+ * return is evaluated as a formula when the export is opened in a spreadsheet
+ * (Excel/LibreOffice/Sheets). Audit rows carry attacker-influenced strings (request paths,
+ * error messages, API-key names), so a logged request like `GET /=HYPERLINK("https://evil…")`
+ * would become a live formula in the operator's spreadsheet. Neutralize by prefixing an
+ * apostrophe — the spreadsheet then shows the value as text. This mangles the export only (the
+ * dashboard UI shows the raw value).
+ * 2. Structural quoting (pre-existing rule): a value containing `"`, `,`, a newline or a carriage
+ * return is wrapped in double quotes with inner quotes doubled.
*/
export function escapeCsvCell(value: unknown): string {
let s = value === undefined || value === null ? '' : String(value);
- if (/^[=+\-@]/.test(s)) s = `'${s}`;
- return /[",\n]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s;
+ if (/^[=+\-@\t\r]/.test(s)) s = `'${s}`;
+ return /[",\r\n]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s;
}