T09 · Insecure Skill Coding Practices
- Location
fakturator.py:237- Finding
CSV Formula Injection Through Unescaped Invoice Fields
- Content
View full analysis
Vulnerability Details
File Location:
fakturator.py:237-256
Vulnerability Type: CSV formula injection
Risk Level: MediumVulnerable Code
python lines.append(f"Numer;{invoice['invoice_number']}") lines.append(f"Data wystawienia;{invoice['invoice_date']}") lines.append(f"Data sprzedaży;{invoice['sale_date']}") lines.append(f"Termin płatności;{invoice['payment_date']}") lines.append(f"Forma płatności;{invoice['payment_method']}") lines.append("") lines.append("SPRZEDAWCA") lines.append(f"Nazwa;{invoice['seller']['name']}") lines.append(f"Adres;{invoice['seller']['address']}") lines.append(f"NIP;{invoice['seller']['nip']}") lines.append("") lines.append("NABYWCA") lines.append(f"Nazwa;{invoice['buyer']['name']}") lines.append(f"Adres;{invoice['buyer']['address']}") lines.append(f"NIP;{invoice['buyer']['nip']}") lines.append("") lines.append("POZYCJE") lines.append("Lp;Nazwa;Ilość;Cena jedn.;VAT %;Netto;Brutto") for i, item in enumerate(invoice['items'], 1): lines.append(f"{i};{item.get('name','')};{item.get('quantity',1)};{item.get('amount',0)};{item.get('vat_pct',23)};{item.get('total',0)};{item.get('brutto',0)}")Technical Analysis
The CSV formatter inserts invoice data directly into semicolon-delimited cells without applying CSV quoting or spreadsheet formula neutralization. Several of these values—including invoice numbers, payment fields, seller and buyer details, addresses, and item names—may originate from an external party.
Spreadsheet applications may treat a cell beginning with
=,+,-, or@as a formula rather than plain text. Consequently, a value such as:text =HYPERLINK("https://attacker.example","Open invoice")can retain formula semantics in the generated output. The implementation also fails to escape semicolons, quotation marks, carriage returns, and line feeds, allowing attackers to alter the apparent row and column structure and place a formula at the beginning of a new cell.
Using Pyth ...[truncated 1522 chars]
- Remediation
View remediation
Remediation Suggestions
- Serialize rows with Python's standard
csvmodule instead of manually joining fields. Configure the intended semicolon delimiter and explicit newline handling. - Treat every externally controlled text value as potentially dangerous, including invoice metadata, seller and buyer fields, payment fields, notes, and item names.
- Before CSV serialization, neutralize values whose first effective character is
=,+,-, or@. Account for leading spaces, tabs, carriage returns, and line feeds that may be ignored by spreadsheet applications. - Prefix dangerous values with an apostrophe or apply another documented safe-text convention appropriate for the supported spreadsheet applications.
- Preserve numeric fields as validated numeric types rather than accepting arbitrary strings.
- Add tests covering formula prefixes, leading whitespace, semicolons, quotation marks, embedded CR/LF characters, and multiline values.
- Document that CSV exports contain untrusted business data and should be imported as text where spreadsheet software supports explicit column typing.
A possible defensive helper is:
python import csv import io FORMULA_PREFIXES = ("=", "+", "-", "@") def safe_spreadsheet_text(value): text = str(value) if text.lstrip(" \t\r\n").startswith(FORMULA_PREFIXES): return "'" + text return text def write_csv_rows(rows): output = io.StringIO(newline="") writer = csv.writer( output, delimiter=";", quotechar='"', quoting=csv.QUOTE_ALL, lineterminator="\n", ) for row in rows: writer.writerow([safe_spreadsheet_text(value) for value in row]) return output.getvalue()Numeric fields should be handled separately where possible so that legitimate negative numbers are not unnecessarily converted to text.
- Serialize rows with Python's standard
