Improper Neutralization of Formula Elements in a CSV File

Incomplete Base
Structure: Simple
Description

This vulnerability occurs when an application writes user-supplied data into a CSV file without properly sanitizing special characters. Spreadsheet programs like Excel or Google Sheets interpret entries starting with '=' as executable formulas, which can lead to malicious command execution when the file is opened.

Extended Description

Many applications export data to CSV files for easy viewing in spreadsheet software. However, these programs treat cells beginning with characters like =, +, -, or @ as formulas. If an attacker can inject data like `=HYPERLINK("http://malicious-site.com", "Click")` or a formula that calls external scripts, simply opening the CSV file can trigger data theft, remote code execution, or other harmful actions on the user's machine. Developers often overlook this risk because CSV files are seen as simple data dumps, not executable content. To prevent this, you must sanitize or escape formula-initiating characters in all user-controlled fields before writing them to a CSV. This ensures that data is displayed as static text, not as an active command, when the file is imported into a spreadsheet application.

Common Consequences 1
Scope: Confidentiality

Impact: Read Application DataExecute Unauthorized Code or Commands

Current versions of Excel warn users of untrusted content.

Potential Mitigations 3
Phase: Implementation
When generating CSV output, ensure that formula-sensitive metacharacters are effectively escaped or removed from all data before storage in the resultant CSV. Risky characters include '=' (equal), '+' (plus), '-' (minus), and '@' (at).

Effectiveness: Moderate

Phase: Implementation
If a field starts with a formula character, prepend it with a ' (single apostrophe), which prevents Excel from executing the formula.

Effectiveness: Moderate

Phase: Architecture and Design
Certain implementations of spreadsheet software might disallow formulas from executing if the file is untrusted, or if the file is not authored by the current user.

Effectiveness: Limited

Demonstrative Examples 1
Hyperlinks or other commands can be executed when a cell begins with the formula identifier, '='

Code Example:

Attack
Other

=HYPERLINK(link_location, [friendly_name])

Stripping the leading equals sign, or simply not executing formulas from untrusted sources, impedes malicious activity.

Code Example:

Good
Other

HYPERLINK(link_location, [friendly_name])

Observed Examples 3
CVE-2019-12134Low privileged user can trigger CSV injection through a contact form field value
CVE-2019-4521Cloud management product allows arbitrary command execution via CSV injection
CVE-2019-17661CSV injection in content management system via formula code in a first or last name
References 4
CSV Injection
OWASP
02-02-2020
ID: REF-21
Data Extraction to Command Execution CSV Injection
Jamie Rougvie
06-09-2019
ID: REF-22
The Absurdly Underestimated Dangers of CSV Injection
George Mauer
07-10-2017
ID: REF-23
Comma Separated Vulnerabilities
James Kettle
29-08-2014
ID: REF-24
Applicable Platforms
Languages:
Not Language-Specific : Undetermined
Technologies:
Other : Undetermined
Modes of Introduction
Implementation
Alternate Terms

CSV Injection

Formula Injection

Excel Macro Injection