T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/import_health.py:130
- Finding
- Unbounded ZIP and CSV Processing Can Cause Resource Exhaustion<![CDATA[ ## Vulnerability Details **File Location**: `scripts/import_health.py`, lines 130–164 **Vulnerability Type**: Uncontrolled resource consumption during archive processing **Risk Level**: Medium ### Vulnerable Code ```python def parse_zip(zip_path): """Extract and parse CSVs from a ZIP export.""" data = {"vitals": []} try: with zipfile.ZipFile(zip_path, 'r') as z: for filename in z.namelist(): if not filename.endswith('.csv'): continue metric_name = None for key, val in METRIC_MAP.items(): if key in filename: metric_name = val break if not metric_name: continue with z.open(filename) as f: content = io.TextIOWrapper(f) reader = csv.DictReader(content) for row in reader: value = row.get("value") or row.get("qty") date = row.get("startDate") or row.get("date") unit = row.get("unit") or "" if value and date: try: val_float = float(value) if value.replace('.', '', 1).replace('-', '', 1).isdigit() else value except: val_float = value data["vitals"].append({ "metric": metric_name, "value": val_float, "date": date, "unit": unit, "source": "Apple Watch Ultra 2" }) except OSError as e: print(f"⚠️ ZIP file locked: {e}") return {"vitals": []} return data ``` ### ...[truncated 2248 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. **Set an archive size limit** - Reject ZIP files whose compressed size exceeds a configurable maximum before opening them. 2. **Validate ZIP metadata before processing** - Inspect each `ZipInfo` entry. - Enforce maximum entry count, per-entry uncompressed size, and total uncompressed size. - Reject suspicious compression ratios indicative of ZIP bombs. 3. **Limit imported records** - Define maximum rows per CSV and maximum total records per execution. - Stop processing and report a controlled error when a limit is reached. 4. **Process records incrementally** - Avoid retaining the complete archive contents in `data["vitals"]`. - Validate, deduplicate, and persist records in bounded batches. 5. **Apply execution limits** - Add an overall processing deadline or run the importer under operating-system CPU and memory limits. 6. **Harden exception handling** - Explicitly handle `zipfile.BadZipFile`, oversized-archive errors, decoding failures, CSV parsing errors, and resource-limit violations. - Fail closed without attempting partial processing of an archive that violates limits. 7. **Restrict the input directory** - Ensure only the expected user and trusted synchronization service can write to the Health Export directory. - Consider moving candidate archives into a controlled staging directory after validating ownership, type, and size. ]]>
