T09 · Insecure Skill Coding Practices
- Location
tool.py:56- Finding
Terminal Control-Sequence Injection Through Untrusted Log Content
- Content
View full analysis
Vulnerability Details
File Location:
tool.py, lines 26-38 and 56-70
Vulnerability Type: Unsanitized terminal output
Risk Level: MediumVulnerable Code
python def colorize_log_line(line): """Apply colors to different log levels and timestamps.""" # Colorize timestamps (ISO or common formats) line = re.sub(r'(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})', f'{Fore.CYAN}\\1{Fore.RESET}', line) line = re.sub(r'(\d{2}:\d{2}:\d{2})', f'{Fore.CYAN}\\1{Fore.RESET}', line) # Colorize log levels line = re.sub(r'(ERROR)', f'{Style.RESET_ALL}{Fore.RED}[\\1]{Fore.RESET}{Style.RESET_ALL}', line, flags=re.IGNORECASE) line = re.sub(r'(WARN|WARNING)', f'{Style.RESET_ALL}{Fore.YELLOW}[\\1]{Fore.RESET}{Style.RESET_ALL}', line, flags=re.IGNORECASE) line = re.sub(r'(INFO)', f'{Style.RESET_ALL}{Fore.GREEN}[\\1]{Fore.RESET}{Style.RESET_ALL}', line, flags=re.IGNORECASE) line = re.sub(r'(DEBUG)', f'{Style.RESET_ALL}{Fore.CYAN}[\\1]{Fore.RESET}{Style.RESET_ALL}', line, flags=re.IGNORECASE) return linepython for line in recent_lines: print(colorize_log_line(line.rstrip())) # Follow mode while True: where = f.tell() line = f.readline() if line: print(colorize_log_line(line.rstrip())) else: time.sleep(0.1) f.seek(where) else: # Print last N lines all_lines = f.readlines() for line in all_lines[-lines:]: print(colorize_log_line(line.rstrip()))Technical Analysis
The application treats log-file contents as terminal-ready text. The
colorize_log_line()function adds its own ANSI formatting but does not remove, encode, or visibly escape control characters already present in the input. The resulting attacker-contr ...[truncated 2116 chars]- Remediation
View remediation
Remediation Suggestions
Treat all log content as untrusted before writing it to an interactive terminal.
- Strip or visibly encode C0/C1 control characters, the ESC character, CSI sequences, OSC sequences, and other non-printable terminal controls before applying LogFlux's own formatting.
- Preserve only safe whitespace such as tabs when explicitly desired; represent other control bytes using escaped forms such as
\x1b,\r, or Unicode control pictures. - Apply sanitization before
colorize_log_line()so only ANSI sequences generated internally by the application can reach the terminal. - Consider disabling color automatically when output is not attached to a TTY.
- If raw output is required, expose it only through an explicit warning-bearing option rather than making it the default.
- Add tests containing CSI cursor movement, screen-clearing sequences, OSC hyperlinks, OSC 52 clipboard requests, carriage returns, and other control characters to verify that they are rendered inert.
Example hardening approach:
python ANSI_ESCAPE = re.compile( r'[\x1b\x9b](?:\][^\x07]*(?:\x07|\x1b\\)|' r'[[\]()#;?]*(?:(?:[a-zA-Z\d]*(?:;[-a-zA-Z\d/#&.:=?%@~_]+)*)?\x07|' r'(?:(?:\d{1,4}(?:[;:]\d{0,4})*)?[\dA-PR-TZcf-nq-uy=;<>~])))' ) def sanitize_terminal_text(text): text = ANSI_ESCAPE.sub('', text) return ''.join( ch if ch in '\t' or ch.isprintable() else repr(ch)[1:-1] for ch in text )Invoke
sanitize_terminal_text(line.rstrip())before colorization and printing.
