Many web applications let staff download data as a CSV file: a list of sign-ups, support tickets, orders or survey responses. The export code is usually simple and the data is “just text”. Yet that text was often typed by members of the public, and when the file is opened in a spreadsheet, some of it may be treated not as text but as a formula. That is CSV injection, also called formula injection.
This article explains how it works, what an attacker can actually do with it, and how to make exports safe without breaking the data for everyone else. It is written for developers, product owners and anyone who handles exported files.
How it happens
Spreadsheets decide what a cell contains from its first character. Type =SUM(A1:A3) and Excel calculates a sum; type Ana and it stores text. When a spreadsheet opens a CSV file, it applies the same rule to every value in the file.
So imagine a sign-up form with a “Company” field. Someone enters:
=HYPERLINK("https://example.com/login","Click to verify")
Your application stores those characters exactly as typed — correctly, since they are just text. Weeks later, someone in your team exports the sign-ups to CSV and opens the file. In the Company column, instead of a strange string, they see a blue link that says Click to verify.
Nothing was hacked in the traditional sense. The database is fine, the export is a valid CSV, and the spreadsheet is doing exactly what spreadsheets do. The problem sits in the gap between systems: data that is safe in one context (a database field) is executable in another (a spreadsheet cell).
Which characters trigger it
The OWASP Foundation, which maintains widely used guidance on web security, lists the characters that can make a spreadsheet treat a cell as a formula:
=equals sign+plus sign-minus sign@at sign- tab (
0x09) - carriage return (
0x0D)
Plus, minus and @ are included because spreadsheets accept them as alternative ways to start a formula (+1+2 and @SUM(…) both calculate). The tab and carriage return are included because some spreadsheets strip them, leaving a formula character at the start.
What an attacker can do
The real-world risk depends on the spreadsheet and its settings, but the main categories are:
- Convincing phishing links. As in the example above, a cell can display trusted-looking text while linking somewhere else. Because the link appears inside your own company’s report, people are more likely to click it.
- Leaking data from the same sheet. Formulas can read other cells. In spreadsheet apps that can fetch web content, a formula can build a web address that includes the contents of neighbouring cells — for example other customers’ email addresses — and request it, sending that data to the attacker’s server when the sheet calculates.
- Running programs (older or misconfigured software). Historically, a feature called Dynamic Data Exchange (DDE) allowed formulas to launch other programs. Recent versions of Excel block or warn about this by default, and users are shown security prompts, but organisations running old versions or permissive settings remain exposed.
- Corrupting reports. Even without malice, a value such as
-Ana-or=== notes ===can turn into an error (#NAME?) and disrupt totals or lookups.
The person who opens the file is the victim, not the person who typed the value. That is why CSV injection is easy to overlook: the developer who builds the export never sees the problem, and the person who sees it may not recognise it as an attack.
How to prevent it
There is no fix that is perfect for every situation, because any change that stops a spreadsheet interpreting a value also changes the value for other programs. The best approach combines several layers.
1. Neutralise formula-like cells in spreadsheet exports
OWASP’s main recommendation is to make the spreadsheet treat risky cells as text by prefixing them with a single quote (apostrophe):
=HYPERLINK("https://example.com","Open") → '=HYPERLINK("https://example.com","Open")
A leading apostrophe stops the value from starting with a formula character, so the spreadsheet treats the cell as text and displays it literally. Depending on the app and how the file is opened, the apostrophe may be hidden or shown as part of the text. Apply this to any cell that begins with one of the trigger characters.
Two refinements make this much less disruptive:
- Leave genuine numbers alone. Negative numbers like
-12.5and signed values like+3start with a trigger character, but they are not formulas. Checking whether the whole value is a plain number before prefixing avoids changing them. - Make it a deliberate option. The apostrophe becomes part of the value for any program that is not a spreadsheet — a database import, a script or an API would see
'=…. Exports intended for machines should not be altered. Label the option clearly (“Spreadsheet-safe export”) and explain that it may change cell text.
Every RapidConvertor tool that writes CSV or TSV — the CSV Cleaner, JSON to CSV converter and CSV ⇄ TSV converter — offers exactly this option, switched off by default, and reports how many cells it changed.
2. Validate input where you can
If a field should contain a phone number, a postcode or a company name, there is rarely a good reason for it to start with =. Input validation that rejects or flags such values reduces risk at the source. Be cautious, though: blanket rules break legitimate data — some names and notes genuinely start with a hyphen, and phone numbers start with +. Validation is a useful layer, not a complete fix.
3. Prefer formats that spreadsheets do not execute
If the export is for another system, offer JSON instead of (or as well as) CSV. Where people need to work in a spreadsheet, generating a native .xlsx file with every user-supplied cell explicitly typed as text avoids interpretation altogether, because the spreadsheet never has to guess.
4. Train the people who open exports
Spreadsheet apps warn before enabling external content or launching other programs. Make sure staff who handle exports know not to accept those prompts for data files, and to be suspicious of links inside reports.
What does not work
- Wrapping values in quotes. CSV quoting (
"=1+1") only tells the CSV parser where the value starts and ends; the spreadsheet still sees=1+1and calculates it. - Escaping on input and storing the escaped value. Adding an apostrophe in the database corrupts the data for every other use. Neutralise at export time, for the specific destination.
- Relying on a single blocked list of functions. Spreadsheets have hundreds of functions, and new ones are added. Treat any formula-like cell as risky.
A quick checklist for developers
- Find every feature that exports user-supplied data as CSV or TSV.
- Decide whether each export is for people (spreadsheets) or machines (other systems).
- For spreadsheet exports, prefix cells starting with
=,+,-,@, tab or carriage return with'— but leave plain numbers unchanged. - For machine exports, leave values untouched and document the format.
- Add input validation for fields with a known shape.
- Write a test with formula-like values to make sure the export behaves as intended.
Summary
CSV injection turns user-typed text into spreadsheet formulas when an export is opened. It can produce convincing phishing links, leak data from the sheet and, on old or misconfigured software, do worse. The practical defence is to neutralise formula-like cells — prefixing them with an apostrophe while leaving real numbers alone — in exports meant for spreadsheets, combined with sensible input validation and awareness among the people who open the files.