Neutralize formula characters before exporting user text to CSV
Neutralize formula-starting characters when exporting user text to CSV. A Python fixture checks 13 hostile cells, round-trip parsing and a numeric trade-off.
A support team exports tickets to CSV and opens the file in a spreadsheet. One customer typed their name as a formula. Nothing in the web app treated it as dangerous, because in the app it is only text. In the spreadsheet it may be a formula.
OWASP calls this CSV injection, or formula injection. Its page explains that spreadsheet programs such as Excel and LibreOffice Calc can interpret a cell that starts with = as a formula, and lists the cell-starting characters to watch: =, +, -, @, tab, carriage return and line feed, plus full-width variants of the first four that some locales may treat as formula starters. The same page says plainly that the attack is hard to mitigate and that there is no universal sanitization strategy that is safe for every spreadsheet application and every downstream consumer.
This guide tests one mitigation from that page with a small Python fixture. It was prepared with AI assistance and reviewed on 18 November 2026. It makes no security guarantee. The fixture never opens a spreadsheet application.
The rule under test
OWASP describes a rule aimed at Excel: prefix a cell starting with =, +, - or @ with a tab character inside the quoted field. This fixture also prefixes the other listed starters; that broader handling is a local test choice, not evidence of spreadsheet behavior. It also states the cost. The tab stays in the data, so the rule suits files meant for people to read in a spreadsheet, and it may hurt programs that import the file later.
DANGEROUS = ('=', '+', '-', '@', '\t', '\r', '\n',
'\uff1d', '\uff0b', '\uff0d', '\uff20')
def neutralize(value):
text = '' if value is None else str(value)
return '\t' + text if text.startswith(DANGEROUS) else text
def export(rows):
out = io.StringIO()
w = csv.writer(out, quoting=csv.QUOTE_ALL, lineterminator='\r\n')
for row in rows:
w.writerow([neutralize(c) for c in row])
return out.getvalue()
Two details matter. The check is on the start of the cell text, after any value is converted to a string. And every field is written with quotes, so a comma or quote mark in the input cannot start a new cell. OWASP warns that an attacker can use a separator or quote to place a dangerous character at the start of a new cell, which is why escaping the CSV structure is part of the job and not only the first character.
What the fixture checks
The test builds one row of 13 hostile strings and one row of 8 ordinary values. The hostile row includes each plain starter, tab, carriage return and line feed, the four full-width variants, and two strings from OWASP's examples that mix a formula with quotes and separators. The ordinary row includes text with an equals sign or hyphen in the middle, a number, a comma, an empty string, None and an embedded quote.
It writes the CSV, parses it back with Python's strict csv.reader, and asserts:
- Both rows come back with the same number of cells, so no input split a cell.
- Every hostile cell equals a single tab plus the original text.
- Every ordinary cell equals its original value, with
Nonebecoming an empty string. - No parsed cell starts with a formula character. Cells that begin with the tab count as safe under this rule.
- The integer
5stays5, and-5becomes a tab plus-5.
All checks pass on Python 3.10.12. That last item is a trade-off to decide on purpose. A negative number exported as a real number would now be text, so a spreadsheet will not sum it. If an export carries numbers, write numeric columns from typed values you control and apply the rule only to free text columns.
What passing does not show
The test parses the CSV with a CSV library. It does not show what Excel, LibreOffice or Google Sheets does with the file. OWASP notes that Excel may drop quotes or escape characters when a file is saved and reopened, so a file that was safe when exported may not stay safe after a user edits and saves it. It also notes the tab prefix is an observed Excel behavior that may differ elsewhere.
The fixture also does not cover other file formats, such as XLSX, where the right fix is to write the value as a string cell type instead of a formula. It does not cover anything that reads the export automatically. A parser that expects the original text would need to strip the tab, and a program that trims whitespace would remove the protection.
Practical choices
- Decide who uses the file. A human opening it in a spreadsheet and a program importing it need different rules. OWASP's tab prefix helps the first and can break the second.
- Consider whether a format that stores typed cells fits the use, after checking its own rules.
- Escape on export, at the one place the file is built, and test that place with hostile input.
- Say in the product what the file is for, so nobody assumes it is safe to re-save and share.
- Keep untrusted text out of columns that need to remain numeric.
This is a regression test for one known mistake: writing untrusted text straight into a cell. It is not a full defense.
Sources
- OWASP, CSV Injection: https://owasp.org/www-community/attacks/CSV_Injection