T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/fastmail.py:164
- Finding
- Unsanitized Email Content Enables Terminal Output Injection<![CDATA[ ## Vulnerability Details **File Location**: `scripts/fastmail.py`, lines 164–187 and 316–322 **Vulnerability Type**: Terminal control-sequence injection through untrusted email content **Risk Level**: Medium ### Vulnerable Code ```python print(f" Subject: {e['subject']}") print(f" From: {fr_name} <{fr}>") print(f" To: {to}") if cc: print(f" CC: {cc}") print(f" Date: {e['receivedAt']}") print(f" Status: {'UNREAD' if unread else 'read'}") print(f" {'─' * 60}") body_text = "" for part in e.get("textBody", []): val = e.get("bodyValues", {}).get(part["partId"], {}) if val.get("value"): body_text += val["value"] if not body_text: for part in e.get("htmlBody", []): val = e.get("bodyValues", {}).get(part["partId"], {}) if val.get("value"): body_text += val["value"] print(body_text[:5000] if body_text else " (no body)") ``` Additional affected output path: ```python subj = e.get("subject", "(no subject)") print(f" {flag} {dt} | {fr:30s} | {subj}") print(f" {e['preview'][:120]}") print(f" id: {e['id']}") print() ``` ### Technical Analysis Email subjects, sender names, sender addresses, previews, and message bodies are controlled by external email senders. The script prints these values directly to the terminal without removing or escaping terminal control characters. A malicious message can contain ANSI or OSC escape sequences. Depending on the terminal emulator, these sequences can: - Change colors or overwrite visible terminal content. - Hide or forge status messages and command output. - Render deceptive terminal hyperlinks. - Change terminal titles. - Attempt clipboard manipulation through supported OSC sequences. - Mislead a human or an AI agent that consumes captured terminal output. Truncating the body by character count does not mitigate the issue because a functional escape sequence can fit within a few bytes. This flaw does not, by itself, execute shell ...[truncated 1321 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Sanitize every untrusted field before printing it in human-readable mode, including subject, sender name, sender address, recipient fields, preview, and body. 2. Remove or visibly escape C0 and C1 control characters, particularly `ESC` (`\x1b`), while selectively preserving safe formatting characters such as newline and tab. 3. Apply sanitization after truncation as well as before display, ensuring that truncation cannot leave malformed control sequences. 4. Use a dedicated helper consistently: ```python import re _CONTROL_CHARS = re.compile( r"[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\x9f]" ) def safe_terminal_text(value): if value is None: return "" return _CONTROL_CHARS.sub( lambda match: f"\\x{ord(match.group()):02x}", str(value), ) ``` 5. Pass every remotely sourced value through `safe_terminal_text()` before interpolation. 6. Prefer JSON output for agent-to-agent processing. Consumers should parse JSON rather than treating human-readable terminal output as trusted instructions. 7. Add tests covering ANSI color sequences, cursor movement, OSC hyperlinks, OSC clipboard sequences, carriage returns, backspaces, and embedded null bytes. ]]>
