diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 70b5c3a365..2a7599ae5d 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -16,7 +16,7 @@ use Appwrite\Utopia\Database\Validator\Queries\Projects; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; use PHPMailer\PHPMailer\PHPMailer; -use Utopia\Abuse\Adapters\TimeLimit; +use Utopia\Abuse\Adapters\Database as AbuseDatabase; use Utopia\App; use Utopia\Audit\Audit; use Utopia\Cache\Cache; @@ -209,7 +209,7 @@ App::post('/v1/projects') $audit = new Audit($dbForProject); $audit->setup(); - $abuse = new TimeLimit('', 0, 1, $dbForProject); + $abuse = new AbuseDatabase('', 0, 1, $dbForProject); $abuse->setup(); /** @var array $collections */ diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index a4d7de1a67..7ae66443e1 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -18,7 +18,7 @@ use Appwrite\Messaging\Adapter\Realtime; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; use Utopia\Abuse\Abuse; -use Utopia\Abuse\Adapters\TimeLimit; +use Utopia\Abuse\Adapters\Database as AbuseDatabase; use Utopia\App; use Utopia\Cache\Adapter\Filesystem; use Utopia\Cache\Cache; @@ -373,15 +373,15 @@ App::init() * Abuse Check */ $abuseKeyLabel = $route->getLabel('abuse-key', 'url:{url},ip:{ip}'); - $timeLimitArray = []; + $abuseAdapterArray = []; $abuseKeyLabel = (!is_array($abuseKeyLabel)) ? [$abuseKeyLabel] : $abuseKeyLabel; foreach ($abuseKeyLabel as $abuseKey) { $start = $request->getContentRangeStart(); $end = $request->getContentRangeEnd(); - $timeLimit = new TimeLimit($abuseKey, $route->getLabel('abuse-limit', 0), $route->getLabel('abuse-time', 3600), $dbForProject); - $timeLimit + $abuseAdapter = new AbuseDatabase($abuseKey, $route->getLabel('abuse-limit', 0), $route->getLabel('abuse-time', 3600), $dbForProject); + $abuseAdapter ->setParam('{projectId}', $project->getId()) ->setParam('{userId}', $user->getId()) ->setParam('{userAgent}', $request->getUserAgent('')) @@ -389,7 +389,7 @@ App::init() ->setParam('{url}', $request->getHostname() . $route->getPath()) ->setParam('{method}', $request->getMethod()) ->setParam('{chunkId}', (int) ($start / ($end + 1 - $start))); - $timeLimitArray[] = $timeLimit; + $abuseAdapterArray[] = $abuseAdapter; } $closestLimit = null; @@ -398,17 +398,17 @@ App::init() $isPrivilegedUser = Auth::isPrivilegedUser($roles); $isAppUser = Auth::isAppUser($roles); - foreach ($timeLimitArray as $timeLimit) { + foreach ($abuseAdapterArray as $abuseAdapter) { foreach ($request->getParams() as $key => $value) { // Set request params as potential abuse keys if (!empty($value)) { - $timeLimit->setParam('{param-' . $key . '}', (\is_array($value)) ? \json_encode($value) : $value); + $abuseAdapter->setParam('{param-' . $key . '}', (\is_array($value)) ? \json_encode($value) : $value); } } - $abuse = new Abuse($timeLimit); - $remaining = $timeLimit->remaining(); - $limit = $timeLimit->limit(); - $time = (new \DateTime($timeLimit->time()))->getTimestamp() + $route->getLabel('abuse-time', 3600); + $abuse = new Abuse($abuseAdapter); + $remaining = $abuseAdapter->remaining(); + $limit = $abuseAdapter->limit(); + $time = (new \DateTime($abuseAdapter->time()))->getTimestamp() + $route->getLabel('abuse-time', 3600); if ($limit && ($remaining < $closestLimit || is_null($closestLimit))) { $closestLimit = $remaining; diff --git a/app/http.php b/app/http.php index 76a5c2899e..baae6799b7 100644 --- a/app/http.php +++ b/app/http.php @@ -9,7 +9,7 @@ use Swoole\Http\Request as SwooleRequest; use Swoole\Http\Response as SwooleResponse; use Swoole\Http\Server; use Swoole\Process; -use Utopia\Abuse\Adapters\TimeLimit; +use Utopia\Abuse\Adapters\Database as AbuseDatabase; use Utopia\App; use Utopia\Audit\Audit; use Utopia\CLI\Console; @@ -101,8 +101,8 @@ $http->on(Constant::EVENT_START, function (Server $http) use ($payloadSize, $reg $audit->setup(); } - if ($dbForConsole->getCollection(TimeLimit::COLLECTION)->isEmpty()) { - $adapter = new TimeLimit("", 0, 1, $dbForConsole); + if ($dbForConsole->getCollection(AbuseDatabase::COLLECTION)->isEmpty()) { + $adapter = new AbuseDatabase("", 0, 1, $dbForConsole); $adapter->setup(); } diff --git a/app/realtime.php b/app/realtime.php index 9c3c2b4d6a..b2aeaf21d0 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -13,7 +13,7 @@ use Swoole\Runtime; use Swoole\Table; use Swoole\Timer; use Utopia\Abuse\Abuse; -use Utopia\Abuse\Adapters\TimeLimit; +use Utopia\Abuse\Adapters\Database as AbuseDatabase; use Utopia\App; use Utopia\Cache\Adapter\Sharding; use Utopia\Cache\Cache; @@ -463,12 +463,12 @@ $server->onOpen(function (int $connection, SwooleRequest $request) use ($server, * * Abuse limits are connecting 128 times per minute and ip address. */ - $timeLimit = new TimeLimit('url:{url},ip:{ip}', 128, 60, $dbForProject); - $timeLimit + $abuseAdapter = new AbuseDatabase('url:{url},ip:{ip}', 128, 60, $dbForProject); + $abuseAdapter ->setParam('{ip}', $request->getIP()) ->setParam('{url}', $request->getURI()); - $abuse = new Abuse($timeLimit); + $abuse = new Abuse($abuseAdapter); if (System::getEnv('_APP_OPTIONS_ABUSE', 'enabled') === 'enabled' && $abuse->check()) { throw new Exception(Exception::REALTIME_TOO_MANY_MESSAGES, 'Too many requests'); @@ -563,13 +563,13 @@ $server->onMessage(function (int $connection, string $message) use ($server, $re * * Abuse limits are sending 32 times per minute and connection. */ - $timeLimit = new TimeLimit('url:{url},connection:{connection}', 32, 60, $database); + $abuseDatabase = new AbuseDatabase('url:{url},connection:{connection}', 32, 60, $database); - $timeLimit + $abuseDatabase ->setParam('{connection}', $connection) ->setParam('{container}', $containerId); - $abuse = new Abuse($timeLimit); + $abuse = new Abuse($abuseDatabase); if ($abuse->check() && System::getEnv('_APP_OPTIONS_ABUSE', 'enabled') === 'enabled') { throw new Exception(Exception::REALTIME_TOO_MANY_MESSAGES, 'Too many messages.'); diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index 7d164c2b5d..717f6dfacb 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -7,7 +7,7 @@ use Appwrite\Extend\Exception; use Executor\Executor; use Throwable; use Utopia\Abuse\Abuse; -use Utopia\Abuse\Adapters\TimeLimit; +use Utopia\Abuse\Adapters\Database as AbuseDatabase; use Utopia\Audit\Audit; use Utopia\Cache\Adapter\Filesystem; use Utopia\Cache\Cache; @@ -493,7 +493,7 @@ class Deletes extends Action $projectCollectionIds = [ ...\array_keys(Config::getParam('collections', [])['projects']), Audit::COLLECTION, - TimeLimit::COLLECTION, + AbuseDatabase::COLLECTION, ]; $limit = \count($projectCollectionIds) + 25; @@ -686,8 +686,8 @@ class Deletes extends Action { $projectId = $project->getId(); $dbForProject = $getProjectDB($project); - $timeLimit = new TimeLimit("", 0, 1, $dbForProject); - $abuse = new Abuse($timeLimit); + $abuseAdapter = new AbuseDatabase("", 0, 1, $dbForProject); + $abuse = new Abuse($abuseAdapter); try { $abuse->cleanup($abuseRetention);