T09 · Insecure Skill Coding Practices
- Location
scripts/organize_music.py:924- Finding
Spreadsheet Formula Injection in Generated CSV Reports
- Content
View full analysis
Vulnerability Details
File Location:
scripts/organize_music.py:681-688andscripts/organize_music.py:924-928
Vulnerability Type: CSV formula injection
Risk Level: MediumComplete Code Snippet
python self.report.append({ '类型': '音乐', '原路径': src, '目标路径': str(target_path), '歌手': full_artist, '第一歌手': first_artist, '歌曲': title, '状态': '干跑-待移动', '音质': rank, '大小KB': file_size_kb(src), '来源': source_kind })python with open(self.report_path, 'w', newline='', encoding='utf-8-sig') as fp: w = csv.DictWriter(fp, fieldnames=['类型', '原路径', '目标路径', '歌手', '第一歌手', '歌曲', '状态', '音质', '大小KB', '来源']) w.writeheader() w.writerows(self.report)Technical Analysis
Artist names, song titles, source paths, and related values originate from media filenames or metadata tags. These externally controlled values are placed into the report and written directly to a CSV file without neutralizing spreadsheet formula prefixes.
CSV quoting does not prevent formula interpretation. If a value begins with
=,+,-, or@, spreadsheet software may interpret the cell as a formula when the report is opened. Depending on the spreadsheet application and its security settings, a malicious formula may initiate external requests, disclose report contents, create misleading links, or invoke other dangerous spreadsheet functionality.The project documentation explicitly recommends opening generated reports in Excel or LibreOffice, making spreadsheet interpretation part of the expected workflow.
Attack Path
- An attacker supplies a music file whose filename or metadata artist/title field begins with a spreadsheet formula prefix.
- The owner places the crafted file in a library scanned by
organize_music.py. - The script extracts the attacker-controlled value and adds it to
self.report. csv.DictWriter.writerows()writes the va ...[truncated 917 chars]
- Remediation
View remediation
Remediation Suggestions
- Apply a centralized CSV-safety function to every externally derived string before writing reports.
- Prefix cells beginning with
=,+,-,@, tab, or carriage return with a single quote. - Preserve an unmodified machine-readable report in JSON if exact source values are required.
- Document that reports should be imported with formula evaluation disabled.
- Add tests using filenames and tags such as
=1+1,+SUM(1,1),-1+2, and@SUM(1,1). - Do not rely on
csv.DictWriterquoting alone, because quoting addresses CSV syntax rather than spreadsheet formula execution.
Example hardening:
python def csv_safe(value): if not isinstance(value, str): return value if value.startswith(('=', '+', '-', '@', '\t', '\r')): return "'" + value return value safe_rows = [ {key: csv_safe(value) for key, value in row.items()} for row in self.report ] w.writerows(safe_rows)
