Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Cache/VerifyRateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cake\Cache\Cache;
use Cake\Cache\Engine\FileEngine;
use Cake\Cache\Engine\NullEngine as CacheNullEngine;
use LogicException;

class VerifyRateLimiter {

Expand Down Expand Up @@ -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);
Expand Down
30 changes: 30 additions & 0 deletions tests/TestCase/Cache/VerifyRateLimiterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
}
}

}
46 changes: 46 additions & 0 deletions tests/test_app/src/Cache/Engine/DecoratedFileEngine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

namespace TestApp\Cache\Engine;

use Cake\Cache\CacheEngine;
use Cake\Cache\Engine\FileEngine;
use DateInterval;

/**
* Wraps a FileEngine the way DebugKit's DebugEngine wraps every configured cache.
*/
class DecoratedFileEngine extends CacheEngine {

public function __construct(protected FileEngine $engine) {
}

public function set(string $key, mixed $value, DateInterval|int|null $ttl = null): bool {
return $this->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);
}

}
Loading