T09 · Insecure Skill Coding Practices
Error
- Location
- converter.py:911
- Finding
- Spreadsheet Formula Injection in Generated Excel Workbooks<![CDATA[ ## Vulnerability Details **File Location**: `converter.py`, lines 911-930 **Vulnerability Type**: Untrusted spreadsheet formula injection **Risk Level**: High ### Vulnerable Code ```python for r, row_data in enumerate(rows, 2): for col, field in enumerate(TEMPLATE_COLUMNS, 1): cell = ws.cell(row=r, column=col, value=_template_cell_value(row_data, field)) if field == "日期": cell.number_format = "@" ``` The same unsafe write occurs in the summary workbook: ```python for r, row_data in enumerate(all_rows, 2): for col, field in enumerate(headers, 1): cell = ws.cell(row=r, column=col, value=_template_cell_value(row_data, field)) if field == "日期": cell.number_format = "@" ``` ### Technical Analysis Values originating from untrusted bank statement files are written directly into Excel cells without neutralizing spreadsheet formula prefixes. Text fields such as counterparty name, bank name, account number, summary, and remarks can therefore contain values beginning with `=`, `+`, `-`, or `@`. Spreadsheet software may interpret these values as formulas rather than literal text. Depending on the spreadsheet application and its security settings, a malicious formula can: - Display deceptive or manipulated content. - Reference other cells or workbook data. - initiate external-link requests that disclose information. - Abuse legacy formula capabilities or external data handlers. - Cause disruptive calculations or resource consumption. Setting the text number format only for date cells does not protect the other attacker-controlled fields. The issue affects both each source-specific workbook and the consolidated `汇总.xlsx` workbook. ### Attack Path 1. An attacker creates or modifies an otherwise valid supported bank statement. 2. The attacker inserts a formula-prefixed value into a mapped textual field, such as a remark or counterparty name. 3. A user runs the converter on the crafted statement ...[truncated 973 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions Introduce a single output-sanitization function for all untrusted textual fields before assigning values to cells. ```python DANGEROUS_FORMULA_PREFIXES = ("=", "+", "-", "@") def safe_excel_text(value): if value is None: return "" text = str(value) if text.startswith(DANGEROUS_FORMULA_PREFIXES): return "'" + text return text ``` Apply this protection to fields intended to be text, including summary, remarks, counterparty information, account identifiers, source company names, and any other source-derived labels. Keep verified numeric amount fields numeric where possible instead of converting every field indiscriminately. Additional hardening measures: 1. Use explicit field schemas that distinguish dates, numeric amounts, and literal text. 2. Set textual cells to the string data type where appropriate. 3. Sanitize values after trimming leading whitespace, or reject suspicious leading control characters that could obscure a formula prefix. 4. Add tests for payloads beginning with `=`, `+`, `-`, `@`, tabs, carriage returns, and leading whitespace. 5. Verify generated files in all supported spreadsheet applications because formula handling differs between products. ]]>
