Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache type not usable if global sys_tmp_dir not writeable #65

Open
Lokilein opened this issue Oct 18, 2022 · 11 comments · May be fixed by #73
Open

Cache type not usable if global sys_tmp_dir not writeable #65

Lokilein opened this issue Oct 18, 2022 · 11 comments · May be fixed by #73
Labels
Bug Something isn't working

Comments

@Lokilein
Copy link

Lokilein commented Oct 18, 2022

BC Break Report

Q A
Version 2.3.x-dev

Summary

When you define an own cache_dir, the system always tries the default sys_get_temp_dir first, even though you are not using it. If anything is wrong with it (like no write access), you cannot create a FileSystem Adapter at all.

Previous behavior

The sys_get_temp_dir() was only used in case that no other dir was defined.

Current behavior

I've provided the following config for the filesystem:

'name'    => 'filesystem',
'adapter' => 'filesystem',
'options' => [
    'cache_dir' => 'data/cache/',
    'ttl'       => '3600'
],
'plugins' => [
    [
        'name'    => 'exception_handler',
        'options' => [
            'throw_exceptions' => true,
        ],
    ],
    [
        'name' => 'Serializer'
    ]
]

As you can see, the cache_dir is provided in the settings.
However, since the new version, the FileSystemOptions always calls the setCacheDir with the parameter null:
$this->setCacheDir(null); (FileSystemOptions.php:115)

This results in a fallback to sys_get_temp_dir(), which will then be checked and - on my test maschine - was not writable. The Adapter throws and cannot be created then, though right after that, it would overwrite the CacheDir with the correct and working path.

How to reproduce

Define your sys_temp_dir in your php.ini to a non-existing or not-writable path.
Then try to create an adapter with a valid cache_dir configuration.
It will not work and fails with one exception of the method normalizeCacheDirectory

@Ocramius
Copy link
Member

@Lokilein does this not affect 2.2.x?

@froschdesign
Copy link
Member

@Lokilein

However, since the new version, the FileSystemOptions always calls the setCacheDir with the parameter null: $this->setCacheDir(null); (FileSystemOptions.php:115)

After that, your configuration is passed down a few layers:

$this->setCacheDir(null);
parent::__construct($options);

https://github.com/laminas/laminas-stdlib/blob/9c92afc89381d9f251aff6edf7bbd5db42c82071/src/AbstractOptions.php#L38-L43

https://github.com/laminas/laminas-stdlib/blob/9c92afc89381d9f251aff6edf7bbd5db42c82071/src/AbstractOptions.php#L52-L75

https://github.com/laminas/laminas-stdlib/blob/9c92afc89381d9f251aff6edf7bbd5db42c82071/src/AbstractOptions.php#L113-L131

And if something is wrong then the adapter will throw an exception:

private function normalizeCacheDirectory(string $cacheDir): string
{
if (! is_dir($cacheDir)) {
throw new Exception\InvalidArgumentException(
"Cache directory '{$cacheDir}' not found or not a directory"
);
} elseif (! is_writable($cacheDir)) {
throw new Exception\InvalidArgumentException(
"Cache directory '{$cacheDir}' not writable"
);
} elseif (! is_readable($cacheDir)) {
throw new Exception\InvalidArgumentException(
"Cache directory '{$cacheDir}' not readable"
);
}
return rtrim(realpath($cacheDir), DIRECTORY_SEPARATOR);
}

@Lokilein
Copy link
Author

@Lokilein does this not affect 2.2.x?

Might be so, I just updated and it occured to me with this version. We've updated from 1.2 though.

@Ocramius
Copy link
Member

If you've updated from 1.x, then BC breaks are expected anyway: re-labeling as bug

@Ocramius Ocramius added Bug Something isn't working and removed BC Break labels Oct 18, 2022
@Lokilein
Copy link
Author

@froschdesign Correctly. At the end, it works like it should, but as it checks the "null" aka sys_tmp_dir as well now, even though it will be overwritten soon after, it breaks where it should not break.

@Ocramius Agree!

@Ocramius
Copy link
Member

@Lokilein are you able to provide a failing test, perhaps? 🤔

@Lokilein
Copy link
Author

@Ocramius Hmm, not sure what you mean?

I think you just need to configure a valid Filesystem Cache with a valid cache_dir (like my example above). And if you now configure your php.ini to set your sys_tmp_dir=/does/not/exist or similar, the error should already occure.

I cannot provide my/our code as example, as we have a quite big monolith and an own Factory for the caches. That would be a overhead and not easy to extract...

@froschdesign
Copy link
Member

@Lokilein
Write a test that illustrates the problem and send it as a pull request. Compare with the existing tests:

final class FilesystemOptionsTest extends AbstractAdapterOptionsTest

@Lokilein
Copy link
Author

@froschdesign
hmm... I am currently working on Windows, which makes the tests difficult, but even though - I am not sure how to make sure that the sys_temp_dir is not writeable, as it is also not changable by ini_set or something and I will not change the rights of the default folder (and I hope that this would not work :D)

But, when I run the tests with an invalid php.ini setting (C:\doesnotexist), my tests are already failing with an error, while they all pass when the sys_tmp_dir is valid.

With the invalid setting, I receive:
[ErrorException] tempnam(): file created in the system's temporary directory

I think that this fails because createAdapterOptions calls the constructor and this is enough to reproduce - depending on the configured path.

@Ocramius
Copy link
Member

docker run --rm -ti -v $(pwd):/app -w /app php:7 bash and work from there? 🤔

@tck
Copy link

tck commented Sep 5, 2023

Pull request to fix this issue #73

@boesing boesing linked a pull request Sep 6, 2023 that will close this issue
bcremer added a commit to bcremer/laminas-cache-storage-adapter-filesystem that referenced this issue Jan 29, 2024
bcremer added a commit to bcremer/laminas-cache-storage-adapter-filesystem that referenced this issue Jan 29, 2024
Signed-off-by: Benjamin Cremer <benjamin.cremer@check24.de>
bcremer added a commit to bcremer/laminas-cache-storage-adapter-filesystem that referenced this issue Jan 29, 2024
Signed-off-by: Benjamin Cremer <benjamin.cremer@check24.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants