Skip to content

ext/phar: improve .phar madic directory preservation logic in phar::addEmptyDir()#22011

Merged
Girgias merged 1 commit into
php:masterfrom
LamentXU123:sec-refactor
May 18, 2026
Merged

ext/phar: improve .phar madic directory preservation logic in phar::addEmptyDir()#22011
Girgias merged 1 commit into
php:masterfrom
LamentXU123:sec-refactor

Conversation

@LamentXU123
Copy link
Copy Markdown
Contributor

@LamentXU123 LamentXU123 commented May 11, 2026

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:

	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. (no need to concern about ../ though, it will be ignored.)

<?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 with the .phar dir created, while if the dir is .phar it will raise an error.

Secondly, it only matches the prefix. That means, /.pharxxx will not be allowed to create, which is not a magic dir.

<?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!

Copy link
Copy Markdown
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks sensible to me, thank you :)

@Girgias Girgias merged commit f99989f into php:master May 18, 2026
19 checks passed
@LamentXU123
Copy link
Copy Markdown
Contributor Author

Ideally this should target php-8.4 -- shell I reopen this again?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants