T09 · Insecure Skill Coding Practices
- Location
index.js:67- Finding
Terminal Escape-Sequence Injection Through Untrusted Trend Data
- Content
View full analysis
{ const rank = t.rank.toString().padStart(2, ' '); const name = t.name.length > 28 ? t.name.substring(0, 27) + '…' : t.name.padEnd(30); const volume = t.volume === 'N/A' ? chalk.gray(t.volume) : chalk.cyan(t.volume); console.log(`${chalk.gray(rank + '.')} ${chalk.white.bold(name)} ${volume.padStart(15)}`); }); ``` ### Technical Analysis The trend name and volume are extracted from an external website and passed to terminal output without removing terminal control characters. Applying Chalk formatting does not neutralize arbitrary ANSI, OSC, or other control sequences already embedded in an untrusted string. Consequently, a compromised or malicious upstream response could place escape sequences inside a trend name or volume. When the default table output is displayed, a compatible terminal may interpret those sequences rather than rendering them as ordinary text. The HTTPS connection limits ordinary network interception, so exploitation generally requires control or compromise of `getdaytrends.com`, manipulation of its trend data source, or another ability to influence the HTML returned by that service. ### Attack Path 1. An attacker obtains the ability to influence trend content returned by the external aggregator or compromises the aggregator. 2. The attacker inserts terminal escape sequences into a trend name or volume. 3. A user runs `x-trends` without JSON mode. 4. The CLI extracts the malicious string and sends it directly to `console.log`. 5. The user's terminal interprets the injected sequence. 6. Depe ...[truncated 908 chars]- Remediation
View remediation
String(value) .replace(/\u001B\][^\u0007]*(?:\u0007|\u001B\\)/g, '') .replace(/\u001B(?:[@-_]|\[[0-?]*[ -/]*[@-~])/g, '') .replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F]/g, ''); const safeName = sanitizeTerminalText(t.name); const safeVolume = sanitizeTerminalText(t.volume); ``` The final implementation should use a well-tested library where possible because terminal escape-sequence syntax is complex. ]]>
