PHPackages                             typo3/phar-stream-wrapper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Security](/categories/security)
4. /
5. typo3/phar-stream-wrapper

ActiveLibrary[Security](/categories/security)

typo3/phar-stream-wrapper
=========================

Interceptors for PHP's native phar:// stream handling

v4.0.0(1y ago)5938.0M—6%12[1 PRs](https://github.com/TYPO3/phar-stream-wrapper/pulls)7MITPHPPHP ^7.1 || ^8.0CI passing

Since Aug 26Pushed 5mo ago9 watchersCompare

[ Source](https://github.com/TYPO3/phar-stream-wrapper)[ Packagist](https://packagist.org/packages/typo3/phar-stream-wrapper)[ Docs](https://typo3.org/)[ RSS](/packages/typo3-phar-stream-wrapper/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (31)Used By (7)Security (3)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/975a8a5103d807bee2fa2f01fbd5b22d0da75e1917a3ee766499dc26099652b9/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f5459504f332f706861722d73747265616d2d777261707065722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/TYPO3/phar-stream-wrapper/?branch=master)[![GitHub Build Status Ubuntu](https://github.com/typo3/phar-stream-wrapper/actions/workflows/tests-ubuntu.yml/badge.svg)](https://github.com/typo3/phar-stream-wrapper/actions/workflows/tests-ubuntu.yml)[![GitHub Build Status Windows](https://github.com/typo3/phar-stream-wrapper/actions/workflows/tests-windows.yml/badge.svg)](https://github.com/typo3/phar-stream-wrapper/actions/workflows/tests-windows.yml)[![Downloads](https://camo.githubusercontent.com/e30874967a633b87b6f98f19ac23f570aa1625ece16ed6a6eecd55144ebfc067/68747470733a2f2f706f7365722e707567782e6f72672f7479706f332f706861722d73747265616d2d777261707065722f646f776e6c6f6164732e737667)](https://packagist.org/packages/typo3/phar-stream-wrapper)

PHP Phar Stream Wrapper
=======================

[](#php-phar-stream-wrapper)

Note

With PHP 8.0.0 the default behavior changed, and meta-data is not deserialized automatically anymore:

- see [`typo3/phar-stream-wrapper` issue #64](https://github.com/TYPO3/phar-stream-wrapper/issues/64)
- see

Abstract &amp; History
----------------------

[](#abstract--history)

Based on Sam Thomas' findings concerning [insecure deserialization in combination with obfuscation strategies](https://www.secarma.com/labs/near-phar-dangerous-unserialization-wherever-you-are.html)allowing to hide Phar files inside valid image resources, the TYPO3 project decided back then to introduce a `PharStreamWrapper` to intercept invocations of the `phar://` stream in PHP and only allow usage for defined locations in the file system.

Since the TYPO3 mission statement is **inspiring people to share**, we thought it would be helpful for others to release our `PharStreamWrapper` as standalone package to the PHP community.

The mentioned security issue was reported to TYPO3 on 10th June 2018 by Sam Thomas and has been addressed concerning the specific attack vector and for this generic `PharStreamWrapper` in TYPO3 versions 7.6.30 LTS, 8.7.17 LTS and 9.3.1 on 12th July 2018.

-
-
-
-
-

License
-------

[](#license)

In general the TYPO3 core is released under the GNU General Public License version 2 or any later version (`GPL-2.0-or-later`). In order to avoid licensing issues and incompatibilities this `PharStreamWrapper` is licenced under the MIT License. In case you duplicate or modify source code, credits are not required but really appreciated.

Credits
-------

[](#credits)

Thanks to [Alex Pott](https://github.com/alexpott), Drupal for creating back-ports of all sources in order to provide compatibility with PHP v5.3.

Installation
------------

[](#installation)

The `PharStreamWrapper` is provided as composer package `typo3/phar-stream-wrapper`and has minimum requirements of PHP v5.3 ([`v2`](https://github.com/TYPO3/phar-stream-wrapper/tree/v2) branch), PHP v7.0 - v8.3 ([`v3`](https://github.com/TYPO3/phar-stream-wrapper/tree/v3) branch) and PHP v7.1 - v8.4+ ([`v3`](https://github.com/TYPO3/phar-stream-wrapper) branch).

### Installation for PHP v7.1 - v8.4

[](#installation-for-php-v71---v84)

```
composer require typo3/phar-stream-wrapper ^4.0

```

### Installation for PHP v7.0 - v8.3

[](#installation-for-php-v70---v83)

```
composer require typo3/phar-stream-wrapper ^3.0

```

### Installation for PHP v5.3

[](#installation-for-php-v53)

```
composer require typo3/phar-stream-wrapper ^2.0

```

Example
-------

[](#example)

The following example is bundled within this package, the shown `PharExtensionInterceptor` denies all stream wrapper invocations files not having the `.phar` suffix. Interceptor logic has to be individual and adjusted to according requirements.

```
\TYPO3\PharStreamWrapper\Manager::initialize(
    (new \TYPO3\PharStreamWrapper\Behavior())
        ->withAssertion(new \TYPO3\PharStreamWrapper\Interceptor\PharExtensionInterceptor())
);

if (in_array('phar', stream_get_wrappers())) {
    stream_wrapper_unregister('phar');
    stream_wrapper_register('phar', \TYPO3\PharStreamWrapper\PharStreamWrapper::class);
}

```

- `PharStreamWrapper` defined as class reference will be instantiated each time `phar://` streams shall be processed.
- `Manager` as singleton pattern being called by `PharStreamWrapper` instances in order to retrieve individual behavior and settings.
- `Behavior` holds reference to interceptor(s) that shall assert correct/allowed invocation of a given `$path` for a given `$command`. Interceptors implement the interface `Assertable`. Interceptors can act individually on the following commands or handle all of them in case they were not defined specifically:
    - `COMMAND_DIR_OPENDIR`
    - `COMMAND_MKDIR`
    - `COMMAND_RENAME`
    - `COMMAND_RMDIR`
    - `COMMAND_STEAM_METADATA`
    - `COMMAND_STREAM_OPEN`
    - `COMMAND_UNLINK`
    - `COMMAND_URL_STAT`

Interceptors
------------

[](#interceptors)

The following interceptor is shipped with the package and ready to use in order to block any Phar invocation of files not having a `.phar` suffix. Besides that individual interceptors are possible of course.

```
class PharExtensionInterceptor implements Assertable
{
    /**
     * Determines whether the base file name has a ".phar" suffix.
     *
     * @param string $path
     * @param string $command
     * @return bool
     * @throws Exception
     */
    public function assert(string $path, string $command): bool
    {
        if ($this->baseFileContainsPharExtension($path)) {
            return true;
        }
        throw new Exception(
            sprintf(
                'Unexpected file extension in "%s"',
                $path
            ),
            1535198703
        );
    }

    /**
     * @param string $path
     * @return bool
     */
    private function baseFileContainsPharExtension(string $path): bool
    {
        $baseFile = Helper::determineBaseFile($path);
        if ($baseFile === null) {
            return false;
        }
        $fileExtension = pathinfo($baseFile, PATHINFO_EXTENSION);
        return strtolower($fileExtension) === 'phar';
    }
}

```

### ConjunctionInterceptor

[](#conjunctioninterceptor)

This interceptor combines multiple interceptors implementing `Assertable`. It succeeds when all nested interceptors succeed as well (logical `AND`).

```
\TYPO3\PharStreamWrapper\Manager::initialize(
    (new \TYPO3\PharStreamWrapper\Behavior())
        ->withAssertion(new ConjunctionInterceptor([
            new PharExtensionInterceptor(),
            new PharMetaDataInterceptor(),
        ]))
);

```

### PharExtensionInterceptor

[](#pharextensioninterceptor)

This (basic) interceptor just checks whether the invoked Phar archive has an according `.phar` file extension. Resolving symbolic links as well as Phar internal alias resolving are considered as well.

```
\TYPO3\PharStreamWrapper\Manager::initialize(
    (new \TYPO3\PharStreamWrapper\Behavior())
        ->withAssertion(new PharExtensionInterceptor())
);

```

### PharMetaDataInterceptor

[](#pharmetadatainterceptor)

This interceptor is actually checking serialized Phar meta-data against PHP objects and would consider a Phar archive malicious in case not only scalar values are found. A custom low-level `Phar\Reader` is used in order to avoid using PHP's `Phar` object which would trigger the initial vulnerability.

```
\TYPO3\PharStreamWrapper\Manager::initialize(
    (new \TYPO3\PharStreamWrapper\Behavior())
        ->withAssertion(new PharMetaDataInterceptor())
);

```

Reader
------

[](#reader)

- `Phar\Reader::__construct(string $fileName)`: Creates low-level reader for Phar archive
- `Phar\Reader::resolveContainer(): Phar\Container`: Resolves model representing Phar archive
- `Phar\Container::getStub(): Phar\Stub`: Resolves (plain PHP) stub section of Phar archive
- `Phar\Container::getManifest(): Phar\Manifest`: Resolves parsed Phar archive manifest as documented at
- `Phar\Stub::getMappedAlias(): string`: Resolves internal Phar archive alias defined in stub using `Phar::mapPhar('alias.phar')` - actually the plain PHP source is analyzed here
- `Phar\Manifest::getAlias(): string` - Resolves internal Phar archive alias defined in manifest using `Phar::setAlias('alias.phar')`
- `Phar\Manifest::getMetaData(): string`: Resolves serialized Phar archive meta-data
- `Phar\Manifest::deserializeMetaData(): mixed`: Resolves deserialized Phar archive meta-data containing only scalar values - in case an object is determined, an according `Phar\DeserializationException` will be thrown

```
$reader = new Phar\Reader('example.phar');
var_dump($reader->resolveContainer()->getManifest()->deserializeMetaData());

```

Helper
------

[](#helper)

- `Helper::determineBaseFile(string $path): string`: Determines base file that can be accessed using the regular file system. For instance the following path `phar:///home/user/bundle.phar/content.txt` would be resolved to `/home/user/bundle.phar`.
- `Helper::resetOpCache()`: Resets PHP's OPcache if enabled as work-around for issues in `include()` or `require()` calls and OPcache delivering wrong results. More details can be found in PHP's bug tracker, for instance like

Security Contact
----------------

[](#security-contact)

In case of finding additional security issues in the TYPO3 project or in this `PharStreamWrapper` package in particular, please get in touch with the [TYPO3 Security Team](mailto:security@typo3.org).

###  Health Score

60

—

FairBetter than 99% of packages

Maintenance56

Moderate activity, may be stable

Popularity61

Solid adoption and visibility

Community32

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 87.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~92 days

Recently: every ~368 days

Total

29

Last Release

230d ago

Major Versions

v2.1.4 → v3.1.52020-06-19

v2.2.0 → v3.1.62020-11-07

v2.2.1 → v3.1.72021-09-20

v2.x-dev → v3.1.82024-12-09

v3.1.8 → v4.0.02024-12-10

PHP version history (6 changes)v1.0.0PHP ^7.0

v1.1.0PHP ^5.3.3|^7.0

v2.2.0PHP ^5.3.3 || ^7.0

v3.1.6PHP ^7.0 || ^8.0

v3.1.8PHP ^7.0 || ~8.0 || ~8.1 || ~8.2 || ~8.3

v4.0.0PHP ^7.1 || ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/88698?v=4)[TYPO3 GitHub Department](/maintainers/typo3)[@TYPO3](https://github.com/TYPO3)

![](https://avatars.githubusercontent.com/u/165630?v=4)[Benni Mack](/maintainers/bmack)[@bmack](https://github.com/bmack)

![](https://avatars.githubusercontent.com/u/402145?v=4)[Oliver Hader](/maintainers/ohader)[@ohader](https://github.com/ohader)

---

Top Contributors

[![ohader](https://avatars.githubusercontent.com/u/402145?v=4)](https://github.com/ohader "ohader (129 commits)")[![alexpott](https://avatars.githubusercontent.com/u/769634?v=4)](https://github.com/alexpott "alexpott (8 commits)")[![lolli42](https://avatars.githubusercontent.com/u/2178068?v=4)](https://github.com/lolli42 "lolli42 (3 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")[![ktomk](https://avatars.githubusercontent.com/u/352517?v=4)](https://github.com/ktomk "ktomk (1 commits)")[![longwave](https://avatars.githubusercontent.com/u/197817?v=4)](https://github.com/longwave "longwave (1 commits)")[![ausi](https://avatars.githubusercontent.com/u/367169?v=4)](https://github.com/ausi "ausi (1 commits)")[![ryanaslett](https://avatars.githubusercontent.com/u/101536?v=4)](https://github.com/ryanaslett "ryanaslett (1 commits)")[![xknown](https://avatars.githubusercontent.com/u/1081870?v=4)](https://github.com/xknown "xknown (1 commits)")

---

Tags

deserializationinsecure-deserializationpharphpsecuritystream-wrapperphppharsecuritystream-wrapper

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/typo3-phar-stream-wrapper/health.svg)

```
[![Health](https://phpackages.com/badges/typo3-phar-stream-wrapper/health.svg)](https://phpackages.com/packages/typo3-phar-stream-wrapper)
```

###  Alternatives

[asbiin/laravel-webauthn

Laravel Webauthn support

309574.8k](/packages/asbiin-laravel-webauthn)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
