Summary
PhpWeasyPrint contains an OS command injection flaw where the configured WeasyPrint binary path can flow directly into a shell command without effective validation.
In deployments that source the binary path from configuration, environment variables, or tenant-controlled settings, an attacker may inject arbitrary shell commands during PDF generation.
Affected context
The vulnerability appears in applications using PhpWeasyPrint to generate PDFs via the WeasyPrint CLI, especially where the binary path is not hard-coded but read from configuration, environment variables, or per-tenant settings.
Because escapeshellarg() is applied before is_executable(), the executable check is performed on a quoted path that never exists, causing the intended safe branch to be unreachable.
As a result, the original $binary string is passed directly into Symfony\Component\Process\Process::fromShellCommandline(), turning any attacker-controlled portion of the binary path into a shell injection vector.
Exploit preconditions
- Attack surface: Application endpoints or background jobs that trigger HTML/URL-to-PDF generation via PhpWeasyPrint.
- Required access: Ability to influence the WeasyPrint binary path (e.g., via configuration management, environment variables, multi-tenant settings, or an administration panel).
- Trigger point: Invocation of the Pdf generation routine that constructs the WeasyPrint shell command with the attacker-influenced binary string.
- Why exploitation is possible: The executable check is applied to a shell-escaped path, making it always fail; consequently, the unescaped, attacker-influenced
$binaryvalue is passed directly tofromShellCommandline(), which interprets it as a full shell command rather than a safe argument.
Why this happens
The core issue is an OS command injection pattern where input meant to represent a binary path is ultimately treated as part of a shell command line.
The code first wraps the binary path with escapeshellarg(), then calls is_executable() on the quoted string, which cannot match any real filesystem path on POSIX systems.
Because the check always fails, the supposedly "safe" path that would have used the escaped value is never executed, and the original, unvalidated $binary string is sent directly into Process::fromShellCommandline().
This behavior aligns with CWE-78: Improper Neutralization of Special Elements used in an OS Command, where user-influenced data reaches a shell interpreter without correct validation, quoting, or separation of command and arguments.
Potential impact
- Execution of arbitrary OS commands with the privileges of the PHP application process
- Unauthorized access to application data, configuration files, or local secrets
- Lateral movement within the hosting environment or adjacent services
- Service disruption or denial of service by spawning malicious or long-running processes
- Planting of backdoors or modification of application code and dependencies
General mitigation guidance
Effective mitigation focuses on eliminating shell interpretation of dynamic values and constraining where executable paths originate.
Common defensive patterns include:
- Avoid building shell command lines from concatenated strings; instead, use APIs that separate command and arguments as structured arrays or parameters.
- Treat the binary path as a fixed, trusted configuration set at deploy-time, not a user- or tenant-controlled setting.
- Validate executable paths against an allowlist of known-safe locations or specific filenames rather than accepting arbitrary strings.
- Prefer process-execution libraries that bypass the shell by default and do not require manual escaping of arguments.
- Run the PHP application under least-privilege accounts and apply OS-level controls (AppArmor, SELinux, container profiles) to limit the blast radius of any process execution.
How teams detect issues like this
Detection typically combines code-centric and configuration-centric approaches:
- Static analysis rules looking for
Process::fromShellCommandline()or similar shell-execution APIs fed by configuration values or variables ultimately influenced by user input. - Code review checklists that flag manual construction of command-line strings, especially where executable paths or options are concatenated from dynamic sources.
- Security-focused linters or CI checks that enforce use of argument-vector process APIs instead of shell-based ones, and that require explicit review for any shell invocation.
- Runtime monitoring and logging for unusual subprocess executions, including unexpected binaries, arguments, or spikes in process-spawning from the PHP worker.

