CSV formula injection in invoice exports

CSV formula injection turns text starting with =, +, - or @ into a spreadsheet formula. Protect invoice text, keep credits numeric, test a reopen.

See the PDF-to-Excel example

CSV formula injection happens when a CSV field that starts with =, +, -, @, a tab or a line-break character is opened in a spreadsheet and evaluated as a formula instead of being shown as text. For invoice exports, the exposed fields are the text ones, such as supplier descriptions and references: neutralize those fields before export, keep real negative amounts numeric, and check the values again after the file is saved and reopened.

The OWASP page on CSV injection describes the risk and its limits: a crafted formula can extract data from open spreadsheets or exploit users who click through security warnings, and no single escaping method works for every spreadsheet and every downstream program. Item codes raise a related import problem covered by keeping leading zeros when Excel opens a CSV: in both cases the fix starts with text columns.

What is CSV formula injection?

A CSV file has no cell types. RFC 4180 defines fields, commas, quotes and line breaks, and nothing else. The spreadsheet that opens the file decides what each field means, and a field that looks like a formula can be run as one.

OWASP lists the characters that can start a formula: =, +, - and @, plus tab (0x09), carriage return (0x0D) and line feed (0x0A). Full-width forms such as = and + can behave the same way in some locales. The danger is not limited to the first character of a line: a value containing a delimiter or a quote can open a new cell whose first character is =.

In invoice data, most such values are innocent. A supplier label like =00123, a pack name like +Pack or a reference like -lot is ordinary text on the PDF. The problem is that the spreadsheet does not know that.

How do you prevent CSV injection in an export?

OWASP’s general mitigation for fields that start with a formula character:

  1. Prefix the field with a single quote ('), so the spreadsheet shows it as text.
  2. Enclose every field in double quotes.
  3. Escape any double quote inside the field by doubling it ("").

OWASP also documents the limits. In Excel, the quote-based protection may not survive a save and reopen, which can reactivate the formula. For Excel, OWASP suggests an alternative: a tab character prefixed inside the quoted field. The tab stays in the data and can break automated imports; it suits files meant for human reading. Quoting alone is not a protection: quotes delimit the field, they do not stop its content from being evaluated.

Apply the prefix only to text columns. A sanitizer that prefixes every value starting with - also turns a genuine credit of −12.50 into text, and the amount drops out of every sum.

InvoiceLineLab extracts invoice line items from PDF to Excel or CSV, keeps descriptions and references in their own text columns, apart from the numeric amount columns, and records the source page of each row, so a supplier label such as =00123 can be checked against the PDF. The synthetic example shows the column layout.

What does formula injection look like on an invoice line?

This fictional vendor (supplier) invoice has a gasket line and a credit line. Amounts are in USD, per piece, before tax.

InvoiceLineItem referenceLiteral descriptionQuantityUnit priceLine net
INV-2026-1047GSK-08=00123412.5050.00
INV-2026-1048GSK-08Returned gasket-112.50-12.50
Net total37.50

If the spreadsheet evaluates the description, line 7 shows the number 123. Quantity 4 × 12.50 = 50.00 still passes, the credit −1 × 12.50 = −12.50 still passes, and the net total 50.00 − 12.50 = 37.50 is unchanged. But 123 is not =00123: the reviewer who checks the variant label against the purchase order loses the evidence. Do not approve a line only because its amount agrees.

The value here is harmless and invented; it contains no external reference. Use only invented values like this when you test.

How do you open an untrusted CSV safely in a spreadsheet?

Import the file instead of double-clicking it, and set descriptive columns to Text before loading. Do not accept prompts to enable external content or to update links from a file you did not expect. Then compare the imported values with the source, row by row:

  1. Locate the row by invoice and line identifiers, never by description alone, and keep the PDF page reference.
  2. Compare item reference and exact description with the source.
  3. Check that the description cell holds text, not a formula; then save, close, reopen through the same path and compare again.
  4. Check numeric neighbours separately. A description mismatch is an exception even when quantity, price and net agree.

Which invoice fields need which treatment?

A suspicious prefix is a reason to inspect a field, not evidence that a supplier is malicious. Spreadsheet behavior depends on the receiving application and import path.

Value and intended fieldReview questionExpected handoff
=00123 in descriptionWas the literal label preserved?Text =00123, never its calculated result
+Pack or @Series in descriptionCould the receiver interpret the prefix?Text storage and a verified literal value
-lot in item referenceIs the minus part of an identifier?Full identifier kept as text
−12.50 in a validated amount columnIs this a genuine signed amount?Numeric −12.50 under the declared convention
Text with a delimiter, quotes or a leading control characterDid parsing create or alter a cell?Parsed cells reviewed as well as raw text

If a prefix is added for display, record the original string and the display value separately. A machine import must know which field it reads: a prefix that protects a human viewer becomes an extra character in an automated comparison, and stripping it blindly can bring the formula back. If that contract is unclear, pause the handoff and keep the source record rather than emptying the description.

FAQ

Which characters trigger CSV formula injection?

A field starting with =, +, -, @, tab, carriage return or line feed, and in some locales their full-width forms.

Does wrapping a field in double quotes prevent CSV injection?

No. Quotes keep commas and line breaks inside the field; the spreadsheet can still evaluate the content.

Should negative invoice amounts be escaped?

No. Keep real amounts in numeric columns and validate their sign there. Escaping them turns credits into text that sums ignore.

Is a CSV safe if I only open it to read it?

Opening is when formulas run, so import untrusted files with text columns and decline prompts for external content.

Continue the checks

Commas and quotes inside descriptions raise a structural question: see how CSV quoting keeps a comma inside one field. To investigate a disputed value, trace the spreadsheet row back to its PDF page; to combine suppliers, start from a declared column map for supplier invoices. Descriptions belong to line items rather than header fields.

InvoiceLineLab turns supplier invoice lines into reviewable spreadsheet rows. Compare the fictional invoice and its rows and the invoice data validation checks.

Sources and further reading

Get started with InvoiceLineLab

Enter your work email to get access to InvoiceLineLab. No account or payment is created at this stage.

Submitting sends your email and visit context to our publisher’s infrastructure, named on the Privacy page. No account, payment or automatic email is triggered. Privacy.