Hash client IPs for rate limits with a keyed HMAC
A plain sha256 of an IPv4 address can be reversed by trying all addresses. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2a53675ce5
commit
ad537b9b34
1 file changed
+3
-1
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||||||
namespace App\Service;
|
namespace App\Service;
|
||||||
|
|
||||||
use Cake\Cache\Cache;
|
use Cake\Cache\Cache;
|
||||||
|
use Cake\Utility\Security;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Very small fixed-window rate limiter backed by the "throttle" cache config.
|
* Very small fixed-window rate limiter backed by the "throttle" cache config.
|
||||||
@@ -29,7 +30,8 @@ class ThrottleService
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$key = 'throttle_' . $bucket . '_' . hash('sha256', $identifier);
|
// Keyed hash: a plain sha256 of an IPv4 address can be reversed by trying all 2^32 addresses.
|
||||||
|
$key = 'throttle_' . $bucket . '_' . hash_hmac('sha256', $identifier, 'throttle|' . Security::getSalt());
|
||||||
$now = time();
|
$now = time();
|
||||||
/** @var array{start: int, hits: int}|null $state */
|
/** @var array{start: int, hits: int}|null $state */
|
||||||
$state = Cache::read($key, 'throttle');
|
$state = Cache::read($key, 'throttle');
|
||||||
|
|||||||
Reference in new issue
Block a user