T09 · Insecure Skill Coding Practices
- Location
sizeup.py:127- Finding
Terminal Escape Injection Through Untrusted File Names
- Content
View full analysis
Vulnerability Details
File Location:
sizeup.py:127-145,sizeup.py:203-221; duplicated inscripts/sizeup.py:127-145,scripts/sizeup.py:203-221
Vulnerability Type: Unsanitized terminal output
Risk Level: MediumVulnerable Code
python print(f"{prefix}{connector}{color}{size_str:>8}\033[0m {name}") children = node.get("children", []) # Filter by min_size visible = [c for c in children if c.get("size", 0) >= min_size] truncated = len(visible) - max_items if len(visible) > max_items else 0 visible = visible[:max_items] new_prefix = prefix + (" " if is_last else "│ ") for i, child in enumerate(visible): is_child_last = (i == len(visible) - 1) and truncated <= 0 if "children" in child: print_tree(child, min_size, new_prefix, is_child_last, max_items) else: connector = "└── " if is_child_last else "├── " s = format_size(child.get("size", 0)) cs = "\033[91m" if child["size"] >= 1073741824 else \ "\033[33m" if child["size"] >= 104857600 else \ "\033[36m" if child["size"] >= 10485760 else "\033[0m" print(f"{new_prefix}{connector}{cs}{s:>8}\033[0m {child['name']}")Additional affected output paths include:
python for i, (size, fp) in enumerate(files, 1): rel = os.path.relpath(fp, path) print(f" {i:>3}. {format_size(size):>8} {rel}")python for ext, info in list(exts.items())[:25]: print(f" {ext:<12} {info['count']:>7,} {format_size(info['size']):>10}")Technical Analysis
File names, relative paths, directory names, and extensions are controlled by the contents of the scanned filesystem. The program writes these values directly to an interactive terminal without escaping control characters.
A malicious file name can contain ANSI CSI or OSC escape sequences. When the file is displayed in tree, top-file, or extension mode, the terminal may interp ...[truncated 1516 chars]
- Remediation
View remediation
Remediation Suggestions
Sanitize every filesystem-derived value before writing it to a terminal:
- Replace ASCII control characters, DEL, C1 controls, and escape bytes with visible escaped representations.
- Apply the same sanitizer to names, relative paths, extensions, and the displayed root path.
- Keep raw path values only in structured output such as JSON, where they must remain encoded through
json.dumps(). - Consider disabling application-generated colors when output is not connected to a TTY.
- Add tests using names containing escape, newline, carriage-return, tab, OSC, and CSI sequences.
A minimal defensive helper could use
ascii(value)orrepr(value)for terminal display. A more readable implementation may preserve safe printable Unicode characters while rendering unsafe code points as\xNN,\uNNNN, or named escapes.
