T09 · Insecure Skill Coding Practices
- Location
compare.js:112- Finding
Terminal Control-Sequence Injection Through Untrusted BOM Fields
- Content
View full analysis
Vulnerability Details
File Location:
compare.js, lines 112-135
Vulnerability Type: Improper neutralization of terminal control sequences
Risk Level: MediumVulnerable Code
javascript added.forEach(item => { report += ` ✅ ${item.partNo || item['物料编号']} - ${item.name || item['名称']}\n`; }); removed.forEach(item => { report += ` ➖ ${item.partNo || item['物料编号']} - ${item.name || item['名称']}\n`; }); changed.forEach(({ key, changes }) => { report += ` ⚠️ ${key}:\n`; for (const [field, { old: oldVal, new: newVal, pct }] of Object.entries(changes)) { if (pct) { report += ` ${field}: ${oldVal} → ${newVal} (${pct})\n`; } else { report += ` ${field}: ${oldVal} → ${newVal}\n`; } } });The resulting report is sent directly to the terminal at line 152:
javascript console.log(generateCompareReport(args[0], args[1], result));Technical Analysis
Values read from user-supplied CSV or XLSX files—including part numbers, names, field names, and changed values—are interpolated directly into a terminal report. The code does not remove or escape ANSI, OSC, C0, or C1 terminal control sequences before passing the report to
console.log().A malicious BOM can therefore contain escape sequences that are interpreted by the operator's terminal rather than displayed as ordinary text. Depending on terminal capabilities and configuration, these sequences can:
- Clear, overwrite, conceal, or reposition terminal output.
- Apply deceptive formatting to make malicious content appear trustworthy.
- Insert forged report lines or hide legitimate additions and removals.
- Change the terminal title.
- Attempt OSC-based clipboard modification or other terminal-specific actions.
CSV parsing preserves embedded control characters, and XLSX cell values can likewise carry attacker-controlled strings into the report. The vulnerability does not requ ...[truncated 1643 chars]
- Remediation
View remediation
Remediation Suggestions
-
Sanitize every untrusted value before including it in terminal output. Remove ANSI CSI sequences, OSC sequences, and unsafe C0/C1 control characters while retaining necessary characters such as line separators only when intentionally introduced by the application.
-
Apply sanitization at the final output boundary so all report fields are covered, including part numbers, names, field names, old values, new values, and file names.
-
Use a maintained terminal-string sanitization library where possible. If implementing local sanitization, account for both ESC-based and 8-bit C1 sequence forms rather than only stripping color codes.
-
Render control characters in a visible escaped form, such as
\x1b, when preserving forensic visibility is preferable to deleting them. -
Validate imported BOM data and reject fields containing unexpected control characters. Apply maximum field-length limits to reduce terminal-output abuse.
-
Add regression tests using malicious CSV and XLSX values containing CSI screen-clearing sequences, cursor movement, OSC title changes, and OSC clipboard payloads. Confirm that output contains only inert text.
-
For machine-consumable output, provide a structured JSON mode and avoid rendering untrusted data directly in an interactive terminal.
-
