# GHSA-4v5g-8pq2-32m2: By-passing Protection of PharStreamWrapper Interceptor

> Attackers can bypass PharStreamWrapper protections via exception handling flaws, enabling insecure deserialization and remote code execution in PHP applications.

- **Severity:** Not scored
- **Exploited in the wild:** Not known
- **Login needed:** Not needed
- **Attackable:** Over the internet
- **Victim action needed:** Not needed
- **Fix available:** Yes
- **Published:** 5 Jun 2024

## What is GHSA-4v5g-8pq2-32m2?

GHSA-4v5g-8pq2-32m2 is an unsafe deserialization vulnerability in typo3/phar-stream-wrapper, published on 5 Jun 2024. An attacker can exploit it over the internet without logging in, with no action from the victim.

### Official description

Insecure deserialization is a vulnerability which occurs when untrusted data is used to abuse the logic of an application. In July 2018, the vulnerability of insecure deserialization when executing Phar archives was addressed by removing the known attack vector in the TYPO3 core. For more details read the corresponding TYPO3 advisory.

In addition, a new interceptor was introduced to protect possible (but unknown) vulnerabilities in 3rd party components like TYPO3 extensions. Basically, the PharStreamWrapper intercepts direct invocations of Phar archives and allows or denies further processing based on individual rules.

Recently, the PharStreamWrapper was extracted from the TYPO3 core and released as standalone package under the MIT license. It is now available for any PHP driven project.

The stream wrapper overwrites the existing Phar handling of PHP, applies its own assertions and then restores the native PHP Phar handling for the corresponding commands (e.g. file_exists, include, fopen) to continue processing. After that, the native PHP Phar handling gets disabled and is overwritten by the logic of the PharStreamWrapper again. This is the only way to control invocations of Phar archives as PHP only allows a single handler for each corresponding stream.

We were informed that exception and error handlers in custom applications (e.g. TYPO3 extensions) sometimes didn't return to the original operating sequence of the PharStreamWrapper. A possible consequence was that the unprotected native PHP Phar handling remained active and therefore became vulnerable for the basic issue of insecure deserialization again.

Examples
Take a look at the following examples showing how the handling is by-passed in custom application code.

Scenario A: Exception thrown from code organized in a Phar archive
```
try {
    include('phar://path-to-archive/good-archive.phar');
} catch (\Throwable $throwable) {
    // not doing much here, continue execution
}
// the insecure value can be anything that is or was user-submitted
// and cannot be trusted in terms of security, $_GET is just used as example
$insecureValue = $_GET['path'];
// the value might be 'phar://path-to-archive/malicious-archive.phar'
file_exists($insecureValue);
```
Scenario B: Errors converted to exceptions and thrown when interacting with archive contents
```
// set error handler in order to convert errors to exceptions
set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
   throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});
// interacting with Phar archive
try {
   $resource = opendir('phar://path-to-archive/good-archive.phar/non-existing-path/');
   closedir($resource);
} catch (\Throwable $throwable) {
   // not doing much here, continue execution
}
// the insecure value can be anything that is or was user-submitted
// and cannot be trusted in terms of security, $_GET is just used as example
$insecureValue = $_GET['path'];
// the value might be 'phar://path-to-archive/malicious-archive.phar'
file_exists($insecureValue);
```

## Which versions are affected by GHSA-4v5g-8pq2-32m2?

4 open source packages ship an affected version, including typo3/phar-stream-wrapper.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `typo3/phar-stream-wrapper` | Packagist | From 1.0.0 | 2.0.1 |
| `typo3/phar-stream-wrapper` | Packagist | From 1.0.0 | 2.0.1 |
| `typo3/phar-stream-wrapper` | Packagist | From 3.0.0 | 3.0.1 |
| `typo3/phar-stream-wrapper` | Packagist | From 3.0.0 | 3.0.1 |

## How do I fix GHSA-4v5g-8pq2-32m2?

Upgrade typo3/phar-stream-wrapper to a fixed version for your release line: 2.0.1 or 3.0.1.

## Is GHSA-4v5g-8pq2-32m2 being exploited?

There is no known exploitation of GHSA-4v5g-8pq2-32m2 in the wild and no public exploit code.

## Fix prompt for AI coding agents

```text
Check this codebase for GHSA-4v5g-8pq2-32m2 and fix it if it is affected.

What it is: GHSA-4v5g-8pq2-32m2 is an unsafe deserialization vulnerability in typo3/phar-stream-wrapper, published on 5 Jun 2024. An attacker can exploit it over the internet without logging in, with no action from the victim.
Exploitation: There is no known exploitation of GHSA-4v5g-8pq2-32m2 in the wild and no public exploit code.

Affected packages and fixed versions:
- typo3/phar-stream-wrapper (Packagist): affected from 1.0.0; fixed in 2.0.1
- typo3/phar-stream-wrapper (Packagist): affected from 1.0.0; fixed in 2.0.1
- typo3/phar-stream-wrapper (Packagist): affected from 3.0.0; fixed in 3.0.1
- typo3/phar-stream-wrapper (Packagist): affected from 3.0.0; fixed in 3.0.1

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/GHSA-4v5g-8pq2-32m2/ (Markdown: https://insights.sbomify.com/GHSA-4v5g-8pq2-32m2/index.md)
```

## Sources

- OSV: https://osv.dev/vulnerability/GHSA-4v5g-8pq2-32m2

---
From sbomify Insights: https://insights.sbomify.com/GHSA-4v5g-8pq2-32m2/
Summaries are generated and may contain mistakes; the official description above is the record.
