Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions csv-table-hub-main/src/__tests__/export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,32 @@ describe("downloadErrorReport CSV", () => {
// value should be quoted
expect(text).toContain('"say ""hello"", world"');
});

// #83: user-supplied values must not run as formulas when the CSV is opened.
it.each([
['=HYPERLINK("http://x","y")', '\'=HYPERLINK(""http://x"",""y"")'],
["+cmd|' /C calc'!A0", "'+cmd|' /C calc'!A0"],
["-2+3", "'-2+3"],
["@SUM(A1:A2)", "'@SUM(A1:A2)"],
])("neutralises formula payload %s", async (value, expected) => {
const rows = buildErrorRows("data.csv", [{ rowNumber: 1, column: "c", value, reason: "test" }]);
downloadErrorReport("data.csv", rows, "csv");
const text = await cap.blob!.text();
expect(text).toContain(expected);
expect(text).not.toMatch(
new RegExp(`,${value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(,|$)`, "m"),
);
});

it("leaves plain negative and signed numbers unchanged", async () => {
const rows = buildErrorRows("data.csv", [
{ rowNumber: 1, column: "n", value: "-5", reason: "+3.2" },
]);
downloadErrorReport("data.csv", rows, "csv");
const text = await cap.blob!.text();
expect(text).toContain(",-5,");
expect(text).toContain(",+3.2,");
});
});

// ─── downloadErrorReport — XLSX ────────────────────────────────────────────────
Expand Down
11 changes: 9 additions & 2 deletions csv-table-hub-main/src/lib/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,16 @@ export type AuditReportRow = {

// ─── CSV ──────────────────────────────────────────────────────────────────────

// Cells starting with these characters are run as formulas by Excel/Sheets
// (OWASP "CSV injection"). Report cells hold user-supplied CSV values, so
// prefix them with a quote. Plain numbers like -5 or +3.2 are left alone.
const FORMULA_START = /^[=+\-@\t\r]/;
const PLAIN_NUMBER = /^[+-]?\d+(\.\d+)?$/;

function escapeCsv(v: unknown): string {
const s = v === undefined || v === null ? "" : String(v);
return /[",\n]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s;
let s = v === undefined || v === null ? "" : String(v);
if (FORMULA_START.test(s) && !PLAIN_NUMBER.test(s)) s = `'${s}`;
return /[",\n\r]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s;
}

function toCsvBlob(headers: string[], rows: Record<string, unknown>[]): Blob {
Expand Down
Loading