From 14ccf84d23df4d15c6b48ef88b8dcf0728bc38c7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 5 Jan 2026 20:59:31 +0000 Subject: [PATCH] Implement reset() method for Abuse interface and adapters Co-authored-by: Meldiron <19310830+Meldiron@users.noreply.github.com> --- src/Abuse/Abuse.php | 12 ++++ src/Abuse/Adapter.php | 9 +++ src/Abuse/Adapters/ReCaptcha.php | 14 +++++ src/Abuse/Adapters/TimeLimit.php | 16 +++++ .../Adapters/TimeLimit/Appwrite/TablesDB.php | 58 ++++++++++++++++++ src/Abuse/Adapters/TimeLimit/Database.php | 59 +++++++++++++++++++ src/Abuse/Adapters/TimeLimit/Redis.php | 21 +++++++ src/Abuse/Adapters/TimeLimit/RedisCluster.php | 24 ++++++++ tests/Abuse/Base.php | 29 +++++++++ 9 files changed, 242 insertions(+) diff --git a/src/Abuse/Abuse.php b/src/Abuse/Abuse.php index 948939913d..cdb6f9c54b 100644 --- a/src/Abuse/Abuse.php +++ b/src/Abuse/Abuse.php @@ -53,4 +53,16 @@ class Abuse { return $this->adapter->cleanup($timestamp); } + + /** + * Reset + * + * Reset the count to 0 for the current adapter + * + * @return void + */ + public function reset(): void + { + $this->adapter->reset(); + } } diff --git a/src/Abuse/Adapter.php b/src/Abuse/Adapter.php index 7de8967870..b511d0319b 100644 --- a/src/Abuse/Adapter.php +++ b/src/Abuse/Adapter.php @@ -83,4 +83,13 @@ abstract class Adapter * @return bool */ abstract public function cleanup(int $timestamp): bool; + + /** + * Reset + * + * Reset the count to 0 + * + * @return void + */ + abstract public function reset(): void; } diff --git a/src/Abuse/Adapters/ReCaptcha.php b/src/Abuse/Adapters/ReCaptcha.php index 15ecbdda84..0a8120228d 100644 --- a/src/Abuse/Adapters/ReCaptcha.php +++ b/src/Abuse/Adapters/ReCaptcha.php @@ -117,4 +117,18 @@ class ReCaptcha extends Adapter { throw new Exception('Method not supported'); } + + /** + * Reset + * + * Reset is not applicable for ReCaptcha adapter + * + * @return void + * + * @throws Exception + */ + public function reset(): void + { + throw new Exception('Method not supported'); + } } diff --git a/src/Abuse/Adapters/TimeLimit.php b/src/Abuse/Adapters/TimeLimit.php index d0bd655a7a..073c8f3dff 100644 --- a/src/Abuse/Adapters/TimeLimit.php +++ b/src/Abuse/Adapters/TimeLimit.php @@ -37,6 +37,8 @@ abstract class TimeLimit extends Adapter abstract protected function hit(string $key, int $timestamp): void; + abstract protected function set(string $key, int $timestamp, int $value): void; + /** * Check * @@ -102,4 +104,18 @@ abstract class TimeLimit extends Adapter { return $this->timestamp; } + + /** + * Reset + * + * Reset the count to 0 for the current key and timestamp + * + * @return void + * + * @throws \Exception + */ + public function reset(): void + { + $this->set($this->parseKey(), $this->timestamp, 0); + } } diff --git a/src/Abuse/Adapters/TimeLimit/Appwrite/TablesDB.php b/src/Abuse/Adapters/TimeLimit/Appwrite/TablesDB.php index d731813b09..84223a0ad0 100644 --- a/src/Abuse/Adapters/TimeLimit/Appwrite/TablesDB.php +++ b/src/Abuse/Adapters/TimeLimit/Appwrite/TablesDB.php @@ -250,6 +250,64 @@ class TablesDB extends TimeLimit $this->count++; } + /** + * @param string $key + * @param int $timestamp + * @param int $value + * @return void + * + * @throws \Throwable + */ + protected function set(string $key, int $timestamp, int $value): void + { + if (0 == $this->limit) { // No limit no point for counting + return; + } + + $timestamp = $this->toDateTime($timestamp); + + $response = $this->tablesDB->listRows($this->databaseId, self::TABLE_ID, [ + Query::equal('key', [$key]), + Query::equal('time', [$timestamp]), + ]); + $rows = $response['rows']; + $data = $rows[0] ?? null; + + if (\is_null($data)) { + $data = [ + 'key' => $key, + 'time' => $timestamp, + 'count' => $value, + ]; + + try { + $this->tablesDB->createRow($this->databaseId, self::TABLE_ID, ID::unique(), $data); + } catch (AppwriteException $err) { + if ($err->getType() !== 'row_already_exists') { + throw $err; + } + + $response = $this->tablesDB->listRows($this->databaseId, self::TABLE_ID, [ + Query::equal('key', [$key]), + Query::equal('time', [$timestamp]), + ]); + $rows = $response['rows']; + + $data = $rows[0] ?? null; + + if (!is_null($data)) { + $this->tablesDB->updateRow($this->databaseId, self::TABLE_ID, $data['$id'], ['count' => $value]); + } else { + throw new \Exception('Document Not Found'); + } + } + } else { + $this->tablesDB->updateRow($this->databaseId, self::TABLE_ID, $data['$id'], ['count' => $value]); + } + + $this->count = $value; + } + /** * Get abuse logs * diff --git a/src/Abuse/Adapters/TimeLimit/Database.php b/src/Abuse/Adapters/TimeLimit/Database.php index ae36f01cd0..19539c0767 100644 --- a/src/Abuse/Adapters/TimeLimit/Database.php +++ b/src/Abuse/Adapters/TimeLimit/Database.php @@ -215,6 +215,65 @@ class Database extends TimeLimit $this->count++; } + /** + * @param string $key + * @param int $timestamp + * @param int $value + * @return void + * + * @throws AuthorizationException|Structure|\Exception|\Throwable + */ + protected function set(string $key, int $timestamp, int $value): void + { + if (0 == $this->limit) { // No limit no point for counting + return; + } + + $timestamp = $this->toDateTime($timestamp); + Authorization::skip(function () use ($timestamp, $key, $value) { + $data = $this->db->findOne(self::COLLECTION, [ + Query::equal('key', [$key]), + Query::equal('time', [$timestamp]), + ]); + + if ($data->isEmpty()) { + $data = [ + '$permissions' => [], + 'key' => $key, + 'time' => $timestamp, + 'count' => $value, + '$collection' => self::COLLECTION, + ]; + + try { + $this->db->createDocument(self::COLLECTION, new Document($data)); + } catch (Duplicate $e) { + // Duplicate in case of race condition - update existing document + $data = $this->db->findOne(self::COLLECTION, [ + Query::equal('key', [$key]), + Query::equal('time', [$timestamp]), + ]); + + if (!$data->isEmpty()) { + /** @var Document $data */ + $this->db->updateDocument(self::COLLECTION, $data->getId(), new Document([ + 'count' => $value, + ])); + } else { + throw new \Exception('Document Not Found'); + } + } + } else { + /** @var Document $data */ + $this->db->updateDocument(self::COLLECTION, $data->getId(), new Document([ + 'count' => $value, + ])); + } + }); + + $this->count = $value; + } + /** * Get abuse logs * diff --git a/src/Abuse/Adapters/TimeLimit/Redis.php b/src/Abuse/Adapters/TimeLimit/Redis.php index 19602613a2..139f742de5 100644 --- a/src/Abuse/Adapters/TimeLimit/Redis.php +++ b/src/Abuse/Adapters/TimeLimit/Redis.php @@ -77,6 +77,27 @@ class Redis extends TimeLimit $this->count = ($this->count ?? 0) + 1; } + /** + * @param string $key + * @param int $timestamp + * @param int $value + * @return void + */ + protected function set(string $key, int $timestamp, int $value): void + { + if (0 == $this->limit) { // No limit no point for counting + return; + } + + $key = self::NAMESPACE . '__' . $key . '__' . $timestamp; + $this->redis->multi() + ->set($key, (string)$value) + ->expire($key, $this->ttl) + ->exec(); + + $this->count = $value; + } + /** * Get abuse logs * diff --git a/src/Abuse/Adapters/TimeLimit/RedisCluster.php b/src/Abuse/Adapters/TimeLimit/RedisCluster.php index 6acc99a8f4..ed3d1bca45 100644 --- a/src/Abuse/Adapters/TimeLimit/RedisCluster.php +++ b/src/Abuse/Adapters/TimeLimit/RedisCluster.php @@ -79,6 +79,30 @@ class RedisCluster extends TimeLimit $this->count = ($this->count ?? 0) + 1; } + /** + * Set count for a key at specific timestamp + * + * @param string $key + * @param int $timestamp + * @param int $value + * @return void + */ + protected function set(string $key, int $timestamp, int $value): void + { + if (0 == $this->limit) { // No limit no point for counting + return; + } + + $key = self::NAMESPACE . '__'. $key .'__'. $timestamp; + + $this->redis->multi(); + $this->redis->set($key, (string)$value); + $this->redis->expire($key, $this->ttl); + $this->redis->exec(); + + $this->count = $value; + } + /** * Get abuse logs with proper cursor-based pagination * diff --git a/tests/Abuse/Base.php b/tests/Abuse/Base.php index 81a4d5e4e2..6aefd41f4a 100644 --- a/tests/Abuse/Base.php +++ b/tests/Abuse/Base.php @@ -96,4 +96,33 @@ abstract class Base extends TestCase $this->assertSame($adapter->time(), $now); $this->assertSame(true, \is_int($adapter->time())); } + + /** + * Test the reset functionality + */ + public function testReset(): void + { + $adapter = $this->getAdapter('reset-test-{{ip}}', 5, 1); + $adapter->setParam('{{ip}}', '192.168.1.1'); + $abuse = new Abuse($adapter); + + // Make 3 checks + $this->assertSame($abuse->check(), false); + $this->assertSame($abuse->check(), false); + $this->assertSame($abuse->check(), false); + + // Reset the count + $abuse->reset(); + + // Should be able to make 5 checks again + $adapter = $this->getAdapter('reset-test-{{ip}}', 5, 1); + $adapter->setParam('{{ip}}', '192.168.1.1'); + $abuse = new Abuse($adapter); + $this->assertSame($abuse->check(), false); + $this->assertSame($abuse->check(), false); + $this->assertSame($abuse->check(), false); + $this->assertSame($abuse->check(), false); + $this->assertSame($abuse->check(), false); + $this->assertSame($abuse->check(), true); + } }