T09 · Insecure Skill Coding Practices
- Location
scripts/convert_96_to_24.py:51- Finding
Workbook Formula Injection Through Unsanitized Cell Copying
- Content
View full analysis
Vulnerability Details
File Location:
scripts/convert_96_to_24.py, lines 51-54
Vulnerability Type: Spreadsheet formula injection
Risk Level: Mediumpython def copy_sheet_data(src_ws, dst_ws): for r in range(1, src_ws.max_row + 1): for c in range(1, src_ws.max_column + 1): dst_ws.cell(r, c, src_ws.cell(r, c).value)The vulnerable function is invoked when constructing the output workbook:
python copy_sheet_data(src_ws, ws96)Technical Analysis
The function copies values from a potentially untrusted source workbook into newly created cells without validating formula-control prefixes or explicitly preserving values as text. When openpyxl receives a string beginning with
=, it may assign the destination cell a formula data type rather than a literal string.Consequently, attacker-controlled text from the input workbook can become an active formula in the generated
source96worksheet. Loading the source workbook withdata_only=Truedoes not fully prevent this condition: although it reads cached values for cells already represented as formulas, a deliberately supplied text cell whose content begins with a formula marker can still be reassigned as a formula when written to the destination workbook.Formula execution depends on the spreadsheet client and its security configuration. Potential payloads include references to external resources, deceptive hyperlinks, data manipulation, and other spreadsheet-supported operations. This finding does not establish arbitrary operating-system command execution by the Python process itself.
Attack Path
- An attacker prepares an XLSX workbook containing the expected headers and 96 non-empty data rows so that it passes source-sheet validation.
- The attacker places a text value beginning with a spreadsheet formula marker, particularly
=, in a copied cell such as the time column. - A user runs the conversion scri ...[truncated 1282 chars]
- Remediation
View remediation
Remediation Suggestions
Treat all copied workbook content as untrusted and distinguish literal text from trusted formulas before assigning values to destination cells.
- Neutralize text beginning with spreadsheet control prefixes such as
=,+,-, or@when formulas are not expected. For example, prefix such values with an apostrophe so spreadsheet applications render them as literal text. - Preserve the source cell's intended data type explicitly rather than relying only on value reassignment. Do not convert source text into formulas implicitly.
- Permit formulas only in cells generated from fixed, application-controlled formula templates, such as those written to
formula24. - Validate source headers, time labels, and data columns against expected types. Numeric metric columns should reject strings and formulas rather than copying or averaging them.
- Add regression tests using text values such as
=1+1,+SUM(1,1),-1+2, and@SUM(1,1). Confirm that copied cells remain literal text and are not saved with a formula data type. - Consider rejecting input workbooks containing unexpected formulas or formula-like text when the intended input format is strictly numeric.
A defensive helper can be used when copying text:
python def safe_excel_value(value): if isinstance(value, str) and value.startswith(("=", "+", "-", "@")): return "'" + value return valueApply this helper to untrusted values before assigning them to destination cells. If negative numeric strings are valid input, validation should first distinguish properly parsed numeric values from arbitrary text so legitimate data is not unnecessarily altered.
- Neutralize text beginning with spreadsheet control prefixes such as
