Security: Command Injection via config parameter (CWE-78)
Security Vulnerability Report
Type: Command Injection (CWE-78) Severity: HIGH (CVSS 7.3 — AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:N) Reporter: Conner Webber ([email protected]) 90-day disclosure deadline: 2026-06-06
Summary
The config parameter accepted by all public OCR functions (image_to_string, image_to_data, image_to_boxes, image_to_osd, image_to_pdf, run_and_get_output) is passed through shlex.split() and appended directly to subprocess arguments without validation. An attacker who controls the config value can inject arbitrary tesseract CLI flags.
Affected Code
File: pytesseract/pytesseract.py, lines 266-267
if config:
cmd_args += shlex.split(config, posix=not_windows)Attack Vector
An attacker controlling the config parameter can inject --tessdata-dir pointing to a directory containing a malicious .traineddata file. Tesseract loads these as native LSTM models, which enables arbitrary code execution.
This is a realistic scenario in web applications that expose OCR configuration to users (e.g., allowing users to set page segmentation mode, OEM, or language via form inputs that get passed to pytesseract).
Example:
# User-controlled input flows into config
pytesseract.image_to_string(image, config=user_input)
# Malicious input:
user_input = "--tessdata-dir /tmp/malicious --oem 1 -l evil"
# Loads /tmp/malicious/evil.traineddata as an LSTM model → code executionAdditional Issue
The lang parameter (lines 263-264) is passed directly to the command without validation against the existing LANG_PATTERN regex, which could also be leveraged for injection.
Suggested Fix
- Validate
configagainst an allowlist of known tesseract flags (e.g.,--psm,--oem,-c). Reject or strip unknown flags, especially--tessdata-dir. - Validate
langagainstLANG_PATTERNbefore appending to subprocess args. - Consider using a structured API for config options rather than accepting a raw string.
Note on Disclosure
Private vulnerability reporting is not enabled on this repository and no SECURITY.md was found. This issue is being filed publicly as no private channel is available. I recommend enabling GitHub Private Vulnerability Reporting for future reports.
Source: madmaze/pytesseract