s9y / Serendipity

A PHP blog software
https://s9y.org
BSD 3-Clause "New" or "Revised" License
206 stars 86 forks source link

Uncaught Error with voku/simple-cache makes s9y unusable in secured environments #817

Closed Flip1 closed 10 months ago

Flip1 commented 10 months ago

If "use cache" is selected in the installer and as long as "_config/useInternalCache" is set to "true" in the mysql database, the following error appears:

Uncaught Error: Call to undefined function opcache_get_status()
bundled-libs/voku/simple-cache/src/voku/cache/AdapterOpCache.php Zeile 36
Backtrace
#0 bundled-libs/voku/simple-cache/src/voku/cache/Cache.php(255): voku\cache\AdapterOpCache->__construct(Object(Closure))
#1 bundled-libs/voku/simple-cache/src/voku/cache/Cache.php(161): voku\cache\Cache->autoConnectToAvailableCacheSystem(Object(voku\cache\CacheAdapterAutoManager), false)
#2 include/functions.inc.php(1422): voku\cache\Cache->__construct(NULL, NULL, false, true, false, false, false, false, '', Object(voku\cache\CacheAdapterAutoManager), false)
#3 include/functions.inc.php(1438): serendipity_setupCache()
#4 include/functions_entries.inc.php(217): serendipity_getCacheItem('4e53b073ffe1625...')
#5 include/genpage.inc.php(55): serendipity_fetchEntries(NULL, true, 15)
#6 include/functions_routing.inc.php(272): include('/home/webpages/...')
#7 index.php(89): serveCategory(Array)
#8 {main}

This affects frontend and backend and makes s9y completely unusable.

After turning "_config/useInternalCache" to "false", s9y works generally fine. Unless a new entry is created or an entry is edited. Then the following (same) error happens:

Uncaught Error: Call to undefined function opcache_get_status()
bundled-libs/voku/simple-cache/src/voku/cache/AdapterOpCache.php Zeile 36
Backtrace
#0 bundled-libs/voku/simple-cache/src/voku/cache/Cache.php(255): voku\cache\AdapterOpCache->__construct(Object(Closure))
#1 bundled-libs/voku/simple-cache/src/voku/cache/Cache.php(161): voku\cache\Cache->autoConnectToAvailableCacheSystem(Object(voku\cache\CacheAdapterAutoManager), false)
#2 include/functions.inc.php(1422): voku\cache\Cache->__construct(NULL, NULL, false, true, false, false, false, false, '', Object(voku\cache\CacheAdapterAutoManager), false)
#3 include/functions.inc.php(1428): serendipity_setupCache()
#4 include/functions_entries.inc.php(1548): serendipity_cleanCache()
#5 include/functions_config.inc.php(840): serendipity_updertEntry(Array)
#6 serendipity_admin.php(58): serendipity_iframe(Array, 'save')
#7 {main}

This affects version 2.4.0 and 2.5beta1, webhoster is "lima-city.de", PHP-Version is 8.1. I think, a quick fix can be, to prevent executing of "cleanCache" when "_config/useInternalCache" is "false". And the internal cache should be turned of in secured environments.

Flip1 commented 10 months ago

This maybe depends to #594 and #595.

onli commented 10 months ago

I don't think that's exactly https://github.com/s9y/Serendipity/issues/594, though it's certainly related. In there, it was about Opcache being restricted, which is supposed to throw a warning. The error you show is a different error message: opcache_get_status doesn't exist. It seems like lima-city has the opcache completely disabled? That seems highly unusual to me, but I might be wrong about that.

I think, a quick fix can be, to prevent executing of "cleanCache" when "_config/useInternalCache" is "false".

I basically agree, but we could also keep the fix simpler by catching this one level below. It seems what is triggered here regardless of the setting is serendipity_cleanCache() from the functions.inc.php. It calls serendipity_setupCache, and that is what dies. The first lines of that function should probably be:

global $serendipity;
if  (! isset($serendipity['useInternalCache']) || ! $serendipity['useInternalCache']) {
    return;
}

Could you test this fix?

Alternatively, in the same function we could try to catch the thrown error. Not sure which line, I'd test that step by step. Maybe you are also up for that?

Thanks for the report!

garvinhicking commented 10 months ago

@onli how about adding a function_exists check in compat.inc.php and disable this serendipity cache option dynamically, if not existing?

onli commented 10 months ago

@garvinhicking I think that would be okay.

The only thing making me hesitate: Theoretically, voku/simple-cache can also use something else for caching than the Opcache, like Redis. I always wanted to test that and make it available, but never took the time.

But since our configuration targets the Opcache you are right, to test for that existing is completely warranted.

garvinhicking commented 10 months ago

@onli @Flip1 I've just committed a patch that should hopefully disable internal cache when not available, can you check if that works for you?

Flip1 commented 10 months ago

I updated to the latest commit, the error has disappeared and everything seems to be working fine. If I set "useInternalCache" to "true", everything continues to work normally without errors. I think your commit does it. Thank you for the quick fix! :-)