mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Implement reset() method for Abuse interface and adapters
Co-authored-by: Meldiron <19310830+Meldiron@users.noreply.github.com>
This commit is contained in:
1 parent
8a72773e9f
commit
14ccf84d23
9 files changed
+242
No files matched your search
@@ -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,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
|
||||
*
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user