bovigo / vfsStream

vfsStream is a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with any unit test framework, like PHPUnit or SimpleTest.
BSD 3-Clause "New" or "Revised" License
1.42k stars 102 forks source link

../ is not correctly recognized in VfsStream #267

Closed Sanderluc5 closed 3 years ago

Sanderluc5 commented 3 years ago

I'm currently trying out some functionality that's normally working fine in native filesystems. But the the problem is that ../ and ./ are not working as intended. It seems ../ is not recognized as going directory back etc.

For example this is the code i tested:

$root = vfsStream::setup('');

mkdir('vfs://etc');
file_put_contents('vfs://etc/../test', 'test');
echo file_get_contents('vfs://test');

I'm getting the following error:

Warning: file_get_contents(vfs://test): Failed to open stream: "org\bovigo\vfs\vfsStreamWrapper::stream_open" call failed

I expect to create the file /test. But instead the file /etc/test gets created. Is this some kind of bug? I'm currently working on a Path Traversal simulation so this would be a very nice functionality to have.

bizurkur commented 3 years ago

This is because of https://github.com/bovigo/vfsStream/issues/211. vfsStream doesn't support a blank name (/) for root. So what's happening is it thinks etc is root and won't let you create files above that level.

If you modify your test slightly, things work:

$root = vfsStream::setup('root'); // <-- giving a name for root is the important part
$root = vfsStream::setup(); // <-- this is the same as above
mkdir($root->url() . '/etc');
file_put_contents($root->url() . '/etc/../test', 'test');
echo file_get_contents($root->url() . '/test');

Closing as #211 is the issue.


If you absolutely need / as the root name "right now" you could try mockfs. It's the exact same concept as vfs, but has some key differences. The test you're trying to do here works in mockfs.

use MockFileSystem\MockFileSystem as mockfs;

mockfs::create();
mkdir('mfs:///etc'); // note the three `/`. Two are for the scheme separator, one is to designate root.
file_put_contents('mfs:///etc/../test', 'test');
echo file_get_contents('mfs:///test');
// prints "test"
Sanderluc5 commented 3 years ago

MockFS is exactly what i needed. Thanks for your time and efforts.