CSV injection: when an export file becomes a program
CSV injection, also called formula injection, happens when an application writes user-supplied text into a CSV file and a spreadsheet later treats a cell as a formula instead of as text. The person attacked is whoever opens the export, usually a colleague with access to reports. The weak spot is the code that writes the file, and a filter in front of the site sees only the earlier request that stored the text.
Quick facts
- Also called
- Formula injection, spreadsheet injection
- Weakness
- CWE-1236, improper neutralization of formula elements in a CSV file; a child of CWE-74
- OWASP category
- A05:2025 Injection
- Leading characters
- OWASP lists =, +, -, @, plus tab and carriage return, as cell starts to treat with care
- The file format
- RFC 4180 is informational and says nothing about formulas
- Main fix
- Neutralise cells that start with a formula character when the file is written, and quote fields as RFC 4180 describes
Definitions follow the OWASP CSV Injection page and the CWE entry above. The file format is described in RFC 4180, published in October 2005 and later updated by RFC 7111.
How a cell turns into a formula
A CSV file is plain text: rows on separate lines, values separated by commas. RFC 4180 registers the text/csv type and sets out the quoting rules. A field that contains a comma, a double quote or a line break is wrapped in double quotes, and a double quote inside such a field is written twice. That is the whole grammar. The RFC does not mention formulas, because a CSV file has no concept of them.
Spreadsheet programs add the meaning. When they open a file, a cell whose text begins with an equals sign is read as a formula, not as a string. CWE-1236 puts it this way: spreadsheet software may interpret certain elements as commands, and a crafted value can make that happen when a user opens the exported file.
So the sequence has three steps, and the application controls only the first two:
- Text goes in. A name, a comment, a ticket subject or an address arrives through a form, an API call or an import, and the application stores it unchanged.
- Text goes out. A report or export feature writes the stored value into a cell, with quoting that satisfies RFC 4180 but does nothing about a first character.
- A person opens the file. The spreadsheet decides what the cell is. By then the application is no longer involved.
The first character is the hinge. OWASP names =, +, - and @, and also the tab and carriage return characters. It adds that full-width forms of these characters can act as formula starters in some locales, and that a separator or quote inside a value can begin a new cell, so the dangerous character does not have to be at the start of the submitted text.
Who is attacked, and what is at stake
Most injection flaws hit the server. This one hits a desktop. The attacker needs a way to leave text in a system, and then waits for someone with access to export that data. Typical stores are the ones people feed constantly: contact forms, order notes, profile fields, support tickets, event registrations, app reviews. The reader is typically an employee preparing a report, which is why the file is trusted.
OWASP describes three kinds of harm. The first is compromise of the user's computer through weaknesses in the spreadsheet software. The second is compromise that relies on people clicking through the warnings a spreadsheet shows for a file downloaded from a site they know. The third is pulling data out of the open spreadsheet or of other spreadsheets open at the same time. The CWE entry adds that a formula can reach hyperlinks or the local command line. This page does not show any formula; the references explain the mechanism and that is enough to decide what to fix.
The stakes are therefore different from a flaw in the web tier. Nothing on your server is harmed, the damage happens on the machine of the person who trusted your export, and the log on the server shows only ordinary form submissions. Compare cross-site scripting, where stored text is also interpreted later by a viewer the attacker never touches, and file upload, where the file itself is the carrier. Here the file is something your own application generates.
Whose bug is it?
The spreadsheet vendors have long treated opening a formula as intended behaviour, and OWASP notes that many bug bounty programmes exclude the issue. That is a statement about where the bug is accepted, not a reason to leave it unfixed. CWE-1236 places the weakness in the product that saves the data: it writes user-provided values to a CSV file without neutralising elements a spreadsheet may read as commands.
It also helps to know that no single escape works everywhere. The CWE entry rates each mitigation as moderate at best, because spreadsheet products behave differently. OWASP adds a trap: Excel may strip quotes or escape characters when a file is saved and reopened, and that can bring back formulas that were neutralised in the original export. Plan for defence in layers, with the export fix first and the settings on the reading side second.
Where a WAF helps and where it does not
A web application firewall inspects requests as they pass through. For this flaw, that means the moment a value is submitted. The moment that matters, a person opening a file in a spreadsheet, happens on a laptop, after an export that may be hours or months later, and no request passes through the filter at that point.
What a filter can do is limited, and the limits are worth stating plainly:
- It sees input, not the export. A rule can flag a form field that begins with a formula character. It cannot tell whether that field will ever be exported, or how the exporting code quotes it.
- Legitimate input looks the same. A phone number with a leading plus, a negative amount, a handle that starts with @ and a note that begins with a dash are ordinary data. Blocking them breaks real forms; see false positives.
- Other paths bypass it. Values can reach the database through an import, a back-office tool or an integration that does not pass through the filtered hostname.
The honest conclusion: the filter can give you a record of which fields receive text of this shape, and nothing more. Treat any match as information about where to look in the export code.
Bridge WAF has three modes, off, monitor and block. Start in monitor, which records matches and serves every request, then read the activity log before deciding anything. If matches turn out to be mostly real customers, which is likely for this pattern, block is the wrong tool and the fix belongs in the export. The general method is in monitor mode. Bridge WAF filters requests; it does not change how your application writes files.
How to fix it in the export
The control belongs where the file is built, because that is the one place that knows the value is going into a cell. Apply it to every column and to header cells that come from data, not only to the fields that look risky.
- Quote as RFC 4180 describes. Wrap fields in double quotes and double any embedded double quote. This keeps a value from spilling into the next cell, which OWASP flags as a way to put a dangerous character at the start of a new cell.
- Neutralise the first character. If a cell begins with =, +, -, @, tab or carriage return, put a single quote in front. OWASP lists this together with the quoting step, and CWE-1236 describes the apostrophe as the way to stop Excel from executing the cell. CWE rates its effect in other spreadsheet software as unclear.
- Consider the tab variant. OWASP also describes a tab prefix inside the quoted field for Excel. The tab stays in the data, so a program that reads the export back may need to strip it.
- Handle look-alike characters. The full-width forms OWASP mentions belong in the same check, so the test should not be a plain comparison against four ASCII characters.
- Reject where a value has a fixed shape. Names, ticket references and phone numbers have narrow formats. Validate them at input, and keep the free-text fields for the export step.
- Separate human and machine exports. A file read only by people can carry the visible prefix. A file that feeds another program can keep exact values if the consumer is not a spreadsheet. Say in the file name or the download page which one it is.
On the reading side, the CWE entry suggests spreadsheet software that blocks formulas in files from untrusted or foreign sources, and rates it limited because it relies on how people open files. A clear note on the download button that the file contains user-provided text, and should be opened with formulas disabled, costs little. Training on the warnings a spreadsheet displays belongs in the same plan.
The family resemblance to other output problems is useful. Like CRLF injection, the flaw sits where text meets a format with its own control characters, and the repair is neutralising on output. Like command injection and SQL injection, it treats data as instructions. The category page Injection (A05:2025) puts them together.
How to check your own exports
You do not need a hostile string to test this. In a staging copy, store a harmless marker value that begins with each character of the list above and carries plain words after it, for example the word "test" after a leading dash. Run every export feature that touches those fields, then open the files in a text editor, not in a spreadsheet. Check the first character of each marker cell. If it is still the formula character, the export has no neutralisation.
Keep the list of export features next to the list of input forms. A form field that feeds one export is easy to forget when a second report is added six months later. A unit test on the shared function that writes cells is cheaper than a review of every report.
Do not open the test files in a spreadsheet with formulas enabled while you are checking whether it is safe to do so. The text editor tells you the same thing without the risk.
Questions
What is CSV injection?
CSV injection is a flaw in which an application writes user-supplied text into a CSV file without neutralising cells that begin with a formula character, so a spreadsheet may treat the cell as a formula when someone opens the export. It is listed as CWE-1236.
Which characters are risky at the start of a cell?
OWASP lists the equals sign, plus, minus and at sign, along with the tab and carriage return characters, and notes that full-width forms of some of them can act as formula starters in some locales.
Does RFC 4180 protect against it?
No. RFC 4180 only describes the text/csv format and how to quote fields that contain commas, double quotes or line breaks. It does not mention formulas, so correct quoting alone does not stop a spreadsheet from reading a cell as one.
Can a WAF stop CSV injection?
Only in part. A WAF sees the request that stores the text, not the later moment when someone opens the exported file, and legitimate input such as a phone number with a leading plus looks the same. It can record which fields receive such text; the fix has to be made in the export code.
How do you prevent CSV injection?
When the file is written, quote fields as RFC 4180 describes and put a single quote in front of any cell that begins with a formula character. Apply it to every column. No single method works in every spreadsheet product, so also open untrusted files with formulas disabled.