is_readable() not working on files inside phar
Description
Using the following code to create a basic phar file:
$stub = <<<STUB
<?php
Phar::interceptFileFuncs();
set_include_path('phar://' . __FILE__ . PATH_SEPARATOR . get_include_path());
include "index.php";
__HALT_COMPILER();
STUB;
$index = <<<INDEX
<?php
var_dump(file_exists('phar:///path/to/archive.phar/index.php')) . PHP_EOL;
var_dump(is_readable('phar:///path/to/archive.phar/index.php')) . PHP_EOL;
var_dump(file_exists('index.php')) . PHP_EOL;
var_dump(is_readable('index.php')) . PHP_EOL;
INDEX;
$phar = new Phar('archive.phar');
$phar->addFromString('index.php', $index);
$phar->setStub($stub);
Then executing the following code:
php archive.phar
Resulted in this output:
bool(true)
bool(false)
bool(true)
bool(false)
But I expected this output instead:
bool(true)
bool(true)
bool(true)
bool(true)
Or executing the following code:
<?php
var_dump(is_readable('phar:///path/to/archive.phar/index.php'));
Resulted in this output:
bool(false)
But I expected this output instead:
bool(true)
PHP Version
PHP 8.0.12
Operating System
Ubuntu 20.04
You need to use a phar wrapper:
var_dump(is_readable('phar://path/to/archive.phar/index.php'));
You need to use a phar wrapper:
var_dump(is_readable('phar://path/to/archive.phar/index.php'));
Sorry, that's what i was actually using. Error in the bug report that i just fixed. The bug is still there. Please look at the more detailed example and re-open it :)
is_readable() with the proper phar URL returns false for me. This is on Windows, though. However, file_exists('index.php') should not return true for you, since there is no index.php involved (only within the phar).
is_readable() with the proper phar URL should return true. Not false. So it seems it is also a problem on windows.
file_exists('index.php') is executed in the context of the phar so true is the expected result.
Sorry, my bad; it actually returned true for me, and I didn't really look at the modified code. Anyhow, I now did, and I get:
bool(true)
bool(true)
bool(true)
bool(true)
So this means it is an OS related bug?
I've just tested it on my MacOS php and it returns the same result as on my Ubuntu VM.
I'm on Ubuntu and get 4x true as well.
It's driving me nuts...
I confirm that the problem only seem to orignate from the phar created on my ubuntu VM and executed as a non root user. If i execute the exact same phar creation script on a plain Digital Ocean VM, docker image..., the phar created works as expected. So it seems the phar created from the same script gives different results...
The problematic phar is attached archive.phar.zip. You should be able to reproduce the bug by creating an /home/app directory on Ubuntu 20.04, dropping the file inside and execute it as non root user.
Please tell me i'm not crazy...
Hmm, not sure whether this is Phar specific; doesn't the same happen with plain files. Also, please check whether you can read the "unreadable" files (e.g. using file_get_contents()).
This seems Phar specific as it only happens when we execute our app as a phar. Also, adding those two lines to the index.php file confirms that the file is read.
echo file_get_contents('phar:///path/to/archive.phar/index.php') . PHP_EOL;
echo file_get_contents('index.php') . PHP_EOL;
Did any of you managed to reproduce with the archive i attached?
I can reproduce it with the archive file you gave us. I cannot reproduce it if I create the archive myself.
The difference between your and my archive is that index.php has permission bits 0640 in your archive and 0644 in my archive. In both cases the uid on index.php is 0, so we should look at the last digit, which is 0 in your case (meaning: no perissions) and 4 in my case (meaning: read access). I verified this by debugging phar_fancy_stat().
So there seems to be some confusion going on in how phar handles permissions I think because the fact that file_get_contents works but the phar claims we have no access is inconsistent.
The difference between your and my archive is that index.php has permission bits 0640 in your archive and 0644 in my archive.
Thanks for pointing that out! All our vms have indeed default umask set to 0077 for security purpose. Thus the permissions in the phar created on our servers.
So there seems to be some confusion going on in how phar handles permissions I think because the fact that file_get_contents works but the phar claims we have no access is inconsistent.
Yes indeed. I feel less crazy now thanks to you :)
There's multiple things wrong here. I've tried for 2 hours to fix this but there are conflicting expectations from the phar code. The is_readable etc functions work by statting the path and then checking the mode bits wrt the uid/gid. The uid/gid for phar entries are always 0 and therefore end up checking the mode bits for the "world". That's wrong of course as the mode bits in the phar entries have no enforcement whatsoever. Trying to fix this by overriding the behaviour for the is_* functions works, for relative paths, but not for absolute paths as it solely relies on the (cached) stat buffer with the mode bits + uid/gid check. Fixing this in the mode bits breaks all phar tests that test behaviour with chmod() / fileperms(), but works for all path types.
This is as close as I can make it (for now): https://gist.github.com/nielsdos/f5898b22dff433899442f1c536d3285f It fails the absolute path is_readable check.