Summary
PhpWeasyPrint contains a PHAR-based unsafe deserialization bug where case-sensitive blocking of the phar:// stream wrapper can be bypassed using alternative casing.
On supported PHP 7 versions, this allows an attacker who can control the output filename and a crafted PHAR archive to trigger metadata deserialization and potentially execute arbitrary code.
Affected context
The vulnerability occurs in applications that use pontedilana/php-weasyprint to generate PDFs from URLs or HTML, and allow user-influenced values to reach the output filename parameter.
It is specific to deployments running PHP 7.4+ where PHAR stream handling still triggers automatic metadata deserialization via filesystem functions.
The risk materializes when the library's filename validation is trusted to block phar:// but is bypassed using different casing, causing a PHAR path to reach file_exists() in prepareOutput().
Exploit preconditions
- Attack surface: Application endpoints or code paths that call PhpWeasyPrint's PDF generation APIs with user-controlled or partially user-controlled output filenames.
- Required access: Ability to influence the output file path (e.g., via HTTP parameters, headers, or indirect configuration) and to host or reference a malicious PHAR archive accessible to the target.
- Trigger point: Passing a
phar://URL with altered casing (e.g.,PHAR://) as the output filename so that it passes the case-sensitive blacklist and is used infile_exists()inprepareOutput(). - Why exploitation is possible: PHP stream wrappers are case-insensitive, while the library's blacklist is case-sensitive, allowing PHAR URLs to slip through and invoke implicit PHAR metadata deserialization when accessed via filesystem functions on PHP 7.
Why this happens
The underlying bug is a combination of unsafe deserialization and incomplete input validation on a security-sensitive sink.
PhpWeasyPrint attempts to block the phar:// stream wrapper using a case-sensitive blacklist, but PHP treats stream wrapper schemes case-insensitively.
As a result, PHAR paths using different casing bypass validation and are passed to file_exists(), which in PHP 7 can trigger automatic deserialization of PHAR metadata.
This aligns with CWE-502: Unsafe Deserialization, where untrusted data (the PHAR archive metadata) is implicitly deserialized, enabling attacker-controlled code execution via gadget chains present in the application or its dependencies.
Potential impact
- Execution of arbitrary PHP code in the context of the web server or PHP process
- Complete compromise of the affected web application's runtime environment
- Access to sensitive data handled by the application, including credentials or personal information
- Use of the compromised server as a foothold for lateral movement within the infrastructure
- Defacement, data manipulation, or disruption of PDF generation workflows
General mitigation guidance
Effective mitigation typically focuses on eliminating PHAR-based deserialization paths and hardening filename handling around stream wrappers.
Recommended defensive patterns include:
- Treat any user-influenced file path or URL as untrusted and normalize and validate it against a strict allowlist of expected schemes (e.g.,
file, local paths), rather than relying on blacklists forphar://. - Avoid invoking filesystem functions on PHAR-capable paths when they can be influenced by external input, especially in environments where PHAR deserialization behaviors are enabled.
- Prefer structured configuration or fixed output directories and filenames, decoupling them from direct user input wherever possible.
- Run PDF generation processes with least privilege (reduced filesystem and network access) and, where feasible, isolate them in separate processes or containers to limit blast radius.
- Disable unnecessary PHP stream wrappers or PHAR functionality at the platform or application level when not required by the application's design.
How teams detect issues like this
Organizations can detect this class of vulnerability and similar issues through multiple layers of review and automation:
- Static analysis: Define rules that flag uses of
file_exists(),file_get_contents(), and related filesystem functions when the target path is derived from user input, especially if it may containphar://or other dynamic stream wrappers. - Code review signals: During manual review, pay special attention to case-sensitive blacklist checks, string contains checks on protocols or schemes, and any logic that assumes case-sensitive behavior for stream wrappers.
- Configuration and policy checks: Enforce secure coding guidelines that discourage user-controlled filenames and mandate allowlist-based validation for protocols, file extensions, and directories.
- Runtime monitoring: Instrument logging around file access paths and detect suspicious usage patterns involving
phar://or other unexpected schemes reaching filesystem functions. - Automated tooling: Use SAST and dependency-scanning tools to identify known unsafe deserialization patterns and PHAR-related sinks in PHP codebases and third-party libraries.

