T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/script.sh:63
- Finding
- Unescaped User-Controlled Data in JSON and CSV Exports<![CDATA[ ## Vulnerability Details **File Location**: `scripts/script.sh`, lines 63–83 **Vulnerability Type**: Improper output encoding, JSON injection, and CSV formula injection **Risk Level**: Medium ### Vulnerable Code ```bash for f in "$DATA_DIR"/*.log; do [ -f "$f" ] || continue local name=$(basename "$f" .log) while IFS='|' read -r ts val; do [ $first -eq 1 ] && first=0 || echo "," >> "$out" printf ' {"type":"%s","time":"%s","value":"%s"}' "$name" "$ts" "$val" >> "$out" done < "$f" done echo "" >> "$out" echo "]" >> "$out" ;; csv) 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 ;; ``` The relevant unsafe output operations are at lines 68 and 80. ### Technical Analysis Arguments supplied to commands such as `check`, `generate`, and `report` are stored without normalization in log files under `~/.local/share/changelog/`. The export function subsequently inserts those stored values directly into JSON and CSV output. For JSON output, the script uses string interpolation without escaping quotation marks, backslashes, control characters, or line breaks. A value containing JSON syntax can terminate the intended string and inject additional properties or objects, or simply produce invalid JSON. Downstream programs may reject the export or process attacker-injected structure as trusted data. For CSV output, fields are concatenated with commas without RFC 4180 quoting. Commas, quotation marks, carriage returns, and line feeds can alter the row or column structure. In addition, values beginning with charact ...[truncated 1589 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Generate JSON through a proper serializer rather than manual string interpolation. For example, use `jq` with `--arg` so quotation marks, backslashes, control characters, and newlines are escaped correctly. 2. Implement RFC 4180-compliant CSV encoding: - Enclose every field in double quotes. - Replace each embedded double quote with two double quotes. - Preserve embedded commas and line breaks only inside properly quoted fields. 3. If CSV files may be opened in spreadsheet applications, neutralize formula-leading values. Prefix cells beginning with `=`, `+`, `-`, or `@` with a single quote or apply another documented spreadsheet-safe encoding policy. 4. Treat log contents as untrusted data even though the files are local, because command arguments and imported or modified log entries can be attacker-controlled. 5. Add regression tests covering quotation marks, backslashes, commas, carriage returns, newlines, Unicode characters, JSON fragments, and spreadsheet formula prefixes. 6. Validate generated JSON with a standards-compliant parser and test generated CSV with a standards-compliant CSV reader before reporting a successful export. ]]>
