T09 · Insecure Skill Coding Practices
- Location
scripts/cli.js:7- Finding
Unsafe CSV Generation Enables Spreadsheet Formula Injection and Malformed Exports
- Content
View full analysis
Vulnerability Details
File Location:
scripts/cli.js, lines 7-10
Vulnerability Type: CSV/spreadsheet formula injection and improper CSV encoding
Risk Level: LowVulnerable Code
js function writeCsv(header, rows, out){ const csv = [header.join(',')].concat(rows.map(r => header.map(h => (r[h]===undefined?'':String(r[h]).replace(/\n/g,' '))).join(','))).join('\n'); fs.writeFileSync(out, csv); }Technical Analysis
The CSV exporter converts API-derived values to strings and concatenates them using commas. It does not perform standards-compliant CSV encoding:
- Embedded commas are not protected by quoting.
- Embedded double quotes are not escaped.
- Carriage returns are not removed or encoded.
- Cells beginning with spreadsheet formula indicators such as
=,+,-, or@are not neutralized.
Consequently, attacker-controlled or compromised API data can alter the exported CSV structure. When a generated file is opened in spreadsheet software, a formula-leading value may be interpreted as an active formula rather than inert data.
Exploitation requires a malicious or compromised upstream response, or another means of introducing crafted values into fields returned by the IAMMETER API. The audit found no evidence that this project itself manipulates the API response maliciously.
Attack Path
- An attacker gains the ability to influence a value returned in IAMMETER energy-history data, such as through a compromised upstream service or account data.
- The attacker supplies a value containing CSV delimiters or beginning with a spreadsheet formula marker.
- A user runs the
historycommand with the--outoption. writeCsv()inserts the crafted value directly into the CSV without quoting, escaping, or formula neutralization.- The user opens the exported file in spreadsheet software.
- The spreadsheet may evaluate the crafted formula or display structurally corrupted data.
Impact Assessment
The issue does n ...[truncated 713 chars]
- Remediation
View remediation
Remediation Suggestions
- Replace manual CSV concatenation with a maintained CSV serialization library that correctly quotes fields and escapes embedded quotes.
- If implementing serialization locally:
- Convert each value to a string.
- Escape every
"as"". - Wrap fields containing commas, quotes, carriage returns, or line feeds in double quotes.
- Neutralize spreadsheet formulas in untrusted values. For cells beginning with
=,+,-,@, tab, or carriage return, prefix the value with an apostrophe or apply another documented formula-injection mitigation suitable for the target spreadsheet application. - Preserve the distinction between raw machine-readable CSV and spreadsheet-safe export modes where data fidelity requirements prohibit altering formula-leading text.
- Add tests covering commas, quotes, CR/LF sequences, empty values, Unicode, and formula-leading payloads.
- Document that exported API data must be treated as untrusted when opened in spreadsheet software.
