Interactive Playground
Experiment with the vulnerable code and security rule below. Edit the code to see how the rule detects different vulnerability patterns.
pathfinder scan --ruleset python/PYTHON-LANG-SEC-094 --project .About This Rule
Understanding the vulnerability and how it is detected
CSV formula injection (also known as CSV injection or formula injection) occurs when user-supplied data containing spreadsheet formula characters (=, +, -, @, tab, newline) is exported to CSV format and subsequently opened in a spreadsheet application such as Microsoft Excel or Google Sheets.
Spreadsheet applications interpret cells starting with =, +, -, or @ as formulas and execute them. An attacker who can inject data starting with =HYPERLINK("http://evil.com/", "Click me") or =cmd|'/C calc.exe'!A0 (on Windows with DDE enabled) can cause the spreadsheet to make outbound network requests, execute system commands, or display deceptive content when the CSV is opened.
Python's csv.writer() does not protect against formula injection. Use defusedcsv or sanitize field values by prepending a single quote or tab to fields starting with formula characters.
Security Implications
Potential attack scenarios if this vulnerability is exploited
Remote Code Execution via DDE in Excel
Microsoft Excel supports Dynamic Data Exchange (DDE) which allows cells to execute system commands. Formula injection payloads like =cmd|'/C calc.exe'!A0 can execute arbitrary commands on Windows systems with DDE enabled (disabled by default in recent Excel versions but still a risk in legacy environments).
Server-Side Request Forgery via Hyperlinks
=HYPERLINK() formula injection can embed clickable links in spreadsheet cells that redirect users to attacker-controlled URLs when clicked, enabling phishing attacks or tracking of user behavior.
Data Exfiltration via Spreadsheet Formulas
Formulas that reference other cells or perform lookups can exfiltrate data visible in the spreadsheet by encoding it in URL parameters of outbound hyperlink requests, potentially exposing sensitive information to attackers.
Deceptive Content Injection
Formula injection can display deceptive values in spreadsheet cells that differ from the actual CSV data, potentially misleading users about financial figures, status codes, or other important data when they open the exported file.
How to Fix
Recommended remediation steps
- 1Sanitize all user-controlled CSV field values by prepending a tab character or single quote to values starting with =, +, -, or @.
- 2Use the defusedcsv library as a drop-in replacement for csv.writer() that automatically handles formula injection protection.
- 3Apply csv.QUOTE_ALL quoting mode to ensure all fields are quoted, which prevents newline injection in CSV fields.
- 4Include a Content-Disposition: attachment header and Content-Type: text/csv header when serving CSV files to prevent browsers from rendering them.
- 5Educate users about the risks of opening CSV files from untrusted sources in spreadsheet applications.
Detection Scope
How Code Pathfinder analyzes your code for this vulnerability
This rule detects calls to csv.writer() and csv.DictWriter() in Python source code. All call sites are flagged for review to ensure formula injection protection is applied to user-controlled field values in the output. This is a LOW severity audit rule since not all CSV usage involves data that reaches spreadsheet applications.
Compliance & Standards
Industry frameworks and regulations that require detection of this vulnerability
References
External resources and documentation
Similar Rules
Explore related security rules for Python
Insecure XML Parsing (XXE Vulnerability)
xml.etree.ElementTree is vulnerable to XML External Entity (XXE) attacks. Use defusedxml for safe XML parsing.
Insecure xml.dom.minidom Usage (XXE)
xml.dom.minidom is vulnerable to XML External Entity (XXE) attacks. Use defusedxml.minidom for safe XML parsing.
Frequently Asked Questions
Common questions about csv.writer Audit (Formula Injection Risk)
New feature
Get these findings posted directly on your GitHub pull requests
The csv.writer Audit (Formula Injection Risk) rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.