T09 · Insecure Skill Coding Practices
- Location
scripts/extract_data.py:92- Finding
Spreadsheet Formula Injection Through Attacker-Controlled Image Filenames
- Content
View full analysis
Vulnerability Details
File Location:
scripts/extract_data.py:92andscripts/extract_data.py:187-190
Vulnerability Type: Spreadsheet formula injection
Risk Level: MediumVulnerable Code
python # scripts/extract_data.py:187-190 records = extract_rental_data(texts) for r in records: r['图片'] = f"{os.path.basename(img_path)}-{i+1}" all_records.extend(records)python # scripts/extract_data.py:90-93 for row, record in enumerate(records, 2): ws.cell(row=row, column=1, value=row-1) ws.cell(row=row, column=2, value=record.get('图片', '')) ws.cell(row=row, column=3, value=record.get('区域', ''))Technical Analysis
The image filename originates from a user-supplied command-line path. Its basename is placed into each generated record without validation and is subsequently written directly to an XLSX cell.
Spreadsheet values beginning with
=can be stored as formulas byopenpyxl. Spreadsheet applications may also treat values beginning with other formula-triggering characters, such as+,-, or@, as active expressions in some contexts. Appending-{i+1}does not reliably neutralize formulas because an attacker can construct a filename for which the resulting cell value remains a valid expression.The vulnerability requires OCR to produce at least one rental record because filename cells are only generated for extracted records.
Attack Path
- An attacker creates or distributes an image whose filename begins with a spreadsheet formula, such as a formula that performs a network request.
- The victim processes the file using:
bash python3 scripts/extract_data.py --images 'ATTACKER_CONTROLLED_FILENAME' --output data.xlsx - OCR finds text matching the rental-data patterns, causing at least one record to be generated.
- The attacker-controlled basename is copied into the
图片field and written directly to the workbook. - The ...[truncated 912 chars]
- Remediation
View remediation
Remediation Suggestions
Sanitize every user-controlled or OCR-controlled value before writing it to a spreadsheet cell.
- Prefix values beginning with formula-triggering characters with an apostrophe.
- Explicitly store such cells as strings.
- Apply the protection through a centralized helper so that future exported fields receive the same treatment.
- Add tests covering
=,+,-,@, tabs, carriage returns, and leading whitespace before formula characters.
Example hardening:
python def safe_spreadsheet_value(value): if not isinstance(value, str): return value dangerous_prefixes = ("=", "+", "-", "@", "\t", "\r") if value.startswith(dangerous_prefixes): return "'" + value return value image_cell = ws.cell( row=row, column=2, value=safe_spreadsheet_value(record.get("图片", "")) ) image_cell.data_type = "s"The same helper should be applied to all text fields written to XLSX or CSV output, including any fields populated from OCR results in future versions.
