T09 · Insecure Skill Coding Practices
- Location
scripts/script.sh:22- Finding
Terminal Escape-Sequence Injection Through Untrusted Output
- Content
View full analysis
Vulnerability Details
File Location:
scripts/script.sh:22-25,scripts/script.sh:72-73,scripts/script.sh:126-127,scripts/script.sh:155-166,scripts/script.sh:189-191,scripts/script.sh:225-226,scripts/script.sh:255-258, andscripts/script.sh:274
Vulnerability Type: Unsanitized terminal output and terminal control-sequence injection
Risk Level: MediumVulnerable Code
bash info() { echo -e "${BLUE}ℹ${NC} $*"; } success() { echo -e "${GREEN}✔${NC} $*"; } warn() { echo -e "${YELLOW}⚠${NC} $*"; } error() { echo -e "${RED}✖${NC} $*" >&2; }User-controlled pattern and text values are passed to the affected output helper:
bash info "Testing pattern: ${CYAN}${pattern}${NC}" info "Against text: ${CYAN}${text}${NC}"File content and regex matches are also printed without escaping terminal control characters:
python if matches: matching_lines += 1 total_matches += len(matches) # Highlight matches in the line highlighted = line offset = 0 parts = [] last_end = 0 for m in matches: parts.append(line[last_end:m.start()]) parts.append(f'\033[1;31m{m.group()}\033[0m') last_end = m.end() parts.append(line[last_end:]) highlighted = ''.join(parts) print(f' {line_num:5}: {highlighted}')Technical Analysis
The skill writes user-controlled patterns, text, filenames, matched file content, replacement strings, and extracted groups directly to a terminal. It does not escape control characters before output. The shell helper functions compound the problem by using
echo -e, which interprets backslash escape sequences contained in their arguments.Consequently, an input containing ANSI CSI sequences or OSC sequences may be interpreted by the operator's terminal rather than displayed as inert text. Depending on terminal capabilities and configuration, an attacker m ...[truncated 1405 chars]
- Remediation
View remediation
Remediation Suggestions
-
Replace
echo -ewithprintfusing a fixed format string:bash info() { printf '%bℹ%b %s\n' "$BLUE" "$NC" "$*" }Keep trusted color constants separate from the untrusted
%svalue. -
Sanitize all user-controlled values and file content before displaying them. Convert control bytes to visible escaped representations such as
\x1b,\r, and\x07. -
In Python, introduce a shared terminal-sanitization function and apply it to filenames, patterns, matching lines, matches, and extracted groups before printing:
python def terminal_safe(value): return ''.join( ch if ch == '\t' or ord(ch) >= 0x20 and ord(ch) != 0x7f else f'\\x{ord(ch):02x}' for ch in str(value) ) -
Emit color sequences only when stdout is connected to an interactive terminal, and support a
--no-coloroption or theNO_COLORenvironment variable. -
Add regression tests containing ESC, BEL, carriage return, OSC hyperlink, and OSC clipboard sequences to verify that untrusted bytes are rendered visibly rather than interpreted.
-
