T09 · Insecure Skill Coding Practices
- Location
src/html_reporter.py:373- Finding
Spreadsheet Formula Injection in Generated CSV Reports
- Content
View full analysis
Vulnerability Details
File Location:
src/html_reporter.py:373-379; duplicate vulnerable writer atmain.py:274-280
Vulnerability Type: CSV/spreadsheet formula injection
Risk Level: MediumVulnerable Code
src/html_reporter.py:373-379:python def _write_csv(path: Path, rows: list[dict]) -> None: import csv keys = sorted({k for row in rows for k in row}) if rows else ["empty"] with path.open("w", newline="", encoding="utf-8-sig") as f: writer = csv.DictWriter(f, fieldnames=keys) writer.writeheader() writer.writerows(rows)main.py:274-280:python def _write_csv(path: Path, rows: list[dict]) -> None: import csv keys = sorted({k for row in rows for k in row}) if rows else ["empty"] with path.open("w", newline="", encoding="utf-8-sig") as f: writer = csv.DictWriter(f, fieldnames=keys, extrasaction="ignore") writer.writeheader() writer.writerows(rows)Untrusted document-derived data enters exported records through code such as
src/fact_extractor.py:95-128:python fact = FinancialFact( fact_id=f"F{len(facts)+1:06d}", metric=segment.metric_canonical_exact, raw_metric=segment.metric_alias, metric_canonical_exact=segment.metric_canonical_exact, metric_category=segment.metric_category, raw_value=raw, raw_value_text=raw, value=value, currency="人民币", raw_unit=raw_unit, unit_source=unit_source, unit_confidence=UNIT_CONFIDENCE.get(unit_source, 0.0), unit=raw_unit, period=period, period_exact=period_exact, period_type=period_type, scope=_scope(haystack, ev.filename), subject="发行人", category=_category(ev), value_type=value_type, is_percent=value_type in {"percent", "percentage_point"}, is_calculated=segment.is_calculated, source_doc_id=ev.doc_id, filename=ev.filena ...[truncated 3181 chars]- Remediation
View remediation
Remediation Suggestions
Introduce a single centralized CSV-cell sanitizer and apply it to every value written by both CSV writer implementations.
- Treat any string beginning with
=,+,-, or@as potentially dangerous. - Account for leading tabs, carriage returns, newlines, and other whitespace that spreadsheet applications may ignore before formula evaluation.
- Prefix dangerous values with an apostrophe or another spreadsheet-compatible text marker.
- Preserve numeric Python values as numbers; sanitize only string fields.
- Apply the protection consistently to
financial_facts.csv,comparison_exclusions.csv,caliber_notes.csv,arithmetic_skips.csv,parse_errors.csv, andunparsed_items.csv. - Add regression tests covering every formula prefix and leading-whitespace bypass.
- Document that CSV reports contain untrusted source-document content and should not be opened with formula execution enabled.
Example hardening implementation:
python FORMULA_PREFIXES = ("=", "+", "-", "@") def _safe_csv_cell(value): if not isinstance(value, str): return value candidate = value.lstrip(" \t\r\n") if candidate.startswith(FORMULA_PREFIXES): return "'" + value return value def _sanitize_csv_rows(rows: list[dict]) -> list[dict]: return [ {key: _safe_csv_cell(value) for key, value in row.items()} for row in rows ] def _write_csv(path: Path, rows: list[dict]) -> None: import csv safe_rows = _sanitize_csv_rows(rows) keys = sorted({k for row in safe_rows for k in row}) if safe_rows else ["empty"] with path.open("w", newline="", encoding="utf-8-sig") as f: writer = csv.DictWriter(f, fieldnames=keys, extrasaction="ignore") writer.writeheader() writer.writerows(safe_rows)The separate writer in
src/document_parser.pyshould use the same sanitizer rather than maintaining an independen ...[truncated 23 chars]- Treat any string beginning with
