Merge branch 'main' of https://github.com/appwrite/appwrite into feat-storage-virtual-folders

This commit is contained in:
Torsten Dittmann committed 2026-07-07 18:28:41 +04:00
commit fe4b1d6c2f
48 files changed
+1769 -300

No files matched your search

+2
View File
@@ -142,6 +142,8 @@ _APP_MIGRATION_HOST=appwrite
_APP_MIGRATIONS_FIREBASE_CLIENT_ID=
_APP_MIGRATIONS_FIREBASE_CLIENT_SECRET=
_APP_PROJECT_REGIONS=default
_APP_GEO_SECRET=your-secret-key
_APP_GEO_ENDPOINT=http://appwrite-geo/v1
_APP_VCS_GITHUB_APP_ID=
_APP_VCS_GITHUB_APP_NAME=
_APP_VCS_GITHUB_CLIENT_ID=
+16 -8
View File
@@ -8,6 +8,7 @@ env:
COMPOSE_FILE: docker-compose.yml:docker-compose.override.yml
IMAGE: appwrite-dev
K6_VERSION: '0.53.0'
DOCKERHUB_AUTH_AVAILABLE: ${{ secrets.DOCKERHUB_TOKEN != '' }}
on:
pull_request:
@@ -188,8 +189,8 @@ jobs:
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const runner4 = `runs-on=${context.runId}/runner=4cpu-linux-x64/volume=60g/spot=price-capacity-optimized/extras=otel+ecr-cache`;
const runner8 = `runs-on=${context.runId}/runner=8cpu-linux-x64/volume=60g/spot=price-capacity-optimized/extras=otel+ecr-cache`;
const runner4 = `runs-on=${context.runId}/runner=4cpu-linux-x64/volume=60g/spot=price-capacity-optimized/retry=when-interrupted/extras=otel+ecr-cache`;
const runner8 = `runs-on=${context.runId}/runner=8cpu-linux-x64/volume=60g/spot=price-capacity-optimized/retry=when-interrupted/extras=otel+ecr-cache`;
const services = [
{ name: 'Account', runner: runner4, functional: true, processes: null, timeout: 8 },
@@ -256,7 +257,7 @@ jobs:
build:
name: Build
runs-on: runs-on=${{ github.run_id }}/runner=4cpu-linux-x64/volume=60g/spot=price-capacity-optimized/extras=otel+s3-cache+ecr-cache
runs-on: runs-on=${{ github.run_id }}/runner=4cpu-linux-x64/volume=60g/spot=price-capacity-optimized/retry=when-interrupted/extras=otel+s3-cache+ecr-cache
timeout-minutes: 30
steps:
- name: Checkout repository
@@ -265,6 +266,7 @@ jobs:
submodules: recursive
- name: Login to Docker Hub
if: ${{ env.DOCKERHUB_AUTH_AVAILABLE == 'true' }}
uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
@@ -289,7 +291,7 @@ jobs:
unit:
name: Tests / Unit
runs-on: runs-on=${{ github.run_id }}/runner=4cpu-linux-x64/volume=60g/spot=price-capacity-optimized/extras=otel+ecr-cache
runs-on: runs-on=${{ github.run_id }}/runner=4cpu-linux-x64/volume=60g/spot=price-capacity-optimized/retry=when-interrupted/extras=otel+ecr-cache
timeout-minutes: 15
needs: build
permissions:
@@ -301,6 +303,7 @@ jobs:
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Login to Docker Hub
if: ${{ env.DOCKERHUB_AUTH_AVAILABLE == 'true' }}
uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
@@ -353,7 +356,7 @@ jobs:
e2e_general:
name: Tests / E2E / General
runs-on: runs-on=${{ github.run_id }}/runner=4cpu-linux-x64/volume=60g/spot=price-capacity-optimized/extras=otel+ecr-cache
runs-on: runs-on=${{ github.run_id }}/runner=4cpu-linux-x64/volume=60g/spot=price-capacity-optimized/retry=when-interrupted/extras=otel+ecr-cache
timeout-minutes: 15
needs: build
permissions:
@@ -365,6 +368,7 @@ jobs:
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Login to Docker Hub
if: ${{ env.DOCKERHUB_AUTH_AVAILABLE == 'true' }}
uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
@@ -467,6 +471,7 @@ jobs:
fi
- name: Login to Docker Hub
if: ${{ env.DOCKERHUB_AUTH_AVAILABLE == 'true' }}
uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
@@ -549,7 +554,7 @@ jobs:
e2e_abuse:
name: Tests / E2E / Abuse (${{ matrix.mode }})
runs-on: runs-on=${{ github.run_id }}/runner=4cpu-linux-x64/volume=60g/spot=price-capacity-optimized/extras=otel+ecr-cache
runs-on: runs-on=${{ github.run_id }}/runner=4cpu-linux-x64/volume=60g/spot=price-capacity-optimized/retry=when-interrupted/extras=otel+ecr-cache
timeout-minutes: 15
needs: [build, matrix]
permissions:
@@ -567,6 +572,7 @@ jobs:
fetch-depth: 1
- name: Login to Docker Hub
if: ${{ env.DOCKERHUB_AUTH_AVAILABLE == 'true' }}
uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
@@ -627,7 +633,7 @@ jobs:
e2e_screenshots:
name: Tests / E2E / Screenshots (${{ matrix.mode }})
runs-on: runs-on=${{ github.run_id }}/runner=4cpu-linux-x64/volume=60g/spot=price-capacity-optimized/extras=otel+ecr-cache
runs-on: runs-on=${{ github.run_id }}/runner=4cpu-linux-x64/volume=60g/spot=price-capacity-optimized/retry=when-interrupted/extras=otel+ecr-cache
timeout-minutes: 15
needs: [build, matrix]
permissions:
@@ -643,6 +649,7 @@ jobs:
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Login to Docker Hub
if: ${{ env.DOCKERHUB_AUTH_AVAILABLE == 'true' }}
uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
@@ -711,7 +718,7 @@ jobs:
benchmark:
name: Benchmark
if: github.event_name == 'pull_request' || github.event_name == 'push'
runs-on: runs-on=${{ github.run_id }}/runner=4cpu-linux-x64/volume=60g/spot=price-capacity-optimized/extras=otel+ecr-cache
runs-on: runs-on=${{ github.run_id }}/runner=4cpu-linux-x64/volume=60g/spot=price-capacity-optimized/retry=when-interrupted/extras=otel+ecr-cache
timeout-minutes: 20
needs: build
permissions:
@@ -727,6 +734,7 @@ jobs:
fetch-depth: 1
- name: Login to Docker Hub
if: ${{ env.DOCKERHUB_AUTH_AVAILABLE == 'true' }}
uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
+1 -1
View File
@@ -12,7 +12,7 @@ RUN composer install --ignore-platform-reqs --optimize-autoloader \
--no-plugins --no-scripts --prefer-dist \
`if [ "$TESTING" != "true" ]; then echo "--no-dev"; fi`
FROM appwrite/base:1.4.3 AS base
FROM appwrite/base:1.4.4 AS base
LABEL maintainer="team@appwrite.io"
Binary file not shown.
+132
View File
@@ -892,6 +892,138 @@ return [
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('continentCode'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => 2,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('latitude'),
'type' => Database::VAR_FLOAT,
'format' => '',
'size' => 8,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('longitude'),
'type' => Database::VAR_FLOAT,
'format' => '',
'size' => 8,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('timeZone'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => 255,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('weatherCode'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => 255,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('postalCode'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => 255,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('autonomousSystemNumber'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => 255,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('autonomousSystemOrganization'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => 255,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('connectionType'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => 255,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('connectionUsageType'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => 255,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('connectionOrganization'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => 255,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('isp'),
'type' => Database::VAR_STRING,
'format' => '',
'size' => 255,
'signed' => true,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
],
[
'$id' => ID::custom('osCode'),
'type' => Database::VAR_STRING,
+10
View File
@@ -54,6 +54,11 @@ return [
'description' => 'Rate limit for the current endpoint has been exceeded. Please try again after some time.',
'code' => 429,
],
Exception::GENERAL_RESOURCE_LOCKED => [
'name' => Exception::GENERAL_RESOURCE_LOCKED,
'description' => 'The requested resource is currently being modified by another request. Please retry after a brief delay.',
'code' => 409,
],
Exception::GENERAL_SMTP_DISABLED => [
'name' => Exception::GENERAL_SMTP_DISABLED,
'description' => 'SMTP is disabled on your Appwrite instance. You can <a href="/docs/email-delivery">learn more about setting up SMTP</a> in our docs.',
@@ -512,6 +517,11 @@ return [
'description' => 'The given file extension is not supported.',
'code' => 400,
],
Exception::STORAGE_IMAGE_RESOLUTION_EXCEEDED => [
'name' => Exception::STORAGE_IMAGE_RESOLUTION_EXCEEDED,
'description' => 'The image resolution exceeds the maximum allowed for processing.',
'code' => 400,
],
Exception::STORAGE_INVALID_FILE_SIZE => [
'name' => Exception::STORAGE_INVALID_FILE_SIZE,
'description' => 'The file size is either not valid or exceeds the maximum allowed size. Please check the file or the value of the _APP_STORAGE_LIMIT environment variable.',
+14
View File
@@ -323,6 +323,20 @@ return [
'icon' => '/images/services/messaging.png',
'platforms' => ['client', 'server', 'console'],
],
'notifications' => [
'key' => 'notifications',
'name' => 'Notifications',
'subtitle' => 'The Notifications service allows you to read and manage your Appwrite Console notifications.',
'description' => '/docs/services/notifications.md',
'controller' => '', // Uses modules
'sdk' => true,
'docs' => true,
'docsUrl' => '',
'tests' => true,
'optional' => false,
'icon' => '',
'platforms' => ['client', 'server', 'console'],
],
'advisor' => [
'key' => 'advisor',
'name' => 'Advisor',
+27
View File
@@ -43,6 +43,15 @@ return [
'question' => '',
'filter' => ''
],
[
'name' => '_APP_LOCKING_ENABLED',
'description' => 'Enable distributed locking for platform writes. Locks coordinate concurrent updates across API pods so read-modify-write operations on shared documents do not lose updates. By default, set to \'enabled\'. Set to \'disabled\' as an emergency kill switch; locks become no-ops and concurrent writes will race.',
'introduction' => '1.9.3',
'default' => 'enabled',
'required' => false,
'question' => '',
'filter' => ''
],
[
'name' => '_APP_OPTIONS_FORCE_HTTPS',
'description' => 'Allows you to force HTTPS connection to your API. This feature redirects any HTTP call to HTTPS and adds the \'Strict-Transport-Security\' header to all HTTP responses. By default, set to \'enabled\'. To disable, set to \'disabled\'. This feature will work only when your ports are set to default 80 and 443, and you have set up wildcard certificates with DNS challenge.',
@@ -348,6 +357,24 @@ return [
'required' => false,
'question' => '',
'filter' => ''
],
[
'name' => '_APP_GEO_ENDPOINT',
'description' => 'Internal endpoint of the geo service used to resolve IP geolocation for locale and session enrichment. Leave empty to disable geolocation lookups. Defaults to the bundled `appwrite-geo` container.',
'introduction' => 'TBD',
'default' => 'http://appwrite-geo/v1',
'required' => false,
'question' => '',
'filter' => ''
],
[
'name' => '_APP_GEO_SECRET',
'description' => 'Bearer token used to authenticate requests from the Appwrite server to the geo service. Must match the `GEO_SECRET` configured on the `appwrite-geo` container. Change it from the default value before running in production.',
'introduction' => 'TBD',
'default' => 'your-secret-key',
'required' => false,
'question' => '',
'filter' => 'token'
]
],
],
+81 -30
View File
@@ -21,6 +21,7 @@ use Appwrite\Event\Publisher\Mail as MailPublisher;
use Appwrite\Event\Publisher\Messaging as MessagingPublisher;
use Appwrite\Extend\Exception;
use Appwrite\Hooks\Hooks;
use Appwrite\Locale\GeoRecord;
use Appwrite\Network\Validator\Redirect;
use Appwrite\OpenSSL\OpenSSL;
use Appwrite\SDK\AuthType;
@@ -40,7 +41,6 @@ use Appwrite\Utopia\Request;
use Appwrite\Utopia\Response;
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumberUtil;
use MaxMind\Db\Reader;
use Utopia\Auth\Hashes\Sha;
use Utopia\Auth\Proofs\Code as ProofsCode;
use Utopia\Auth\Proofs\Password as ProofsPassword;
@@ -80,8 +80,7 @@ use Utopia\Validator\WhiteList;
$oauthDefaultSuccess = '/console/auth/oauth2/success';
$oauthDefaultFailure = '/console/auth/oauth2/failure';
$createSession = function (string $userId, string $secret, Request $request, Response $response, User $user, Database $dbForProject, Document $project, array $platform, Locale $locale, Reader $geodb, Event $queueForEvents, Bus $bus, Store $store, ProofsToken $proofForToken, ProofsCode $proofForCode, bool $domainVerification, ?string $cookieDomain, Authorization $authorization) {
$createSession = function (string $userId, string $secret, Request $request, Response $response, User $user, Database $dbForProject, Document $project, array $platform, Locale $locale, GeoRecord $geoRecord, Event $queueForEvents, Bus $bus, Store $store, ProofsToken $proofForToken, ProofsCode $proofForCode, bool $domainVerification, ?string $cookieDomain, Authorization $authorization) {
// Attempt to decode secret as a JWT (used by OAuth2 token flow to carry provider info)
$oauthProvider = null;
@@ -123,7 +122,6 @@ $createSession = function (string $userId, string $secret, Request $request, Res
$duration = $project->getAttribute('auths', [])['duration'] ?? TOKEN_EXPIRATION_LOGIN_LONG;
$detector = new Detector($request->getUserAgent('UNKNOWN'));
$record = $geodb->get($request->getIP());
$sessionSecret = $proofForToken->generate();
$factor = (match ($verifiedToken->getAttribute('type')) {
@@ -171,7 +169,19 @@ $createSession = function (string $userId, string $secret, Request $request, Res
'userAgent' => $request->getUserAgent('UNKNOWN'),
'ip' => $request->getIP(),
'factors' => [$factor],
'countryCode' => ($record) ? \strtolower($record['country']['iso_code']) : '--',
'countryCode' => \strtolower($geoRecord->getCountryCode()),
'continentCode' => $geoRecord->getContinentCode() === '--' ? null : $geoRecord->getContinentCode(),
'latitude' => $geoRecord->getLatitude(),
'longitude' => $geoRecord->getLongitude(),
'timeZone' => $geoRecord->getTimeZone(),
'weatherCode' => $geoRecord->getWeatherCode(),
'postalCode' => $geoRecord->getPostalCode(),
'autonomousSystemNumber' => $geoRecord->getAutonomousSystemNumber(),
'autonomousSystemOrganization' => $geoRecord->getAutonomousSystemOrganization(),
'connectionType' => $geoRecord->getConnectionType(),
'connectionUsageType' => $geoRecord->getConnectionUsageType(),
'connectionOrganization' => $geoRecord->getConnectionOrganization(),
'isp' => $geoRecord->getIsp(),
'expire' => DateTime::addSeconds(new \DateTime(), $duration)
],
$providerCredentials,
@@ -319,7 +329,8 @@ Http::post('/v1/account')
Query::equal('providerEmail', [$email]),
]);
if (!$identityWithMatchingEmail->isEmpty()) {
throw new Exception(Exception::GENERAL_BAD_REQUEST); /** Return a generic bad request to prevent exposing existing accounts */
throw new Exception(Exception::GENERAL_BAD_REQUEST);
/** Return a generic bad request to prevent exposing existing accounts */
}
if ($project->getAttribute('auths', [])['personalDataCheck'] ?? false) {
@@ -566,7 +577,8 @@ Http::get('/v1/account/sessions')
$sessions = $user->getAttribute('sessions', []);
$current = $user->sessionVerify($store->getProperty('secret', ''), $proofForToken);
foreach ($sessions as $key => $session) {/** @var Document $session */
foreach ($sessions as $key => $session) {
/** @var Document $session */
$countryName = $locale->getText('countries.' . strtolower($session->getAttribute('countryCode')), $locale->getText('locale.country.unknown'));
$session->setAttribute('countryName', $countryName);
@@ -621,7 +633,8 @@ Http::delete('/v1/account/sessions')
$sessions = $user->getAttribute('sessions', []);
$currentSession = null;
foreach ($sessions as $session) {/** @var Document $session */
foreach ($sessions as $session) {
/** @var Document $session */
$dbForProject->deleteDocument('sessions', $session->getId());
if (!$domainVerification) {
@@ -695,7 +708,8 @@ Http::get('/v1/account/sessions/:sessionId')
? $user->sessionVerify($store->getProperty('secret', ''), $proofForToken)
: $sessionId;
foreach ($sessions as $session) {/** @var Document $session */
foreach ($sessions as $session) {
/** @var Document $session */
if ($sessionId === $session->getId()) {
$countryName = $locale->getText('countries.' . strtolower($session->getAttribute('countryCode')), $locale->getText('locale.country.unknown'));
@@ -928,7 +942,7 @@ Http::post('/v1/account/sessions/email')
->inject('project')
->inject('platform')
->inject('locale')
->inject('geodb')
->inject('geoRecord')
->inject('queueForEvents')
->inject('bus')
->inject('hooks')
@@ -938,7 +952,7 @@ Http::post('/v1/account/sessions/email')
->inject('domainVerification')
->inject('cookieDomain')
->inject('authorization')
->action(function (string $email, string $password, Request $request, Response $response, User $user, Database $dbForProject, Document $project, array $platform, Locale $locale, Reader $geodb, Event $queueForEvents, Bus $bus, Hooks $hooks, Store $store, ProofsPassword $proofForPassword, ProofsToken $proofForToken, bool $domainVerification, ?string $cookieDomain, Authorization $authorization) {
->action(function (string $email, string $password, Request $request, Response $response, User $user, Database $dbForProject, Document $project, array $platform, Locale $locale, GeoRecord $geoRecord, Event $queueForEvents, Bus $bus, Hooks $hooks, Store $store, ProofsPassword $proofForPassword, ProofsToken $proofForToken, bool $domainVerification, ?string $cookieDomain, Authorization $authorization) {
$email = \strtolower($email);
$protocol = $request->getProtocol();
@@ -962,7 +976,6 @@ Http::post('/v1/account/sessions/email')
$duration = $project->getAttribute('auths', [])['duration'] ?? TOKEN_EXPIRATION_LOGIN_LONG;
$detector = new Detector($request->getUserAgent('UNKNOWN'));
$record = $geodb->get($request->getIP());
$secret = $proofForToken->generate();
$session = new Document(array_merge(
[
@@ -975,7 +988,19 @@ Http::post('/v1/account/sessions/email')
'userAgent' => $request->getUserAgent('UNKNOWN'),
'ip' => $request->getIP(),
'factors' => ['password'],
'countryCode' => ($record) ? \strtolower($record['country']['iso_code']) : '--',
'countryCode' => \strtolower($geoRecord->getCountryCode()),
'continentCode' => $geoRecord->getContinentCode() === '--' ? null : $geoRecord->getContinentCode(),
'latitude' => $geoRecord->getLatitude(),
'longitude' => $geoRecord->getLongitude(),
'timeZone' => $geoRecord->getTimeZone(),
'weatherCode' => $geoRecord->getWeatherCode(),
'postalCode' => $geoRecord->getPostalCode(),
'autonomousSystemNumber' => $geoRecord->getAutonomousSystemNumber(),
'autonomousSystemOrganization' => $geoRecord->getAutonomousSystemOrganization(),
'connectionType' => $geoRecord->getConnectionType(),
'connectionUsageType' => $geoRecord->getConnectionUsageType(),
'connectionOrganization' => $geoRecord->getConnectionOrganization(),
'isp' => $geoRecord->getIsp(),
'expire' => DateTime::addSeconds(new \DateTime(), $duration)
],
$detector->getOS(),
@@ -1078,7 +1103,7 @@ Http::post('/v1/account/sessions/anonymous')
->inject('user')
->inject('project')
->inject('dbForProject')
->inject('geodb')
->inject('geoRecord')
->inject('queueForEvents')
->inject('store')
->inject('proofForPassword')
@@ -1086,7 +1111,7 @@ Http::post('/v1/account/sessions/anonymous')
->inject('domainVerification')
->inject('cookieDomain')
->inject('authorization')
->action(function (Request $request, Response $response, Locale $locale, User $user, Document $project, Database $dbForProject, Reader $geodb, Event $queueForEvents, Store $store, ProofsPassword $proofForPassword, ProofsToken $proofForToken, bool $domainVerification, ?string $cookieDomain, Authorization $authorization) {
->action(function (Request $request, Response $response, Locale $locale, User $user, Document $project, Database $dbForProject, GeoRecord $geoRecord, Event $queueForEvents, Store $store, ProofsPassword $proofForPassword, ProofsToken $proofForToken, bool $domainVerification, ?string $cookieDomain, Authorization $authorization) {
$protocol = $request->getProtocol();
if ('console' === $project->getId()) {
@@ -1136,7 +1161,6 @@ Http::post('/v1/account/sessions/anonymous')
// Create session token
$duration = $project->getAttribute('auths', [])['duration'] ?? TOKEN_EXPIRATION_LOGIN_LONG;
$detector = new Detector($request->getUserAgent('UNKNOWN'));
$record = $geodb->get($request->getIP());
$secret = $proofForToken->generate();
$session = new Document(array_merge(
@@ -1149,7 +1173,19 @@ Http::post('/v1/account/sessions/anonymous')
'userAgent' => $request->getUserAgent('UNKNOWN'),
'ip' => $request->getIP(),
'factors' => ['anonymous'],
'countryCode' => ($record) ? \strtolower($record['country']['iso_code']) : '--',
'countryCode' => \strtolower($geoRecord->getCountryCode()),
'continentCode' => $geoRecord->getContinentCode() === '--' ? null : $geoRecord->getContinentCode(),
'latitude' => $geoRecord->getLatitude(),
'longitude' => $geoRecord->getLongitude(),
'timeZone' => $geoRecord->getTimeZone(),
'weatherCode' => $geoRecord->getWeatherCode(),
'postalCode' => $geoRecord->getPostalCode(),
'autonomousSystemNumber' => $geoRecord->getAutonomousSystemNumber(),
'autonomousSystemOrganization' => $geoRecord->getAutonomousSystemOrganization(),
'connectionType' => $geoRecord->getConnectionType(),
'connectionUsageType' => $geoRecord->getConnectionUsageType(),
'connectionOrganization' => $geoRecord->getConnectionOrganization(),
'isp' => $geoRecord->getIsp(),
'expire' => DateTime::addSeconds(new \DateTime(), $duration)
],
$detector->getOS(),
@@ -1234,7 +1270,7 @@ Http::post('/v1/account/sessions/token')
->inject('project')
->inject('platform')
->inject('locale')
->inject('geodb')
->inject('geoRecord')
->inject('queueForEvents')
->inject('bus')
->inject('store')
@@ -1434,8 +1470,8 @@ Http::get('/v1/account/sessions/oauth2/:provider/redirect')
->inject('devKey')
->inject('user')
->inject('dbForProject')
->inject('geoRecord')
->inject('dbForPlatform')
->inject('geodb')
->inject('queueForEvents')
->inject('store')
->inject('proofForPassword')
@@ -1444,7 +1480,7 @@ Http::get('/v1/account/sessions/oauth2/:provider/redirect')
->inject('domainVerification')
->inject('cookieDomain')
->inject('authorization')
->action(function (string $provider, string $code, string $state, string $error, string $error_description, Request $request, Response $response, Document $project, Validator $redirectValidator, Document $devKey, User $user, Database $dbForProject, Database $dbForPlatform, Reader $geodb, Event $queueForEvents, Store $store, ProofsPassword $proofForPassword, ProofsToken $proofForToken, array $plan, bool $domainVerification, ?string $cookieDomain, Authorization $authorization) use ($oauthDefaultSuccess) {
->action(function (string $provider, string $code, string $state, string $error, string $error_description, Request $request, Response $response, Document $project, Validator $redirectValidator, Document $devKey, User $user, Database $dbForProject, GeoRecord $geoRecord, Database $dbForPlatform, Event $queueForEvents, Store $store, ProofsPassword $proofForPassword, ProofsToken $proofForToken, array $plan, bool $domainVerification, ?string $cookieDomain, Authorization $authorization) use ($oauthDefaultSuccess) {
$protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https';
$port = $request->getPort();
$callbackBase = $protocol . '://' . $request->getHostname();
@@ -1798,7 +1834,8 @@ Http::get('/v1/account/sessions/oauth2/:provider/redirect')
Query::notEqual('userInternalId', $user->getSequence()),
]);
if (!empty($identitiesWithMatchingEmail)) {
$failureRedirect(Exception::GENERAL_BAD_REQUEST); /** Return a generic bad request to prevent exposing existing accounts */
$failureRedirect(Exception::GENERAL_BAD_REQUEST);
/** Return a generic bad request to prevent exposing existing accounts */
}
$dbForProject->createDocument('identities', new Document([
@@ -1935,7 +1972,6 @@ Http::get('/v1/account/sessions/oauth2/:provider/redirect')
// If the `token` param is not set, we persist the session in a cookie
} else {
$detector = new Detector($request->getUserAgent('UNKNOWN'));
$record = $geodb->get($request->getIP());
$secret = $proofForToken->generate();
$session = new Document(array_merge([
@@ -1951,7 +1987,19 @@ Http::get('/v1/account/sessions/oauth2/:provider/redirect')
'userAgent' => $request->getUserAgent('UNKNOWN'),
'ip' => $request->getIP(),
'factors' => [TYPE::EMAIL, 'oauth2'], // include a special oauth2 factor to bypass MFA checks
'countryCode' => ($record) ? \strtolower($record['country']['iso_code']) : '--',
'countryCode' => \strtolower($geoRecord->getCountryCode()),
'continentCode' => $geoRecord->getContinentCode() === '--' ? null : $geoRecord->getContinentCode(),
'latitude' => $geoRecord->getLatitude(),
'longitude' => $geoRecord->getLongitude(),
'timeZone' => $geoRecord->getTimeZone(),
'weatherCode' => $geoRecord->getWeatherCode(),
'postalCode' => $geoRecord->getPostalCode(),
'autonomousSystemNumber' => $geoRecord->getAutonomousSystemNumber(),
'autonomousSystemOrganization' => $geoRecord->getAutonomousSystemOrganization(),
'connectionType' => $geoRecord->getConnectionType(),
'connectionUsageType' => $geoRecord->getConnectionUsageType(),
'connectionOrganization' => $geoRecord->getConnectionOrganization(),
'isp' => $geoRecord->getIsp(),
'expire' => DateTime::addSeconds(new \DateTime(), $duration)
], $detector->getOS(), $detector->getClient(), $detector->getDevice()));
@@ -2513,7 +2561,8 @@ Http::post('/v1/account/tokens/email')
Query::equal('providerEmail', [$email]),
]);
if (!$identityWithMatchingEmail->isEmpty()) {
throw new Exception(Exception::GENERAL_BAD_REQUEST); /** Return a generic bad request to prevent exposing existing accounts */
throw new Exception(Exception::GENERAL_BAD_REQUEST);
/** Return a generic bad request to prevent exposing existing accounts */
}
$userId = $userId === 'unique()' ? ID::unique() : $userId;
@@ -2831,7 +2880,7 @@ Http::put('/v1/account/sessions/magic-url')
->inject('project')
->inject('platform')
->inject('locale')
->inject('geodb')
->inject('geoRecord')
->inject('queueForEvents')
->inject('bus')
->inject('store')
@@ -2839,10 +2888,10 @@ Http::put('/v1/account/sessions/magic-url')
->inject('domainVerification')
->inject('cookieDomain')
->inject('authorization')
->action(function ($userId, $secret, $request, $response, $user, $dbForProject, $project, $platform, $locale, $geodb, $queueForEvents, $bus, $store, $proofForCode, $domainVerification, $cookieDomain, $authorization) use ($createSession) {
->action(function ($userId, $secret, $request, $response, $user, $dbForProject, $project, $platform, $locale, $geoRecord, $queueForEvents, $bus, $store, $proofForCode, $domainVerification, $cookieDomain, $authorization) use ($createSession) {
$proofForToken = new ProofsToken(TOKEN_LENGTH_MAGIC_URL);
$proofForToken->setHash(new Sha());
$createSession($userId, $secret, $request, $response, $user, $dbForProject, $project, $platform, $locale, $geodb, $queueForEvents, $bus, $store, $proofForToken, $proofForCode, $domainVerification, $cookieDomain, $authorization);
$createSession($userId, $secret, $request, $response, $user, $dbForProject, $project, $platform, $locale, $geoRecord, $queueForEvents, $bus, $store, $proofForToken, $proofForCode, $domainVerification, $cookieDomain, $authorization);
});
Http::put('/v1/account/sessions/phone')
@@ -2882,7 +2931,7 @@ Http::put('/v1/account/sessions/phone')
->inject('project')
->inject('platform')
->inject('locale')
->inject('geodb')
->inject('geoRecord')
->inject('queueForEvents')
->inject('bus')
->inject('store')
@@ -3370,7 +3419,8 @@ Http::patch('/v1/account/email')
Query::notEqual('userInternalId', $user->getSequence()),
]);
if (!$identityWithMatchingEmail->isEmpty()) {
throw new Exception(Exception::GENERAL_BAD_REQUEST); /** Return a generic bad request to prevent exposing existing accounts */
throw new Exception(Exception::GENERAL_BAD_REQUEST);
/** Return a generic bad request to prevent exposing existing accounts */
}
$emailMetadata = [
@@ -3446,7 +3496,8 @@ Http::patch('/v1/account/email')
}
$dbForProject->purgeCachedDocument('users', $user->getId());
} catch (Duplicate) {
throw new Exception(Exception::GENERAL_BAD_REQUEST); /** Return a generic bad request to prevent exposing existing accounts */
throw new Exception(Exception::GENERAL_BAD_REQUEST);
/** Return a generic bad request to prevent exposing existing accounts */
}
$queueForEvents->setParam('userId', $user->getId());
+12 -38
View File
@@ -1,11 +1,11 @@
<?php
use Appwrite\Locale\GeoRecord;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\Method;
use Appwrite\SDK\Response as SDKResponse;
use Appwrite\Utopia\Request;
use Appwrite\Utopia\Response;
use MaxMind\Db\Reader;
use Utopia\Config\Config;
use Utopia\Database\Document;
use Utopia\Http\Http;
@@ -31,43 +31,17 @@ Http::get('/v1/locale')
->inject('request')
->inject('response')
->inject('locale')
->inject('geodb')
->action(function (Request $request, Response $response, Locale $locale, Reader $geodb) {
$eu = Config::getParam('locale-eu');
$currencies = Config::getParam('locale-currencies');
$output = [];
$ip = $request->getIP();
$output['ip'] = $ip;
$currency = null;
$record = $geodb->get($ip);
if ($record) {
$output['countryCode'] = $record['country']['iso_code'];
$output['country'] = $locale->getText('countries.' . strtolower($record['country']['iso_code']), $locale->getText('locale.country.unknown'));
$output['continent'] = $locale->getText('continents.' . strtolower($record['continent']['code']), $locale->getText('locale.country.unknown'));
$output['continentCode'] = $record['continent']['code'];
$output['eu'] = (\in_array($record['country']['iso_code'], $eu)) ? true : false;
foreach ($currencies as $code => $element) {
if (isset($element['locations']) && isset($element['code']) && \in_array($record['country']['iso_code'], $element['locations'])) {
$currency = $element['code'];
}
}
$output['currency'] = $currency;
} else {
$output['countryCode'] = '--';
$output['country'] = $locale->getText('locale.country.unknown');
$output['continent'] = $locale->getText('locale.country.unknown');
$output['continentCode'] = '--';
$output['eu'] = false;
$output['currency'] = $currency;
}
$response->dynamic(new Document($output), Response::MODEL_LOCALE);
->inject('geoRecord')
->action(function (Request $request, Response $response, Locale $locale, GeoRecord $geoRecord) {
$response->dynamic(new Document([
'ip' => $geoRecord->getIp(),
'countryCode' => $geoRecord->getCountryCode(),
'country' => $geoRecord->getCountryName(),
'continentCode' => $geoRecord->getContinentCode(),
'continent' => $geoRecord->getContinent(),
'eu' => $geoRecord->isEu(),
'currency' => $geoRecord->getCurrency() ?? '',
]), Response::MODEL_LOCALE);
});
Http::get('/v1/locale/codes')
+4 -6
View File
@@ -17,6 +17,7 @@ use Appwrite\Event\Message\Delete as DeleteMessage;
use Appwrite\Event\Publisher\Delete as DeletePublisher;
use Appwrite\Extend\Exception;
use Appwrite\Hooks\Hooks;
use Appwrite\Locale\GeoRecord;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\ContentType;
use Appwrite\SDK\Deprecated;
@@ -30,7 +31,6 @@ use Appwrite\Utopia\Database\Validator\Queries\Targets;
use Appwrite\Utopia\Database\Validator\Queries\Users;
use Appwrite\Utopia\Request;
use Appwrite\Utopia\Response;
use MaxMind\Db\Reader;
use Utopia\Auth\Hash;
use Utopia\Auth\Hashes\Argon2;
use Utopia\Auth\Hashes\Bcrypt;
@@ -2289,11 +2289,11 @@ Http::post('/v1/users/:userId/sessions')
->inject('dbForProject')
->inject('project')
->inject('locale')
->inject('geodb')
->inject('geoRecord')
->inject('queueForEvents')
->inject('store')
->inject('proofForToken')
->action(function (string $userId, Request $request, Response $response, Database $dbForProject, Document $project, Locale $locale, Reader $geodb, Event $queueForEvents, Store $store, Token $proofForToken) {
->action(function (string $userId, Request $request, Response $response, Database $dbForProject, Document $project, Locale $locale, GeoRecord $geoRecord, Event $queueForEvents, Store $store, Token $proofForToken) {
$user = $dbForProject->getDocument('users', $userId);
if ($user->isEmpty()) {
throw new Exception(Exception::USER_NOT_FOUND);
@@ -2301,8 +2301,6 @@ Http::post('/v1/users/:userId/sessions')
$secret = $proofForToken->generate();
$detector = new Detector($request->getUserAgent('UNKNOWN'));
$record = $geodb->get($request->getIP());
$duration = $project->getAttribute('auths', [])['duration'] ?? TOKEN_EXPIRATION_LOGIN_LONG;
$expire = DateTime::formatTz(DateTime::addSeconds(new \DateTime(), $duration));
@@ -2316,7 +2314,7 @@ Http::post('/v1/users/:userId/sessions')
'userAgent' => $request->getUserAgent('UNKNOWN'),
'factors' => ['server'],
'ip' => $request->getIP(),
'countryCode' => ($record) ? \strtolower($record['country']['iso_code']) : '--',
'countryCode' => \strtolower($geoRecord->getCountryCode()),
'expire' => $expire,
],
$detector->getOS(),
+42 -41
View File
@@ -12,6 +12,8 @@ use Appwrite\Event\Message\Delete as DeleteMessage;
use Appwrite\Event\Publisher\Certificate;
use Appwrite\Event\Publisher\Delete as DeletePublisher;
use Appwrite\Extend\Exception as AppwriteException;
use Appwrite\Locale\GeoRecord;
use Appwrite\Locking\Lock;
use Appwrite\Network\Cors;
use Appwrite\Platform\Appwrite;
use Appwrite\SDK\AuthType;
@@ -47,7 +49,6 @@ use Appwrite\Utopia\Response\Filters\V26 as ResponseV26;
use Appwrite\Utopia\View;
use Executor\Exception\Timeout as ExecutorTimeout;
use Executor\Executor;
use MaxMind\Db\Reader;
use Swoole\Http\Request as SwooleRequest;
use Swoole\Table;
use Utopia\Bus\Bus;
@@ -76,7 +77,7 @@ use Utopia\Validator\Text;
Config::setParam('cookieSamesite', Response::COOKIE_SAMESITE_NONE);
function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, Authorization $authorization, ?Key $apiKey, DeletePublisher $publisherForDeletes, int $executionsRetentionCount)
function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Event $queueForEvents, Bus $bus, Executor $executor, GeoRecord $geoRecord, callable $isResourceBlocked, array $platform, string $previewHostname, Authorization $authorization, ?Key $apiKey, DeletePublisher $publisherForDeletes, int $executionsRetentionCount, Lock $lock)
{
$host = $request->getHostname();
if (!empty($previewHostname)) {
@@ -145,9 +146,16 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S
if (!$project->isEmpty() && $project->getId() !== 'console') {
$accessedAt = $project->getAttribute('accessedAt', 0);
if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) {
$authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([
'accessedAt' => DateTime::now()
])));
$projectInternalId = (string) ($project->getSequence() ?: $project->getId());
$lock->tryWithKey(
'lock:platform:'.$projectInternalId.':projects:'.$project->getId().':accessedAt',
fn () => $authorization->skip(fn () => $dbForPlatform->updateDocument(
'projects',
$project->getId(),
new Document(['accessedAt' => DateTime::now()])
)),
target: 'projects'
);
}
/**
@@ -391,16 +399,10 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S
$headers['x-appwrite-trigger'] = 'http';
$headers['x-appwrite-user-jwt'] = '';
if (!empty($ip)) {
$record = $geodb->get($ip);
if ($record) {
$eu = Config::getParam('locale-eu');
$headers['x-appwrite-country-code'] = $record['country']['iso_code'] ?? '';
$headers['x-appwrite-continent-code'] = $record['continent']['code'] ?? '';
$headers['x-appwrite-continent-eu'] = (\in_array($record['country']['iso_code'], $eu)) ? 'true' : 'false';
}
if (!empty($ip) && !$geoRecord->isEmpty()) {
$headers['x-appwrite-country-code'] = $geoRecord->getCountryCode();
$headers['x-appwrite-continent-code'] = $geoRecord->getContinentCode();
$headers['x-appwrite-continent-eu'] = $geoRecord->isEu() ? 'true' : 'false';
}
$headersFiltered = [];
@@ -828,7 +830,7 @@ Http::init()
->inject('getProjectDB')
->inject('locale')
->inject('localeCodes')
->inject('geodb')
->inject('geoRecord')
->inject('queueForEvents')
->inject('bus')
->inject('executor')
@@ -841,8 +843,9 @@ Http::init()
->inject('authorization')
->inject('publisherForDeletes')
->inject('executionsRetentionCount')
->inject('lock')
->inject('params')
->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Document $project, Database $dbForPlatform, callable $getProjectDB, Locale $locale, array $localeCodes, Reader $geodb, Event $queueForEvents, Bus $bus, Executor $executor, array $platform, callable $isResourceBlocked, string $previewHostname, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeletePublisher $publisherForDeletes, int $executionsRetentionCount, array $params) {
->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Document $project, Database $dbForPlatform, callable $getProjectDB, Locale $locale, array $localeCodes, GeoRecord $geoRecord, Event $queueForEvents, Bus $bus, Executor $executor, array $platform, callable $isResourceBlocked, string $previewHostname, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeletePublisher $publisherForDeletes, int $executionsRetentionCount, Lock $lock, array $params) {
/*
* Appwrite Router
*/
@@ -850,7 +853,7 @@ Http::init()
$platformHostnames = $platform['hostnames'] ?? [];
// Only run Router when external domain
if (!\in_array($hostname, $platformHostnames) || !empty($previewHostname)) {
if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $publisherForDeletes, $executionsRetentionCount)) {
if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geoRecord, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $publisherForDeletes, $executionsRetentionCount, $lock)) {
$utopia->match($request)?->route->label('router', true);
}
}
@@ -1142,7 +1145,7 @@ Http::options()
->inject('queueForEvents')
->inject('bus')
->inject('executor')
->inject('geodb')
->inject('geoRecord')
->inject('isResourceBlocked')
->inject('platform')
->inject('previewHostname')
@@ -1153,14 +1156,15 @@ Http::options()
->inject('authorization')
->inject('publisherForDeletes')
->inject('executionsRetentionCount')
->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, Document $project, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeletePublisher $publisherForDeletes, int $executionsRetentionCount) {
->inject('lock')
->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, GeoRecord $geoRecord, callable $isResourceBlocked, array $platform, string $previewHostname, Document $project, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeletePublisher $publisherForDeletes, int $executionsRetentionCount, Lock $lock) {
/*
* Appwrite Router
*/
$platformHostnames = $platform['hostnames'] ?? [];
// Only run Router when external domain
if (!in_array($request->getHostname(), $platformHostnames) || !empty($previewHostname)) {
if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $publisherForDeletes, $executionsRetentionCount)) {
if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geoRecord, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $publisherForDeletes, $executionsRetentionCount, $lock)) {
$utopia->match($request)?->route->label('router', true);
}
}
@@ -1547,7 +1551,7 @@ Http::get('/robots.txt')
->inject('queueForEvents')
->inject('bus')
->inject('executor')
->inject('geodb')
->inject('geoRecord')
->inject('isResourceBlocked')
->inject('platform')
->inject('previewHostname')
@@ -1555,13 +1559,14 @@ Http::get('/robots.txt')
->inject('authorization')
->inject('publisherForDeletes')
->inject('executionsRetentionCount')
->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeletePublisher $publisherForDeletes, int $executionsRetentionCount) {
->inject('lock')
->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, GeoRecord $geoRecord, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeletePublisher $publisherForDeletes, int $executionsRetentionCount, Lock $lock) {
$platformHostnames = $platform['hostnames'] ?? [];
if (in_array($request->getHostname(), $platformHostnames) || !empty($previewHostname)) {
$template = new View(__DIR__ . '/../views/general/robots.phtml');
$response->text($template->render(false));
} else {
if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $publisherForDeletes, $executionsRetentionCount)) {
if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geoRecord, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $publisherForDeletes, $executionsRetentionCount, $lock)) {
$utopia->match($request)?->route->label('router', true);
}
}
@@ -1581,7 +1586,7 @@ Http::get('/humans.txt')
->inject('queueForEvents')
->inject('bus')
->inject('executor')
->inject('geodb')
->inject('geoRecord')
->inject('isResourceBlocked')
->inject('platform')
->inject('previewHostname')
@@ -1589,13 +1594,14 @@ Http::get('/humans.txt')
->inject('authorization')
->inject('publisherForDeletes')
->inject('executionsRetentionCount')
->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeletePublisher $publisherForDeletes, int $executionsRetentionCount) {
->inject('lock')
->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, GeoRecord $geoRecord, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeletePublisher $publisherForDeletes, int $executionsRetentionCount, Lock $lock) {
$platformHostnames = $platform['hostnames'] ?? [];
if (in_array($request->getHostname(), $platformHostnames) || !empty($previewHostname)) {
$template = new View(__DIR__ . '/../views/general/humans.phtml');
$response->text($template->render(false));
} else {
if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $publisherForDeletes, $executionsRetentionCount)) {
if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geoRecord, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $publisherForDeletes, $executionsRetentionCount, $lock)) {
$utopia->match($request)?->route->label('router', true);
}
}
@@ -1685,22 +1691,17 @@ Http::get('/v1/ping')
}
$pingCount = $project->getAttribute('pingCount', 0) + 1;
$pingedAt = DateTime::now();
$project
->setAttribute('pingCount', $pingCount)
->setAttribute('pingedAt', $pingedAt);
if ($pingCount === 1) {
$project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([
'pingCount' => $pingCount,
'pingedAt' => DateTime::now(),
])));
$authorization->skip(function () use ($dbForPlatform, $project) {
$dbForPlatform->updateDocument('projects', $project->getId(), new Document([
'pingCount' => $project->getAttribute('pingCount'),
'pingedAt' => $project->getAttribute('pingedAt')
]));
});
$queueForEvents
->setParam('projectId', $project->getId())
->setPayload($response->output($project, Response::MODEL_PROJECT));
$queueForEvents
->setParam('projectId', $project->getId())
->setPayload($response->output($project, Response::MODEL_PROJECT));
}
$response->text('Pong!');
});
+37 -12
View File
@@ -16,6 +16,7 @@ use Appwrite\Event\Webhook;
use Appwrite\Extend\Exception;
use Appwrite\Extend\Exception as AppwriteException;
use Appwrite\Functions\EventProcessor;
use Appwrite\Locking\Lock;
use Appwrite\Platform\Modules\Storage\Config\CacheControl;
use Appwrite\Platform\Modules\Storage\Config\StorageCacheControl;
use Appwrite\Reference\Renderer;
@@ -24,7 +25,6 @@ use Appwrite\Usage\Context;
use Appwrite\Utopia\Database\Documents\User;
use Appwrite\Utopia\Request;
use Appwrite\Utopia\Response;
use Throwable;
use Utopia\Abuse\Abuse;
use Utopia\Bus\Bus;
use Utopia\Cache\Adapter\Filesystem;
@@ -60,9 +60,10 @@ Http::init()
->inject('team')
->inject('apiKey')
->inject('authorization')
->inject('lock')
->inject('impersonatorUser')
->inject('targetUser')
->action(function (Route $route, Request $request, Database $dbForPlatform, Database $dbForProject, AuditContext $auditContext, Document $project, User $user, ?Document $session, array $servers, string $mode, Document $team, ?Key $apiKey, Authorization $authorization, Document $impersonatorUser, User $targetUser) {
->action(function (Route $route, Request $request, Database $dbForPlatform, Database $dbForProject, AuditContext $auditContext, Document $project, User $user, ?Document $session, array $servers, string $mode, Document $team, ?Key $apiKey, Authorization $authorization, Lock $lock, Document $impersonatorUser, User $targetUser) {
/**
* Handle user authentication and session validation.
@@ -161,24 +162,28 @@ Http::init()
// For standard keys, update last accessed time
if (\in_array($apiKey->getType(), [API_KEY_STANDARD, API_KEY_ORGANIZATION, API_KEY_ACCOUNT])) {
$dbKey = null;
$keyOwnerInternalId = '';
if (! empty($apiKey->getProjectId())) {
$dbKey = $project->find(
key: 'secret',
find: $request->getHeaderLine('x-appwrite-key', ''),
subject: 'keys'
);
$keyOwnerInternalId = (string) ($project->getSequence() ?: $project->getId());
} elseif (! empty($apiKey->getUserId())) {
$dbKey = $user->find(
key: 'secret',
find: $request->getHeaderLine('x-appwrite-key', ''),
subject: 'keys'
);
$keyOwnerInternalId = (string) ($user->getSequence() ?: $user->getId());
} elseif (! empty($apiKey->getTeamId())) {
$dbKey = $team->find(
key: 'secret',
find: $request->getHeaderLine('x-appwrite-key', ''),
subject: 'keys'
);
$keyOwnerInternalId = (string) ($team->getSequence() ?: $team->getId());
}
if (!$dbKey) {
@@ -203,13 +208,19 @@ Http::init()
$sdks[] = $sdk;
$updates->setAttribute('sdks', $sdks);
$updates->setAttribute('accessedAt', Datetime::now());
$updates->setAttribute('accessedAt', DateTime::now());
}
}
if (! $updates->isEmpty()) {
$dbForPlatform->getAuthorization()->skip(fn () => $dbForPlatform->updateDocument('keys', $dbKey->getId(), $updates));
$updatedKey = $updates->isEmpty()
? null
: $lock->tryWithKey(
'lock:platform:'.$keyOwnerInternalId.':keys:'.$dbKey->getId(),
fn () => $authorization->skip(fn () => $dbForPlatform->updateDocument('keys', $dbKey->getId(), $updates)),
target: 'keys'
);
if ($updatedKey instanceof Document) {
if (! empty($apiKey->getProjectId())) {
$dbForPlatform->getAuthorization()->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));
} elseif (! empty($apiKey->getUserId())) {
@@ -372,9 +383,16 @@ Http::init()
if ($project->getId() !== 'console') {
$accessedAt = $project->getAttribute('accessedAt', 0);
if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) {
$authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([
'accessedAt' => DateTime::now()
])));
$projectInternalId = (string) ($project->getSequence() ?: $project->getId());
$lock->tryWithKey(
'lock:platform:'.$projectInternalId.':projects:'.$project->getId().':accessedAt',
fn () => $authorization->skip(fn () => $dbForPlatform->updateDocument(
'projects',
$project->getId(),
new Document(['accessedAt' => DateTime::now()])
)),
target: 'projects'
);
}
}
@@ -390,9 +408,16 @@ Http::init()
'accessedAt' => $user->getAttribute('accessedAt')
]));
} else {
$authorization->skip(fn () => $dbForPlatform->updateDocument('users', $user->getId(), new Document([
'accessedAt' => $user->getAttribute('accessedAt')
])));
$userInternalId = (string) ($user->getSequence() ?: $user->getId());
$lock->tryWithKey(
'lock:platform:'.$userInternalId.':users:'.$user->getId().':accessedAt',
fn () => $authorization->skip(fn () => $dbForPlatform->updateDocument(
'users',
$user->getId(),
new Document(['accessedAt' => $user->getAttribute('accessedAt')])
)),
target: 'users'
);
}
}
}
@@ -1177,7 +1202,7 @@ Http::shutdown()
$authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([
'onboarding' => $byMethod,
])));
} catch (Throwable) {
} catch (\Throwable) {
// Missing `onboarding` attribute on upgraded installs must not break the request lifecycle.
}
});
+9 -8
View File
@@ -1,9 +1,9 @@
<?php
use Appwrite\Extend\Exception;
use Appwrite\Locale\GeoRecord;
use Appwrite\Utopia\Database\Documents\User;
use Appwrite\Utopia\Request;
use MaxMind\Db\Reader;
use Utopia\Config\Config;
use Utopia\Database\DateTime;
use Utopia\Database\Document;
@@ -36,16 +36,17 @@ Http::init()
->inject('route')
->inject('request')
->inject('project')
->inject('geodb')
->inject('geoRecord')
->inject('user')
->inject('authorization')
->action(function (Route $route, Request $request, Document $project, Reader $geodb, User $user, Authorization $authorization) {
->action(function (Route $route, Request $request, Document $project, GeoRecord $geoRecord, User $user, Authorization $authorization) {
$denylist = System::getEnv('_APP_CONSOLE_COUNTRIES_DENYLIST', '');
if (!empty($denylist && $project->getId() === 'console')) {
$countries = explode(',', $denylist);
$record = $geodb->get($request->getIP()) ?? [];
$country = $record['country']['iso_code'] ?? '';
if (in_array($country, $countries)) {
if (!empty($denylist) && $project->getId() === 'console') {
// A missing or unknown geo lookup ("--") is treated as allowed and falls
// through to the membership check below, matching the pre-geo-service behavior.
$countries = \array_map('strtoupper', \array_map('trim', explode(',', $denylist)));
$country = \strtoupper($geoRecord->getCountryCode());
if (in_array($country, $countries, true)) {
throw new Exception(Exception::GENERAL_REGION_ACCESS_DENIED);
}
}
-4
View File
@@ -5,7 +5,6 @@ use Appwrite\GraphQL\Promises\Adapter\Swoole;
use Appwrite\Hooks\Hooks;
use Appwrite\PubSub\Adapter\Redis as PubSub;
use Appwrite\URL\URL as AppwriteURL;
use MaxMind\Db\Reader;
use Utopia\Cache\Adapter\Redis as RedisCache;
use Utopia\Config\Config;
use Utopia\Console;
@@ -449,9 +448,6 @@ $register->set('smtp', function () {
timelimit: 30,
);
});
$register->set('geodb', function () {
return new Reader(__DIR__ . '/../assets/dbip/dbip-country-lite-2026-06.mmdb');
});
$register->set('passwordsDictionary', function () {
$content = \file_get_contents(__DIR__ . '/../assets/security/10k-common-passwords');
$content = explode("\n", $content);
-2
View File
@@ -333,8 +333,6 @@ function getDevice(string $root, string $connection = ''): Device
}
}
$container->set('geodb', fn ($register) => $register->get('geodb'), ['register']);
$container->set('passwordsDictionary', fn ($register) => $register->get('passwordsDictionary'), ['register']);
$container->set('servers', function () {
+92
View File
@@ -14,6 +14,8 @@ use Appwrite\Event\Webhook;
use Appwrite\Extend\Exception;
use Appwrite\Functions\EventProcessor;
use Appwrite\GraphQL\Schema;
use Appwrite\Locale\GeoRecord;
use Appwrite\Locking\Lock;
use Appwrite\Network\Cors;
use Appwrite\Network\Platform;
use Appwrite\Network\Validator\Origin;
@@ -34,6 +36,7 @@ use Utopia\Auth\Proofs\Token;
use Utopia\Auth\Store;
use Utopia\Cache\Cache;
use Utopia\Config\Config;
use Utopia\Console;
use Utopia\Database\Database;
use Utopia\Database\DateTime as DatabaseDateTime;
use Utopia\Database\Document;
@@ -41,9 +44,12 @@ use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
use Utopia\DI\Container;
use Utopia\Domains\Domain;
use Utopia\Fetch\Client;
use Utopia\Http\Http;
use Utopia\Locale\Locale;
use Utopia\Lock\Distributed as DistributedLock;
use Utopia\Logger\Log;
use Utopia\Logger\Logger;
use Utopia\Pools\Group;
use Utopia\Queue\Publisher;
use Utopia\Queue\Queue;
@@ -65,6 +71,17 @@ return function (Container $context): void {
$context->set('logger', fn ($register) => $register->get('logger'), ['register']);
$context->set('lock', function (Group $pools, Telemetry $telemetry, ?Logger $logger, Document $project): Lock {
return new Lock(
fn (string $key, int $ttl, Closure $callback): mixed => $pools->get('lock')->use(
fn (\Redis $redis): mixed => $callback(new DistributedLock($redis, $key, $ttl))
),
$telemetry,
$logger,
$project
);
}, ['pools', 'telemetry', 'logger', 'project']);
$context->set('authorization', fn () => new Authorization(), []);
$context->set('store', fn (): Store => new Store(), []);
@@ -1319,4 +1336,79 @@ return function (Container $context): void {
$adapter->setTimeout((int) System::getEnv('_APP_EMBEDDING_TIMEOUT', '30000'));
return new Agent($adapter);
}, ['register']);
$context->set('geoRecord', function ($request, Locale $locale, callable $getGeoForIp) {
$ip = $request->getIp();
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
Console::warning("Invalid IP address: {$ip}");
$ip = '0.0.0.0';
}
return $getGeoForIp($locale, $ip);
}, ['request', 'locale', 'getGeoForIp']);
$context->set('getGeoForIp', function () {
return function (Locale $locale, string $ip): GeoRecord {
$record = null;
$geoEndpoint = System::getEnv('_APP_GEO_ENDPOINT', '');
$geoSecret = System::getEnv('_APP_GEO_SECRET', '');
if (!empty($geoEndpoint) && !empty($geoSecret) && filter_var($ip, FILTER_VALIDATE_IP)) {
try {
$client = new Client();
$client->addHeader('Authorization', 'Bearer ' . $geoSecret);
$client->setTimeout(3000);
$response = $client->fetch(\rtrim($geoEndpoint, '/') . "/ips/{$ip}", Client::METHOD_GET);
if ($response->getStatusCode() === 200) {
$body = $response->json();
if (\is_array($body)) {
$record = $body;
}
}
} catch (\Throwable $th) {
Console::warning('Geo service unavailable: ' . $th->getMessage());
}
}
$countryCode = \strtoupper($record['countryCode'] ?? '--');
$continentCode = \strtoupper($record['continentCode'] ?? '--');
$eu = \array_map('strtoupper', Config::getParam('locale-eu'));
$currencies = Config::getParam('locale-currencies');
$currency = null;
if ($countryCode !== '--') {
foreach ($currencies as $element) {
if (isset($element['locations'], $element['code']) && \in_array($countryCode, $element['locations'], true)) {
$currency = $element['code'];
break;
}
}
}
$autonomousSystemNumber = $record['autonomousSystemNumber'] ?? null;
return (new GeoRecord([
'ip' => $ip,
'countryCode' => $countryCode,
'continentCode' => $continentCode,
'eu' => $countryCode !== '--' && \in_array($countryCode, $eu, true),
'currency' => $currency,
'latitude' => $record['latitude'] ?? null,
'longitude' => $record['longitude'] ?? null,
'timeZone' => $record['timeZone'] ?? null,
'weatherCode' => $record['weatherCode'] ?? null,
'postalCode' => $record['postalCode'] ?? null,
'autonomousSystemNumber' => $autonomousSystemNumber === null ? null : (string) $autonomousSystemNumber,
'autonomousSystemOrganization' => $record['autonomousSystemOrganization'] ?? null,
'connectionType' => $record['connection'] ?? null,
'connectionUsageType' => $record['user'] ?? $record['type'] ?? null,
'connectionOrganization' => $record['organization'] ?? null,
'isp' => $record['isp'] ?? null,
]))
->setLocale($locale);
};
}, []);
};
Generated
+6 -6
View File
@@ -3736,16 +3736,16 @@
},
{
"name": "utopia-php/detector",
"version": "0.2.4",
"version": "0.2.6",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/detector.git",
"reference": "a564381108518ce6d18b6ab441f1448bd8355afd"
"reference": "2b2744be7ea028307dc9352eeec109cd1f9ab2d3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/detector/zipball/a564381108518ce6d18b6ab441f1448bd8355afd",
"reference": "a564381108518ce6d18b6ab441f1448bd8355afd",
"url": "https://api.github.com/repos/utopia-php/detector/zipball/2b2744be7ea028307dc9352eeec109cd1f9ab2d3",
"reference": "2b2744be7ea028307dc9352eeec109cd1f9ab2d3",
"shasum": ""
},
"require": {
@@ -3775,9 +3775,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/detector/issues",
"source": "https://github.com/utopia-php/detector/tree/0.2.4"
"source": "https://github.com/utopia-php/detector/tree/0.2.6"
},
"time": "2026-02-17T11:26:34+00:00"
"time": "2026-07-01T14:00:11+00:00"
},
{
"name": "utopia-php/di",
+25
View File
@@ -76,6 +76,7 @@ services:
- redis
- ${_APP_DB_HOST:-mongodb}
- appwrite-embedding
- appwrite-geo
environment:
- _APP_ENV
- _APP_EDITION
@@ -170,6 +171,7 @@ services:
- _APP_EXECUTOR_SECRET
- _APP_EXECUTOR_HOST
- _APP_LOGGING_CONFIG
- _APP_LOCKING_ENABLED
- _APP_MAINTENANCE_INTERVAL
- _APP_MAINTENANCE_RETENTION_EXECUTION
- _APP_MAINTENANCE_RETENTION_CACHE
@@ -202,6 +204,8 @@ services:
- _APP_CUSTOM_DOMAIN_DENY_LIST
- _APP_TRUSTED_HEADERS
- _APP_MIGRATION_HOST
- _APP_GEO_SECRET
- _APP_GEO_ENDPOINT
appwrite-console:
logging:
driver: json-file
@@ -1096,6 +1100,27 @@ services:
image: appwrite/browser:0.3.2
networks:
- appwrite
appwrite-geo:
container_name: appwrite-geo
logging:
driver: json-file
options:
max-file: "5"
max-size: 10m
image: appwrite/geo:0.3.0
restart: unless-stopped
networks:
- appwrite
environment:
- GEO_SECRET=$_APP_GEO_SECRET
healthcheck:
test: 'php -r "exit(json_decode(file_get_contents(''http://localhost/v1/health''))->status === ''ok'' ? 0 : 1);"'
interval: 10s
timeout: 5s
retries: 3
start_period: 5s
openruntimes-executor:
container_name: exc1
hostname: exc1
+1
View File
@@ -0,0 +1 @@
Check the Appwrite geo service is up and connection is successful.
-1
View File
@@ -1 +0,0 @@
Get the number of logs that are waiting to be processed in the Appwrite internal queue server.
+3
View File
@@ -0,0 +1,3 @@
The Notifications service allows you to read and manage the notifications shown in your Appwrite Console. Use it to list the notifications delivered to the currently logged-in user, mark a notification as read, and retrieve the Appwrite logo used when rendering notifications.
Notifications keep you informed about important events and updates related to your account and projects. This service operates in the scope of the current Console user and is intended for Console integrations.
+2
View File
@@ -45,6 +45,7 @@ class Exception extends \Exception
public const string GENERAL_SERVICE_DISABLED = 'general_service_disabled';
public const string GENERAL_UNAUTHORIZED_SCOPE = 'general_unauthorized_scope';
public const string GENERAL_RATE_LIMIT_EXCEEDED = 'general_rate_limit_exceeded';
public const string GENERAL_RESOURCE_LOCKED = 'general_resource_locked';
public const string GENERAL_SMTP_DISABLED = 'general_smtp_disabled';
public const string GENERAL_PHONE_DISABLED = 'general_phone_disabled';
public const string GENERAL_ARGUMENT_INVALID = 'general_argument_invalid';
@@ -151,6 +152,7 @@ class Exception extends \Exception
public const string STORAGE_DEVICE_NOT_FOUND = 'storage_device_not_found';
public const string STORAGE_FILE_EMPTY = 'storage_file_empty';
public const string STORAGE_FILE_TYPE_UNSUPPORTED = 'storage_file_type_unsupported';
public const string STORAGE_IMAGE_RESOLUTION_EXCEEDED = 'storage_image_resolution_exceeded';
public const string STORAGE_INVALID_FILE_SIZE = 'storage_invalid_file_size';
public const string STORAGE_INVALID_FILE = 'storage_invalid_file';
public const string STORAGE_BUCKET_ALREADY_EXISTS = 'storage_bucket_already_exists';
+141
View File
@@ -0,0 +1,141 @@
<?php
namespace Appwrite\Locale;
use Utopia\Database\Document;
use Utopia\Locale\Locale;
class GeoRecord extends Document
{
private ?Locale $locale = null;
public function setLocale(Locale $locale): self
{
$this->locale = $locale;
return $this;
}
public function isEmpty(): bool
{
return $this->getAttribute('countryCode', '--') === '--';
}
public function getIp(): string
{
return $this->getAttribute('ip', '');
}
public function getCountryCode(): string
{
$code = $this->getAttribute('countryCode', '--');
return $code === '--' ? '--' : \strtoupper($code);
}
public function getCountryName(): string
{
if ($this->locale === null) {
return '';
}
$unknown = $this->locale->getText('locale.country.unknown');
$code = $this->getAttribute('countryCode', '--');
if ($code === '--') {
return $unknown;
}
return $this->locale->getText('countries.' . \strtolower($code), $unknown);
}
public function getContinent(): string
{
if ($this->locale === null) {
return '';
}
$unknown = $this->locale->getText('locale.country.unknown');
$code = $this->getAttribute('continentCode', '--');
if ($code === '--') {
return $unknown;
}
return $this->locale->getText('continents.' . \strtolower($code), $unknown);
}
public function getContinentCode(): string
{
return $this->getAttribute('continentCode', '--');
}
public function isEu(): bool
{
return $this->getAttribute('eu', false);
}
public function getCurrency(): ?string
{
return $this->getAttribute('currency');
}
public function getTimeZone(): ?string
{
return $this->getAttribute('timeZone');
}
public function getWeatherCode(): ?string
{
return $this->getAttribute('weatherCode');
}
public function getPostalCode(): ?string
{
return $this->getAttribute('postalCode');
}
public function getLatitude(): ?float
{
$value = $this->getAttribute('latitude');
return $value === null ? null : (float) $value;
}
public function getLongitude(): ?float
{
$value = $this->getAttribute('longitude');
return $value === null ? null : (float) $value;
}
public function getIsp(): ?string
{
return $this->getAttribute('isp');
}
public function getAutonomousSystemNumber(): ?string
{
return $this->getAttribute('autonomousSystemNumber');
}
public function getAutonomousSystemOrganization(): ?string
{
return $this->getAttribute('autonomousSystemOrganization');
}
public function getConnectionType(): ?string
{
return $this->getAttribute('connectionType');
}
public function getConnectionUsageType(): ?string
{
return $this->getAttribute('connectionUsageType');
}
public function getConnectionOrganization(): ?string
{
return $this->getAttribute('connectionOrganization');
}
}
+232
View File
@@ -0,0 +1,232 @@
<?php
namespace Appwrite\Locking;
use Appwrite\Extend\Exception;
use Closure;
use Throwable;
use Utopia\Console;
use Utopia\Database\Document;
use Utopia\Lock\Lock as UtopiaLock;
use Utopia\Logger\Log;
use Utopia\Logger\Logger;
use Utopia\System\System;
use Utopia\Telemetry\Adapter as Telemetry;
final class Lock
{
private const FAIL_TTL_SECONDS = 10;
private const FAIL_WAIT_SECONDS = 3.0;
private const SKIP_TTL_SECONDS = 5;
private const SKIP_WAIT_SECONDS = 0.0;
private const REPORT_RATE_LIMIT_SECONDS = 60;
private const OUTCOME_ACQUIRED = 'acquired';
private const OUTCOME_SKIPPED = 'skipped';
private const OUTCOME_CONTENDED = 'contended';
private const OUTCOME_BACKEND_ERROR = 'backend_error';
private const OUTCOME_RELEASE_ERROR = 'release_error';
private readonly bool $enabled;
private readonly mixed $attempts;
private readonly string $projectInternalId;
/** @var array<string,int> */
private static array $lastReportAt = [];
/**
* @var Closure(string, int, Closure(UtopiaLock): mixed): mixed
*/
private readonly Closure $useLock;
public function __construct(
Closure $useLock,
Telemetry $telemetry,
private readonly ?Logger $logger,
Document $project,
) {
$this->useLock = $useLock;
$this->enabled = System::getEnv('_APP_LOCKING_ENABLED', 'enabled') !== 'disabled';
$this->attempts = $telemetry->createCounter('lock.attempts', null, 'Distributed lock acquire outcomes');
$this->projectInternalId = (string) ($project->getSequence() ?: $project->getId());
}
public function withKey(
string $key,
Closure $fn,
string $target,
int $ttl = self::FAIL_TTL_SECONDS,
float $waitTimeout = self::FAIL_WAIT_SECONDS,
): mixed {
return $this->execute(
$key,
$target,
$fn,
ttl: $ttl,
waitTimeout: $waitTimeout,
skipOnContention: false,
);
}
public function tryWithKey(
string $key,
Closure $fn,
string $target,
int $ttl = self::SKIP_TTL_SECONDS,
): mixed {
return $this->execute(
$key,
$target,
$fn,
ttl: $ttl,
waitTimeout: self::SKIP_WAIT_SECONDS,
skipOnContention: true,
);
}
private function execute(
string $key,
string $target,
Closure $fn,
int $ttl = self::FAIL_TTL_SECONDS,
float $waitTimeout = self::FAIL_WAIT_SECONDS,
bool $skipOnContention = false,
): mixed {
if (! $this->enabled) {
return $fn();
}
$labels = ['target' => $target, 'project' => $this->projectInternalId];
$callbackException = false;
try {
return ($this->useLock)($key, $ttl, function (UtopiaLock $lock) use ($fn, $key, $labels, $skipOnContention, $target, $waitTimeout, &$callbackException): mixed {
return $this->executeWithLock($lock, $key, $target, $labels, $fn, $waitTimeout, $skipOnContention, $callbackException);
});
} catch (\RedisException $e) {
if ($callbackException) {
throw $e;
}
$this->attempts->add(1, ['outcome' => self::OUTCOME_BACKEND_ERROR, ...$labels]);
$this->reportError(self::OUTCOME_BACKEND_ERROR, $key, $target, $e);
return $fn();
} catch (\Exception $e) {
if ($callbackException) {
throw $e;
}
// Pool exhaustion throws a bare \Exception before lock acquisition.
// Only that exact class is fail-open; subclasses must propagate.
if (get_class($e) !== \Exception::class) {
throw $e;
}
$this->attempts->add(1, ['outcome' => self::OUTCOME_BACKEND_ERROR, ...$labels]);
$this->reportError(self::OUTCOME_BACKEND_ERROR, $key, $target, $e);
return $fn();
}
}
/**
* @param array{target: string, project: string} $labels
*/
private function executeWithLock(
UtopiaLock $lock,
string $key,
string $target,
array $labels,
Closure $fn,
float $waitTimeout,
bool $skipOnContention,
bool &$callbackException,
): mixed {
try {
$acquired = $lock->acquire($waitTimeout);
} catch (\RedisException $e) {
$this->attempts->add(1, ['outcome' => self::OUTCOME_BACKEND_ERROR, ...$labels]);
$this->reportError(self::OUTCOME_BACKEND_ERROR, $key, $target, $e);
$callbackException = true;
return $fn();
}
if (! $acquired) {
if ($skipOnContention) {
$this->attempts->add(1, ['outcome' => self::OUTCOME_SKIPPED, ...$labels]);
return null;
}
$this->attempts->add(1, ['outcome' => self::OUTCOME_CONTENDED, ...$labels]);
throw new Exception(Exception::GENERAL_RESOURCE_LOCKED);
}
$this->attempts->add(1, ['outcome' => self::OUTCOME_ACQUIRED, ...$labels]);
$callbackException = true;
try {
return $fn();
} finally {
try {
$lock->release();
} catch (Throwable $e) {
$this->attempts->add(1, ['outcome' => self::OUTCOME_RELEASE_ERROR, ...$labels]);
$this->reportError(self::OUTCOME_RELEASE_ERROR, $key, $target, $e);
}
}
}
/**
* Rate-limit backend/release reports so outages don't flood Sentry.
*/
private function reportError(string $action, string $key, string $target, Throwable $e): void
{
Console::warning("Lock {$action} for {$key}: {$e->getMessage()}");
if ($this->logger === null) {
return;
}
$bucket = $action.':'.$target;
$now = time();
if ((self::$lastReportAt[$bucket] ?? 0) + self::REPORT_RATE_LIMIT_SECONDS > $now) {
return;
}
self::$lastReportAt[$bucket] = $now;
$log = new Log();
$log->setNamespace('http');
$log->setServer(System::getEnv('_APP_LOGGING_SERVICE_IDENTIFIER', \gethostname()));
$log->setVersion(APP_VERSION_STABLE);
$log->setType(Log::TYPE_WARNING);
$log->setMessage('Distributed lock '.$action.': '.$e->getMessage());
$log->setAction("lock.{$action}");
$log->setEnvironment(System::getEnv('_APP_ENV', 'development') === 'production'
? Log::ENVIRONMENT_PRODUCTION
: Log::ENVIRONMENT_STAGING);
$log->addTag('lock.target', $target);
$log->addTag('lock.project', $this->projectInternalId);
// Strip trailing document ID to keep aggregator cardinality bounded.
$log->addTag('lock.key_pattern', preg_replace('/:[^:]+$/', ':*', $key));
$log->addTag('code', $e->getCode());
$log->addExtra('file', $e->getFile());
$log->addExtra('line', $e->getLine());
$log->addExtra('trace', $e->getTraceAsString());
try {
$this->logger->addLog($log);
} catch (Throwable) {
}
}
}
+42 -4
View File
@@ -317,7 +317,19 @@ abstract class Migration
$attributes = $collection['attributes'];
$attributeKeys = \array_column($collection['attributes'], '$id');
$database->purgeCachedCollection($collectionId);
$existingIds = \array_map(
fn ($attribute) => $attribute->getId(),
$database->getCollection($collectionId)->getAttribute('attributes', [])
);
foreach ($attributeIds as $attributeId) {
if (\in_array($attributeId, $existingIds, true)) {
Console::warning("Skipping attribute \"{$attributeId}\" in collection {$collectionId}: Attribute already exists");
continue;
}
$attributeKey = \array_search($attributeId, $attributeKeys);
if ($attributeKey === false) {
@@ -334,10 +346,36 @@ abstract class Migration
$attributesToCreate[] = $attribute;
}
$database->createAttributes(
collection: $collectionId,
attributes: $attributesToCreate,
);
if (empty($attributesToCreate)) {
return;
}
try {
$database->createAttributes(
collection: $collectionId,
attributes: $attributesToCreate,
);
} catch (Duplicate) {
foreach ($attributesToCreate as $attribute) {
try {
$database->createAttribute(
collection: $collectionId,
id: $attribute['$id'],
type: $attribute['type'],
size: $attribute['size'],
required: $attribute['required'],
default: $attribute['default'],
signed: $attribute['signed'] ?? true,
array: $attribute['array'] ?? false,
format: $attribute['format'] ?? '',
formatOptions: $attribute['formatOptions'] ?? [],
filters: $attribute['filters'],
);
} catch (Duplicate) {
Console::warning("Skipping attribute \"{$attribute['$id']}\" in collection {$collectionId}: Attribute already exists");
}
}
}
}
/**
@@ -74,6 +74,9 @@ class Base extends Action
$providerInstallationId = $installation->getAttribute('providerInstallationId', '');
$privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY');
$githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID');
if (empty($providerInstallationId)) {
throw new Exception(Exception::INSTALLATION_NOT_FOUND);
}
$github->initializeVariables($providerInstallationId, $privateKey, $githubAppId);
$owner = $github->getOwnerName($providerInstallationId);
$providerRepositoryId = $function->getAttribute('providerRepositoryId', '');
@@ -166,6 +169,9 @@ class Base extends Action
$providerInstallationId = $installation->getAttribute('providerInstallationId', '');
$privateKey = System::getEnv('_APP_VCS_GITHUB_PRIVATE_KEY');
$githubAppId = System::getEnv('_APP_VCS_GITHUB_APP_ID');
if (empty($providerInstallationId)) {
throw new Exception(Exception::INSTALLATION_NOT_FOUND);
}
$github->initializeVariables($providerInstallationId, $privateKey, $githubAppId);
$owner = $github->getOwnerName($providerInstallationId);
$providerRepositoryId = $site->getAttribute('providerRepositoryId', '');
@@ -103,6 +103,10 @@ class Create extends Base
$installation = $dbForPlatform->getDocument('installations', $function->getAttribute('installationId'));
if ($installation->isEmpty()) {
throw new Exception(Exception::INSTALLATION_NOT_FOUND);
}
$deployment = $this->redeployVcsFunction(
request: $request,
function: $function,
@@ -11,6 +11,7 @@ use Appwrite\Event\Publisher\Func as FunctionPublisher;
use Appwrite\Extend\Exception;
use Appwrite\Extend\Exception as AppwriteException;
use Appwrite\Functions\Validator\Headers;
use Appwrite\Locale\GeoRecord;
use Appwrite\Platform\Modules\Compute\Base;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\Method;
@@ -20,7 +21,6 @@ use Appwrite\Utopia\Database\Documents\User;
use Appwrite\Utopia\Response;
use Executor\Exception\Timeout as ExecutorTimeout;
use Executor\Executor;
use MaxMind\Db\Reader;
use Utopia\Auth\Proofs\Token;
use Utopia\Auth\Store;
use Utopia\Config\Config;
@@ -98,7 +98,7 @@ class Create extends Base
->inject('queueForEvents')
->inject('usage')
->inject('publisherForFunctions')
->inject('geodb')
->inject('geoRecord')
->inject('store')
->inject('proofForToken')
->inject('executor')
@@ -126,7 +126,7 @@ class Create extends Base
Event $queueForEvents,
Context $usage,
FunctionPublisher $publisherForFunctions,
Reader $geodb,
GeoRecord $geoRecord,
Store $store,
Token $proofForToken,
Executor $executor,
@@ -241,16 +241,10 @@ class Create extends Base
$ip = $request->getIP();
$headers['x-appwrite-client-ip'] = $ip;
if (!empty($ip)) {
$record = $geodb->get($ip);
if ($record) {
$eu = Config::getParam('locale-eu');
$headers['x-appwrite-country-code'] = $record['country']['iso_code'] ?? '';
$headers['x-appwrite-continent-code'] = $record['continent']['code'] ?? '';
$headers['x-appwrite-continent-eu'] = (\in_array($record['country']['iso_code'], $eu)) ? 'true' : 'false';
}
if (!empty($ip) && !$geoRecord->isEmpty()) {
$headers['x-appwrite-country-code'] = $geoRecord->getCountryCode();
$headers['x-appwrite-continent-code'] = $geoRecord->getContinentCode();
$headers['x-appwrite-continent-eu'] = $geoRecord->isEu() ? 'true' : 'false';
}
$headersFiltered = [];
@@ -260,8 +254,6 @@ class Create extends Base
}
}
$status = $async ? 'waiting' : 'processing';
if (!is_null($scheduledAt)) {
@@ -749,7 +749,7 @@ class Builds extends Action
} else {
$outputDirectory = $deployment->getAttribute('buildOutput') ?? $resource->getAttribute('outputDirectory');
if ($resource->getCollection() === 'sites') {
$command = $this->prepareSiteBuildCommand($command, $outputDirectory ?? '');
$command = $this->prepareSiteBuildCommand($command, $outputDirectory ?? '', $resource->getAttribute('framework', ''));
}
$command = 'tar -zxf /tmp/code.tar.gz -C /mnt/code && helpers/build.sh ' . \trim(\escapeshellarg($command));
@@ -1399,7 +1399,7 @@ class Builds extends Action
return \substr(\hash('sha256', \json_encode($hashContext, JSON_THROW_ON_ERROR)), 0, 48);
}
protected function prepareSiteBuildCommand(string $command, string $outputDirectory): string
protected function prepareSiteBuildCommand(string $command, string $outputDirectory, string $framework): string
{
$listFilesCommand = 'echo "{APPWRITE_DETECTION_SEPARATOR_START}" && cd /usr/local/build';
@@ -1407,7 +1407,14 @@ class Builds extends Action
$listFilesCommand .= ' && cd ' . \escapeshellarg($outputDirectory);
}
$listFilesCommand .= ' && find . -name \'node_modules\' -prune -o -type f -print && echo "{APPWRITE_DETECTION_SEPARATOR_END}"';
foreach (SSR::FRAMEWORK_FILES[$framework] ?? [] as $file) {
$listFilesCommand .= ' && ( [ -e ' . \escapeshellarg($file) . ' ] && echo ' . \escapeshellarg($file) . ' || true )';
}
// Static fallback detection only needs to distinguish 0, 1, or 2+ HTML files, so cap the output
$listFilesCommand .= ' && find . -name \'node_modules\' -prune -o -type f -name \'*.html\' -print | head -n 2';
$listFilesCommand .= ' && echo "{APPWRITE_DETECTION_SEPARATOR_END}"';
if (empty($command)) {
return $listFilesCommand;
@@ -0,0 +1,83 @@
<?php
namespace Appwrite\Platform\Modules\Health\Http\Health\Geo;
use Appwrite\Extend\Exception;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\ContentType;
use Appwrite\SDK\Method;
use Appwrite\SDK\Response as SDKResponse;
use Appwrite\Utopia\Response;
use Utopia\Database\Document;
use Utopia\Fetch\Client;
use Utopia\Platform\Action;
use Utopia\Platform\Scope\HTTP;
use Utopia\System\System;
class Get extends Action
{
use HTTP;
public static function getName(): string
{
return 'getGeo';
}
public function __construct()
{
$this
->setHttpMethod(Action::HTTP_REQUEST_METHOD_GET)
->setHttpPath('/v1/health/geo')
->desc('Get geo')
->groups(['api', 'health'])
->label('scope', 'health.read')
->label('sdk', new Method(
namespace: 'health',
group: 'health',
name: 'getGeo',
description: '/docs/references/health/get-geo.md',
auth: [AuthType::ADMIN, AuthType::KEY],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_HEALTH_STATUS,
)
],
contentType: ContentType::JSON
))
->inject('response')
->callback($this->action(...));
}
public function action(Response $response): void
{
$geoEndpoint = System::getEnv('_APP_GEO_ENDPOINT', '');
if (empty($geoEndpoint)) {
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Geo service is not configured');
}
$checkStart = \microtime(true);
try {
$client = new Client();
$client->setTimeout(3000);
$result = $client->fetch(\rtrim($geoEndpoint, '/') . '/health', Client::METHOD_GET);
if ($result->getStatusCode() !== 200) {
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Geo service returned status ' . $result->getStatusCode());
}
} catch (Exception $e) {
throw $e;
} catch (\Throwable $th) {
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Geo service is unreachable: ' . $th->getMessage());
}
$response->dynamic(new Document([
'name' => 'geo',
'status' => 'pass',
'ping' => \round((\microtime(true) - $checkStart) * 1000),
]), Response::MODEL_HEALTH_STATUS);
}
}
@@ -1,60 +0,0 @@
<?php
namespace Appwrite\Platform\Modules\Health\Http\Health\Queue\Logs;
use Appwrite\Event\Publisher\Audit;
use Appwrite\Platform\Modules\Health\Http\Health\Queue\Base;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\ContentType;
use Appwrite\SDK\Method;
use Appwrite\SDK\Response as SDKResponse;
use Appwrite\Utopia\Response;
use Utopia\Database\Document;
use Utopia\Validator\Integer;
class Get extends Base
{
public static function getName(): string
{
return 'getQueueLogs';
}
public function __construct()
{
$this
->setHttpMethod(Base::HTTP_REQUEST_METHOD_GET)
->setHttpPath('/v1/health/queue/logs')
->desc('Get logs queue')
->groups(['api', 'health'])
->label('scope', 'health.read')
->label('sdk', new Method(
namespace: 'health',
group: 'queue',
name: 'getQueueLogs',
description: '/docs/references/health/get-queue-logs.md',
auth: [AuthType::ADMIN, AuthType::KEY],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_HEALTH_QUEUE,
)
],
contentType: ContentType::JSON
))
->param('threshold', 5000, new Integer(true), 'Queue size threshold. When hit (equal or higher), endpoint returns server error. Default value is 5000.', true)
->inject('publisherForAudits')
->inject('response')
->callback($this->action(...));
}
public function action(int|string $threshold, Audit $publisherForAudits, Response $response): void
{
$threshold = (int) $threshold;
$size = $publisherForAudits->getSize();
$this->assertQueueThreshold($size, $threshold);
$response->dynamic(new Document(['size' => $size]), Response::MODEL_HEALTH_QUEUE);
}
}
@@ -6,6 +6,7 @@ use Appwrite\Platform\Modules\Health\Http\Health\AntiVirus\Get as GetAntivirus;
use Appwrite\Platform\Modules\Health\Http\Health\Cache\Get as GetCache;
use Appwrite\Platform\Modules\Health\Http\Health\Certificate\Get as GetCertificate;
use Appwrite\Platform\Modules\Health\Http\Health\DB\Get as GetDB;
use Appwrite\Platform\Modules\Health\Http\Health\Geo\Get as GetGeo;
use Appwrite\Platform\Modules\Health\Http\Health\Get as GetHealth;
use Appwrite\Platform\Modules\Health\Http\Health\PubSub\Get as GetPubSub;
use Appwrite\Platform\Modules\Health\Http\Health\Queue\Audits\Get as GetQueueAudits;
@@ -15,7 +16,6 @@ use Appwrite\Platform\Modules\Health\Http\Health\Queue\Databases\Get as GetQueue
use Appwrite\Platform\Modules\Health\Http\Health\Queue\Deletes\Get as GetQueueDeletes;
use Appwrite\Platform\Modules\Health\Http\Health\Queue\Failed\Get as GetFailedJobs;
use Appwrite\Platform\Modules\Health\Http\Health\Queue\Functions\Get as GetQueueFunctions;
use Appwrite\Platform\Modules\Health\Http\Health\Queue\Logs\Get as GetQueueLogs;
use Appwrite\Platform\Modules\Health\Http\Health\Queue\Mails\Get as GetQueueMails;
use Appwrite\Platform\Modules\Health\Http\Health\Queue\Messaging\Get as GetQueueMessaging;
use Appwrite\Platform\Modules\Health\Http\Health\Queue\Migrations\Get as GetQueueMigrations;
@@ -46,10 +46,10 @@ class Http extends Service
$this->addAction(GetStorageLocal::getName(), new GetStorageLocal());
$this->addAction(GetStorage::getName(), new GetStorage());
$this->addAction(GetAntivirus::getName(), new GetAntivirus());
$this->addAction(GetGeo::getName(), new GetGeo());
$this->addAction(GetQueueAudits::getName(), new GetQueueAudits());
$this->addAction(GetQueueWebhooks::getName(), new GetQueueWebhooks());
$this->addAction(GetQueueLogs::getName(), new GetQueueLogs());
$this->addAction(GetQueueCertificates::getName(), new GetQueueCertificates());
$this->addAction(GetQueueBuilds::getName(), new GetQueueBuilds());
$this->addAction(GetQueueDatabases::getName(), new GetQueueDatabases());
@@ -106,6 +106,10 @@ class Create extends Base
$installation = $dbForPlatform->getDocument('installations', $site->getAttribute('installationId'));
if ($installation->isEmpty()) {
throw new Exception(Exception::INSTALLATION_NOT_FOUND);
}
$deployment = $this->redeployVcsSite(
request: $request,
site: $site,
@@ -240,6 +240,24 @@ class Get extends Action
$decompressionTime = \microtime(true) - $startTime - $downloadTime - $decryptionTime;
$maxWidth = \Imagick::getResourceLimit(\Imagick::RESOURCETYPE_WIDTH);
$maxHeight = \Imagick::getResourceLimit(\Imagick::RESOURCETYPE_HEIGHT);
$maxArea = \Imagick::getResourceLimit(\Imagick::RESOURCETYPE_AREA);
$dimensions = \getimagesizefromstring($source);
if ($dimensions !== false) {
[$sourceWidth, $sourceHeight] = $dimensions;
if (
($maxWidth > 0 && $sourceWidth > $maxWidth) ||
($maxHeight > 0 && $sourceHeight > $maxHeight) ||
($maxArea > 0 && $sourceWidth * $sourceHeight > $maxArea)
) {
throw new Exception(
Exception::STORAGE_IMAGE_RESOLUTION_EXCEEDED,
\sprintf('Image resolution %dx%d exceeds the maximum allowed %dx%d or %d total pixels', $sourceWidth, $sourceHeight, $maxWidth, $maxHeight, $maxArea)
);
}
}
try {
$image = new Image($source);
} catch (\Exception $e) {
@@ -5,13 +5,13 @@ namespace Appwrite\Platform\Modules\Teams\Http\Memberships\Status;
use Appwrite\Detector\Detector;
use Appwrite\Event\Event;
use Appwrite\Extend\Exception;
use Appwrite\Locale\GeoRecord;
use Appwrite\Platform\Action;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\Method;
use Appwrite\SDK\Response as SDKResponse;
use Appwrite\Utopia\Request;
use Appwrite\Utopia\Response;
use MaxMind\Db\Reader;
use Utopia\Auth\Proofs\Token;
use Utopia\Auth\Store;
use Utopia\Config\Config;
@@ -70,7 +70,7 @@ class Update extends Action
->inject('dbForProject')
->inject('authorization')
->inject('project')
->inject('geodb')
->inject('geoRecord')
->inject('queueForEvents')
->inject('store')
->inject('proofForToken')
@@ -79,7 +79,7 @@ class Update extends Action
->callback($this->action(...));
}
public function action(string $teamId, string $membershipId, string $userId, string $secret, Request $request, Response $response, Document $targetUser, Database $dbForProject, Authorization $authorization, $project, Reader $geodb, Event $queueForEvents, Store $store, Token $proofForToken, bool $domainVerification, ?string $cookieDomain)
public function action(string $teamId, string $membershipId, string $userId, string $secret, Request $request, Response $response, Document $targetUser, Database $dbForProject, Authorization $authorization, $project, GeoRecord $geoRecord, Event $queueForEvents, Store $store, Token $proofForToken, bool $domainVerification, ?string $cookieDomain)
{
$protocol = $request->getProtocol();
@@ -133,7 +133,6 @@ class Update extends Action
$authorization->addRole(Role::user($targetUser->getId())->toString());
$detector = new Detector($request->getUserAgent('UNKNOWN'));
$record = $geodb->get($request->getIP());
$authDuration = $project->getAttribute('auths', [])['duration'] ?? TOKEN_EXPIRATION_LOGIN_LONG;
$expire = DateTime::addSeconds(new \DateTime(), $authDuration);
$secret = $proofForToken->generate();
@@ -152,7 +151,7 @@ class Update extends Action
'userAgent' => $request->getUserAgent('UNKNOWN'),
'ip' => $request->getIP(),
'factors' => ['email'],
'countryCode' => ($record) ? \strtolower($record['country']['iso_code']) : '--',
'countryCode' => \strtolower($geoRecord->getCountryCode()),
'expire' => DateTime::addSeconds(new \DateTime(), $authDuration)
], $detector->getOS(), $detector->getClient(), $detector->getDevice()));
@@ -3306,4 +3306,33 @@ final class FunctionsCustomServerTest extends Scope
}
}
public function testCreateVcsDeploymentWithoutInstallation(): void
{
$function = $this->createFunction([
'functionId' => ID::unique(),
'name' => 'Test VCS No Installation',
'runtime' => 'node-22',
'entrypoint' => 'index.js',
]);
$this->assertEquals(201, $function['headers']['status-code']);
$functionId = $function['body']['$id'];
/**
* Test for FAILURE
*/
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments/vcs', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'type' => 'branch',
'reference' => 'main',
]);
$this->assertEquals(404, $deployment['headers']['status-code']);
$this->assertEquals('installation_not_found', $deployment['body']['type']);
$this->cleanupFunction($functionId);
}
}
-7
View File
@@ -264,7 +264,6 @@ trait Base
public const string GET_CACHE_HEALTH = 'get_cache_health';
public const string GET_TIME_HEALTH = 'get_time_health';
public const string GET_WEBHOOKS_QUEUE_HEALTH = 'get_webhooks_queue_health';
public const string GET_LOGS_QUEUE_HEALTH = 'get_logs_queue_health';
public const string GET_CERTIFICATES_QUEUE_HEALTH = 'get_certificates_queue_health';
public const string GET_FUNCTION_QUEUE_HEALTH = 'get_functions_queue_health';
public const string GET_LOCAL_STORAGE_HEALTH = 'get_local_storage_health';
@@ -2451,12 +2450,6 @@ trait Base
size
}
}';
case self::GET_LOGS_QUEUE_HEALTH:
return 'query getLogsQueueHealth {
healthGetQueueLogs {
size
}
}';
case self::GET_CERTIFICATES_QUEUE_HEALTH:
return 'query getCertificatesQueueHealth {
healthGetQueueCertificates {
-21
View File
@@ -124,27 +124,6 @@ final class HealthTest extends Scope
return $webhooksQueueHealth;
}
public function testGetLogsQueueHealth(): array
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::GET_LOGS_QUEUE_HEALTH);
$graphQLPayload = [
'query' => $query,
];
$logsQueueHealth = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $graphQLPayload);
$this->assertIsArray($logsQueueHealth['body']['data']);
$this->assertArrayNotHasKey('errors', $logsQueueHealth['body']);
$logsQueueHealth = $logsQueueHealth['body']['data']['healthGetQueueLogs'];
$this->assertIsArray($logsQueueHealth);
return $logsQueueHealth;
}
public function testGetCertificatesQueueHealth(): array
{
$projectId = $this->getProject()['$id'];
@@ -1,19 +0,0 @@
<?php
declare(strict_types=1);
namespace Tests\E2E\Services\Health;
final class LogsQueueTest extends HealthBase
{
public function testLogsQueue(): void
{
$response = $this->callGet('/health/queue/logs');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertIsInt($response['body']['size']);
$this->assertLessThan(100, $response['body']['size']);
$failure = $this->callGet('/health/queue/logs', ['threshold' => '0']);
$this->assertEquals(503, $failure['headers']['status-code']);
}
}
@@ -836,7 +836,7 @@ final class ProjectsConsoleClientTest extends Scope
$this->assertEquals(201, $response['headers']['status-code']);
$id = $response['body']['$id'];
// Increase ping 3x
// Ping 3x; only the first is recorded, so pingCount stays at 1
for ($i = 0; $i < 3; $i++) {
$response = $this->client->call(
Client::METHOD_GET,
@@ -1113,7 +1113,7 @@ final class ProjectsConsoleClientTest extends Scope
$this->assertSame('', $response['body']['smtpUsername']);
$this->assertSame('', $response['body']['smtpPassword']); // Write only
$this->assertSame('', $response['body']['smtpSecure']);
$this->assertSame(3, $response['body']['pingCount']);
$this->assertSame(1, $response['body']['pingCount']);
$this->assertIsString($response['body']['pingedAt']);
$this->assertNotEmpty($response['body']['pingedAt']);
@@ -3089,4 +3089,35 @@ final class SitesCustomServerTest extends Scope
$this->cleanupSite($siteId);
}
}
public function testCreateVcsDeploymentWithoutInstallation(): void
{
$site = $this->createSite([
'siteId' => ID::unique(),
'name' => 'Test VCS No Installation',
'framework' => 'other',
'buildRuntime' => 'node-22',
'outputDirectory' => './',
'fallbackFile' => '',
]);
$this->assertEquals(201, $site['headers']['status-code']);
$siteId = $site['body']['$id'];
/**
* Test for FAILURE
*/
$deployment = $this->client->call(Client::METHOD_POST, '/sites/' . $siteId . '/deployments/vcs', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'type' => 'branch',
'reference' => 'main',
]);
$this->assertEquals(404, $deployment['headers']['status-code']);
$this->assertEquals('installation_not_found', $deployment['body']['type']);
$this->cleanupSite($siteId);
}
}
@@ -1006,6 +1006,37 @@ trait StorageBase
$this->assertEquals(404, $file8['headers']['status-code']);
}
public function testFilePreviewOversized(): void
{
$data = $this->setupBucketFile();
$bucketId = $data['bucketId'];
$file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', array_merge([
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'fileId' => ID::unique(),
'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/disk-a/image-bomb.png'), 'image/png', 'image-bomb.png'),
'permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
]);
$this->assertEquals(201, $file['headers']['status-code']);
$this->assertNotEmpty($file['body']['$id']);
$preview = $this->client->call(Client::METHOD_GET, '/storage/buckets/' . $bucketId . '/files/' . $file['body']['$id'] . '/preview', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals(400, $preview['headers']['status-code']);
$this->assertEquals(Exception::STORAGE_IMAGE_RESOLUTION_EXCEEDED, $preview['body']['type']);
$this->assertStringContainsString('60000x1', $preview['body']['message']);
}
public function testFilePreviewCache(): void
{
$data = $this->setupBucketFile();
Binary file not shown.

After

Width:  |  Height:  |  Size: 138 B

+1 -1
View File
@@ -1 +1 @@
{"swagger":"2.0","info":{"version":"0.8.0","title":"Appwrite","description":"Appwrite backend as a service cuts up to 70% of the time and costs required for building a modern application. We abstract and simplify common development tasks behind a REST APIs, to help you develop your app in a fast and secure way. For full API documentation and tutorials go to [https:\/\/appwrite.io\/docs](https:\/\/appwrite.io\/docs)","termsOfService":"https:\/\/appwrite.io\/policy\/terms","contact":{"name":"Appwrite Team","url":"https:\/\/appwrite.io\/support","email":"team@appwrite.io"},"license":{"name":"BSD-3-Clause","url":"https:\/\/raw.githubusercontent.com\/appwrite\/appwrite\/master\/LICENSE"}},"host":"appwrite.io","basePath":"\/v1","schemes":["https"],"consumes":["application\/json","multipart\/form-data"],"produces":["application\/json"],"securityDefinitions":{"Project":{"type":"apiKey","name":"X-Appwrite-Project","description":"Your project ID","in":"header","x-appwrite":{"demo":"5df5acd0d48c2"}},"Key":{"type":"apiKey","name":"X-Appwrite-Key","description":"Your secret API key","in":"header","x-appwrite":{"demo":"919c2d18fb5d4...a2ae413da83346ad2"}},"JWT":{"type":"apiKey","name":"X-Appwrite-JWT","description":"Your secret JSON Web Token","in":"header","x-appwrite":{"demo":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ..."}},"Locale":{"type":"apiKey","name":"X-Appwrite-Locale","description":"","in":"header","x-appwrite":{"demo":"en"}},"Mode":{"type":"apiKey","name":"X-Appwrite-Mode","description":"","in":"header","x-appwrite":{"demo":""}}},"paths":{"\/account":{"get":{"summary":"Get Account","operationId":"accountGet","consumes":["application\/json"],"produces":["application\/json"],"tags":["account"],"description":"Get currently logged in user data as JSON object.","responses":{"200":{"description":"User","schema":{"$ref":"#\/definitions\/user"}}},"x-appwrite":{"method":"get","weight":44,"cookies":false,"type":"","demo":"account\/get.md","edit":"https:\/\/github.com\/appwrite\/appwrite\/edit\/master\/docs\/references\/account\/get.md","rate-limit":0,"rate-time":3600,"rate-key":"url:{url},ip:{ip}","scope":"account","platforms":["client","server"],"packaging":false,"auth":{"Project":[]}},"security":[{"Project":[],"JWT":[]}]},"post":{"summary":"Create Account","operationId":"accountCreate","consumes":["application\/json"],"produces":["application\/json"],"tags":["account"],"description":"Use this endpoint to allow a new user to register a new account in your project. After the user registration completes successfully, you can use the [\/account\/verfication](\/docs\/client\/account#accountCreateVerification) route to start verifying the user email address. To allow the new user to login to their new account, you need to create a new [account session](\/docs\/client\/account#accountCreateSession).","responses":{"201":{"description":"User","schema":{"$ref":"#\/definitions\/user"}}},"x-appwrite":{"method":"create","weight":36,"cookies":false,"type":"","demo":"account\/create.md","edit":"https:\/\/github.com\/appwrite\/appwrite\/edit\/master\/docs\/references\/account\/create.md","rate-limit":10,"rate-time":3600,"rate-key":"url:{url},ip:{ip}","scope":"public","platforms":["client"],"packaging":false,"auth":{"Project":[]}},"security":[{"Project":[]}],"parameters":[{"name":"payload","in":"body","schema":{"type":"object","properties":{"email":{"type":"string","description":"User email.","default":null,"x-example":"email@example.com"},"password":{"type":"string","description":"User password. Must be between 6 to 32 chars.","default":null,"x-example":"password"},"name":{"type":"string","description":"User name. Max length: 128 chars.","default":"","x-example":"[NAME]"}},"required":["email","password"]}}]},"delete":{"summary":"Delete Account","operationId":"accountDelete","consumes":["application\/json"],"produces":[],"tags":["account"],"description":"Delete a currently logged in user account. Behind the scene, the user record is not deleted but permanently blocked from any access. This is done to avoid deleted accounts being overtaken by new users with the same email address. Any user-related resources like documents or storage files should be deleted separately.","responses":{"204":{"description":"No content"}},"x-appwrite":{"method":"delete","weight":52,"cookies":false,"type":"","demo":"account\/delete.md","edit":"https:\/\/github.com\/appwrite\/appwrite\/edit\/master\/docs\/references\/account\/delete.md","rate-limit":0,"rate-time":3600,"rate-key":"url:{url},ip:{ip}","scope":"account","platforms":["client","server"],"packaging":false,"auth":{"Project":[]}},"security":[{"Project":[],"JWT":[]}]}},"\/account\/email":{"patch":{"summary":"Update Account Email","operationId":"accountUpdateEmail","consumes":["application\/json"],"produces":["application\/json"],"tags":["account"],"description":"Update currently logged in user account email address. After changing user address, user confirmation status is being reset and a new confirmaLine truncated
{"swagger":"2.0","info":{"version":"0.8.0","title":"Appwrite","description":"Appwrite backend as a service cuts up to 70% of the time and costs required for building a modern application. We abstract and simplify common development tasks behind a REST APIs, to help you develop your app in a fast and secure way. For full API documentation and tutorials go to [https://appwrite.io/docs](https://appwrite.io/docs)","termsOfService":"https://appwrite.io/policy/terms","contact":{"name":"Appwrite Team","url":"https://appwrite.io/support","email":"team@appwrite.io"},"license":{"name":"BSD-3-Clause","url":"https://raw.githubusercontent.com/appwrite/appwrite/master/LICENSE"}},"host":"appwrite.io","basePath":"/v1","schemes":["https"],"consumes":["application/json","multipart/form-data"],"produces":["application/json"],"securityDefinitions":{"Project":{"type":"apiKey","name":"X-Appwrite-Project","description":"Your project ID","in":"header","x-appwrite":{"demo":"5df5acd0d48c2"}},"Key":{"type":"apiKey","name":"X-Appwrite-Key","description":"Your secret API key","in":"header","x-appwrite":{"demo":"919c2d18fb5d4...a2ae413da83346ad2"}},"JWT":{"type":"apiKey","name":"X-Appwrite-JWT","description":"Your secret JSON Web Token","in":"header","x-appwrite":{"demo":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ..."}},"Locale":{"type":"apiKey","name":"X-Appwrite-Locale","description":"","in":"header","x-appwrite":{"demo":"en"}},"Mode":{"type":"apiKey","name":"X-Appwrite-Mode","description":"","in":"header","x-appwrite":{"demo":""}}},"paths":{"/account":{"get":{"summary":"Get Account","operationId":"accountGet","consumes":["application/json"],"produces":["application/json"],"tags":["account"],"description":"Get currently logged in user data as JSON object.","responses":{"200":{"description":"User","schema":{"$ref":"#/definitions/user"}}},"x-appwrite":{"method":"get","weight":44,"cookies":false,"type":"","demo":"account/get.md","edit":"https://github.com/appwrite/appwrite/edit/master/docs/references/account/get.md","rate-limit":0,"rate-time":3600,"rate-key":"url:{url},ip:{ip}","scope":"account","platforms":["client","server"],"packaging":false,"auth":{"Project":[]}},"security":[{"Project":[],"JWT":[]}]},"post":{"summary":"Create Account","operationId":"accountCreate","consumes":["application/json"],"produces":["application/json"],"tags":["account"],"description":"Use this endpoint to allow a new user to register a new account in your project. After the user registration completes successfully, you can use the [/account/verfication](/docs/client/account#accountCreateVerification) route to start verifying the user email address. To allow the new user to login to their new account, you need to create a new [account session](/docs/client/account#accountCreateSession).","responses":{"201":{"description":"User","schema":{"$ref":"#/definitions/user"}}},"x-appwrite":{"method":"create","weight":36,"cookies":false,"type":"","demo":"account/create.md","edit":"https://github.com/appwrite/appwrite/edit/master/docs/references/account/create.md","rate-limit":10,"rate-time":3600,"rate-key":"url:{url},ip:{ip}","scope":"public","platforms":["client"],"packaging":false,"auth":{"Project":[]}},"security":[{"Project":[]}],"parameters":[{"name":"payload","in":"body","schema":{"type":"object","properties":{"email":{"type":"string","description":"User email.","default":null,"x-example":"email@example.com"},"password":{"type":"string","description":"User password. Must be between 6 to 32 chars.","default":null,"x-example":"password"},"name":{"type":"string","description":"User name. Max length: 128 chars.","default":"","x-example":"[NAME]"}},"required":["email","password"]}}]},"delete":{"summary":"Delete Account","operationId":"accountDelete","consumes":["application/json"],"produces":[],"tags":["account"],"description":"Delete a currently logged in user account. Behind the scene, the user record is not deleted but permanently blocked from any access. This is done to avoid deleted accounts being overtaken by new users with the same email address. Any user-related resources like documents or storage files should be deleted separately.","responses":{"204":{"description":"No content"}},"x-appwrite":{"method":"delete","weight":52,"cookies":false,"type":"","demo":"account/delete.md","edit":"https://github.com/appwrite/appwrite/edit/master/docs/references/account/delete.md","rate-limit":0,"rate-time":3600,"rate-key":"url:{url},ip:{ip}","scope":"account","platforms":["client","server"],"packaging":false,"auth":{"Project":[]}},"security":[{"Project":[],"JWT":[]}]}},"/account/email":{"patch":{"summary":"Update Account Email","operationId":"accountUpdateEmail","consumes":["application/json"],"produces":["application/json"],"tags":["account"],"description":"Update currently logged in user account email address. After changing user address, user confirmation status is being reset and a new confirmation mail is sent. For security measures, user password is required to completLine truncated
+6 -4
View File
@@ -45,20 +45,22 @@ final class BuildsTest extends TestCase
public function testSiteBuildCommandAppendsDetectionFileListing(): void
{
$command = $this->callBuilds('prepareSiteBuildCommand', 'npm run build', './dist folder');
$command = $this->callBuilds('prepareSiteBuildCommand', 'npm run build', './dist folder', 'astro');
$this->assertStringStartsWith('npm run build && echo "{APPWRITE_DETECTION_SEPARATOR_START}"', $command);
$this->assertStringContainsString('cd /usr/local/build && cd \'./dist folder\'', (string) $command);
$this->assertStringContainsString('find . -name \'node_modules\' -prune -o -type f -print', (string) $command);
$this->assertStringContainsString('( [ -e \'server/entry.mjs\' ] && echo \'server/entry.mjs\' || true )', (string) $command);
$this->assertStringContainsString('find . -name \'node_modules\' -prune -o -type f -name \'*.html\' -print | head -n 2', (string) $command);
$this->assertStringEndsWith('echo "{APPWRITE_DETECTION_SEPARATOR_END}"', $command);
}
public function testSiteBuildCommandCanBeOnlyDetectionFileListing(): void
{
$command = $this->callBuilds('prepareSiteBuildCommand', '', '');
$command = $this->callBuilds('prepareSiteBuildCommand', '', '', 'sveltekit');
$this->assertStringStartsWith('echo "{APPWRITE_DETECTION_SEPARATOR_START}"', $command);
$this->assertStringContainsString('cd /usr/local/build && find .', (string) $command);
$this->assertStringContainsString('cd /usr/local/build && ( [ -e \'handler.js\' ] && echo \'handler.js\' || true )', (string) $command);
$this->assertStringContainsString('find . -name \'node_modules\' -prune -o -type f -name \'*.html\' -print | head -n 2', (string) $command);
}
public function testSplitSiteDetectionLogsRemovesDetectionBlock(): void
+549
View File
@@ -0,0 +1,549 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Locking;
use Appwrite\Extend\Exception;
use Appwrite\Locking\Lock;
use PHPUnit\Framework\TestCase;
use Utopia\Config\Config;
use Utopia\Database\Document;
use Utopia\Lock\Exception\Contention as LockContention;
use Utopia\Lock\Lock as UtopiaLock;
use Utopia\Logger\Adapter as LoggerAdapter;
use Utopia\Logger\Log;
use Utopia\Logger\Logger;
use Utopia\Telemetry\Adapter\None as NoTelemetry;
if (! \class_exists(\RedisException::class)) {
\class_alias(LockingRedisException::class, 'RedisException');
}
if (! \defined('APP_VERSION_STABLE')) {
\define('APP_VERSION_STABLE', 'test');
}
final class LockTest extends TestCase
{
/**
* @var array<string, bool>
*/
private array $heldLocks = [];
private Document $project;
private const PROJECT_SEQUENCE = '42';
private const KEY_PREFIX = 'lock:platform:'.self::PROJECT_SEQUENCE.':';
protected function setUp(): void
{
Config::setParam('errors', require __DIR__.'/../../../app/config/errors.php');
$this->heldLocks = [];
$this->project = new Document([
'$id' => 'test-project',
'$sequence' => self::PROJECT_SEQUENCE,
]);
}
private function makeLock(): Lock
{
return new Lock(
$this->withLock(),
new NoTelemetry(),
null,
$this->project,
);
}
private function withLock(): \Closure
{
return fn (string $key, int $ttl, \Closure $callback): mixed => $callback(new MemoryLock($key, $this->heldLocks));
}
public function testTryWithKeyUsesGivenKeyAndInvokesCallback(): void
{
$called = false;
$lock = $this->makeLock();
$lock->tryWithKey(self::KEY_PREFIX.'keys:k1', function () use (&$called) {
$called = true;
}, target: 'keys');
$this->assertTrue($called);
$this->assertArrayNotHasKey(self::KEY_PREFIX.'keys:k1', $this->heldLocks);
}
public function testTryWithKeyUsesShortTryOnceLockWindow(): void
{
$ttl = null;
$timeout = null;
$lock = new Lock(
function (string $key, int $lockTtl, \Closure $callback) use (&$ttl, &$timeout): mixed {
$ttl = $lockTtl;
return $callback(new InspectingLock(new MemoryLock($key, $this->heldLocks), function (float $acquireTimeout) use (&$timeout): void {
$timeout = $acquireTimeout;
}));
},
new NoTelemetry(),
null,
$this->project,
);
$lock->tryWithKey(self::KEY_PREFIX.'keys:k1', fn () => null, target: 'keys');
$this->assertSame(5, $ttl);
$this->assertEqualsWithDelta(0.0, $timeout, PHP_FLOAT_EPSILON);
}
public function testTryWithKeySkipsOnContention(): void
{
$key = self::KEY_PREFIX.'keys:k1';
$this->heldLocks[$key] = true;
$called = false;
$lock = $this->makeLock();
$lock->tryWithKey($key, function () use (&$called) {
$called = true;
}, target: 'keys');
$this->assertFalse($called);
$this->assertArrayHasKey($key, $this->heldLocks);
}
public function testWithKeyUsesShortHttpLockTtlAndAcquireTimeout(): void
{
$ttl = null;
$timeout = null;
$lock = new Lock(
function (string $key, int $lockTtl, \Closure $callback) use (&$ttl, &$timeout): mixed {
$ttl = $lockTtl;
return $callback(new InspectingLock(new MemoryLock($key, $this->heldLocks), function (float $acquireTimeout) use (&$timeout): void {
$timeout = $acquireTimeout;
}));
},
new NoTelemetry(),
null,
$this->project,
);
$lock->withKey(self::KEY_PREFIX.'keys:k1', fn () => null, target: 'keys');
$this->assertSame(10, $ttl);
$this->assertEqualsWithDelta(3.0, $timeout, PHP_FLOAT_EPSILON);
}
public function testBestEffortBackendErrorRunsCallbackUnlocked(): void
{
$lock = new Lock(
fn (string $key, int $ttl, \Closure $callback): mixed => $callback(new ThrowingAcquireLock(new \RedisException('redis unavailable'))),
new NoTelemetry(),
null,
$this->project,
);
$called = false;
$lock->tryWithKey(self::KEY_PREFIX.'keys:k1', function () use (&$called) {
$called = true;
}, target: 'keys');
$this->assertTrue($called);
}
public function testBackendErrorReportDoesNotMutateRequestLog(): void
{
$requestLog = new Log();
$adapter = new RecordingLoggerAdapter();
$logger = new Logger($adapter);
$lock = new Lock(
fn (string $key, int $ttl, \Closure $callback): mixed => $callback(new ThrowingAcquireLock(new \RedisException('redis unavailable'))),
new NoTelemetry(),
$logger,
$this->project,
);
$lock->tryWithKey(self::KEY_PREFIX.'keys:k1', fn () => null, target: 'keys');
$this->assertCount(1, $adapter->logs);
$this->assertNotSame($requestLog, $adapter->logs[0]);
$this->assertSame([], $requestLog->getTags());
$this->assertSame([], $requestLog->getExtra());
}
public function testWithKeyBackendErrorRunsCallbackUnlocked(): void
{
$lock = new Lock(
fn (string $key, int $ttl, \Closure $callback): mixed => $callback(new ThrowingAcquireLock(new \RedisException('redis unavailable'))),
new NoTelemetry(),
null,
$this->project,
);
$called = false;
$lock->withKey(self::KEY_PREFIX.'keys:k1', function () use (&$called) {
$called = true;
}, target: 'keys');
$this->assertTrue($called);
}
public function testBestEffortPoolCheckoutErrorRunsCallbackUnlocked(): void
{
$lock = new Lock(
fn (string $key, int $ttl, \Closure $callback): mixed => throw new \RedisException('pool unavailable'),
new NoTelemetry(),
null,
$this->project,
);
$called = false;
$lock->tryWithKey(self::KEY_PREFIX.'keys:k1', function () use (&$called) {
$called = true;
}, target: 'keys');
$this->assertTrue($called);
}
public function testRedisExceptionThrownByCallbackIsNotTreatedAsBackendError(): void
{
$lock = $this->makeLock();
$this->expectException(\RedisException::class);
$lock->tryWithKey(self::KEY_PREFIX.'keys:k1', fn () => throw new \RedisException('callback failed'), target: 'keys');
}
public function testReleaseErrorAfterCallbackIsLoggedButNotThrown(): void
{
$lock = new Lock(
fn (string $key, int $ttl, \Closure $callback): mixed => $callback(new ThrowingReleaseLock(new MemoryLock($key, $this->heldLocks), new \RedisException('release failed'))),
new NoTelemetry(),
null,
$this->project,
);
$this->assertSame('ok', $lock->withKey(self::KEY_PREFIX.'keys:k1', fn () => 'ok', target: 'keys'));
}
public function testWithKeyThrowsOnContention(): void
{
$key = self::KEY_PREFIX.'projects:p1';
$this->heldLocks[$key] = true;
$lock = $this->makeLock();
$this->expectException(Exception::class);
try {
$lock->withKey($key, fn () => 'never-runs', target: 'projects');
} catch (Exception $e) {
$this->assertSame(Exception::GENERAL_RESOURCE_LOCKED, $e->getType());
throw $e;
}
}
public function testWithKeyReturnsCallbackValueWhenUncontended(): void
{
$lock = $this->makeLock();
$result = $lock->withKey(self::KEY_PREFIX.'projects:p1', fn () => 'ok', target: 'projects');
$this->assertSame('ok', $result);
$this->assertArrayNotHasKey(self::KEY_PREFIX.'projects:p1', $this->heldLocks);
}
public function testWithKeyUsesRawKey(): void
{
$custom = 'lock:test:custom-key';
$called = false;
$lock = $this->makeLock();
$lock->withKey($custom, function () use (&$called) {
$called = true;
}, target: 'custom');
$this->assertTrue($called);
$this->assertArrayNotHasKey($custom, $this->heldLocks);
}
public function testTryWithKeySkipsGivenKeyOnContention(): void
{
$custom = 'lock:test:contended';
$this->heldLocks[$custom] = true;
$lock = $this->makeLock();
$called = false;
$lock->tryWithKey($custom, function () use (&$called): void {
$called = true;
}, target: 'custom', ttl: 5);
$this->assertFalse($called);
}
public function testWithKeyThrowsGivenKeyOnContention(): void
{
$custom = 'lock:test:contended';
$this->heldLocks[$custom] = true;
$lock = $this->makeLock();
$this->expectException(Exception::class);
$lock->withKey($custom, fn () => null, target: 'custom', ttl: 5, waitTimeout: 0.1);
}
public function testDisabledModeRunsCallbackUnlocked(): void
{
$previous = \getenv('_APP_LOCKING_ENABLED');
\putenv('_APP_LOCKING_ENABLED=disabled');
try {
$key = self::KEY_PREFIX.'keys:k1';
$this->heldLocks[$key] = true;
$called = false;
$lock = $this->makeLock();
$lock->tryWithKey($key, function () use (&$called) {
$called = true;
}, target: 'keys');
$this->assertTrue($called);
$this->assertArrayHasKey($key, $this->heldLocks);
} finally {
$previous === false ? \putenv('_APP_LOCKING_ENABLED') : \putenv('_APP_LOCKING_ENABLED='.$previous);
}
}
public function testPoolCheckoutExceptionRunsCallbackUnlocked(): void
{
$lock = new Lock(
fn (string $key, int $ttl, \Closure $callback): mixed => throw new \Exception('Pool \'lock\' is empty'),
new NoTelemetry(),
null,
$this->project,
);
$called = false;
$lock->tryWithKey(self::KEY_PREFIX.'keys:k1', function () use (&$called) {
$called = true;
}, target: 'keys');
$this->assertTrue($called);
}
public function testNonPoolExceptionIsNotSwallowed(): void
{
$lock = new Lock(
fn (string $key, int $ttl, \Closure $callback): mixed => throw new \RuntimeException('unexpected'),
new NoTelemetry(),
null,
$this->project,
);
$this->expectException(\RuntimeException::class);
$lock->tryWithKey(self::KEY_PREFIX.'keys:k1', fn () => null, target: 'keys');
}
}
final class MemoryLock implements UtopiaLock
{
private bool $acquired = false;
/**
* @param array<string, bool> $heldLocks
*/
public function __construct(
private readonly string $key,
private array &$heldLocks,
) {
}
public function acquire(float $timeout = 0.0): bool
{
return $this->tryAcquire();
}
public function tryAcquire(): bool
{
if (isset($this->heldLocks[$this->key])) {
return false;
}
$this->heldLocks[$this->key] = true;
$this->acquired = true;
return true;
}
public function release(): void
{
if (! $this->acquired) {
return;
}
unset($this->heldLocks[$this->key]);
$this->acquired = false;
}
public function withLock(callable $callback, float $timeout = 0.0): mixed
{
if (! $this->acquire($timeout)) {
throw new LockContention("Failed to acquire distributed lock: {$this->key}");
}
try {
return $callback();
} finally {
$this->release();
}
}
}
final class InspectingLock implements UtopiaLock
{
public function __construct(
private readonly UtopiaLock $lock,
private readonly \Closure $inspectTimeout,
) {
}
public function acquire(float $timeout = 0.0): bool
{
($this->inspectTimeout)($timeout);
return $this->lock->acquire($timeout);
}
public function tryAcquire(): bool
{
return $this->lock->tryAcquire();
}
public function release(): void
{
$this->lock->release();
}
public function withLock(callable $callback, float $timeout = 0.0): mixed
{
if (! $this->acquire($timeout)) {
throw new LockContention('Failed to acquire distributed lock');
}
try {
return $callback();
} finally {
$this->release();
}
}
}
final class ThrowingAcquireLock implements UtopiaLock
{
public function __construct(private readonly \Throwable $throwable)
{
}
public function acquire(float $timeout = 0.0): bool
{
throw $this->throwable;
}
public function tryAcquire(): bool
{
throw $this->throwable;
}
public function release(): void
{
}
public function withLock(callable $callback, float $timeout = 0.0): mixed
{
throw $this->throwable;
}
}
final class ThrowingReleaseLock implements UtopiaLock
{
public function __construct(
private readonly UtopiaLock $lock,
private readonly \Throwable $throwable,
) {
}
public function acquire(float $timeout = 0.0): bool
{
return $this->lock->acquire($timeout);
}
public function tryAcquire(): bool
{
return $this->lock->tryAcquire();
}
public function release(): void
{
$this->lock->release();
throw $this->throwable;
}
public function withLock(callable $callback, float $timeout = 0.0): mixed
{
if (! $this->acquire($timeout)) {
throw new LockContention('Failed to acquire distributed lock');
}
try {
return $callback();
} finally {
$this->release();
}
}
}
final class LockingRedisException extends \Exception
{
}
final class RecordingLoggerAdapter extends LoggerAdapter
{
/**
* @var list<Log>
*/
public array $logs = [];
public static function getName(): string
{
return 'recording';
}
public function push(Log $log): int
{
$this->logs[] = $log;
return 200;
}
public function getSupportedTypes(): array
{
return [
Log::TYPE_WARNING,
];
}
public function getSupportedEnvironments(): array
{
return [
Log::ENVIRONMENT_PRODUCTION,
Log::ENVIRONMENT_STAGING,
];
}
public function getSupportedBreadcrumbTypes(): array
{
return [];
}
}
@@ -132,4 +132,55 @@ final class MigrationVersionsTest extends TestCase
$this->assertArrayHasKey('firstSeen', $attributes);
$this->assertArrayHasKey('lastSeen', $attributes);
}
public function testCreateAttributesFromCollectionSkipsExistingAttributes(): void
{
require_once __DIR__ . '/../../../app/init.php';
$authorization = new Authorization();
$database = new Database(new Memory(), new Cache(new NoCache()));
$database
->setAuthorization($authorization)
->setDatabase('migrationV24Functions')
->setNamespace('migration_functions_' . \uniqid());
$database->create();
$database->createCollection('functions');
$migration = new V24();
$migration->setProject(
new Document(['$id' => 'project', '$sequence' => '1']),
$database,
$database,
$authorization,
);
$existing = [
'deploymentRetention',
'startCommand',
'buildSpecification',
'runtimeSpecification',
];
$new = [
'providerBranches',
'providerPaths',
];
\ob_start();
try {
$migration->createAttributesFromCollection($database, 'functions', $existing);
$migration->createAttributesFromCollection($database, 'functions', [...$existing, ...$new]);
$migration->createAttributesFromCollection($database, 'functions', [...$existing, ...$new]);
} finally {
\ob_end_clean();
}
$attributes = [];
foreach ($database->getCollection('functions')->getAttribute('attributes', []) as $attribute) {
$attributes[] = $attribute instanceof Document ? $attribute->getAttribute('$id') : ($attribute['$id'] ?? '');
}
foreach ([...$existing, ...$new] as $id) {
$this->assertContains($id, $attributes);
}
}
}