T09 · Insecure Skill Coding Practices
- Location
scripts/cn_qrcode_reader.py:38- Finding
Terminal Control-Sequence Injection Through Decoded Barcode Data
- Content
View full analysis
Vulnerability Details
File Location:
scripts/cn_qrcode_reader.py, lines 38-45
Vulnerability Type: Unsanitized terminal output
Risk Level: LowVulnerable Code:
python for barcode in barcodes: data = barcode.data.decode('utf-8') barcode_type = barcode.type results.append({ 'type': barcode_type, 'data': data, 'rect': barcode.rect }) if verbose: print(f"[{barcode_type}] {data}") else: print(data)Technical Analysis
Barcode and QR-code contents are attacker-controlled data. The application decodes this data as UTF-8 and writes it directly to the user's terminal without escaping control characters.
A malicious barcode can contain ANSI escape sequences or other non-printable terminal control characters. When printed, a compatible terminal may interpret these characters rather than displaying them literally. This can alter colors, move the cursor, erase or conceal output, forge apparent command results, manipulate terminal titles, or invoke terminal-specific behavior.
The decoded content is treated only as data by Python and is not passed to a shell, so the reviewed code does not directly provide command execution. More severe effects would depend on vulnerabilities or unsafe features in the user's terminal emulator.
Attack Path
- An attacker creates a QR code containing ANSI escape sequences or other terminal control characters.
- The attacker convinces a user to scan the image with this skill.
pyzbarreturns the attacker-controlled barcode content.- The application decodes the content and passes it directly to
print(). - The user's terminal interprets embedded control sequences, allowing the displayed output or terminal state to be manipulated.
Impact Assessment
Exploitation requires a user to process an attacker-supplied image. The attacker may spoof or conceal terminal output a ...[truncated 293 chars]
- Remediation
View remediation
Remediation Suggestions
- Escape decoded values before displaying them in an interactive terminal.
- Render control characters visibly, for example by using
repr(data)or a dedicated escaping function. - Remove C0, C1, ANSI escape, and other non-printable control characters from display output while retaining the original value separately when required.
- Consider offering a machine-readable output mode, such as JSON, with appropriate serialization.
- Clearly distinguish trusted application messages from untrusted barcode content.
- Add tests using payloads containing escape characters such as
\x1b[2J, cursor movement sequences, carriage returns, backspaces, and terminal-title sequences.
