T09 · Insecure Skill Coding Practices
- Location
scripts/analyze_logs.py:552- Finding
Terminal Control-Sequence Injection Through Untrusted Log Messages
- Content
View full analysis
Vulnerability Details
File Location:
scripts/analyze_logs.py, lines 552–555 and 742
Vulnerability Type: Terminal output injection
Risk Level: MediumVulnerable Code
python for i, p in enumerate(result['patterns'], 1): sev = p['severity_label'] out.append(f'[{sev}] #{i} — {p["count"]:,}x occurrences') out.append(f' Message: {p["message"][:200]}')The resulting report is subsequently written directly to the terminal:
python formatter = formatters.get(args.output, format_text) print(formatter(result, args))Technical Analysis
Log messages are untrusted input because external clients, remote services, request parameters, user-agent strings, and other attacker-controlled values are commonly copied into application logs. The text formatter includes these messages in its output without removing ANSI escape sequences or other terminal control characters.
The 200-character truncation does not provide sanitization. A malicious control sequence can fit within this limit and may alter colors, move the cursor, overwrite visible content, hide subsequent findings, forge prompts, or manipulate terminal presentation. The exact behavior depends on the terminal emulator. The code does not independently establish arbitrary command execution, but it does permit manipulation of report integrity and the analyst's terminal display.
Attack Path
- An attacker submits input containing ANSI escape sequences or other terminal control characters to an application whose logs will be analyzed.
- The target application records the attacker-controlled value in a warning, error, or fatal log message.
- An analyst invokes
analyze_logs.pyagainst the affected log. - The analyzer retains the control characters in
LogEntry.message. format_text()inserts the message into the report without sanitization.print()sends the resulting control sequences to the analyst's terminal.- The terminal interprets those ...[truncated 470 chars]
- Remediation
View remediation
Remediation Suggestions
- Remove ANSI escape sequences and unsafe control characters before inserting log data into terminal reports.
- Permit only expected whitespace controls such as newline and tab where they are structurally necessary.
- Apply sanitization to every attacker-controlled field, including messages, source names, and recommendations derived from input.
- Consider rendering nonprintable characters using visible escaped notation such as
\x1b. - Keep the raw value only in a separate, explicitly requested machine-readable output if required for forensic analysis.
- Add tests containing escape characters, cursor-control sequences, carriage returns, backspaces, and bidirectional Unicode controls.
Example hardening approach:
python ANSI_ESCAPE_RE = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') def sanitize_terminal(value): value = ANSI_ESCAPE_RE.sub('', str(value)) return ''.join( ch if ch in '\n\t' or ord(ch) >= 32 else repr(ch)[1:-1] for ch in value )Apply this function before adding any untrusted value to terminal output.
