T09 · Insecure Skill Coding Practices
- Location
scripts/print_file.py:106- Finding
PowerShell Command Injection in File Printing Fallback
- Content
View full analysis
Vulnerability Details
File Location:
scripts/print_file.py:106-148
Vulnerability Type: PowerShell command injection
Risk Level: HighVulnerable Code
python ps_script = f""" $printer = Get-Printer -Name "{printer_name}" $printJob = $printer | Start-PrintJob # Note: PowerShell printing is limited, may not work for all file types """ result = subprocess.run( ['powershell', '-Command', ps_script], capture_output=True, text=True, timeout=30 )The non-text-file branch contains a second vulnerable construction:
python ps_script = f""" $filePath = "{file_path}" $printerName = "{printer_name}" # Set as default printer temporarily $printer = Get-Printer -Name $printerName -ErrorAction Stop $oldDefault = (Get-Printer | Where-Object {{$_.Default -eq $true}}).Name if ($oldDefault -ne $printerName) {{ $printer | Set-Printer -Shared $printer.Shared }} # Print the file Start-Process -FilePath $filePath -Verb Print -WindowStyle Hidden # Restore default printer if ($oldDefault) {{ Get-Printer -Name $oldDefault | Set-Printer -Shared (Get-Printer -Name $oldDefault).Shared }} """ result = subprocess.run( ['powershell', '-Command', ps_script], capture_output=True, text=True, timeout=30 )Technical Analysis
The CLI-controlled
printer_nameandfile_pathvalues are inserted directly into double-quoted PowerShell expressions. PowerShell parses the resulting string as source code because it is passed topowershell -Command.Using an argument list for
subprocess.runprevents command-shell interpretation by Python, but it does not prevent PowerShell from evaluating syntax inside the supplied script. Double-quoted PowerShell strings support variable and subexpression expansion. Consequently, characters such as$()can turn data embedded in a quoted value into executable PowerShell expressions. A valid W ...[truncated 1211 chars]- Remediation
View remediation
Remediation Suggestions
- Do not interpolate CLI values into PowerShell source code.
- Prefer pywin32 or another structured printing API that accepts printer names and file paths as data.
- If PowerShell is unavoidable, pass values through environment variables, standard input, or a parameterized script file and retrieve them as literal data.
- Use PowerShell's stop-parsing or Base64-encoded data only for transporting values, not as a substitute for validation.
- Validate the printer against an enumerated exact-name allowlist.
- Resolve and validate the file path before invocation, require a regular file, and enforce any intended directory or extension restrictions.
- Add regression tests containing PowerShell metacharacters and subexpression syntax in all externally supplied values.
