Summary
PHPUnit before 12.5.8, 11.5.50, 10.5.62, 9.6.33, and 8.5.52 unsafely deserializes .coverage files during PHPT test execution without restricting allowed classes.
An attacker with local file write access to the coverage directory can plant a malicious serialized object that triggers a __wakeup() method, potentially leading to arbitrary code execution when code coverage is enabled.
Affected context
This vulnerability affects projects that use PHPUnit's PHPT test runner with code coverage instrumentation enabled.
It is relevant wherever PHPUnit is executed in automated or semi-automated contexts, such as developer workstations, CI/CD runners, or build servers that:
- Generate code coverage data for PHPT tests, and
- Store
.coveragefiles in a predictable location on disk.
The risk is heightened in shared or automated environments where multiple processes or actors can write to the PHPUnit working directories used for coverage artifacts.
Exploit preconditions
- Attack surface: Local filesystem where PHPUnit reads and writes code coverage
.coveragefiles for PHPT tests. - Required access: Ability to write arbitrary files into the directory where PHPUnit expects
.coveragefiles (e.g., via compromised CI job, poisoned dependency, or local account on the build machine). - Trigger point: Execution of PHPT tests with code coverage enabled, which causes the
cleanupForCoverage()method to deserialize pre-existing.coveragefiles. - Why exploitation is possible: The
cleanupForCoverage()method deserializes coverage data without validation orallowed_classesrestrictions, allowing user-controlled object graphs with attacker-controlled__wakeup()code to be instantiated during test cleanup.
Why this happens
The core issue is an unsafe deserialization pattern inside PHPUnit's handling of coverage artifacts for PHPT tests.
The cleanupForCoverage() method assumes that .coverage files do not exist prior to a given test run and therefore treats their contents as trusted.
It directly deserializes these files without:
- Verifying that they were produced by the current test execution, or
- Restricting the set of allowed classes during deserialization.
This maps to CWE-502: Deserialization of Untrusted Data, where serialized input from a potentially untrusted source is converted back into objects that can execute code via magic methods like __wakeup() or destructors.
The logical trust in "internal" coverage files becomes invalid in environments where local write access can be obtained by attackers, such as compromised CI pipelines or shared build systems.
Potential impact
- Execution of arbitrary PHP code in the context of the user or process running PHPUnit (e.g., CI runner, developer account).
- Compromise of CI/CD infrastructure, including theft of secrets, tokens, or build artifacts.
- Tampering with test results or build outputs, enabling supply-chain attacks on downstream deployments.
- Lateral movement from a build environment into other internal systems, depending on available network and credential access.
- Persistent backdoors on development or build servers via malicious test fixtures or dependencies.
General mitigation guidance
Mitigation should focus on both correct handling of serialized data and hardening of the environments that run PHPUnit:
- Avoid unsafe deserialization patterns
- Treat all serialized data, including internal coverage artifacts, as untrusted unless freshly produced in the same execution flow.
- When deserialization is unavoidable, use safeguards such as
allowed_classesrestrictions or explicit type-checking on deserialized values. - Fail fast on unexpected or pre-existing artifact files instead of attempting to recover from anomalous states.
- Enforce strict artifact lifecycle rules
- Ensure test coverage directories are created fresh per run and not shared across branches, users, or pipelines.
- Treat pre-existing
.coveragefiles as an error condition and stop execution, rather than silently consuming them. - Use per-run, per-branch, or per-job isolation for temporary test artifacts.
- Harden CI/CD and development environments
- Use ephemeral CI runners or clean containers/VMs so that state is not shared between runs.
- Restrict write access to directories where PHPUnit reads coverage data; only the test process should be able to write there.
- Enforce code review, branch protection, and strict control over who can modify test or build-related scripts and configuration.
- Isolate build artifacts and secrets, so that compromise of one project or pipeline does not automatically expose others.
- Reduce privilege and blast radius
- Run PHPUnit and CI jobs with least-privilege accounts that have minimal filesystem and network permissions.
- Segment build networks so that a compromised test runner cannot directly reach sensitive internal services.
How teams detect issues like this
Detection can target both code patterns and operational anomalies:
- Static analysis and code review
- Flag use of
unserialize()or equivalent deserialization calls that operate on data from disk or external sources withoutallowed_classesrestrictions. - Review code paths that assume temporary or "internal" files are always trustworthy, especially for test coverage, caching, or sessions.
- Require explicit validation or anomaly handling when deserializing artifacts that may be left over from previous runs.
- Configuration and policy checks
- Implement CI policies that ensure temporary directories (including coverage output) are unique per run and cleaned afterward.
- Enforce checks that prevent build/test processes from reading pre-existing coverage files without verification.
- Use infrastructure as code and policy-as-code tools to assert that CI runners are ephemeral and isolated.
- Runtime monitoring and logging
- Log and alert when PHPUnit or other tooling encounters unexpected pre-existing coverage artifacts or serialized data.
- Monitor CI runners for unexpected file creations inside test artifact directories, especially by processes other than the primary build job.
- Track execution of PHPUnit with coverage enabled in contexts where it should not run (e.g., production servers).
- Automated security tooling
- Integrate SAST tools configured to identify unsafe deserialization patterns in test infrastructure and build scripts.
- Use supply-chain security scanners to detect malicious or unusual changes in test-related dependencies and build steps.

