From aaf26fd9975aafcadaebef15b1ffdbf3ff2f6d7f Mon Sep 17 00:00:00 2001 From: Tobias Welz Date: Tue, 29 Sep 2026 09:16:34 +0200 Subject: [PATCH] fix: Fall back when a decorated cache engine cannot increment DebugKit wraps every configured cache in a DebugEngine, so the instanceof FileEngine guard in VerifyRateLimiter::increment() does not match and the second counted failure throws "Files cannot be atomically incremented". Catch the LogicException and use the existing read/write fallback. The increment is called on the pool engine directly: Cache::increment() only declares InvalidArgumentException, which hides the LogicException the engine documents. Closes #62 Co-Authored-By: Claude Opus 5.5 --- src/Cache/VerifyRateLimiter.php | 8 +++- .../TestCase/Cache/VerifyRateLimiterTest.php | 30 ++++++++++++ .../src/Cache/Engine/DecoratedFileEngine.php | 46 +++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 tests/test_app/src/Cache/Engine/DecoratedFileEngine.php diff --git a/src/Cache/VerifyRateLimiter.php b/src/Cache/VerifyRateLimiter.php index 07e37de..fc91138 100644 --- a/src/Cache/VerifyRateLimiter.php +++ b/src/Cache/VerifyRateLimiter.php @@ -6,6 +6,7 @@ use Cake\Cache\Cache; use Cake\Cache\Engine\FileEngine; use Cake\Cache\Engine\NullEngine as CacheNullEngine; +use LogicException; class VerifyRateLimiter { @@ -49,7 +50,12 @@ public function increment(string $ip, string $sessionId): void { $cache = Cache::pool($cacheName); $count = false; if (!$cache instanceof FileEngine && !$cache instanceof CacheNullEngine) { - $count = Cache::increment($key, 1, $cacheName); + try { + $count = $cache->increment($key); + } catch (LogicException) { + // Decorated engines (e.g. DebugKit's DebugEngine) hide an engine without atomic increment. + $count = false; + } } if ($count === false) { $count = Cache::read($key, $cacheName); diff --git a/tests/TestCase/Cache/VerifyRateLimiterTest.php b/tests/TestCase/Cache/VerifyRateLimiterTest.php index c386625..98fcc33 100644 --- a/tests/TestCase/Cache/VerifyRateLimiterTest.php +++ b/tests/TestCase/Cache/VerifyRateLimiterTest.php @@ -4,10 +4,12 @@ namespace Captcha\Test\TestCase\Cache; use Cake\Cache\Cache; +use Cake\Cache\Engine\FileEngine; use Cake\TestSuite\TestCase; use Captcha\Cache\RateLimitKey; use Captcha\Cache\RateLimitRegistry; use Captcha\Cache\VerifyRateLimiter; +use TestApp\Cache\Engine\DecoratedFileEngine; class VerifyRateLimiterTest extends TestCase { @@ -52,4 +54,32 @@ public function testFileCacheFallback(): void { } } + /** + * A decorated FileEngine (e.g. DebugKit's DebugEngine) hides the engine class, + * so the limiter must fall back when the atomic increment is not supported. + * + * @return void + */ + public function testDecoratedFileCacheFallback(): void { + $path = TMP . 'captcha_limiter_' . bin2hex(random_bytes(8)) . DIRECTORY_SEPARATOR; + mkdir($path); + $fileEngine = new FileEngine(); + $fileEngine->init(['path' => $path]); + Cache::setConfig('limiter_decorated', ['className' => new DecoratedFileEngine($fileEngine)]); + try { + Cache::clear('limiter_decorated'); + $limiter = new VerifyRateLimiter(['enabled' => true, 'maxFailures' => 3, 'window' => 600, 'scope' => 'ip', 'cache' => 'limiter_decorated']); + $limiter->increment('127.0.0.1', 'one'); + $limiter->increment('127.0.0.1', 'one'); + $this->assertFalse($limiter->limited('127.0.0.1', 'one')); + $limiter->increment('127.0.0.1', 'one'); + $this->assertTrue($limiter->limited('127.0.0.1', 'one')); + } finally { + Cache::clear('limiter_decorated'); + Cache::drop('limiter_decorated'); + array_map('unlink', glob($path . '*') ?: []); + rmdir($path); + } + } + } diff --git a/tests/test_app/src/Cache/Engine/DecoratedFileEngine.php b/tests/test_app/src/Cache/Engine/DecoratedFileEngine.php new file mode 100644 index 0000000..b9e4cfa --- /dev/null +++ b/tests/test_app/src/Cache/Engine/DecoratedFileEngine.php @@ -0,0 +1,46 @@ +engine->set($key, $value, $ttl); + } + + public function get(string $key, mixed $default = null): mixed { + return $this->engine->get($key, $default); + } + + public function increment(string $key, int $offset = 1): int|false { + return $this->engine->increment($key, $offset); + } + + public function decrement(string $key, int $offset = 1): int|false { + return $this->engine->decrement($key, $offset); + } + + public function delete(string $key): bool { + return $this->engine->delete($key); + } + + public function clear(): bool { + return $this->engine->clear(); + } + + public function clearGroup(string $group): bool { + return $this->engine->clearGroup($group); + } + +}