From da8ff0f99c649f02964ccf3081c4a92d7b51c663 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 15:50:03 +0100 Subject: [PATCH 1/7] chore: replace deprecated Route::getParamsValues() in shared API Reconstruct request param values from per-request sources (path values + request params + param defaults) instead of reading the deprecated mutable value store on the shared Route object, mirroring the framework's own Http::getArguments() resolution precedence. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/controllers/shared/api.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 2b39208fcd..7a36b012e3 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -38,6 +38,7 @@ use Utopia\Database\Validator\Authorization\Input; use Utopia\Database\Validator\Roles; use Utopia\Http\Http; use Utopia\Http\Route; +use Utopia\Http\Router; use Utopia\Span\Span; use Utopia\System\System; use Utopia\Telemetry\Adapter as Telemetry; @@ -862,7 +863,14 @@ Http::shutdown() } } - $requestParams = $route->getParamsValues(); + $preparedPath = Router::preparePath($route->getMatchedPath()); + $pathValues = $route->getPathValues($request, $preparedPath[0]); + $reqParams = $request->getParams(); + + $requestParams = []; + foreach ($route->getParams() as $key => $param) { + $requestParams[$key] = $pathValues[$key] ?? $reqParams[$key] ?? $param['default']; + } /** * Abuse labels From ad7a76abbb5d7866ff433ac06a23de3a54e6584d Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 16:07:27 +0100 Subject: [PATCH 2/7] Use injected 'params' local instead of getPathValues reconstruction The http server exposes the route's path values as an injectable 'params' local, so inject it directly rather than rebuilding them via Router::preparePath() + Route::getPathValues(). Query/body params and declared defaults are still merged in to reproduce the full resolved param set (path values take precedence), matching prior getParamsValues() behaviour. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/controllers/shared/api.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 7a36b012e3..c3db25ab72 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -38,7 +38,6 @@ use Utopia\Database\Validator\Authorization\Input; use Utopia\Database\Validator\Roles; use Utopia\Http\Http; use Utopia\Http\Route; -use Utopia\Http\Router; use Utopia\Span\Span; use Utopia\System\System; use Utopia\Telemetry\Adapter as Telemetry; @@ -787,6 +786,7 @@ Http::shutdown() Http::shutdown() ->groups(['api']) ->inject('route') + ->inject('params') ->inject('request') ->inject('response') ->inject('project') @@ -806,7 +806,7 @@ Http::shutdown() ->inject('bus') ->inject('apiKey') ->inject('mode') - ->action(function (Route $route, Request $request, Response $response, Document $project, User $user, Event $queueForEvents, AuditContext $auditContext, Audit $publisherForAudits, Context $usage, UsagePublisher $publisherForUsage, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, Realtime $queueForRealtime, Database $dbForProject, Authorization $authorization, callable $timelimit, EventProcessor $eventProcessor, Bus $bus, ?Key $apiKey, string $mode) use ($parseLabel) { + ->action(function (Route $route, array $params, Request $request, Response $response, Document $project, User $user, Event $queueForEvents, AuditContext $auditContext, Audit $publisherForAudits, Context $usage, UsagePublisher $publisherForUsage, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, Realtime $queueForRealtime, Database $dbForProject, Authorization $authorization, callable $timelimit, EventProcessor $eventProcessor, Bus $bus, ?Key $apiKey, string $mode) use ($parseLabel) { $responsePayload = $response->getPayload(); @@ -863,13 +863,14 @@ Http::shutdown() } } - $preparedPath = Router::preparePath($route->getMatchedPath()); - $pathValues = $route->getPathValues($request, $preparedPath[0]); + // $params holds the URL path values (injected from the route match); + // merge in query/body params and declared defaults to reproduce the + // full resolved param set, with path values taking precedence. $reqParams = $request->getParams(); $requestParams = []; foreach ($route->getParams() as $key => $param) { - $requestParams[$key] = $pathValues[$key] ?? $reqParams[$key] ?? $param['default']; + $requestParams[$key] = $params[$key] ?? $reqParams[$key] ?? $param['default']; } /** From 0bf406e597f2c9e02a226aa0a9e772ba40abacfc Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 16:18:00 +0100 Subject: [PATCH 3/7] Tighten param reconstruction Use Request::getParam() for the query/body+default fallback instead of a cached getParams() copy, dropping the temporary and shortening the diff. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/controllers/shared/api.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index c3db25ab72..d22c80d1b2 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -863,14 +863,11 @@ Http::shutdown() } } - // $params holds the URL path values (injected from the route match); - // merge in query/body params and declared defaults to reproduce the - // full resolved param set, with path values taking precedence. - $reqParams = $request->getParams(); - + // Reproduce the resolved param set: injected path values take precedence + // over query/body params, falling back to each param's declared default. $requestParams = []; foreach ($route->getParams() as $key => $param) { - $requestParams[$key] = $params[$key] ?? $reqParams[$key] ?? $param['default']; + $requestParams[$key] = $params[$key] ?? $request->getParam($key, $param['default']); } /** From cd0936c7214f953ff271f4f21e7ebef20b8596d7 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 16:42:39 +0100 Subject: [PATCH 4/7] Extract response caching into its own shutdown hook Move the cache.resource / cache.resourceType handling out of the large shared shutdown hook into a dedicated Http::shutdown() hook in the 'api' group. It injects only what caching needs (route, params, request, response, project, user, dbForProject, authorization) and guards with early returns, leaving the main hook focused on events, audit, and usage. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/controllers/shared/api.php | 131 +++++++++++++++++++-------------- 1 file changed, 76 insertions(+), 55 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index d22c80d1b2..4c947b5f4e 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -958,61 +958,6 @@ Http::shutdown() $publisherForAudits->enqueue(AuditMessage::fromContext($auditContext)); } - // Cache label - $useCache = $route->getLabel('cache', false); - if ($useCache) { - $resource = $resourceType = null; - $data = $response->getPayload(); - $statusCode = $response->getStatusCode(); - if (! empty($data['payload']) && $statusCode >= 200 && $statusCode < 300) { - $pattern = $route->getLabel('cache.resource', null); - if (! empty($pattern)) { - $resource = $parseLabel($pattern, $responsePayload, $requestParams, $user, $project); - } - - $pattern = $route->getLabel('cache.resourceType', null); - if (! empty($pattern)) { - $resourceType = $parseLabel($pattern, $responsePayload, $requestParams, $user, $project); - } - - $cache = new Cache( - new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId()) - ); - - $key = $request->cacheIdentifier(); - $signature = md5($data['payload']); - $cacheLog = $authorization->skip(fn () => $dbForProject->getDocument('cache', $key)); - $accessedAt = $cacheLog->getAttribute('accessedAt', 0); - $now = DateTime::now(); - if ($cacheLog->isEmpty()) { - try { - $authorization->skip(fn () => $dbForProject->createDocument('cache', new Document([ - '$id' => $key, - 'resource' => $resource, - 'resourceType' => $resourceType, - 'mimeType' => $response->getContentType(), - 'accessedAt' => $now, - 'signature' => $signature, - ]))); - } catch (DuplicateException) { - // Race condition: another concurrent request already created the cache document - $cacheLog = $authorization->skip(fn () => $dbForProject->getDocument('cache', $key)); - } - } elseif (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_CACHE_UPDATE)) > $accessedAt) { - $cacheLog->setAttribute('accessedAt', $now); - $authorization->skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), new Document([ - 'accessedAt' => $cacheLog->getAttribute('accessedAt') - ]))); - // Overwrite the file every APP_CACHE_UPDATE seconds to update the file modified time that is used in the TTL checks in cache->load() - $cache->save($key, $data['payload']); - } - - if ($signature !== $cacheLog->getAttribute('signature')) { - $cache->save($key, $data['payload']); - } - } - } - if ($project->getId() !== 'console') { if (! $user->isPrivileged($authorization->getRoles())) { $bus->dispatch(new RequestCompleted( @@ -1051,6 +996,82 @@ Http::shutdown() } }); +Http::shutdown() + ->groups(['api']) + ->inject('route') + ->inject('params') + ->inject('request') + ->inject('response') + ->inject('project') + ->inject('user') + ->inject('dbForProject') + ->inject('authorization') + ->action(function (Route $route, array $params, Request $request, Response $response, Document $project, User $user, Database $dbForProject, Authorization $authorization) use ($parseLabel) { + if (! $route->getLabel('cache', false)) { + return; + } + + $data = $response->getPayload(); + $statusCode = $response->getStatusCode(); + if (empty($data['payload']) || $statusCode < 200 || $statusCode >= 300) { + return; + } + + // Reproduce the resolved param set: injected path values take precedence + // over query/body params, falling back to each param's declared default. + $requestParams = []; + foreach ($route->getParams() as $key => $param) { + $requestParams[$key] = $params[$key] ?? $request->getParam($key, $param['default']); + } + + $resource = $resourceType = null; + $pattern = $route->getLabel('cache.resource', null); + if (! empty($pattern)) { + $resource = $parseLabel($pattern, $data, $requestParams, $user, $project); + } + + $pattern = $route->getLabel('cache.resourceType', null); + if (! empty($pattern)) { + $resourceType = $parseLabel($pattern, $data, $requestParams, $user, $project); + } + + $cache = new Cache( + new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId()) + ); + + $key = $request->cacheIdentifier(); + $signature = md5($data['payload']); + $cacheLog = $authorization->skip(fn () => $dbForProject->getDocument('cache', $key)); + $accessedAt = $cacheLog->getAttribute('accessedAt', 0); + $now = DateTime::now(); + if ($cacheLog->isEmpty()) { + try { + $authorization->skip(fn () => $dbForProject->createDocument('cache', new Document([ + '$id' => $key, + 'resource' => $resource, + 'resourceType' => $resourceType, + 'mimeType' => $response->getContentType(), + 'accessedAt' => $now, + 'signature' => $signature, + ]))); + } catch (DuplicateException) { + // Race condition: another concurrent request already created the cache document + $cacheLog = $authorization->skip(fn () => $dbForProject->getDocument('cache', $key)); + } + } elseif (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_CACHE_UPDATE)) > $accessedAt) { + $cacheLog->setAttribute('accessedAt', $now); + $authorization->skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), new Document([ + 'accessedAt' => $cacheLog->getAttribute('accessedAt') + ]))); + // Overwrite the file every APP_CACHE_UPDATE seconds to update the file modified time that is used in the TTL checks in cache->load() + $cache->save($key, $data['payload']); + } + + if ($signature !== $cacheLog->getAttribute('signature')) { + $cache->save($key, $data['payload']); + } + }); + Http::init() ->groups(['usage']) ->action(function () { From c9059c75a47dda64150a46529eae12d5ca1fd6b7 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 16:46:17 +0100 Subject: [PATCH 5/7] Use injected 'params' directly for audit resource labels audits.resource labels only reference URL path params (resource IDs), so the audit code can use the injected 'params' local directly instead of reconstructing the full resolved param set. The path+query/body+default reconstruction now lives only in the cache hook, where the screenshot cache.resource label genuinely needs query params. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/controllers/shared/api.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 4c947b5f4e..7c7e7f2555 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -863,13 +863,6 @@ Http::shutdown() } } - // Reproduce the resolved param set: injected path values take precedence - // over query/body params, falling back to each param's declared default. - $requestParams = []; - foreach ($route->getParams() as $key => $param) { - $requestParams[$key] = $params[$key] ?? $request->getParam($key, $param['default']); - } - /** * Abuse labels */ @@ -910,7 +903,7 @@ Http::shutdown() */ $pattern = $route->getLabel('audits.resource', null); if (! empty($pattern)) { - $resource = $parseLabel($pattern, $responsePayload, $requestParams, $user, $project); + $resource = $parseLabel($pattern, $responsePayload, $params, $user, $project); if (! empty($resource) && $resource !== $pattern) { $auditContext->resource = $resource; } From 2e5a29c650993a7e7e6d85ee28666cd5d62ad6f7 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 16:56:46 +0100 Subject: [PATCH 6/7] Extract request-completed/usage into its own shutdown hook; tighten cache hook Move the RequestCompleted dispatch and usage-metrics publishing out of the large shared shutdown hook into a dedicated Http::shutdown() hook that injects only what it needs (request, response, project, user, usage, publisherForUsage, authorization, bus, apiKey). Drop the now-unused usage/publisherForUsage/bus/apiKey/authorization injections from the main hook. Registered after the cache hook so cache writes keep happening before usage publishing, as in the original single-hook ordering. Also tighten the cache hook with guard clauses: hoist the cache/key/ signature setup, make the create path save-and-return explicitly instead of relying on the empty-document signature mismatch, resolve the resource labels lazily inside the create branch (so the param reconstruction and label parsing only run on a cache miss, not on every cached hit), and collapse the trailing save into the existing-entry path. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/controllers/shared/api.php | 156 +++++++++++++++++---------------- 1 file changed, 81 insertions(+), 75 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 7c7e7f2555..bc7b870cbc 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -794,19 +794,14 @@ Http::shutdown() ->inject('queueForEvents') ->inject('auditContext') ->inject('publisherForAudits') - ->inject('usage') - ->inject('publisherForUsage') ->inject('publisherForFunctions') ->inject('queueForWebhooks') ->inject('queueForRealtime') ->inject('dbForProject') - ->inject('authorization') ->inject('timelimit') ->inject('eventProcessor') - ->inject('bus') - ->inject('apiKey') ->inject('mode') - ->action(function (Route $route, array $params, Request $request, Response $response, Document $project, User $user, Event $queueForEvents, AuditContext $auditContext, Audit $publisherForAudits, Context $usage, UsagePublisher $publisherForUsage, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, Realtime $queueForRealtime, Database $dbForProject, Authorization $authorization, callable $timelimit, EventProcessor $eventProcessor, Bus $bus, ?Key $apiKey, string $mode) use ($parseLabel) { + ->action(function (Route $route, array $params, Request $request, Response $response, Document $project, User $user, Event $queueForEvents, AuditContext $auditContext, Audit $publisherForAudits, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, Realtime $queueForRealtime, Database $dbForProject, callable $timelimit, EventProcessor $eventProcessor, string $mode) use ($parseLabel) { $responsePayload = $response->getPayload(); @@ -950,43 +945,6 @@ Http::shutdown() $publisherForAudits->enqueue(AuditMessage::fromContext($auditContext)); } - - if ($project->getId() !== 'console') { - if (! $user->isPrivileged($authorization->getRoles())) { - $bus->dispatch(new RequestCompleted( - project: $project->getArrayCopy(), - request: $request, - response: $response, - )); - } - - // Publish usage metrics if context has data - if (! $usage->isEmpty()) { - $metrics = $usage->getMetrics(); - - // Filter out API key disabled metrics using suffix pattern matching - $disabledMetrics = $apiKey?->getDisabledMetrics() ?? []; - if (! empty($disabledMetrics)) { - $metrics = array_values(array_filter($metrics, function ($metric) use ($disabledMetrics) { - foreach ($disabledMetrics as $pattern) { - if (str_ends_with($metric['key'], $pattern)) { - return false; - } - } - - return true; - })); - } - - $message = new UsageMessage( - project: $project, - metrics: $metrics, - reduce: $usage->getReduce() - ); - - $publisherForUsage->enqueue($message); - } - } }); Http::shutdown() @@ -1010,61 +968,109 @@ Http::shutdown() return; } - // Reproduce the resolved param set: injected path values take precedence - // over query/body params, falling back to each param's declared default. - $requestParams = []; - foreach ($route->getParams() as $key => $param) { - $requestParams[$key] = $params[$key] ?? $request->getParam($key, $param['default']); - } - - $resource = $resourceType = null; - $pattern = $route->getLabel('cache.resource', null); - if (! empty($pattern)) { - $resource = $parseLabel($pattern, $data, $requestParams, $user, $project); - } - - $pattern = $route->getLabel('cache.resourceType', null); - if (! empty($pattern)) { - $resourceType = $parseLabel($pattern, $data, $requestParams, $user, $project); - } - $cache = new Cache( new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId()) ); - $key = $request->cacheIdentifier(); $signature = md5($data['payload']); - $cacheLog = $authorization->skip(fn () => $dbForProject->getDocument('cache', $key)); - $accessedAt = $cacheLog->getAttribute('accessedAt', 0); $now = DateTime::now(); + $cacheLog = $authorization->skip(fn () => $dbForProject->getDocument('cache', $key)); + + // First request for this resource: record it and persist the payload. if ($cacheLog->isEmpty()) { + // Resolve resource labels lazily — only needed when creating the entry. + $requestParams = []; + foreach ($route->getParams() as $paramKey => $param) { + $requestParams[$paramKey] = $params[$paramKey] ?? $request->getParam($paramKey, $param['default']); + } + + $resourcePattern = $route->getLabel('cache.resource', null); + $resourceTypePattern = $route->getLabel('cache.resourceType', null); + try { $authorization->skip(fn () => $dbForProject->createDocument('cache', new Document([ '$id' => $key, - 'resource' => $resource, - 'resourceType' => $resourceType, + 'resource' => empty($resourcePattern) ? null : $parseLabel($resourcePattern, $data, $requestParams, $user, $project), + 'resourceType' => empty($resourceTypePattern) ? null : $parseLabel($resourceTypePattern, $data, $requestParams, $user, $project), 'mimeType' => $response->getContentType(), 'accessedAt' => $now, 'signature' => $signature, ]))); + $cache->save($key, $data['payload']); + + return; } catch (DuplicateException) { - // Race condition: another concurrent request already created the cache document + // Race condition: another concurrent request already created the entry. $cacheLog = $authorization->skip(fn () => $dbForProject->getDocument('cache', $key)); } - } elseif (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_CACHE_UPDATE)) > $accessedAt) { - $cacheLog->setAttribute('accessedAt', $now); - $authorization->skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), new Document([ - 'accessedAt' => $cacheLog->getAttribute('accessedAt') - ]))); - // Overwrite the file every APP_CACHE_UPDATE seconds to update the file modified time that is used in the TTL checks in cache->load() - $cache->save($key, $data['payload']); } - if ($signature !== $cacheLog->getAttribute('signature')) { + // Existing entry: refresh the access time once per APP_CACHE_UPDATE window + // (keeps the file mtime current for TTL checks) and re-persist on content change. + $stale = DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_CACHE_UPDATE)) > $cacheLog->getAttribute('accessedAt', 0); + if ($stale) { + $authorization->skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), new Document([ + 'accessedAt' => $now, + ]))); + } + + if ($stale || $signature !== $cacheLog->getAttribute('signature')) { $cache->save($key, $data['payload']); } }); +Http::shutdown() + ->groups(['api']) + ->inject('request') + ->inject('response') + ->inject('project') + ->inject('user') + ->inject('usage') + ->inject('publisherForUsage') + ->inject('authorization') + ->inject('bus') + ->inject('apiKey') + ->action(function (Request $request, Response $response, Document $project, User $user, Context $usage, UsagePublisher $publisherForUsage, Authorization $authorization, Bus $bus, ?Key $apiKey) { + if ($project->getId() === 'console') { + return; + } + + if (! $user->isPrivileged($authorization->getRoles())) { + $bus->dispatch(new RequestCompleted( + project: $project->getArrayCopy(), + request: $request, + response: $response, + )); + } + + // Publish usage metrics if context has data + if (! $usage->isEmpty()) { + $metrics = $usage->getMetrics(); + + // Filter out API key disabled metrics using suffix pattern matching + $disabledMetrics = $apiKey?->getDisabledMetrics() ?? []; + if (! empty($disabledMetrics)) { + $metrics = array_values(array_filter($metrics, function ($metric) use ($disabledMetrics) { + foreach ($disabledMetrics as $pattern) { + if (str_ends_with($metric['key'], $pattern)) { + return false; + } + } + + return true; + })); + } + + $message = new UsageMessage( + project: $project, + metrics: $metrics, + reduce: $usage->getReduce() + ); + + $publisherForUsage->enqueue($message); + } + }); + Http::init() ->groups(['usage']) ->action(function () { From f0192c6561cb73a5ddfd974d66f9b05c7a641761 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 1 Jun 2026 17:15:29 +0100 Subject: [PATCH 7/7] Split abuse and audit flows into their own shutdown hooks Break the remaining shared 'api' shutdown hook into focused hooks: one for event/realtime/function/webhook dispatch, one for abuse-reset handling, and one for audit logging. Each injects only what it uses, so the wide shared injection list is gone. Registered in the original execution order (events -> abuse -> audit -> cache -> usage) so behaviour is unchanged; the pre-existing 'session' group shutdown hook is unaffected. Applied early-return guards to the events and abuse hooks to flatten their nesting. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/controllers/shared/api.php | 213 +++++++++++++++++---------------- 1 file changed, 113 insertions(+), 100 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index bc7b870cbc..db1bf99edb 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -783,6 +783,118 @@ Http::shutdown() $dbForProject->purgeCachedDocument('users', $userId); }); +Http::shutdown() + ->groups(['api']) + ->inject('route') + ->inject('response') + ->inject('project') + ->inject('queueForEvents') + ->inject('publisherForFunctions') + ->inject('queueForWebhooks') + ->inject('queueForRealtime') + ->inject('dbForProject') + ->inject('eventProcessor') + ->action(function (Route $route, Response $response, Document $project, Event $queueForEvents, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, Realtime $queueForRealtime, Database $dbForProject, EventProcessor $eventProcessor) { + if (empty($queueForEvents->getEvent())) { + return; + } + + if (empty($queueForEvents->getPayload())) { + $queueForEvents->setPayload($response->getPayload()); + } + + // Get project and function/webhook events (cached) + $functionsEvents = $eventProcessor->getFunctionsEvents($project, $dbForProject); + $webhooksEvents = $eventProcessor->getWebhooksEvents($project); + + // Generate events for this operation + $generatedEvents = Event::generateEvents( + $queueForEvents->getEvent(), + $queueForEvents->getParams() + ); + + $allowedOnConsole = !empty(\array_intersect($route->getGroups(), Realtime::CONSOLE_ALLOWLIST)); + if ($project->getId() !== 'console' || $allowedOnConsole) { + $queueForRealtime + ->from($queueForEvents) + ->trigger(); + } + + // Only trigger functions if there are matching function events + if (! empty($functionsEvents)) { + foreach ($generatedEvents as $event) { + if (isset($functionsEvents[$event])) { + $publisherForFunctions->enqueue(FunctionMessage::fromEvent( + event: $queueForEvents->getEvent(), + params: $queueForEvents->getParams(), + project: $queueForEvents->getProject(), + user: $queueForEvents->getUser(), + userId: $queueForEvents->getUserId(), + payload: $queueForEvents->getPayload(), + platform: $queueForEvents->getPlatform(), + )); + break; + } + } + } + + // Only trigger webhooks if there are matching webhook events + if (! empty($webhooksEvents)) { + foreach ($generatedEvents as $event) { + if (isset($webhooksEvents[$event])) { + $queueForWebhooks + ->from($queueForEvents) + ->trigger(); + break; + } + } + } + }); + +Http::shutdown() + ->groups(['api']) + ->inject('route') + ->inject('request') + ->inject('response') + ->inject('project') + ->inject('user') + ->inject('timelimit') + ->action(function (Route $route, Request $request, Response $response, Document $project, User $user, callable $timelimit) { + $abuseEnabled = System::getEnv('_APP_OPTIONS_ABUSE', 'enabled') !== 'disabled'; + $abuseResetCode = $route->getLabel('abuse-reset', []); + $abuseResetCode = \is_array($abuseResetCode) ? $abuseResetCode : [$abuseResetCode]; + + if (! $abuseEnabled || \count($abuseResetCode) === 0 || ! \in_array($response->getStatusCode(), $abuseResetCode)) { + return; + } + + $abuseKeyLabel = $route->getLabel('abuse-key', 'url:{url},ip:{ip}'); + $abuseKeyLabel = (! is_array($abuseKeyLabel)) ? [$abuseKeyLabel] : $abuseKeyLabel; + + foreach ($abuseKeyLabel as $abuseKey) { + $start = $request->getContentRangeStart(); + $end = $request->getContentRangeEnd(); + $timeLimit = $timelimit($abuseKey, $route->getLabel('abuse-limit', 0), $route->getLabel('abuse-time', 3600)); + $timeLimit + ->setParam('{projectId}', $project->getId()) + ->setParam('{userId}', $user->getId()) + ->setParam('{userAgent}', $request->getUserAgent('')) + ->setParam('{ip}', $request->getIP()) + ->setParam('{url}', $request->getHostname() . $route->getPath()) + ->setParam('{method}', $request->getMethod()) + ->setParam('{chunkId}', (int) ($start / ($end + 1 - $start))); + + 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); + } + } + + $abuse = new Abuse($timeLimit); + $abuse->reset(); + } + }); + Http::shutdown() ->groups(['api']) ->inject('route') @@ -791,111 +903,12 @@ Http::shutdown() ->inject('response') ->inject('project') ->inject('user') - ->inject('queueForEvents') ->inject('auditContext') ->inject('publisherForAudits') - ->inject('publisherForFunctions') - ->inject('queueForWebhooks') - ->inject('queueForRealtime') - ->inject('dbForProject') - ->inject('timelimit') - ->inject('eventProcessor') ->inject('mode') - ->action(function (Route $route, array $params, Request $request, Response $response, Document $project, User $user, Event $queueForEvents, AuditContext $auditContext, Audit $publisherForAudits, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, Realtime $queueForRealtime, Database $dbForProject, callable $timelimit, EventProcessor $eventProcessor, string $mode) use ($parseLabel) { - + ->action(function (Route $route, array $params, Request $request, Response $response, Document $project, User $user, AuditContext $auditContext, Audit $publisherForAudits, string $mode) use ($parseLabel) { $responsePayload = $response->getPayload(); - if (! empty($queueForEvents->getEvent())) { - if (empty($queueForEvents->getPayload())) { - $queueForEvents->setPayload($responsePayload); - } - - // Get project and function/webhook events (cached) - $functionsEvents = $eventProcessor->getFunctionsEvents($project, $dbForProject); - $webhooksEvents = $eventProcessor->getWebhooksEvents($project); - - // Generate events for this operation - $generatedEvents = Event::generateEvents( - $queueForEvents->getEvent(), - $queueForEvents->getParams() - ); - - $allowedOnConsole = !empty(\array_intersect($route->getGroups(), Realtime::CONSOLE_ALLOWLIST)); - if ($project->getId() !== 'console' || $allowedOnConsole) { - $queueForRealtime - ->from($queueForEvents) - ->trigger(); - } - - // Only trigger functions if there are matching function events - if (! empty($functionsEvents)) { - foreach ($generatedEvents as $event) { - if (isset($functionsEvents[$event])) { - $publisherForFunctions->enqueue(FunctionMessage::fromEvent( - event: $queueForEvents->getEvent(), - params: $queueForEvents->getParams(), - project: $queueForEvents->getProject(), - user: $queueForEvents->getUser(), - userId: $queueForEvents->getUserId(), - payload: $queueForEvents->getPayload(), - platform: $queueForEvents->getPlatform(), - )); - break; - } - } - } - - // Only trigger webhooks if there are matching webhook events - if (! empty($webhooksEvents)) { - foreach ($generatedEvents as $event) { - if (isset($webhooksEvents[$event])) { - $queueForWebhooks - ->from($queueForEvents) - ->trigger(); - break; - } - } - } - } - - /** - * Abuse labels - */ - $abuseEnabled = System::getEnv('_APP_OPTIONS_ABUSE', 'enabled') !== 'disabled'; - $abuseResetCode = $route->getLabel('abuse-reset', []); - $abuseResetCode = \is_array($abuseResetCode) ? $abuseResetCode : [$abuseResetCode]; - - if ($abuseEnabled && \count($abuseResetCode) > 0 && \in_array($response->getStatusCode(), $abuseResetCode)) { - $abuseKeyLabel = $route->getLabel('abuse-key', 'url:{url},ip:{ip}'); - $abuseKeyLabel = (! is_array($abuseKeyLabel)) ? [$abuseKeyLabel] : $abuseKeyLabel; - - foreach ($abuseKeyLabel as $abuseKey) { - $start = $request->getContentRangeStart(); - $end = $request->getContentRangeEnd(); - $timeLimit = $timelimit($abuseKey, $route->getLabel('abuse-limit', 0), $route->getLabel('abuse-time', 3600)); - $timeLimit - ->setParam('{projectId}', $project->getId()) - ->setParam('{userId}', $user->getId()) - ->setParam('{userAgent}', $request->getUserAgent('')) - ->setParam('{ip}', $request->getIP()) - ->setParam('{url}', $request->getHostname() . $route->getPath()) - ->setParam('{method}', $request->getMethod()) - ->setParam('{chunkId}', (int) ($start / ($end + 1 - $start))); - - 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); - } - } - - $abuse = new Abuse($timeLimit); - $abuse->reset(); - } - } - - /** - * Audit labels - */ $pattern = $route->getLabel('audits.resource', null); if (! empty($pattern)) { $resource = $parseLabel($pattern, $responsePayload, $params, $user, $project);