T09 · Insecure Skill Coding Practices
- Location
scripts/msg.py:53- Finding
Terminal Control-Sequence Injection Through Unsanitized Cached Data
- Content
View full analysis
Vulnerability Details
File Locations:
scripts/msg.py:53,65scripts/contacts.py:42-47,55scripts/stats.py:41,67scripts/extract.py:38-39,61-62,85-86scripts/auto-reply.py:57-59,71,83,101scripts/example.sh:8-9
Vulnerability Type: Unsanitized terminal output
Risk Level: MediumVulnerable Code
scripts/msg.py:65:python for m in results[:10]: print(f" [{m.get('chat_name', '?')}] {m.get('sender', '?')}: {m.get('content', '')[:80]}")scripts/contacts.py:42-47:python print(f"\n👤 {display}") print(f" 📱 微信号: {wxid}") if c.get('tags'): print(f" 🏷️ 标签: {', '.join(c['tags'])}")scripts/stats.py:39-41:python for user, count in users.most_common(10): bar = '█' * min(int(count / max(1, max(users.values())) * 20), 20) print(f" {user:12} {bar} {count}")scripts/extract.py:59-62:python for t in todos[:15]: print(f" ☐ {t['text'][:60]}") print(f" └─ {t['chat']} | {t['sender']}")scripts/auto-reply.py:98-101:python if matched: print(f"匹配到 {len(matched)} 条规则:") for r in sorted(matched, key=lambda x: -x.get('priority', 0)): print(f" → {r['name']}: {r['reply']}")scripts/example.sh:8-9:bash local input="${1:-}" echo "Processing: ${input}"Technical Analysis
The scripts read message contents, sender names, chat names, contact attributes, extracted values, and saved rule values from local JSON caches or command-line arguments. These values are printed directly without escaping terminal control characters.
An attacker-controlled string can contain ANSI CSI sequences, OSC sequences, carriage returns, backspaces, or other C0/C1 control characters. When the string is emitted to a compatible terminal, the terminal may interpret those bytes as commands rather than displaying them as inert text.
Limiting a string with Python slicing, such as
[:80], does not neutralize control sequences. A complete malicious sequence c ...[truncated 2315 chars]- Remediation
View remediation
Remediation Suggestions
- Introduce a shared output-sanitization function and apply it to every externally sourced or persisted string before printing:
python import re CONTROL_SEQUENCE_RE = re.compile( r''' \x1B (?: \][^\x07\x1B]*(?:\x07|\x1B\\) | \[[0-?]*[ -/]*[@-~] | [PX^_].*?\x1B\\ | [@-_] ) |[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F] ''', re.VERBOSE | re.DOTALL, ) def safe_display(value): value = str(value) return CONTROL_SEQUENCE_RE.sub( lambda match: ''.join( f'\\x{ord(character):02x}' for character in match.group(0) ), value, )- Sanitize every message, chat, sender, contact, tag, URL, address, task, rule name, keyword, and reply value at the final output boundary:
python print( f" [{safe_display(m.get('chat_name', '?'))}] " f"{safe_display(m.get('sender', '?'))}: " f"{safe_display(m.get('content', ''))[:80]}" )-
Preserve raw cached data for processing, but never send raw untrusted fields directly to an interactive terminal. Sanitization should occur immediately before presentation.
-
Replace the shell example's raw
echowith a representation that visibly escapes non-printable input:
bash printf 'Processing: %q\n' "$input"-
Add tests covering ESC, CSI, OSC, carriage return, backspace, DEL, newline injection, and truncated escape sequences.
-
If output is consumed by an AI agent, place untrusted message content in an explicitly delimited data structure such as JSON and ensure control characters are serialized rather than interpreted.
-
Consider applying output-length limits after sanitization so escaping cannot unexpectedly expand output beyond intended bounds.
