T09 · Insecure Skill Coding Practices
- Location
mx_data.py:289- Finding
Spreadsheet Formula Injection in Generated Excel Workbooks
- Content
View full analysis
str: """Flatten any value to string """ if v is None: return "" if isinstance(v, (dict, list)): return json.dumps(v, ensure_ascii=False) return str(v) ``` ```python with pd.ExcelWriter(file_path, engine="openpyxl") as writer: for table in tables: df = pd.DataFrame(table["rows"], columns=table["fieldnames"]) df.to_excel(writer, sheet_name=table["sheet_name"], index=False) ``` ### Technical Analysis The application treats values, labels, and other tabular content returned by the remote financial API as trusted spreadsheet content. `flatten_value()` converts these values to strings without checking whether they begin with a spreadsheet formula marker. The resulting values are then passed directly to `pandas.DataFrame.to_excel()` using the `openpyxl` engine. In particular, a string beginning with `=` can be stored as a formula rather than inert text. Consequently, an attacker who can influence the API response—or an attacker who compromises the remote API or an upstream data source—could inject a formula into the generated workbook. The same issue may affect API-derived column labels as well as ordinary cell values because both are written to the workbook without formula neutralization. ### Attack Path 1. An attacker gains the ability to influence a value or label returned by `mkapi2.dfcfs.com`, whether through compromised upstream data, compromised API infrastructure, or another API data-injection vector. 2. The attacker supplies a value beginning with `=`, containing a malicious spreadsheet formula. 3. `table_to_rows()` passes the value through `flatten_value()`, which preserves the formula syntax. 4. `write_output_files()` places the value into a pandas DataFrame and exports i ...[truncated 1199 chars]- Remediation
View remediation
str: text = flatten_value(value) if text.startswith(("=", "+", "-", "@")): return "'" + text return text ``` 2. Apply the sanitizer to every untrusted cell value and column label before constructing the DataFrame: ```python safe_rows = [ { sanitize_excel_cell(key): sanitize_excel_cell(value) for key, value in row.items() } for row in table["rows"] ] safe_columns = [sanitize_excel_cell(name) for name in table["fieldnames"]] df = pd.DataFrame(safe_rows, columns=safe_columns) ``` 3. Prefer explicit text formatting for cells containing remote data. Where practical, write cells through `openpyxl` with their data type forced to text rather than relying only on prefix escaping. 4. Sanitize every API-controlled workbook component, including: - Data cell values - Column headers - Entity and indicator labels - Descriptive metadata written into spreadsheet cells 5. Add regression tests using values such as: ```text =1+1 =HYPERLINK("https://example.invalid","Open") +SUM(1,1) -1+2 @SUM(1,1) ``` The tests should reopen the generated workbook with `openpyxl` and verify that each value is stored as literal text rather than with the formula data type. ]]>
