Quick revision
The short answer
CSV injection, also called formula injection, happens when a cell value starts with =, +, -, @, a tab or a carriage return: spreadsheet programs may treat it as a formula, which can build malicious links, leak other cells or, in old setups, start programs. We mitigate it by prefixing such user-provided values with a single quote (or otherwise escaping them) before writing, always letting OpenCSV or Commons CSV do the quoting rather than concatenating strings, documenting the behaviour for consumers that need raw values, and offering .xlsx with typed string cells to people who open exports in Excel.
What to remember
In this answer, we saw that CSV injection uses the way spreadsheet programs guess types: a value starting with =, +, -, @, tab or carriage return can become a formula when a colleague opens our export. Quoting protects the CSV structure but not the spreadsheet, so we neutralise user-provided values with a leading single quote in one shared function, always write through OpenCSV or Commons CSV rather than concatenating strings, document the behaviour for consumers that need raw values, and offer .xlsx with typed string cells for people who open exports in Excel.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
CSV injection, also called formula injection, happens when a cell value starts with =, +, -, @, a tab or a carriage return: spreadsheet programs may treat it as a formula, which can build malicious links, leak other cells or, in old setups, start programs. We mitigate it by prefixing such user-provided values with a single quote (or otherwise escaping them) before writing, always letting OpenCSV or Commons CSV do the quoting rather than concatenating strings, documenting the behaviour for consumers that need raw values, and offering .xlsx with typed string cells to people who open exports in Excel.
Why this question is asked
CSV exports are usually seen as harmless: they are plain text, there is no code in them, and the data came from our own database. The trick is that the data came into our database from users, and the file will be opened by a different user in a spreadsheet program that happily evaluates formulas. Interviewers ask this scenario because it is a real finding in many security reviews, and the fix has subtle edges. In this answer, we will see how the attack works, which characters trigger it, how to neutralise values when writing with OpenCSV or Commons CSV, why quoting alone is not enough, and how to serve consumers who need raw data.
1How a Plain Text File Becomes a Formula
A CSV file has no types. When Excel, LibreOffice or Google Sheets opens it, they guess the type of every cell from its text: digits become numbers, 2026-11-05 becomes a date, and text starting with = becomes a formula. That guess is the whole problem. The spreadsheet does not know which values were typed by our staff and which by an anonymous user on our website.
Imagine a support system where customers can set their display name. An attacker sets it to a formula that builds a link to their server carrying data from other cells. A support agent exports the ticket list to CSV and opens it in Excel. The cell shows a harmless-looking link text; clicking it sends the neighbouring cells, perhaps other customers' email addresses, to the attacker. Older attack forms used dynamic data exchange (DDE) formulas to run programs on the agent's machine; modern Excel warns about or blocks most of those, but warnings get clicked through and not every program is configured the same way.
Notice that the value is correctly quoted CSV. Quoting protects the structure of the file, the commas and line breaks, but the spreadsheet removes the quotes when it reads the field and then sees a value starting with =. Quoting is necessary, but it is not a defence against this attack.
2Which Characters Trigger It
Spreadsheet programs treat a cell as a formula when it starts with = and, depending on the program, also when it starts with +, - or @, since those can begin expressions too (+1+2 is evaluated as 3 in many of them). A leading tab (0x09) or carriage return (0x0D) can hide one of those characters after whitespace that some programs strip. So the usual rule, also used by OWASP, is to treat a value as dangerous if its first character is any of =, +, -, @, tab or carriage return.
| Value in the CSV | Spreadsheet sees | After neutralising |
|---|---|---|
| Asha | Text | Asha |
| =1+1 | Formula, shows 2 | '=1+1, shown as text |
| +44 20 7946 0000 | May be a formula or number | '+44 20 7946 0000 |
| -5 (user comment) | Number or formula | '-5 |
@SUM(A1:A9) | Formula in some programs | '@SUM(A1:A9) |
| \t=cmd... | Formula after stripping the tab | '\t=cmd... |
3Neutralising Values When Writing
The common mitigation is to prefix dangerous values with a single quote. In spreadsheet programs, a leading apostrophe means treat the rest as text, and the apostrophe itself is not displayed. We apply this only to fields that can contain user-provided text, such as names, comments and addresses, and never to columns we generate as numbers, because -5.00 in an amount column is a legitimate negative number that finance wants to sum.
Notice that we never build lines with string concatenation such as id + "," + name. Concatenation breaks as soon as a name contains a comma, a quote or a line break, and an attacker can use that to inject extra columns or rows. CSVPrinter and CSVWriter quote and escape every field correctly, which protects the file's structure; neutralise protects the spreadsheet. Some teams also wrap the value in quotes inside the field or use a tab prefix; any consistent, tested scheme is fine, as long as it is applied in one shared function used by every export.
4Documenting It for Consumers Who Need Raw Values
The prefix changes the data. A system that imports our CSV programmatically, not through a spreadsheet, will now see '=1+1 instead of =1+1. Usually that is acceptable for display names and comments, but it must be a known, documented behaviour: the export documentation says which columns are neutralised and how, so a consumer can strip a leading quote if it needs the original.
Where machines are the main consumers, a better answer is to give them a separate channel: an API or a raw export option intended for programmatic use, clearly labelled as not for opening in spreadsheet programs. Then the human-facing export can be safe by default without breaking integrations.
5Offering .xlsx for Spreadsheet Users
If the people who download the export open it in Excel anyway, the cleanest fix is to give them an .xlsx file. In an .xlsx, every cell has an explicit type. When we write a value with cell.setCellValue(String) in Apache POI, it is stored as a string cell, and Excel never evaluates a string cell as a formula, whatever it starts with. Numbers are real numbers, dates are real dates, and leading zeros in ids survive.
So a good export screen offers .xlsx for people and CSV for data transfer, and both are generated by a shared library that applies the same rules. That library is also the right place for a unit test that feeds in every dangerous prefix and checks the output, so the fix cannot quietly disappear in the next refactoring.
6Follow-up questions
Is this really our problem, or the spreadsheet program's?
Both, and security reviews will hold us responsible because we produce the file and know it will be opened in a spreadsheet. Spreadsheet vendors have added warnings, but users click through them. Neutralising user-provided values is a cheap control on our side.
Should we also sanitise the data when it is stored?
No. We store what the user entered and escape it for each output context: HTML escaping for web pages, parameters for SQL, neutralising for CSV. Changing data on input breaks other uses and still misses data that arrived through other paths.
Does Excel always evaluate a value starting with + or -?
Not always; behaviour differs between programs, versions and settings, which is exactly why the rule covers all of the triggering characters rather than relying on how one program behaves today.
7Conclusion
In this answer, we saw that CSV injection uses the way spreadsheet programs guess types: a value starting with =, +, -, @, tab or carriage return can become a formula when a colleague opens our export. Quoting protects the CSV structure but not the spreadsheet, so we neutralise user-provided values with a leading single quote in one shared function, always write through OpenCSV or Commons CSV rather than concatenating strings, document the behaviour for consumers that need raw values, and offer .xlsx with typed string cells for people who open exports in Excel.
Open with
CSV has no types, so when someone opens our export in Excel, a value that starts with an equals sign, or in many programs a plus, minus or at sign, or a tab or carriage return hiding one of those, is treated as a formula.
Then, if they want more
If that value came from a user, say a display name, an attacker can make a link that sends other cells to their server, or in old setups use DDE to run commands, on the machine of the colleague who opens the file. Quoting the field doesn't help, because the spreadsheet removes the quotes and then sees the equals sign.
So for every field that can hold user-provided text, I'd prefix risky values with a single quote, which spreadsheets treat as a text marker, in one shared function with unit tests. I would not touch numeric columns we generate, since minus five is a real number.
I'd always write with CSVPrinter or CSVWriter, never by concatenating strings, so quoting and escaping are correct. I'd document which columns are neutralised for consumers that need raw values, or give machines a separate API or raw export. And for people who open exports in Excel, I'd offer .xlsx, where string cells are never evaluated as formulas.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Users download CSV exports and open them in Excel. A security review flags CSV...": https://scrivio-interview-library.netlify.app/java/document-libraries/960eac10a2/