Any application that lets one user type text and another user download it as a spreadsheet can turn that text into a formula that runs on the downloader's machine, with the downloader's access. CSV and formula injection (CWE-1236) is easy to dismiss because the server is never touched, yet the victim is usually the most privileged person in the system: the administrator, finance lead or support manager who pulls the export. This guide covers how the weakness works, why the obvious escaping fails, the export encoding rules in OWASP ASVS 5.0 requirement V1.2.10, the spreadsheet-side controls that reduce the blast radius, and how to test, roll out and measure a fix. It is written for application security engineers, developers who own export and reporting features, and endpoint teams who manage office application settings.
Key takeaways
- The sink is the spreadsheet, not the server. Excel, LibreOffice Calc and Google Sheets read a cell that starts with a trigger character as a formula, even when the CSV field is quoted, so the defence belongs in the export code that writes the file.
- Six leading characters matter, plus the separators. OWASP lists equals, plus, minus, at, tab, carriage return and line feed (and their full-width forms); ASVS V1.2.10 names equals, plus, minus, at, tab and null. A field separator or stray quote can start a new cell, so RFC 4180 quoting must come first.
- Every fix changes the data. A leading single quote or tab neutralises the formula but alters the value, so decide per export whether the file is for people (escape it) or for machines (do not escape, and do not let people open it in a spreadsheet).
- Native formats beat CSV for human reports. Writing an XLSX file with every user value stored as a typed text cell removes the guesswork that makes CSV hard to secure.
- Office settings reduce, not remove, the risk. Keeping DDE server launch off, OLE auto-activation blocked and data connections on prompt limits command execution in Excel, but data exfiltration through formulas in other tools needs the export fix.
How formula injection works
MITRE describes CWE-1236 as a product that saves user-provided data to a CSV file without neutralising, or while incorrectly neutralising, elements that spreadsheet software may interpret as a command. Its parent is CWE-74, the general class of injection into a downstream component, which places it beside the cases in the Input Validation and Output Encoding Guide. The difference from cross-site scripting or SQL injection is timing and location: the payload is stored by the application, written into a file hours or weeks later, and executed by a desktop or browser-based spreadsheet that the application does not control.
The typical chain has four steps:
- Store: a low-privilege user, customer or external party enters text in a field that is later exported: a name, address, ticket subject, comment, timesheet note or support form.
- Export: a privileged user runs a report, audit log export or bulk download, and the application writes that text unchanged into a CSV or spreadsheet file.
- Open: the privileged user opens the file in a spreadsheet application, trusting it because it came from their own system.
- Evaluate: the spreadsheet treats the cell as a formula and evaluates it in the user's session, with access to the open workbook, other files the user can reach and, depending on settings, the network or local programs.
George Mauer's 2017 research showed both main impacts. In Excel, formulas could launch local programs, behind a security warning that users tend to accept for their own exports. In Google Sheets, macros do not run but functions that make web requests, such as IMPORTXML, combined with text concatenation, could send the contents of the sheet, and of other sheets the user could open, to an external server without any warning. OWASP adds a third impact: formulas can be used to reach known flaws in the spreadsheet software itself. MITRE lists real examples, including CVE-2019-12134 (injection through a contact form field) and CVE-2019-17661 (formula code in a name field).
Impact by spreadsheet behaviour
| Impact | What happens | Main condition | Primary control |
|---|---|---|---|
| Local command execution | A formula asks the spreadsheet to start another program through Dynamic Data Exchange | DDE server launch allowed and the user accepts the warning | Export escaping; keep "Enable Dynamic Data Exchange Server Launch" off |
| Data exfiltration | Web request functions send cell contents to an external URL | The application allows external requests from formulas | Export escaping; data connection and external content policies |
| Tracking and beaconing | Opening the file makes a request that reveals who opened it and when | Same as exfiltration | Export escaping; egress logging |
| Misleading content | A HYPERLINK formula shows trusted text but points to a phishing page | None beyond formula evaluation | Export escaping; user awareness |
| Exploit delivery | A formula reaches a parser or function flaw in the spreadsheet software | Unpatched spreadsheet application | Patch office software; export escaping |
| Data integrity | A formula alters what figures display, for example in a finance report | Recipient trusts calculated values | Export escaping; reconcile reports against source data |
The misleading content and integrity impacts need no security warning at all, which is why reducing the spreadsheet's privileges never closes the issue by itself.
What the standards require
OWASP ASVS 5.0 makes the control testable. Requirement V1.2.10, at Level 3 in the Injection Prevention section, says the application must be protected against CSV and formula injection. It sets two rules: follow the escaping rules in RFC 4180 sections 2.6 and 2.7 when exporting CSV, and when exporting to CSV or other spreadsheet formats (it names XLS, XLSX and ODF) escape equals, plus, minus, at, tab and null with a single quote when they appear as the first character of a field value. Although it sits at Level 3, any export that reaches privileged users or customers is a good candidate to meet it regardless of the target level.
RFC 4180 is an Informational RFC that registers the text/csv media type; it states that it does not specify an Internet standard. Its rule 6 says fields containing line breaks, double quotes or commas should be enclosed in double quotes, and rule 7 says a double quote inside a quoted field is escaped by preceding it with another double quote. These rules are the structural layer: they stop a value from breaking out of its own cell. The leading-character rule is the semantic layer: it stops the cell itself from being read as a formula. Both are needed, in that order of thought.
| Source | Trigger characters | Neutralisation it describes |
|---|---|---|
| OWASP ASVS 5.0 V1.2.10 | = + - @ tab null | RFC 4180 quoting, then a single quote before a leading trigger character, for CSV, XLS, XLSX and ODF |
| OWASP CSV Injection page | = + - @ tab, carriage return, line feed, and full-width = + - @ | Wrap every field in double quotes, double any embedded quote, prefix with a single quote; or a tab inside the quotes for Excel |
| MITRE CWE-1236 | = + - @ | Escape or remove trigger characters, or prefix with an apostrophe; both rated moderate effectiveness |
| RFC 4180 | Not addressed | Quoting of fields with commas, quotes and line breaks; doubled quotes inside quoted fields |
The lists differ, so build the superset into one shared function: equals, plus, minus, at, tab, carriage return, line feed, null and the full-width forms of the first four. OWASP notes that some locales, such as Japanese, may treat full-width characters as formula starts.
Choosing a neutralisation approach
There is no escape that is safe in every spreadsheet and lossless for every consumer. OWASP states that no universal sanitisation strategy works for all spreadsheet applications and downstream consumers, and warns that its quoting techniques are not reliable in Excel after the file is saved and reopened, because Excel may drop the quotes. The decision therefore turns on who reads the file.
| Approach | How it works | Strengths | Trade-offs | Use when |
|---|---|---|---|---|
| Single quote prefix (ASVS rule) | Add ' before a value whose first character is a trigger | Matches ASVS V1.2.10; Excel shows the value as text | The quote is part of the data in other tools; lost if the file is re-saved as CSV | Human-facing CSV reports |
| Tab inside quoted field | Add a tab character at the start of the quoted value | OWASP and Mauer describe it as resistant in Excel | Invisible change to the value; breaks programmatic re-import | Files for viewing only |
| Typed text cells in XLSX or ODS | Write user values as string cells with a spreadsheet library, never as formula cells | No ambiguity: the format records the cell type; no visible change | Larger files; a user who re-saves as CSV reintroduces the risk | Default for reports people open |
| Reject or strip on input | Refuse trigger characters at the start of fields that never need them | Simple for narrow fields such as product codes | Breaks legitimate data (phone numbers starting with +, negative numbers, @handles) | Only as an extra layer for tightly typed fields |
| No escaping, machine channel | Raw RFC 4180 output to an API, SFTP drop or pipeline | Data is exact | Unsafe if a person opens the file | System-to-system interchange with no human viewer |
- Escape at export, never at storage: the stored value should stay exact. Escaping on write to the database corrupts data for every other consumer and still misses rows written before the change.
- Treat numbers as numbers: genuine numeric columns (amounts, counts) are written by the export code as numbers. Only free-text values pass through the neutralising function, which avoids prefixing every negative amount.
- Label escaped exports: if a finance team reconciles exported data, tell them which columns are escaped and how, so a leading quote is not mistaken for corruption.
- Split the export: where both people and machines need the data, offer two endpoints: a human report (XLSX, escaped text) and a machine feed (raw CSV or JSON over an authenticated API).
For smaller teams with one or two export features, a single tested helper that applies RFC 4180 quoting and the ASVS prefix is enough. Larger organisations with many services should put the helper in a shared library, add a lint or static analysis rule that flags hand-built CSV strings, and record the decision for each export in the application's threat model.
Spreadsheet-side controls
The export fix protects files your applications produce. Users also open CSV files from partners, customers and downloads, so the spreadsheet's own settings are a second layer. For Excel, Microsoft documents these settings under File > Options > Trust Center > Trust Center Settings > External Content:
| Setting | Documented default | Recommended position |
|---|---|---|
| Enable Dynamic Data Exchange Server Launch (not recommended) | Not selected | Keep off and enforce through policy so users cannot turn it on |
| Enable Dynamic Data Exchange Server Lookup | Selected | Turn off where no business process uses DDE, after testing |
| Security settings for Data Connections | Prompt user about Data Connections | Prompt, or disable for users with no need |
| Security settings for Workbook Links | Prompt user on automatic update for Workbook Links | Prompt |
| OLE automatic activation (BlockOleAutoActivate DWORD under HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Excel\Security) | Blocked by Excel | Leave at 1 (block) |
| Microsoft Query files (.iqy, .oqy, .dqy, .rqy) | Separate option | Always block the connection of untrusted Microsoft Query files |
Microsoft notes that administrators may lock these settings, which is the point: a user facing a warning on their own export will click through it. Manage them centrally with the same tooling used for macro policy. For Google Sheets and other browser-based tools, review the administrator controls your tenant offers for external data and sharing, and remember that Mauer's exfiltration route needed no warning at all. Endpoint teams should also keep office software patched, since OWASP lists exploitation of spreadsheet flaws as one of the impacts. See the End-User Computing and Spreadsheet Risk Guide for the wider governance of spreadsheets in business processes.
Where identity and secrets fit
The victim of formula injection is defined by who opens the file, so the risk scales with that person's access. Administrators exporting user lists, audit logs or API key inventories hold exactly the access an attacker wants. Two practical points follow. First, exports from identity and access tooling (user directories, access review evidence, service account inventories, token listings) carry text that external parties can influence, such as display names and application names, and deserve the same escaping as any customer-facing report. Second, spreadsheets often hold credentials themselves, so a formula that sends a sheet's contents elsewhere can take keys with it. See Secrets in Office Files for how to find and remove those.
How to implement
- Inventory every export: search the codebase for CSV writers, string concatenation with commas or semicolons, spreadsheet libraries and endpoints returning text/csv. Include admin panels, scheduled email reports, audit log downloads and support tooling. Record who downloads each one and whether a person or a system consumes it.
- Classify each export: human report, machine feed or both. Rank by the privilege of the typical downloader and by whether untrusted parties can write any exported field.
- Build one shared function: apply RFC 4180 quoting (enclose fields with commas, quotes or line breaks in double quotes, double embedded quotes), then the leading-character rule for the superset of trigger characters including full-width forms. Keep numeric types out of it.
- Write unit tests from the standards: one test per trigger character, values containing separators and quotes, values beginning with whitespace before a trigger, full-width characters, empty values and very long values. Use harmless formulas such as
=1+2as fixtures; no test needs a live command. - Pilot on one high-value export: ship behind a feature flag to the admin or audit export first. Open the output in each spreadsheet application your users have (Excel desktop, Excel on the web, LibreOffice Calc, Google Sheets, Numbers) and confirm that test values show as text and no formula evaluates.
- Check downstream consumers: confirm with every team that re-imports the file that the escaping does not break them; move machine consumers to a separate raw feed where it does.
- Roll out by risk ring: admin and audit exports, then customer-facing exports, then internal reports. Roll back by turning off the flag per export; because stored data is unchanged, rollback loses nothing.
- Prevent regression: add a static analysis or code review rule that rejects hand-built CSV output, and include CWE-1236 in the security test plan for new export features.
- Harden the desktop layer in parallel: enforce the Excel External Content settings above through policy, with a pilot group first to catch business processes that genuinely use DDE or data connections.
Detection and measurement
- Stored data scan: query free-text columns that are exported for values whose first character is a trigger. Most hits are benign (phone numbers, negative values), but values that begin with a function name followed by an opening bracket deserve review.
- Endpoint telemetry: alert when a spreadsheet process starts a command interpreter or scripting host as a child process; this catches DDE abuse from any source, not just your exports.
- Egress logging: web requests made by spreadsheet applications to new or rare domains immediately after a file is opened are a strong exfiltration signal.
- Testing evidence: include CWE-1236 checks in dynamic testing and record ASVS V1.2.10 results per application.
| Metric | Target | Why it matters |
|---|---|---|
| Exports using the shared neutralising function | 100% of human-facing exports | Shows coverage of the inventory |
| Exports with a recorded consumer classification | 100% | Without it, escaping decisions are guesses |
| Endpoints with DDE server launch enforced off by policy | 100% of managed Windows endpoints | Removes the main command execution path |
| Spreadsheet-spawned interpreter alerts investigated within SLA | 100% | Shows the detection is acted on |
| Reopened CWE-1236 findings | Zero | Measures regression control |
Common failure patterns
- Quoting and calling it fixed: wrapping every field in double quotes satisfies RFC 4180 but does not stop a spreadsheet evaluating a quoted value that starts with a trigger character.
- Checking only the equals sign: plus, minus, at, tab, line breaks and full-width forms are all listed by OWASP, and a filter for one character leaves the rest open.
- Sanitising on input: stripping characters when data is saved corrupts legitimate values, misses legacy rows and gives developers a false sense that exports are safe.
- Escaping machine feeds: applying the prefix to an integration file silently changes values in the receiving system; the fix belongs on human-facing exports only.
- Forgetting re-saved files: OWASP warns that Excel may strip protective quoting on save; a report that is edited, saved as CSV and shared again can become dangerous.
- Dismissing it as low risk: OWASP notes that many bug bounty programmes exclude the issue, but the attacker controls data that is opened by the organisation's most privileged users, which is a poor reason to skip a small code change. See the File Upload Security Guide for the related case of files coming in rather than going out.
Framework mapping
| Framework | Reference | How it applies |
|---|---|---|
| OWASP ASVS 5.0 | V1.2.10 | The testable requirement for CSV and formula injection protection |
| MITRE CWE | CWE-1236 (child of CWE-74) | Weakness classification for findings and scanner rules |
| NIST SP 800-53 Rev. 5 | SI-10 Information Input Validation; SI-15 Information Output Filtering | Validation of input fields and filtering of output for unexpected content |
| NIST SP 800-53 Rev. 5 | SA-11 Developer Testing and Evaluation; SA-11(1) Static Code Analysis | Unit tests and static rules that prevent regression |
| NIST CSF 2.0 | PR.PS-06 | Secure software development practices in the life cycle |
| ISO/IEC 27001:2022 | Annex A 8.28 Secure coding | Secure coding principles applied to export code |
| CIS Controls v8.1 | Control 16 Application Software Security | Secure development and testing of in-house software |
Practitioner checklist
- Inventory every CSV and spreadsheet export, with its typical downloader and consumer type.
- Use one shared export function that applies RFC 4180 quoting and the ASVS V1.2.10 leading-character rule.
- Cover equals, plus, minus, at, tab, carriage return, line feed, null and full-width variants.
- Escape at export time only; keep stored data exact.
- Prefer XLSX or ODS with typed text cells for reports people open.
- Keep machine-to-machine feeds raw and separate from human downloads.
- Test the output in every spreadsheet application your users run, including browser-based ones.
- Enforce Excel DDE server launch off, OLE auto-activation blocked and data connections on prompt through central policy.
- Alert on spreadsheet processes spawning command interpreters and on unusual spreadsheet egress.
- Add CWE-1236 to code review, static analysis and penetration test scopes.
Standards and references
- OWASP: Application Security Verification Standard 5.0
- MITRE: CWE-1236 Improper Neutralization of Formula Elements in a CSV File
- IETF: RFC 4180 Common Format and MIME Type for Comma-Separated Values (CSV) Files
- OWASP: CSV Injection
- Microsoft Learn: Security settings for Dynamic Data Exchange in Excel Trust Center
- Microsoft Support: Manage DDE and OLE activation security settings in Excel
- Microsoft Support: Block or unblock external content in Office documents
- NIST: SP 800-53 Rev. 5
Related NHI Mgmt Group resources: Input Validation and Output Encoding Guide · File Upload Security Guide · Cross-Site Scripting (XSS) Prevention Guide · End-User Computing and Spreadsheet Risk Guide