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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"ext-curl": "*",
"ext-redis": "*",
"utopia-php/database": "3.*.*",
"appwrite/appwrite": "18.*.*"
"appwrite/appwrite": "19.*.*"
},
"require-dev": {
"phpunit/phpunit": "9.*",
Expand Down
442 changes: 241 additions & 201 deletions composer.lock

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/Abuse/Abuse.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,16 @@ public function cleanup(int $timestamp): bool
{
return $this->adapter->cleanup($timestamp);
}

/**
* Reset
*
* Reset the count to 0 for the current adapter
*
* @return void
*/
public function reset(): void
{
$this->adapter->reset();
}
}
9 changes: 9 additions & 0 deletions src/Abuse/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,13 @@ abstract public function getLogs(?int $offset = null, ?int $limit = 25): array;
* @return bool
*/
abstract public function cleanup(int $timestamp): bool;

/**
* Reset
*
* Reset the count to 0
*
* @return void
*/
abstract public function reset(): void;
}
14 changes: 14 additions & 0 deletions src/Abuse/Adapters/ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,18 @@ public function getLogs(?int $offset = null, ?int $limit = 25): array
{
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');
}
}
16 changes: 16 additions & 0 deletions src/Abuse/Adapters/TimeLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ abstract protected function count(string $key, int $timestamp): int;

abstract protected function hit(string $key, int $timestamp): void;

abstract protected function set(string $key, int $timestamp, int $value): void;

/**
* Check
*
Expand Down Expand Up @@ -102,4 +104,18 @@ public function time(): int
{
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);
}
}
54 changes: 54 additions & 0 deletions src/Abuse/Adapters/TimeLimit/Appwrite/TablesDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,60 @@ protected function hit(string $key, int $timestamp): void
$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
*
Expand Down
55 changes: 55 additions & 0 deletions src/Abuse/Adapters/TimeLimit/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,61 @@ protected function hit(string $key, int $timestamp): void
$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
*
Expand Down
19 changes: 19 additions & 0 deletions src/Abuse/Adapters/TimeLimit/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@ protected function hit(string $key, int $timestamp): void
$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
*
Expand Down
21 changes: 21 additions & 0 deletions src/Abuse/Adapters/TimeLimit/RedisCluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ protected function hit(string $key, int $timestamp): void
$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
*
Expand Down
39 changes: 39 additions & 0 deletions tests/Abuse/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,43 @@ public function testTimeFormat(): void
$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();
}
}
}