mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #107 from utopia-php/copilot/add-reset-method-to-abuse-interface
Add reset() method to Abuse interface and adapters
This commit is contained in:
11 files changed
+481
-202
No files matched your search
+1
-1
@@ -23,7 +23,7 @@
|
||||
"ext-curl": "*",
|
||||
"ext-redis": "*",
|
||||
"utopia-php/database": "3.*.*",
|
||||
"appwrite/appwrite": "18.*.*"
|
||||
"appwrite/appwrite": "19.*.*"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "9.*",
|
||||
|
||||
Generated
+241
-201
File diff suppressed because it is too large.
Load diff
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -250,6 +250,60 @@ 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
|
||||
{
|
||||
$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('Unable to find abuse tracking row after race condition handling');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$this->tablesDB->updateRow($this->databaseId, self::TABLE_ID, $data['$id'], ['count' => $value]);
|
||||
}
|
||||
|
||||
$this->count = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get abuse logs
|
||||
*
|
||||
|
||||
@@ -215,6 +215,61 @@ 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
|
||||
{
|
||||
$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('Unable to find abuse tracking document after race condition handling');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/** @var Document $data */
|
||||
$this->db->updateDocument(self::COLLECTION, $data->getId(), new Document([
|
||||
'count' => $value,
|
||||
]));
|
||||
}
|
||||
});
|
||||
|
||||
$this->count = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get abuse logs
|
||||
*
|
||||
|
||||
@@ -77,6 +77,25 @@ class Redis 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
|
||||
{
|
||||
$key = self::NAMESPACE . '__' . $key . '__' . $timestamp;
|
||||
$this->redis->multi()
|
||||
->set($key, (string)$value)
|
||||
->expire($key, $this->ttl)
|
||||
->exec();
|
||||
|
||||
$this->count = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get abuse logs
|
||||
*
|
||||
|
||||
@@ -79,6 +79,27 @@ 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
|
||||
{
|
||||
|
||||
$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
|
||||
*
|
||||
|
||||
@@ -96,4 +96,43 @@ 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, 600);
|
||||
$adapter->setParam('{{ip}}', '192.168.1.1');
|
||||
$abuse = new Abuse($adapter);
|
||||
|
||||
// 5 OK, 6th has limit
|
||||
$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);
|
||||
|
||||
// Reset the count
|
||||
$abuse->reset();
|
||||
|
||||
// Should be 5 more OK, then 6th limit
|
||||
$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);
|
||||
|
||||
// TO be sure, lets do bunch of requests with resets
|
||||
// All should pass successfully
|
||||
$adapter = $this->getAdapter('reset-test-{{ip}}', 2, 600);
|
||||
$adapter->setParam('{{ip}}', '192.168.1.2');
|
||||
$abuse = new Abuse($adapter);
|
||||
for ($i = 0; $i < 15; $i++) {
|
||||
$this->assertSame($abuse->check(), false);
|
||||
$abuse->reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user