[php-src] PR #22011: ext/phar: improve .phar madic directory preservation logic in phar::addEmptyDir()
| From: | LamentXU123 | Date: | Mon, 11 May 2026 13:57:18 +0000 |
| Subject: | [php-src] PR #22011: ext/phar: improve .phar madic directory preservation logic in phar::addEmptyDir() | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-36085@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/22011
Author: LamentXU123
Now, the .phar directory is a magic dir for phar files, and in phar::addEmptyDir(), users
couldn't create a dir naming .phar
The implementation is:
```c
if (zend_string_starts_with_literal(dir_name, ".phar")) {
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create a directory in magic
\".phar\" directory");
RETURN_THROWS();
```
This has two bugs.
Firstly, people can use /.phar to create the .phar dir. The leading / will be ignored.
```php
<?php
$phar = new Phar(__DIR__ . '/test.phar', 0, 'test.phar');
$phar->addEmptyDir('/.phar');
var_dump(is_dir('phar://' . __DIR__ . '/test.phar/.phar'));
```
Will return true, while if the dir is .phar it will raise an error.
Secondly, it only matches the prefix. That said, /.pharxxx will not be allowed to create, which is
not a magic dir.
```php
<?php
$phar = new Phar(__DIR__ . '/test.phar', 0, 'test.phar');
$phar->addEmptyDir('.pharx');
```
This will raise an error.
```
PHP Fatal error: Uncaught BadMethodCallException: Cannot create a directory in magic
".phar" directory in C:\Users\admin\Desktop\bench.php:3
```
This PR fix both by 1. adding a trailing check of the path to make .pharx valid 2. adding a check to
/.phar
cc @Girgias Thanks!