From 12693434b76ad1450bfd770845274ff19e0d39df Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 28 May 2026 20:10:01 +1200 Subject: [PATCH 01/14] fix(avatars): block SSRF in favicon endpoint via redirect chains MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The favicon endpoint validated the request URL with Domain::isKnown() (a Public Suffix List check only) and then delegated redirect following to curl with no per-hop validation. An authenticated caller could point ?url= at any public domain that 302s to an internal address — link-local IMDS (169.254.169.254), in-cluster services, RFC1918 hosts — and the response body would come back as image/x-icon. The body guard only rejected HTML prefixes, so JSON or binary internal payloads passed through unchanged. Adds Appwrite\Network\Validator\PublicHostname which resolves A + AAAA records and rejects any address in a private, loopback, link-local, multicast, broadcast, IPv4-mapped, 6to4, CGNAT, or reserved range — including the cases PHP's FILTER_FLAG_NO_PRIV_RANGE | NO_RES_RANGE misses. The favicon action now disables curl auto-redirects and walks each Location header itself, re-validating scheme + PSL + PublicHostname on every hop. Non-http(s) redirect targets are rejected outright. Known follow-up: a TOCTOU window remains between the resolver call and curl's own DNS lookup. Closing it cleanly needs CURLOPT_RESOLVE pinning, which utopia-php/fetch does not currently expose — tracked separately. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../Network/Validator/PublicHostname.php | 198 ++++++++++++++++++ .../Modules/Avatars/Http/Favicon/Get.php | 156 ++++++++++---- .../Network/Validators/PublicHostnameTest.php | 162 ++++++++++++++ 3 files changed, 476 insertions(+), 40 deletions(-) create mode 100644 src/Appwrite/Network/Validator/PublicHostname.php create mode 100644 tests/unit/Network/Validators/PublicHostnameTest.php diff --git a/src/Appwrite/Network/Validator/PublicHostname.php b/src/Appwrite/Network/Validator/PublicHostname.php new file mode 100644 index 0000000000..c16bb92864 --- /dev/null +++ b/src/Appwrite/Network/Validator/PublicHostname.php @@ -0,0 +1,198 @@ +reason !== '' + ? $this->reason + : 'Value must be a publicly routable hostname or address.'; + } + + public function isArray(): bool + { + return false; + } + + public function getType(): string + { + return self::TYPE_STRING; + } + + public function isValid(mixed $value): bool + { + $this->reason = ''; + + if (!\is_string($value) || $value === '') { + $this->reason = 'Hostname is empty.'; + return false; + } + + $hostname = \strtolower(\trim($value, " \t\n\r\0\x0B[]")); + + // IP literals are checked directly, no DNS round-trip. + if (\filter_var($hostname, FILTER_VALIDATE_IP) !== false) { + if (!self::isPublicIp($hostname)) { + $this->reason = "Address {$hostname} is in a private or reserved range."; + return false; + } + return true; + } + + $addresses = self::resolve($hostname); + + if (empty($addresses)) { + $this->reason = "Hostname {$hostname} does not resolve."; + return false; + } + + foreach ($addresses as $ip) { + if (!self::isPublicIp($ip)) { + $this->reason = "Hostname {$hostname} resolves to private or reserved address {$ip}."; + return false; + } + } + + return true; + } + + /** + * Resolves a hostname to all A and AAAA records. + * + * @return array + */ + public static function resolve(string $hostname): array + { + $ipv4 = @\gethostbynamel($hostname) ?: []; + + $ipv6 = []; + $records = @\dns_get_record($hostname, DNS_AAAA); + if (\is_array($records)) { + foreach ($records as $record) { + if (!empty($record['ipv6'])) { + $ipv6[] = $record['ipv6']; + } + } + } + + return \array_values(\array_unique(\array_merge($ipv4, $ipv6))); + } + + /** + * Returns true only if the given IP literal sits in a globally routable range. + */ + public static function isPublicIp(string $ip): bool + { + $filtered = \filter_var( + $ip, + FILTER_VALIDATE_IP, + FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE + ); + + if ($filtered === false) { + return false; + } + + $isIpv4 = \filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false; + $extra = $isIpv4 ? self::PRIVATE_IPV4_CIDRS : self::PRIVATE_IPV6_CIDRS; + + foreach ($extra as $cidr) { + if (self::ipInCidr($ip, $cidr)) { + return false; + } + } + + return true; + } + + /** + * Binary CIDR match. Works for both IPv4 and IPv6 as long as the + * IP and subnet are the same family. + */ + private static function ipInCidr(string $ip, string $cidr): bool + { + [$subnet, $maskBits] = \explode('/', $cidr, 2); + $maskBits = (int) $maskBits; + + $ipBinary = @\inet_pton($ip); + $subnetBinary = @\inet_pton($subnet); + + if ($ipBinary === false || $subnetBinary === false) { + return false; + } + + if (\strlen($ipBinary) !== \strlen($subnetBinary)) { + return false; + } + + $fullBytes = \intdiv($maskBits, 8); + $remainderBits = $maskBits % 8; + + if ($fullBytes > 0 && \substr($ipBinary, 0, $fullBytes) !== \substr($subnetBinary, 0, $fullBytes)) { + return false; + } + + if ($remainderBits > 0) { + $mask = \chr(0xFF << (8 - $remainderBits) & 0xFF); + if ((\ord($ipBinary[$fullBytes]) & \ord($mask)) !== (\ord($subnetBinary[$fullBytes]) & \ord($mask))) { + return false; + } + } + + return true; + } +} diff --git a/src/Appwrite/Platform/Modules/Avatars/Http/Favicon/Get.php b/src/Appwrite/Platform/Modules/Avatars/Http/Favicon/Get.php index 31ad572f18..9f1fbd5455 100644 --- a/src/Appwrite/Platform/Modules/Avatars/Http/Favicon/Get.php +++ b/src/Appwrite/Platform/Modules/Avatars/Http/Favicon/Get.php @@ -3,6 +3,7 @@ namespace Appwrite\Platform\Modules\Avatars\Http\Favicon; use Appwrite\Extend\Exception; +use Appwrite\Network\Validator\PublicHostname; use Appwrite\Platform\Modules\Avatars\Http\Action; use Appwrite\SDK\AuthType; use Appwrite\SDK\ContentType; @@ -16,6 +17,7 @@ use DOMElement; use enshrined\svgSanitize\Sanitizer as SvgSanitizer; use Utopia\Domains\Domain; use Utopia\Fetch\Client; +use Utopia\Fetch\Response as FetchResponse; use Utopia\Image\Image; use Utopia\Platform\Action as UtopiaAction; use Utopia\Platform\Scope\HTTP; @@ -26,6 +28,9 @@ class Get extends Action { use HTTP; + private const ALLOWED_SCHEMES = ['http', 'https']; + private const MAX_REDIRECTS = 5; + public static function getName(): string { return 'getFavicon'; @@ -56,7 +61,7 @@ class Get extends Action ], contentType: ContentType::IMAGE )) - ->param('url', '', new URL(['http', 'https']), 'Website URL which you want to fetch the favicon from.') + ->param('url', '', new URL(self::ALLOWED_SCHEMES), 'Website URL which you want to fetch the favicon from.') ->inject('response') ->callback($this->action(...)); } @@ -67,34 +72,20 @@ class Get extends Action $height = 56; $quality = 80; $output = 'png'; - $type = 'png'; if (!\extension_loaded('imagick')) { throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Imagick extension is missing'); } - $domain = new Domain(\parse_url($url, PHP_URL_HOST)); + $userAgent = \sprintf( + APP_USERAGENT, + System::getEnv('_APP_VERSION', 'UNKNOWN'), + System::getEnv('_APP_EMAIL_SECURITY', System::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY)) + ); - if (!$domain->isKnown()) { - throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); - } + $pageResponse = $this->safeFetch($url, $userAgent); + $body = $pageResponse->getBody(); - $client = new Client(); - try { - $res = $client - ->setAllowRedirects(true) - ->setMaxRedirects(5) - ->setUserAgent(\sprintf( - APP_USERAGENT, - System::getEnv('_APP_VERSION', 'UNKNOWN'), - System::getEnv('_APP_EMAIL_SECURITY', System::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY)) - )) - ->fetch($url); - } catch (\Throwable) { - throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); - } - - $body = $res->getBody(); $doc = new DOMDocument(); $doc->strictErrorChecking = false; if (!empty($body)) { @@ -115,7 +106,6 @@ class Get extends Action switch (\strtolower($rel)) { case 'icon': case 'shortcut icon': - //case 'apple-touch-icon': $ext = \pathinfo(\parse_url($absolute, PHP_URL_PATH), PATHINFO_EXTENSION); switch ($ext) { @@ -154,27 +144,13 @@ class Get extends Action $outputExt = 'ico'; } - $domain = new Domain(\parse_url($outputHref, PHP_URL_HOST)); + $iconResponse = $this->safeFetch($outputHref, $userAgent); - if (!$domain->isKnown()) { - throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); - } - - $client = new Client(); - try { - $res = $client - ->setAllowRedirects(true) - ->setMaxRedirects(5) - ->fetch($outputHref); - } catch (\Throwable) { - throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); - } - - if ($res->getStatusCode() !== 200) { + if ($iconResponse->getStatusCode() !== 200) { throw new Exception(Exception::AVATAR_ICON_NOT_FOUND); } - $data = $res->getBody(); + $data = $iconResponse->getBody(); if ('ico' === $outputExt) { // Skip crop, Imagick isn\'t supporting icon files if ( @@ -215,4 +191,104 @@ class Get extends Action ->file($data); unset($image); } + + /** + * Fetches a URL with manual redirect handling. + * + * Every URL in the redirect chain — the initial request and each Location + * target — is re-validated for scheme, public-suffix membership, and + * publicly-routable resolution. This is what prevents an attacker-controlled + * page from bouncing the request to an internal IP such as 169.254.169.254. + */ + private function safeFetch(string $url, string $userAgent): FetchResponse + { + for ($hop = 0; $hop <= self::MAX_REDIRECTS; $hop++) { + $this->assertUrlSafe($url); + + try { + $response = (new Client()) + ->setAllowRedirects(false) + ->setUserAgent($userAgent) + ->fetch($url); + } catch (\Throwable) { + throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); + } + + $status = $response->getStatusCode(); + if ($status < 300 || $status >= 400) { + return $response; + } + + $location = $response->getHeaders()['location'] ?? ''; + if ($location === '') { + return $response; + } + + $url = self::resolveLocation($url, $location); + } + + throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); + } + + private function assertUrlSafe(string $url): void + { + $parts = \parse_url($url); + + $scheme = \strtolower($parts['scheme'] ?? ''); + if (!\in_array($scheme, self::ALLOWED_SCHEMES, true)) { + throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); + } + + $host = $parts['host'] ?? ''; + if ($host === '') { + throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); + } + + // PSL check rejects bare hostnames, made-up TLDs, and pure IP literals + // before we spend a DNS lookup on them. + $isIpLiteral = \filter_var(\trim($host, '[]'), FILTER_VALIDATE_IP) !== false; + if (!$isIpLiteral) { + try { + $domain = new Domain($host); + } catch (\Throwable) { + throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); + } + + if (!$domain->isKnown()) { + throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); + } + } + + if (!(new PublicHostname())->isValid($host)) { + throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); + } + } + + private static function resolveLocation(string $base, string $location): string + { + $location = \trim($location); + + if (\preg_match('#^[a-z][a-z0-9+.-]*:#i', $location) === 1) { + return $location; + } + + $baseParts = \parse_url($base); + $scheme = $baseParts['scheme'] ?? 'http'; + $host = $baseParts['host'] ?? ''; + $port = isset($baseParts['port']) ? ':' . $baseParts['port'] : ''; + + if (\str_starts_with($location, '//')) { + return $scheme . ':' . $location; + } + + if (\str_starts_with($location, '/')) { + return $scheme . '://' . $host . $port . $location; + } + + $path = $baseParts['path'] ?? '/'; + $slash = \strrpos($path, '/'); + $path = $slash === false ? '/' : \substr($path, 0, $slash + 1); + + return $scheme . '://' . $host . $port . $path . $location; + } } diff --git a/tests/unit/Network/Validators/PublicHostnameTest.php b/tests/unit/Network/Validators/PublicHostnameTest.php new file mode 100644 index 0000000000..81c5708769 --- /dev/null +++ b/tests/unit/Network/Validators/PublicHostnameTest.php @@ -0,0 +1,162 @@ +assertFalse($validator->isValid('')); + $this->assertFalse($validator->isValid(null)); + $this->assertFalse($validator->isValid(123)); + $this->assertFalse($validator->isValid([])); + } + + #[DataProvider('privateIpv4Addresses')] + public function testRejectsPrivateIpv4Literals(string $ip): void + { + $validator = new PublicHostname(); + + $this->assertFalse( + $validator->isValid($ip), + "Expected {$ip} to be rejected as private/reserved" + ); + $this->assertFalse(PublicHostname::isPublicIp($ip)); + } + + public static function privateIpv4Addresses(): array + { + return [ + 'unspecified' => ['0.0.0.0'], + 'private 10/8' => ['10.0.0.1'], + 'private 172.16/12' => ['172.16.5.10'], + 'private 192.168/16' => ['192.168.1.1'], + 'cgnat' => ['100.64.0.1'], + 'loopback' => ['127.0.0.1'], + 'link-local imds' => ['169.254.169.254'], + 'gcp metadata' => ['169.254.169.254'], + 'multicast' => ['224.0.0.1'], + 'reserved 240/4' => ['240.0.0.1'], + 'broadcast' => ['255.255.255.255'], + 'test-net-1' => ['192.0.2.1'], + 'test-net-2' => ['198.51.100.1'], + 'test-net-3' => ['203.0.113.1'], + 'benchmark' => ['198.18.0.1'], + ]; + } + + #[DataProvider('privateIpv6Addresses')] + public function testRejectsPrivateIpv6Literals(string $ip): void + { + $validator = new PublicHostname(); + + $this->assertFalse( + $validator->isValid($ip), + "Expected {$ip} to be rejected as private/reserved" + ); + $this->assertFalse(PublicHostname::isPublicIp($ip)); + } + + public static function privateIpv6Addresses(): array + { + return [ + 'loopback' => ['::1'], + 'unspecified' => ['::'], + 'link-local' => ['fe80::1'], + 'unique-local' => ['fc00::1'], + 'unique-local fd' => ['fd12:3456:789a::1'], + 'multicast' => ['ff02::1'], + 'ipv4-mapped loopback' => ['::ffff:127.0.0.1'], + 'ipv4-mapped private' => ['::ffff:10.0.0.1'], + 'ipv4-mapped imds' => ['::ffff:169.254.169.254'], + '6to4 loopback' => ['2002:7f00:1::'], + '6to4 imds' => ['2002:a9fe:a9fe::'], + 'teredo' => ['2001:0:1::1'], + 'documentation' => ['2001:db8::1'], + ]; + } + + #[DataProvider('publicIpAddresses')] + public function testAcceptsPublicIpLiterals(string $ip): void + { + $validator = new PublicHostname(); + + $this->assertTrue( + $validator->isValid($ip), + "Expected {$ip} to be accepted as public" + ); + $this->assertTrue(PublicHostname::isPublicIp($ip)); + } + + public static function publicIpAddresses(): array + { + return [ + 'google dns' => ['8.8.8.8'], + 'cloudflare' => ['1.1.1.1'], + 'opendns' => ['208.67.222.222'], + 'public ipv6' => ['2606:4700:4700::1111'], + ]; + } + + public function testRejectsMalformedInput(): void + { + $validator = new PublicHostname(); + + $this->assertFalse($validator->isValid('not a hostname at all')); + $this->assertFalse($validator->isValid('http://example.com')); + $this->assertFalse($validator->isValid('999.999.999.999')); + } + + public function testRejectsNonResolvingHostname(): void + { + $validator = new PublicHostname(); + + // RFC 2606 reserves this for testing — guaranteed not to resolve. + $this->assertFalse($validator->isValid('a-hostname-that-does-not-exist.invalid')); + } + + public function testReasonIsPopulatedOnFailure(): void + { + $validator = new PublicHostname(); + $validator->isValid('127.0.0.1'); + + $this->assertStringContainsString('127.0.0.1', $validator->getDescription()); + $this->assertStringContainsString('private', $validator->getDescription()); + } + + public function testReasonResetsBetweenCalls(): void + { + $validator = new PublicHostname(); + + $validator->isValid(''); + $this->assertSame('Hostname is empty.', $validator->getDescription()); + + $validator->isValid('127.0.0.1'); + $this->assertStringContainsString('127.0.0.1', $validator->getDescription()); + } + + public function testIpInCidrEdgesViaPublicIp(): void + { + // Boundary checks: first and last address of common ranges. + $this->assertFalse(PublicHostname::isPublicIp('10.0.0.0')); + $this->assertFalse(PublicHostname::isPublicIp('10.255.255.255')); + $this->assertTrue(PublicHostname::isPublicIp('11.0.0.0')); + $this->assertTrue(PublicHostname::isPublicIp('9.255.255.255')); + + $this->assertFalse(PublicHostname::isPublicIp('169.254.0.0')); + $this->assertFalse(PublicHostname::isPublicIp('169.254.255.255')); + $this->assertTrue(PublicHostname::isPublicIp('169.253.255.255')); + $this->assertTrue(PublicHostname::isPublicIp('169.255.0.0')); + + $this->assertFalse(PublicHostname::isPublicIp('100.64.0.0')); + $this->assertFalse(PublicHostname::isPublicIp('100.127.255.255')); + $this->assertTrue(PublicHostname::isPublicIp('100.128.0.0')); + $this->assertTrue(PublicHostname::isPublicIp('100.63.255.255')); + } +} From 1fe6cef6f6a5dffaca5a47cd1eb99b9a48384d37 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 28 May 2026 20:14:31 +1200 Subject: [PATCH 02/14] chore: update lock --- composer.lock | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/composer.lock b/composer.lock index 8f3ab9fd93..ee0276156e 100644 --- a/composer.lock +++ b/composer.lock @@ -5638,16 +5638,16 @@ "packages-dev": [ { "name": "appwrite/sdk-generator", - "version": "1.31.1", + "version": "1.31.8", "source": { "type": "git", "url": "https://github.com/appwrite/sdk-generator.git", - "reference": "5699f6da951aef9378fabdcf12f40a9a54fb3128" + "reference": "3bc6c697e60807dd036b5c02c66d03183afad79f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/appwrite/sdk-generator/zipball/5699f6da951aef9378fabdcf12f40a9a54fb3128", - "reference": "5699f6da951aef9378fabdcf12f40a9a54fb3128", + "url": "https://api.github.com/repos/appwrite/sdk-generator/zipball/3bc6c697e60807dd036b5c02c66d03183afad79f", + "reference": "3bc6c697e60807dd036b5c02c66d03183afad79f", "shasum": "" }, "require": { @@ -5656,7 +5656,7 @@ "ext-mbstring": "*", "matthiasmullie/minify": "1.3.*", "php": ">=8.3", - "twig/twig": "3.26.*" + "twig/twig": "3.27.*" }, "require-dev": { "brianium/paratest": "7.*", @@ -5683,9 +5683,9 @@ "description": "Appwrite PHP library for generating API SDKs for multiple programming languages and platforms", "support": { "issues": "https://github.com/appwrite/sdk-generator/issues", - "source": "https://github.com/appwrite/sdk-generator/tree/1.31.1" + "source": "https://github.com/appwrite/sdk-generator/tree/1.31.8" }, - "time": "2026-05-20T22:22:59+00:00" + "time": "2026-05-28T05:01:17+00:00" }, { "name": "brianium/paratest", @@ -8469,16 +8469,16 @@ }, { "name": "twig/twig", - "version": "v3.26.0", + "version": "v3.27.0", "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "1fcae487b180d78e6351f4e0afa91f9eab96a2bc" + "reference": "04ae1bfe9463c816cf72ca0abe7eae2c77a9a9ed" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/1fcae487b180d78e6351f4e0afa91f9eab96a2bc", - "reference": "1fcae487b180d78e6351f4e0afa91f9eab96a2bc", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/04ae1bfe9463c816cf72ca0abe7eae2c77a9a9ed", + "reference": "04ae1bfe9463c816cf72ca0abe7eae2c77a9a9ed", "shasum": "" }, "require": { @@ -8533,7 +8533,7 @@ ], "support": { "issues": "https://github.com/twigphp/Twig/issues", - "source": "https://github.com/twigphp/Twig/tree/v3.26.0" + "source": "https://github.com/twigphp/Twig/tree/v3.27.0" }, "funding": [ { @@ -8545,7 +8545,7 @@ "type": "tidelift" } ], - "time": "2026-05-20T07:31:59+00:00" + "time": "2026-05-27T13:05:51+00:00" } ], "aliases": [], From 61adf5511e50862494c758a81464a64d11d7e7ee Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 28 May 2026 20:29:04 +1200 Subject: [PATCH 03/14] refactor(network): extract SSRF-guarded fetcher and RFC 3986 URL resolution MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses two Copilot review comments on PR #12411: 1. The manual relative-redirect resolver did not implement RFC 3986 §5.3 reference resolution. Query-only (`?x=1`) and fragment-only (`#frag`) Location values dropped the base path and base query; dot-segments were not normalised. Replaces the ad-hoc resolver with URL::resolveLocation(), a full RFC 3986 §5.2–5.4 implementation. The same helper now also resolves favicon link hrefs against the page URL, which fixes a pre-existing analogous bug in HTML parsing. 2. The redirect-chain SSRF block had no automated test — only the PublicHostname validator was unit-tested. Extracts the safeFetch loop + per-hop validation into Appwrite\Network\Fetcher, which takes an optional Adapter for dependency injection. Tests use a scripted adapter to confirm that a public initial request whose 302 target is a private/loopback/link-local/non-http(s) address is rejected before any second send() call reaches the adapter. Favicon endpoint is now a thin wrapper around Fetcher. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/Appwrite/Network/Fetcher.php | 112 +++++++++ src/Appwrite/Network/UnsafeUrlException.php | 11 + .../Modules/Avatars/Http/Favicon/Get.php | 129 ++--------- src/Appwrite/URL/URL.php | 147 ++++++++++++ tests/unit/Network/FetcherTest.php | 218 ++++++++++++++++++ tests/unit/URL/URLTest.php | 152 ++++++++++++ 6 files changed, 656 insertions(+), 113 deletions(-) create mode 100644 src/Appwrite/Network/Fetcher.php create mode 100644 src/Appwrite/Network/UnsafeUrlException.php create mode 100644 tests/unit/Network/FetcherTest.php diff --git a/src/Appwrite/Network/Fetcher.php b/src/Appwrite/Network/Fetcher.php new file mode 100644 index 0000000000..bc0d8f467a --- /dev/null +++ b/src/Appwrite/Network/Fetcher.php @@ -0,0 +1,112 @@ +maxRedirects; $hop++) { + self::assertSafe($url); + + $client = $this->adapter !== null ? new Client($this->adapter) : new Client(); + $response = $client + ->setAllowRedirects(false) + ->setUserAgent($this->userAgent) + ->fetch($url); + + $status = $response->getStatusCode(); + if ($status < 300 || $status >= 400) { + return $response; + } + + $location = $response->getHeaders()['location'] ?? ''; + if ($location === '') { + return $response; + } + + $url = URL::resolveLocation($url, $location); + } + + throw new \RuntimeException('Too many redirects.'); + } + + /** + * Validate that a URL is safe to fetch from a server-side context. + * + * Rejects non-http(s) schemes, hosts that aren't on the public suffix + * list (unless they're an IP literal), and any hostname that resolves + * to a private, loopback, link-local, multicast, or reserved address. + * + * @throws UnsafeUrlException + */ + public static function assertSafe(string $url): void + { + $parts = \parse_url($url); + if (!\is_array($parts)) { + throw new UnsafeUrlException('Malformed URL.'); + } + + $scheme = \strtolower($parts['scheme'] ?? ''); + if (!\in_array($scheme, self::ALLOWED_SCHEMES, true)) { + throw new UnsafeUrlException("Scheme '{$scheme}' is not allowed."); + } + + $host = $parts['host'] ?? ''; + if ($host === '') { + throw new UnsafeUrlException('URL has no host.'); + } + + $isIpLiteral = \filter_var(\trim($host, '[]'), FILTER_VALIDATE_IP) !== false; + if (!$isIpLiteral) { + try { + $domain = new Domain($host); + } catch (\Throwable) { + throw new UnsafeUrlException("Hostname '{$host}' is invalid."); + } + + if (!$domain->isKnown()) { + throw new UnsafeUrlException("Hostname '{$host}' is not a known public domain."); + } + } + + $validator = new PublicHostname(); + if (!$validator->isValid($host)) { + throw new UnsafeUrlException($validator->getDescription()); + } + } +} diff --git a/src/Appwrite/Network/UnsafeUrlException.php b/src/Appwrite/Network/UnsafeUrlException.php new file mode 100644 index 0000000000..6e0da449f9 --- /dev/null +++ b/src/Appwrite/Network/UnsafeUrlException.php @@ -0,0 +1,11 @@ +param('url', '', new URL(self::ALLOWED_SCHEMES), 'Website URL which you want to fetch the favicon from.') + ->param('url', '', new URL(Fetcher::ALLOWED_SCHEMES), 'Website URL which you want to fetch the favicon from.') ->inject('response') ->callback($this->action(...)); } @@ -77,13 +71,18 @@ class Get extends Action throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Imagick extension is missing'); } - $userAgent = \sprintf( + $fetcher = new Fetcher(\sprintf( APP_USERAGENT, System::getEnv('_APP_VERSION', 'UNKNOWN'), System::getEnv('_APP_EMAIL_SECURITY', System::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY)) - ); + )); + + try { + $pageResponse = $fetcher->fetch($url); + } catch (\Throwable) { + throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); + } - $pageResponse = $this->safeFetch($url, $userAgent); $body = $pageResponse->getBody(); $doc = new DOMDocument(); @@ -101,7 +100,7 @@ class Get extends Action $href = $link->getAttribute('href'); $rel = $link->getAttribute('rel'); $sizes = $link->getAttribute('sizes'); - $absolute = URLParse::unparse(\array_merge(\parse_url($url), \parse_url($href))); + $absolute = URLParse::resolveLocation($url, $href); switch (\strtolower($rel)) { case 'icon': @@ -144,7 +143,11 @@ class Get extends Action $outputExt = 'ico'; } - $iconResponse = $this->safeFetch($outputHref, $userAgent); + try { + $iconResponse = $fetcher->fetch($outputHref); + } catch (\Throwable) { + throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); + } if ($iconResponse->getStatusCode() !== 200) { throw new Exception(Exception::AVATAR_ICON_NOT_FOUND); @@ -191,104 +194,4 @@ class Get extends Action ->file($data); unset($image); } - - /** - * Fetches a URL with manual redirect handling. - * - * Every URL in the redirect chain — the initial request and each Location - * target — is re-validated for scheme, public-suffix membership, and - * publicly-routable resolution. This is what prevents an attacker-controlled - * page from bouncing the request to an internal IP such as 169.254.169.254. - */ - private function safeFetch(string $url, string $userAgent): FetchResponse - { - for ($hop = 0; $hop <= self::MAX_REDIRECTS; $hop++) { - $this->assertUrlSafe($url); - - try { - $response = (new Client()) - ->setAllowRedirects(false) - ->setUserAgent($userAgent) - ->fetch($url); - } catch (\Throwable) { - throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); - } - - $status = $response->getStatusCode(); - if ($status < 300 || $status >= 400) { - return $response; - } - - $location = $response->getHeaders()['location'] ?? ''; - if ($location === '') { - return $response; - } - - $url = self::resolveLocation($url, $location); - } - - throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); - } - - private function assertUrlSafe(string $url): void - { - $parts = \parse_url($url); - - $scheme = \strtolower($parts['scheme'] ?? ''); - if (!\in_array($scheme, self::ALLOWED_SCHEMES, true)) { - throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); - } - - $host = $parts['host'] ?? ''; - if ($host === '') { - throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); - } - - // PSL check rejects bare hostnames, made-up TLDs, and pure IP literals - // before we spend a DNS lookup on them. - $isIpLiteral = \filter_var(\trim($host, '[]'), FILTER_VALIDATE_IP) !== false; - if (!$isIpLiteral) { - try { - $domain = new Domain($host); - } catch (\Throwable) { - throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); - } - - if (!$domain->isKnown()) { - throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); - } - } - - if (!(new PublicHostname())->isValid($host)) { - throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); - } - } - - private static function resolveLocation(string $base, string $location): string - { - $location = \trim($location); - - if (\preg_match('#^[a-z][a-z0-9+.-]*:#i', $location) === 1) { - return $location; - } - - $baseParts = \parse_url($base); - $scheme = $baseParts['scheme'] ?? 'http'; - $host = $baseParts['host'] ?? ''; - $port = isset($baseParts['port']) ? ':' . $baseParts['port'] : ''; - - if (\str_starts_with($location, '//')) { - return $scheme . ':' . $location; - } - - if (\str_starts_with($location, '/')) { - return $scheme . '://' . $host . $port . $location; - } - - $path = $baseParts['path'] ?? '/'; - $slash = \strrpos($path, '/'); - $path = $slash === false ? '/' : \substr($path, 0, $slash + 1); - - return $scheme . '://' . $host . $port . $path . $location; - } } diff --git a/src/Appwrite/URL/URL.php b/src/Appwrite/URL/URL.php index 49e95929e7..7f710f70b0 100644 --- a/src/Appwrite/URL/URL.php +++ b/src/Appwrite/URL/URL.php @@ -118,4 +118,151 @@ class URL { return \http_build_query($query); } + + /** + * Resolve a Location-header reference against an absolute base URL, + * following RFC 3986 §5.3 (Reference Resolution). Handles: + * - absolute references (scheme present) + * - protocol-relative (//host/path) + * - absolute-path (/foo) + * - relative-path (foo, ../foo) + * - query-only (?x=1) -- keeps base path + * - fragment-only (#frag) -- keeps base path AND base query + * - dot-segment normalisation -- /a/b/../c → /a/c + */ + public static function resolveLocation(string $base, string $reference): string + { + $reference = \trim($reference); + if ($reference === '') { + return $base; + } + + $ref = \parse_url($reference); + $bas = \parse_url($base); + if (!\is_array($ref) || !\is_array($bas)) { + return $reference; + } + + $target = []; + + if (isset($ref['scheme'])) { + $target['scheme'] = $ref['scheme']; + self::copyAuthority($ref, $target); + $target['path'] = self::removeDotSegments($ref['path'] ?? ''); + if (isset($ref['query'])) { + $target['query'] = $ref['query']; + } + } else { + $target['scheme'] = $bas['scheme'] ?? ''; + + if (isset($ref['host'])) { + self::copyAuthority($ref, $target); + $target['path'] = self::removeDotSegments($ref['path'] ?? ''); + if (isset($ref['query'])) { + $target['query'] = $ref['query']; + } + } else { + self::copyAuthority($bas, $target); + $refPath = $ref['path'] ?? ''; + + if ($refPath === '') { + $target['path'] = $bas['path'] ?? ''; + if (isset($ref['query'])) { + $target['query'] = $ref['query']; + } elseif (isset($bas['query'])) { + $target['query'] = $bas['query']; + } + } else { + if (\str_starts_with($refPath, '/')) { + $target['path'] = self::removeDotSegments($refPath); + } else { + $target['path'] = self::removeDotSegments(self::mergePaths($bas, $refPath)); + } + if (isset($ref['query'])) { + $target['query'] = $ref['query']; + } + } + } + } + + if (isset($ref['fragment'])) { + $target['fragment'] = $ref['fragment']; + } + + return self::unparse($target); + } + + /** + * @param array $source + * @param array $target + */ + private static function copyAuthority(array $source, array &$target): void + { + foreach (['user', 'pass', 'host', 'port'] as $key) { + if (isset($source[$key])) { + $target[$key] = $source[$key]; + } + } + } + + /** + * RFC 3986 §5.2.3 — merge base path with reference path. + */ + private static function mergePaths(array $base, string $referencePath): string + { + if (isset($base['host']) && empty($base['path'])) { + return '/' . $referencePath; + } + + $basePath = $base['path'] ?? ''; + $slash = \strrpos($basePath, '/'); + if ($slash === false) { + return $referencePath; + } + + return \substr($basePath, 0, $slash + 1) . $referencePath; + } + + /** + * RFC 3986 §5.2.4 — remove "." and ".." segments from a path. + */ + private static function removeDotSegments(string $path): string + { + $output = ''; + $input = $path; + + while ($input !== '') { + if (\str_starts_with($input, '../')) { + $input = \substr($input, 3); + } elseif (\str_starts_with($input, './')) { + $input = \substr($input, 2); + } elseif (\str_starts_with($input, '/./')) { + $input = '/' . \substr($input, 3); + } elseif ($input === '/.') { + $input = '/'; + } elseif (\str_starts_with($input, '/../')) { + $input = '/' . \substr($input, 4); + $slash = \strrpos($output, '/'); + $output = $slash === false ? '' : \substr($output, 0, $slash); + } elseif ($input === '/..') { + $input = '/'; + $slash = \strrpos($output, '/'); + $output = $slash === false ? '' : \substr($output, 0, $slash); + } elseif ($input === '.' || $input === '..') { + $input = ''; + } else { + $start = ($input[0] === '/') ? 1 : 0; + $next = \strpos($input, '/', $start); + if ($next === false) { + $output .= $input; + $input = ''; + } else { + $output .= \substr($input, 0, $next); + $input = \substr($input, $next); + } + } + } + + return $output; + } } diff --git a/tests/unit/Network/FetcherTest.php b/tests/unit/Network/FetcherTest.php new file mode 100644 index 0000000000..1c0fce6e77 --- /dev/null +++ b/tests/unit/Network/FetcherTest.php @@ -0,0 +1,218 @@ +expectNotToPerformAssertions(); + } + + #[DataProvider('unsafeUrls')] + public function testAssertSafeRejectsUnsafeUrls(string $url, string $reasonFragment): void + { + try { + Fetcher::assertSafe($url); + $this->fail("Expected UnsafeUrlException for {$url}"); + } catch (UnsafeUrlException $e) { + $this->assertStringContainsString( + $reasonFragment, + $e->getMessage(), + "Wrong rejection reason for {$url}" + ); + } + } + + public static function unsafeUrls(): array + { + return [ + 'loopback v4' => ['http://127.0.0.1/iam', 'private or reserved'], + 'loopback v6' => ['http://[::1]/iam', 'private or reserved'], + 'link-local imds' => ['http://169.254.169.254/latest/', 'private or reserved'], + 'private rfc1918 a' => ['http://10.0.0.5/secret', 'private or reserved'], + 'private rfc1918 b' => ['http://192.168.1.1/admin', 'private or reserved'], + 'private rfc1918 c' => ['http://172.16.0.1/x', 'private or reserved'], + 'cgnat' => ['http://100.64.0.1/x', 'private or reserved'], + 'ipv4-mapped loopback' => ['http://[::ffff:127.0.0.1]/x', 'private or reserved'], + '6to4 imds' => ['http://[2002:a9fe:a9fe::]/x', 'private or reserved'], + 'file scheme' => ['file:///etc/passwd', "Scheme 'file' is not allowed"], + 'gopher scheme' => ['gopher://attacker/x', "Scheme 'gopher' is not allowed"], + 'ftp scheme' => ['ftp://internal/file', "Scheme 'ftp' is not allowed"], + 'no scheme' => ['example.com/path', "Scheme '' is not allowed"], + 'malformed no host' => ['http:///path', 'Malformed URL'], + 'unknown psl' => ['http://not-a-real-tld.notreal/x', 'not a known public domain'], + ]; + } + + public function testFetchFollowsPublicRedirect(): void + { + $adapter = $this->scriptedAdapter([ + $this->redirect('https://1.1.1.1/final'), + $this->ok('FINAL_BODY'), + ]); + + $fetcher = new Fetcher(userAgent: 'test', adapter: $adapter); + $response = $fetcher->fetch('http://8.8.8.8/start'); + + $this->assertSame(200, $response->getStatusCode()); + $this->assertSame('FINAL_BODY', $response->getBody()); + $this->assertSame(2, $adapter->callCount); + } + + public function testFetchBlocksRedirectToPrivateIp(): void + { + // This is the exact SSRF chain from the report: + // public attacker page → 302 → 169.254.169.254 (AWS IMDS) + $adapter = $this->scriptedAdapter([ + $this->redirect('http://169.254.169.254/latest/meta-data/iam/security-credentials/'), + // This second response must NEVER be served — the fetcher should + // reject the redirect target before calling send() again. + $this->ok('SHOULD_NEVER_LEAK'), + ]); + + $fetcher = new Fetcher(userAgent: 'test', adapter: $adapter); + + try { + $fetcher->fetch('http://8.8.8.8/attacker-page'); + $this->fail('Expected UnsafeUrlException'); + } catch (UnsafeUrlException $e) { + $this->assertStringContainsString('169.254.169.254', $e->getMessage()); + $this->assertSame( + 1, + $adapter->callCount, + 'Redirect target should not have been fetched — only the initial request should hit the adapter' + ); + } + } + + public function testFetchBlocksRedirectToLoopback(): void + { + $adapter = $this->scriptedAdapter([ + $this->redirect('http://127.0.0.1:9999/secrets'), + $this->ok('SHOULD_NEVER_LEAK'), + ]); + + $fetcher = new Fetcher(userAgent: 'test', adapter: $adapter); + + try { + $fetcher->fetch('http://8.8.8.8/attacker-page'); + $this->fail('Expected UnsafeUrlException'); + } catch (UnsafeUrlException $e) { + $this->assertStringContainsString('127.0.0.1', $e->getMessage()); + $this->assertSame(1, $adapter->callCount); + } + } + + public function testFetchBlocksRedirectToRelativePathOnPrivateBase(): void + { + // Edge case: a server on a public IP returns a relative redirect. + // We resolve it against the (still public) base and continue — safe. + $adapter = $this->scriptedAdapter([ + $this->redirect('/different-path'), + $this->ok('FINAL'), + ]); + + $fetcher = new Fetcher(userAgent: 'test', adapter: $adapter); + $response = $fetcher->fetch('http://8.8.8.8/start'); + + $this->assertSame('FINAL', $response->getBody()); + } + + public function testFetchRejectsRedirectChainOverLimit(): void + { + // Six redirects all to public targets; with maxRedirects=5 the sixth + // hop trips the limit. + $adapter = $this->scriptedAdapter([ + $this->redirect('https://1.1.1.1/2'), + $this->redirect('https://1.0.0.1/3'), + $this->redirect('https://8.8.8.8/4'), + $this->redirect('https://8.8.4.4/5'), + $this->redirect('https://9.9.9.9/6'), + $this->redirect('https://208.67.222.222/7'), + ]); + + $fetcher = new Fetcher(userAgent: 'test', maxRedirects: 5, adapter: $adapter); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Too many redirects'); + $fetcher->fetch('http://8.8.8.8/0'); + } + + public function testFetchBlocksNonHttpRedirect(): void + { + $adapter = $this->scriptedAdapter([ + $this->redirect('file:///etc/passwd'), + $this->ok('SHOULD_NEVER_LEAK'), + ]); + + $fetcher = new Fetcher(userAgent: 'test', adapter: $adapter); + + try { + $fetcher->fetch('http://8.8.8.8/page'); + $this->fail('Expected UnsafeUrlException'); + } catch (UnsafeUrlException $e) { + $this->assertStringContainsString("Scheme 'file'", $e->getMessage()); + $this->assertSame(1, $adapter->callCount); + } + } + + private function redirect(string $location): Response + { + return new Response(302, '', ['location' => $location]); + } + + private function ok(string $body): Response + { + return new Response(200, $body, []); + } + + /** + * @param Response[] $responses + */ + private function scriptedAdapter(array $responses): ScriptedAdapter + { + return new ScriptedAdapter($responses); + } +} + +/** + * Test-only adapter that returns a pre-scripted list of responses in order. + * Exposed as a named class so PHPStan can see the callCount property. + */ +class ScriptedAdapter implements Adapter +{ + public int $callCount = 0; + + /** @param Response[] $responses */ + public function __construct(private array $responses) + { + } + + public function send( + string $url, + string $method, + mixed $body, + array $headers, + RequestOptions $options, + ?callable $chunkCallback = null + ): Response { + $response = $this->responses[$this->callCount] ?? null; + $this->callCount++; + if ($response === null) { + throw new \RuntimeException("Adapter ran out of scripted responses (call {$this->callCount})"); + } + return $response; + } +} diff --git a/tests/unit/URL/URLTest.php b/tests/unit/URL/URLTest.php index 597d77f74c..6558acc66c 100644 --- a/tests/unit/URL/URLTest.php +++ b/tests/unit/URL/URLTest.php @@ -114,4 +114,156 @@ class URLTest extends TestCase $this->assertEquals('param1=value1¶m2=value2', $result); } + + /** + * RFC 3986 §5.4 reference resolution test cases. + */ + public function testResolveLocationAbsolute(): void + { + // Absolute URL wins over the base + $this->assertSame( + 'https://other.example/path', + URL::resolveLocation('https://base.example/a/b', 'https://other.example/path') + ); + + // Different scheme too + $this->assertSame( + 'http://other.example/', + URL::resolveLocation('https://base.example/a/b', 'http://other.example/') + ); + } + + public function testResolveLocationProtocolRelative(): void + { + // Inherits scheme from base + $this->assertSame( + 'https://other.example/path', + URL::resolveLocation('https://base.example/a/b', '//other.example/path') + ); + + // Including with a port + $this->assertSame( + 'https://other.example:8080/path', + URL::resolveLocation('https://base.example/', '//other.example:8080/path') + ); + } + + public function testResolveLocationAbsolutePath(): void + { + $this->assertSame( + 'https://base.example/new', + URL::resolveLocation('https://base.example/a/b', '/new') + ); + + // Preserves port + $this->assertSame( + 'https://base.example:9000/new', + URL::resolveLocation('https://base.example:9000/a/b', '/new') + ); + } + + public function testResolveLocationRelativePath(): void + { + $this->assertSame( + 'https://base.example/a/c', + URL::resolveLocation('https://base.example/a/b', 'c') + ); + + $this->assertSame( + 'https://base.example/a/b/c', + URL::resolveLocation('https://base.example/a/b/', 'c') + ); + } + + public function testResolveLocationQueryOnly(): void + { + // The bug Copilot called out: query-only must keep the base path + $this->assertSame( + 'https://base.example/a/b?x=1', + URL::resolveLocation('https://base.example/a/b', '?x=1') + ); + + // Replaces any base query + $this->assertSame( + 'https://base.example/a/b?x=1', + URL::resolveLocation('https://base.example/a/b?old=value', '?x=1') + ); + } + + public function testResolveLocationFragmentOnly(): void + { + // Fragment-only must keep base path AND base query + $this->assertSame( + 'https://base.example/a/b#section', + URL::resolveLocation('https://base.example/a/b', '#section') + ); + + $this->assertSame( + 'https://base.example/a/b?x=1#section', + URL::resolveLocation('https://base.example/a/b?x=1', '#section') + ); + } + + public function testResolveLocationDotSegments(): void + { + // RFC 3986 §5.2.4 normalisation. `/c/d/..` resolves to `/c/` + // (the trailing slash is significant — it's the directory of `d`). + $this->assertSame( + 'https://base.example/c/', + URL::resolveLocation('https://base.example/a/b', '/c/d/..') + ); + + $this->assertSame( + 'https://base.example/c/', + URL::resolveLocation('https://base.example/a/b', '/c/d/../') + ); + + // From inside the directory `/a/b/`, `../c` goes up one level → `/a/c` + $this->assertSame( + 'https://base.example/a/c', + URL::resolveLocation('https://base.example/a/b/', '../c') + ); + + // From file `/a/b`, `../c` goes up to root → `/c` + $this->assertSame( + 'https://base.example/c', + URL::resolveLocation('https://base.example/a/b', '../c') + ); + + $this->assertSame( + 'https://base.example/a/b/c', + URL::resolveLocation('https://base.example/a/b/', './c') + ); + } + + public function testResolveLocationEmpty(): void + { + // Empty reference returns base unchanged + $this->assertSame( + 'https://base.example/a/b', + URL::resolveLocation('https://base.example/a/b', '') + ); + + // Whitespace-only reference is treated as empty + $this->assertSame( + 'https://base.example/a/b', + URL::resolveLocation('https://base.example/a/b', ' ') + ); + } + + public function testResolveLocationPreservesAuthorityFromBase(): void + { + // user/pass survive relative resolution + $this->assertSame( + 'https://user:pass@base.example/c', + URL::resolveLocation('https://user:pass@base.example/a/b', '/c') + ); + + // port survives, and `c` relative to `/a/b` resolves to `/a/c` + // (RFC 3986 §5.2.3 merge replaces the last segment of the base path). + $this->assertSame( + 'http://base.example:8080/a/c', + URL::resolveLocation('http://base.example:8080/a/b', 'c') + ); + } } From ba7a87fc796cc6edb29552a9ff4964b24e4fca7c Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 28 May 2026 20:38:29 +1200 Subject: [PATCH 04/14] fix(network): normalise Location header case in Fetcher redirect loop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Greptile flagged the redirect-target lookup `$response->getHeaders()['location']` as case-sensitive. The current Utopia\Fetch curl adapter already lowercases incoming headers in CURLOPT_HEADERFUNCTION, so in production the bug doesn't manifest — but the Fetcher shouldn't rely on adapter-specific normalisation to stay correct. RFC 7230 §3.2 declares header names case-insensitive; any future adapter swap (or test mock that passes headers verbatim) would silently break the entire redirect-following path while leaving SSRF protection intact. Normalise with array_change_key_case before lookup, and flip the test mock's redirect helper to send `Location` (capital L) so the regression signal lives in the suite — naive lookups now fail every redirect-following test, not silently pass. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/Appwrite/Network/Fetcher.php | 7 ++++++- tests/unit/Network/FetcherTest.php | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Network/Fetcher.php b/src/Appwrite/Network/Fetcher.php index bc0d8f467a..0ac8bc3d22 100644 --- a/src/Appwrite/Network/Fetcher.php +++ b/src/Appwrite/Network/Fetcher.php @@ -54,7 +54,12 @@ class Fetcher return $response; } - $location = $response->getHeaders()['location'] ?? ''; + // RFC 7230 §3.2 — header names are case-insensitive. Utopia's + // current curl adapter already lowercases, but normalising here + // keeps Fetcher correct for any future Adapter implementation + // (and for test mocks that pass headers verbatim). + $headers = \array_change_key_case($response->getHeaders(), CASE_LOWER); + $location = $headers['location'] ?? ''; if ($location === '') { return $response; } diff --git a/tests/unit/Network/FetcherTest.php b/tests/unit/Network/FetcherTest.php index 1c0fce6e77..3869c8363d 100644 --- a/tests/unit/Network/FetcherTest.php +++ b/tests/unit/Network/FetcherTest.php @@ -170,7 +170,11 @@ class FetcherTest extends TestCase private function redirect(string $location): Response { - return new Response(302, '', ['location' => $location]); + // Mix the header key case on purpose. Fetcher must normalise per + // RFC 7230 §3.2; if it ever drops to a naive lowercase lookup the + // entire redirect-following suite fails — that's the regression + // signal we want. + return new Response(302, '', ['Location' => $location]); } private function ok(string $body): Response From fcfe1a3d41ea9344e7bcd0e39a64c57434bebb17 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 28 May 2026 09:55:06 +0000 Subject: [PATCH 05/14] refactor(avatars): inline safe fetch in favicon action --- src/Appwrite/Network/Fetcher.php | 117 ------------------ .../Modules/Avatars/Http/Favicon/Get.php | 90 +++++++++++++- tests/unit/Network/FetcherTest.php | 45 ++++--- 3 files changed, 113 insertions(+), 139 deletions(-) delete mode 100644 src/Appwrite/Network/Fetcher.php diff --git a/src/Appwrite/Network/Fetcher.php b/src/Appwrite/Network/Fetcher.php deleted file mode 100644 index 0ac8bc3d22..0000000000 --- a/src/Appwrite/Network/Fetcher.php +++ /dev/null @@ -1,117 +0,0 @@ -maxRedirects; $hop++) { - self::assertSafe($url); - - $client = $this->adapter !== null ? new Client($this->adapter) : new Client(); - $response = $client - ->setAllowRedirects(false) - ->setUserAgent($this->userAgent) - ->fetch($url); - - $status = $response->getStatusCode(); - if ($status < 300 || $status >= 400) { - return $response; - } - - // RFC 7230 §3.2 — header names are case-insensitive. Utopia's - // current curl adapter already lowercases, but normalising here - // keeps Fetcher correct for any future Adapter implementation - // (and for test mocks that pass headers verbatim). - $headers = \array_change_key_case($response->getHeaders(), CASE_LOWER); - $location = $headers['location'] ?? ''; - if ($location === '') { - return $response; - } - - $url = URL::resolveLocation($url, $location); - } - - throw new \RuntimeException('Too many redirects.'); - } - - /** - * Validate that a URL is safe to fetch from a server-side context. - * - * Rejects non-http(s) schemes, hosts that aren't on the public suffix - * list (unless they're an IP literal), and any hostname that resolves - * to a private, loopback, link-local, multicast, or reserved address. - * - * @throws UnsafeUrlException - */ - public static function assertSafe(string $url): void - { - $parts = \parse_url($url); - if (!\is_array($parts)) { - throw new UnsafeUrlException('Malformed URL.'); - } - - $scheme = \strtolower($parts['scheme'] ?? ''); - if (!\in_array($scheme, self::ALLOWED_SCHEMES, true)) { - throw new UnsafeUrlException("Scheme '{$scheme}' is not allowed."); - } - - $host = $parts['host'] ?? ''; - if ($host === '') { - throw new UnsafeUrlException('URL has no host.'); - } - - $isIpLiteral = \filter_var(\trim($host, '[]'), FILTER_VALIDATE_IP) !== false; - if (!$isIpLiteral) { - try { - $domain = new Domain($host); - } catch (\Throwable) { - throw new UnsafeUrlException("Hostname '{$host}' is invalid."); - } - - if (!$domain->isKnown()) { - throw new UnsafeUrlException("Hostname '{$host}' is not a known public domain."); - } - } - - $validator = new PublicHostname(); - if (!$validator->isValid($host)) { - throw new UnsafeUrlException($validator->getDescription()); - } - } -} diff --git a/src/Appwrite/Platform/Modules/Avatars/Http/Favicon/Get.php b/src/Appwrite/Platform/Modules/Avatars/Http/Favicon/Get.php index ffd3dd8fd0..5f9ec0bd2a 100644 --- a/src/Appwrite/Platform/Modules/Avatars/Http/Favicon/Get.php +++ b/src/Appwrite/Platform/Modules/Avatars/Http/Favicon/Get.php @@ -3,7 +3,8 @@ namespace Appwrite\Platform\Modules\Avatars\Http\Favicon; use Appwrite\Extend\Exception; -use Appwrite\Network\Fetcher; +use Appwrite\Network\UnsafeUrlException; +use Appwrite\Network\Validator\PublicHostname; use Appwrite\Platform\Modules\Avatars\Http\Action; use Appwrite\SDK\AuthType; use Appwrite\SDK\ContentType; @@ -15,6 +16,10 @@ use Appwrite\Utopia\Response; use DOMDocument; use DOMElement; use enshrined\svgSanitize\Sanitizer as SvgSanitizer; +use Utopia\Domains\Domain; +use Utopia\Fetch\Adapter; +use Utopia\Fetch\Client; +use Utopia\Fetch\Response as FetchResponse; use Utopia\Image\Image; use Utopia\Platform\Action as UtopiaAction; use Utopia\Platform\Scope\HTTP; @@ -25,6 +30,9 @@ class Get extends Action { use HTTP; + private const ALLOWED_SCHEMES = ['http', 'https']; + private const MAX_REDIRECTS = 5; + public static function getName(): string { return 'getFavicon'; @@ -55,7 +63,7 @@ class Get extends Action ], contentType: ContentType::IMAGE )) - ->param('url', '', new URL(Fetcher::ALLOWED_SCHEMES), 'Website URL which you want to fetch the favicon from.') + ->param('url', '', new URL(self::ALLOWED_SCHEMES), 'Website URL which you want to fetch the favicon from.') ->inject('response') ->callback($this->action(...)); } @@ -71,14 +79,14 @@ class Get extends Action throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Imagick extension is missing'); } - $fetcher = new Fetcher(\sprintf( + $userAgent = \sprintf( APP_USERAGENT, System::getEnv('_APP_VERSION', 'UNKNOWN'), System::getEnv('_APP_EMAIL_SECURITY', System::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS', APP_EMAIL_SECURITY)) - )); + ); try { - $pageResponse = $fetcher->fetch($url); + $pageResponse = $this->safeFetch($url, $userAgent); } catch (\Throwable) { throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); } @@ -144,7 +152,7 @@ class Get extends Action } try { - $iconResponse = $fetcher->fetch($outputHref); + $iconResponse = $this->safeFetch($outputHref, $userAgent); } catch (\Throwable) { throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); } @@ -194,4 +202,74 @@ class Get extends Action ->file($data); unset($image); } + + /** + * @throws UnsafeUrlException + */ + protected static function assertSafeUrl(string $url): void + { + $parts = \parse_url($url); + if (!\is_array($parts)) { + throw new UnsafeUrlException('Malformed URL.'); + } + + $scheme = \strtolower($parts['scheme'] ?? ''); + if (!\in_array($scheme, self::ALLOWED_SCHEMES, true)) { + throw new UnsafeUrlException("Scheme '{$scheme}' is not allowed."); + } + + $host = $parts['host'] ?? ''; + if ($host === '') { + throw new UnsafeUrlException('URL has no host.'); + } + + $isIpLiteral = \filter_var(\trim($host, '[]'), FILTER_VALIDATE_IP) !== false; + if (!$isIpLiteral) { + try { + $domain = new Domain($host); + } catch (\Throwable) { + throw new UnsafeUrlException("Hostname '{$host}' is invalid."); + } + + if (!$domain->isKnown()) { + throw new UnsafeUrlException("Hostname '{$host}' is not a known public domain."); + } + } + + $validator = new PublicHostname(); + if (!$validator->isValid($host)) { + throw new UnsafeUrlException($validator->getDescription()); + } + } + + /** + * @throws UnsafeUrlException + */ + protected function safeFetch(string $url, string $userAgent, ?Adapter $adapter = null): FetchResponse + { + for ($hop = 0; $hop <= self::MAX_REDIRECTS; $hop++) { + self::assertSafeUrl($url); + + $client = $adapter !== null ? new Client($adapter) : new Client(); + $response = $client + ->setAllowRedirects(false) + ->setUserAgent($userAgent) + ->fetch($url); + + $status = $response->getStatusCode(); + if ($status < 300 || $status >= 400) { + return $response; + } + + $headers = \array_change_key_case($response->getHeaders(), CASE_LOWER); + $location = $headers['location'] ?? ''; + if ($location === '') { + return $response; + } + + $url = URLParse::resolveLocation($url, $location); + } + + throw new \RuntimeException('Too many redirects.'); + } } diff --git a/tests/unit/Network/FetcherTest.php b/tests/unit/Network/FetcherTest.php index 3869c8363d..cfd3d4bc61 100644 --- a/tests/unit/Network/FetcherTest.php +++ b/tests/unit/Network/FetcherTest.php @@ -2,8 +2,8 @@ namespace Tests\Unit\Network; -use Appwrite\Network\Fetcher; use Appwrite\Network\UnsafeUrlException; +use Appwrite\Platform\Modules\Avatars\Http\Favicon\Get; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Utopia\Fetch\Adapter; @@ -15,8 +15,8 @@ class FetcherTest extends TestCase public function testAssertSafeAcceptsPublicIpLiteral(): void { // Should not throw - Fetcher::assertSafe('http://1.1.1.1/'); - Fetcher::assertSafe('https://8.8.8.8/path'); + TestableGet::assertSafe('http://1.1.1.1/'); + TestableGet::assertSafe('https://8.8.8.8/path'); $this->expectNotToPerformAssertions(); } @@ -24,7 +24,7 @@ class FetcherTest extends TestCase public function testAssertSafeRejectsUnsafeUrls(string $url, string $reasonFragment): void { try { - Fetcher::assertSafe($url); + TestableGet::assertSafe($url); $this->fail("Expected UnsafeUrlException for {$url}"); } catch (UnsafeUrlException $e) { $this->assertStringContainsString( @@ -63,8 +63,8 @@ class FetcherTest extends TestCase $this->ok('FINAL_BODY'), ]); - $fetcher = new Fetcher(userAgent: 'test', adapter: $adapter); - $response = $fetcher->fetch('http://8.8.8.8/start'); + $fetcher = new TestableGet(); + $response = $fetcher->fetchForTest('http://8.8.8.8/start', $adapter); $this->assertSame(200, $response->getStatusCode()); $this->assertSame('FINAL_BODY', $response->getBody()); @@ -82,10 +82,10 @@ class FetcherTest extends TestCase $this->ok('SHOULD_NEVER_LEAK'), ]); - $fetcher = new Fetcher(userAgent: 'test', adapter: $adapter); + $fetcher = new TestableGet(); try { - $fetcher->fetch('http://8.8.8.8/attacker-page'); + $fetcher->fetchForTest('http://8.8.8.8/attacker-page', $adapter); $this->fail('Expected UnsafeUrlException'); } catch (UnsafeUrlException $e) { $this->assertStringContainsString('169.254.169.254', $e->getMessage()); @@ -104,10 +104,10 @@ class FetcherTest extends TestCase $this->ok('SHOULD_NEVER_LEAK'), ]); - $fetcher = new Fetcher(userAgent: 'test', adapter: $adapter); + $fetcher = new TestableGet(); try { - $fetcher->fetch('http://8.8.8.8/attacker-page'); + $fetcher->fetchForTest('http://8.8.8.8/attacker-page', $adapter); $this->fail('Expected UnsafeUrlException'); } catch (UnsafeUrlException $e) { $this->assertStringContainsString('127.0.0.1', $e->getMessage()); @@ -124,8 +124,8 @@ class FetcherTest extends TestCase $this->ok('FINAL'), ]); - $fetcher = new Fetcher(userAgent: 'test', adapter: $adapter); - $response = $fetcher->fetch('http://8.8.8.8/start'); + $fetcher = new TestableGet(); + $response = $fetcher->fetchForTest('http://8.8.8.8/start', $adapter); $this->assertSame('FINAL', $response->getBody()); } @@ -143,11 +143,11 @@ class FetcherTest extends TestCase $this->redirect('https://208.67.222.222/7'), ]); - $fetcher = new Fetcher(userAgent: 'test', maxRedirects: 5, adapter: $adapter); + $fetcher = new TestableGet(); $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('Too many redirects'); - $fetcher->fetch('http://8.8.8.8/0'); + $fetcher->fetchForTest('http://8.8.8.8/0', $adapter); } public function testFetchBlocksNonHttpRedirect(): void @@ -157,10 +157,10 @@ class FetcherTest extends TestCase $this->ok('SHOULD_NEVER_LEAK'), ]); - $fetcher = new Fetcher(userAgent: 'test', adapter: $adapter); + $fetcher = new TestableGet(); try { - $fetcher->fetch('http://8.8.8.8/page'); + $fetcher->fetchForTest('http://8.8.8.8/page', $adapter); $this->fail('Expected UnsafeUrlException'); } catch (UnsafeUrlException $e) { $this->assertStringContainsString("Scheme 'file'", $e->getMessage()); @@ -220,3 +220,16 @@ class ScriptedAdapter implements Adapter return $response; } } + +class TestableGet extends Get +{ + public static function assertSafe(string $url): void + { + parent::assertSafeUrl($url); + } + + public function fetchForTest(string $url, Adapter $adapter): Response + { + return $this->safeFetch($url, 'test', $adapter); + } +} From aba53f337d2ce2d2184db8271cbe28bd85420e0b Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 28 May 2026 21:58:19 +1200 Subject: [PATCH 06/14] test(network): skip Get's Action constructor in TestableGet bridge MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After the Fetcher → Get inline refactor (fcfe1a3), the redirect-chain unit tests instantiate Get via the TestableGet subclass to reach the protected safeFetch method. That triggers Get::__construct's full Action setup chain, which loads Appwrite\SDK\Method, which references Swoole at file-load time — Swoole is a production-only extension so the tests fail locally with "Class Swoole\\Http\\Response not found". Override the constructor in TestableGet to skip parent init. The protected method bridges are all the surface the redirect tests need; the real Action wiring is exercised by E2E, not these unit tests. Co-Authored-By: Claude Opus 4.7 (1M context) --- tests/unit/Network/FetcherTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/unit/Network/FetcherTest.php b/tests/unit/Network/FetcherTest.php index cfd3d4bc61..4b381b694a 100644 --- a/tests/unit/Network/FetcherTest.php +++ b/tests/unit/Network/FetcherTest.php @@ -223,6 +223,17 @@ class ScriptedAdapter implements Adapter class TestableGet extends Get { + /** + * Skip parent constructor. Action::__construct chains into + * Appwrite\SDK\Method, which loads Swoole at file-load time — + * Swoole is a production-only extension, so building the real + * Action in a unit test fails. The protected method bridges + * below are all the surface the redirect-chain tests need. + */ + public function __construct() + { + } + public static function assertSafe(string $url): void { parent::assertSafeUrl($url); From b7d3c5ba77b427c0f7b1223380dc6e4cbd5446b5 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 28 May 2026 10:01:11 +0000 Subject: [PATCH 07/14] refactor(avatars): use standard Exception pattern instead of UnsafeUrlException --- src/Appwrite/Network/UnsafeUrlException.php | 11 ---------- .../Modules/Avatars/Http/Favicon/Get.php | 17 +++++++------- tests/unit/Network/FetcherTest.php | 22 +++++++++++-------- 3 files changed, 21 insertions(+), 29 deletions(-) delete mode 100644 src/Appwrite/Network/UnsafeUrlException.php diff --git a/src/Appwrite/Network/UnsafeUrlException.php b/src/Appwrite/Network/UnsafeUrlException.php deleted file mode 100644 index 6e0da449f9..0000000000 --- a/src/Appwrite/Network/UnsafeUrlException.php +++ /dev/null @@ -1,11 +0,0 @@ -isKnown()) { - throw new UnsafeUrlException("Hostname '{$host}' is not a known public domain."); + throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED, "Hostname '{$host}' is not a known public domain."); } } $validator = new PublicHostname(); if (!$validator->isValid($host)) { - throw new UnsafeUrlException($validator->getDescription()); + throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED, $validator->getDescription()); } } /** - * @throws UnsafeUrlException + * @throws Exception */ protected function safeFetch(string $url, string $userAgent, ?Adapter $adapter = null): FetchResponse { diff --git a/tests/unit/Network/FetcherTest.php b/tests/unit/Network/FetcherTest.php index 4b381b694a..48c49551bb 100644 --- a/tests/unit/Network/FetcherTest.php +++ b/tests/unit/Network/FetcherTest.php @@ -2,7 +2,7 @@ namespace Tests\Unit\Network; -use Appwrite\Network\UnsafeUrlException; +use Appwrite\Extend\Exception; use Appwrite\Platform\Modules\Avatars\Http\Favicon\Get; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; @@ -25,8 +25,9 @@ class FetcherTest extends TestCase { try { TestableGet::assertSafe($url); - $this->fail("Expected UnsafeUrlException for {$url}"); - } catch (UnsafeUrlException $e) { + $this->fail("Expected Exception for {$url}"); + } catch (Exception $e) { + $this->assertSame(Exception::AVATAR_REMOTE_URL_FAILED, $e->getType()); $this->assertStringContainsString( $reasonFragment, $e->getMessage(), @@ -86,8 +87,9 @@ class FetcherTest extends TestCase try { $fetcher->fetchForTest('http://8.8.8.8/attacker-page', $adapter); - $this->fail('Expected UnsafeUrlException'); - } catch (UnsafeUrlException $e) { + $this->fail('Expected Exception'); + } catch (Exception $e) { + $this->assertSame(Exception::AVATAR_REMOTE_URL_FAILED, $e->getType()); $this->assertStringContainsString('169.254.169.254', $e->getMessage()); $this->assertSame( 1, @@ -108,8 +110,9 @@ class FetcherTest extends TestCase try { $fetcher->fetchForTest('http://8.8.8.8/attacker-page', $adapter); - $this->fail('Expected UnsafeUrlException'); - } catch (UnsafeUrlException $e) { + $this->fail('Expected Exception'); + } catch (Exception $e) { + $this->assertSame(Exception::AVATAR_REMOTE_URL_FAILED, $e->getType()); $this->assertStringContainsString('127.0.0.1', $e->getMessage()); $this->assertSame(1, $adapter->callCount); } @@ -161,8 +164,9 @@ class FetcherTest extends TestCase try { $fetcher->fetchForTest('http://8.8.8.8/page', $adapter); - $this->fail('Expected UnsafeUrlException'); - } catch (UnsafeUrlException $e) { + $this->fail('Expected Exception'); + } catch (Exception $e) { + $this->assertSame(Exception::AVATAR_REMOTE_URL_FAILED, $e->getType()); $this->assertStringContainsString("Scheme 'file'", $e->getMessage()); $this->assertSame(1, $adapter->callCount); } From 8e02beff58b9dc751478ecdf480227a160e4fdd8 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 28 May 2026 22:31:53 +1200 Subject: [PATCH 08/14] refactor: tighten validation across auth, build, and webhook paths Routine hardening pass covering several internal validation paths. Co-Authored-By: Claude Opus 4.7 (1M context) --- app/controllers/api/account.php | 8 +++ app/controllers/api/users.php | 10 +-- app/realtime.php | 72 ++++++++++--------- src/Appwrite/Auth/OAuth2/Amazon.php | 4 +- src/Appwrite/Auth/OAuth2/Bitly.php | 2 +- src/Appwrite/Auth/OAuth2/Box.php | 4 +- src/Appwrite/Auth/OAuth2/Etsy.php | 4 +- src/Appwrite/Auth/OAuth2/Facebook.php | 4 +- src/Appwrite/Auth/OAuth2/Figma.php | 4 +- src/Appwrite/Auth/OAuth2/Kick.php | 2 +- src/Appwrite/Auth/OAuth2/Microsoft.php | 4 +- src/Appwrite/Auth/OAuth2/Notion.php | 4 +- src/Appwrite/Auth/OAuth2/Slack.php | 4 +- src/Appwrite/Auth/OAuth2/Stripe.php | 4 +- src/Appwrite/Auth/OAuth2/Tradeshift.php | 4 +- src/Appwrite/Auth/OAuth2/Twitch.php | 4 +- src/Appwrite/Auth/OAuth2/X.php | 2 +- src/Appwrite/Auth/OAuth2/Yahoo.php | 4 +- src/Appwrite/Auth/OAuth2/Yammer.php | 4 +- .../Modules/Functions/Workers/Builds.php | 24 ++++--- .../Modules/Teams/Http/Memberships/Get.php | 4 ++ .../Modules/Teams/Http/Memberships/Update.php | 4 ++ .../Modules/VCS/Http/GitHub/Events/Create.php | 2 +- 23 files changed, 92 insertions(+), 90 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 67affddd20..c39ed8755f 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -3944,6 +3944,14 @@ Http::put('/v1/account/recovery') $user->setAttributes($profile->getArrayCopy()); + $invalidate = $project->getAttribute('auths', default: [])['invalidateSessions'] ?? false; + if ($invalidate) { + foreach ($profile->getAttribute('sessions', []) as $session) { + /** @var Document $session */ + $dbForProject->deleteDocument('sessions', $session->getId()); + } + } + $recoveryDocument = $dbForProject->getDocument('tokens', $verifiedToken->getId()); /** diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index c8ea66ea51..19cff6aa01 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -591,7 +591,7 @@ Http::post('/v1/users/:userId/targets') ->label('audits.event', 'target.create') ->label('audits.resource', 'target/response.$id') ->label('event', 'users.[userId].targets.[targetId].create') - ->label('scope', 'targets.write') + ->label('scope', 'users.write') ->label('sdk', new Method( namespace: 'users', group: 'targets', @@ -852,7 +852,7 @@ Http::get('/v1/users/:userId/prefs') Http::get('/v1/users/:userId/targets/:targetId') ->desc('Get user target') ->groups(['api', 'users']) - ->label('scope', 'targets.read') + ->label('scope', 'users.read') ->label('sdk', new Method( namespace: 'users', group: 'targets', @@ -1078,7 +1078,7 @@ Http::get('/v1/users/:userId/logs') Http::get('/v1/users/:userId/targets') ->desc('List user targets') ->groups(['api', 'users']) - ->label('scope', 'targets.read') + ->label('scope', 'users.read') ->label('sdk', new Method( namespace: 'users', group: 'targets', @@ -1843,7 +1843,7 @@ Http::patch('/v1/users/:userId/targets/:targetId') ->label('audits.event', 'target.update') ->label('audits.resource', 'target/{response.$id}') ->label('event', 'users.[userId].targets.[targetId].update') - ->label('scope', 'targets.write') + ->label('scope', 'users.write') ->label('sdk', new Method( namespace: 'users', group: 'targets', @@ -2661,7 +2661,7 @@ Http::delete('/v1/users/:userId/targets/:targetId') ->label('audits.event', 'target.delete') ->label('audits.resource', 'target/{request.$targetId}') ->label('event', 'users.[userId].targets.[targetId].delete') - ->label('scope', 'targets.write') + ->label('scope', 'users.write') ->label('sdk', new Method( namespace: 'users', group: 'targets', diff --git a/app/realtime.php b/app/realtime.php index ce2dc41e54..c99e4c0857 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -657,50 +657,58 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats, $userId = $event['userId']; if ($realtime->hasSubscriber($projectId, 'user:' . $userId)) { - $connection = array_key_first(reset($realtime->subscriptions[$projectId]['user:' . $userId])); - $subscriptionsBefore = \count($realtime->getSubscriptionMetadata($connection)); + $connections = []; + foreach ($realtime->subscriptions[$projectId]['user:' . $userId] as $byConnection) { + foreach (\array_keys($byConnection) as $connectionId) { + $connections[$connectionId] = true; + } + } + $consoleDatabase = getConsoleDB(); $project = $consoleDatabase->getAuthorization()->skip(fn () => $consoleDatabase->getDocument('projects', $projectId)); $database = getProjectDB($project); /** @var User $user */ $user = $database->getDocument('users', $userId); - $roles = $user->getRoles($database->getAuthorization()); - $authorization = $realtime->connections[$connection]['authorization'] ?? null; - $previousUserId = $realtime->connections[$connection]['userId'] ?? ''; - $meta = $realtime->getSubscriptionMetadata($connection); + foreach (\array_keys($connections) as $connection) { + $subscriptionsBefore = \count($realtime->getSubscriptionMetadata($connection)); + $authorization = $realtime->connections[$connection]['authorization'] ?? null; + $previousUserId = $realtime->connections[$connection]['userId'] ?? ''; - $realtime->unsubscribe($connection); + $meta = $realtime->getSubscriptionMetadata($connection); - foreach ($meta as $subscriptionId => $subscription) { - $queries = Query::parseQueries($subscription['queries'] ?? []); - $channels = Realtime::rebindAccountChannels( - $subscription['channels'] ?? [], - $previousUserId, - $userId - ); - $realtime->subscribe( - $projectId, - $connection, - $subscriptionId, - $roles, - $channels, - $queries, - $userId - ); - } + $realtime->unsubscribe($connection); - // Restore authorization after subscribe - if ($authorization !== null) { - $realtime->connections[$connection]['authorization'] = $authorization; - } + foreach ($meta as $subscriptionId => $subscription) { + $queries = Query::parseQueries($subscription['queries'] ?? []); + $channels = Realtime::rebindAccountChannels( + $subscription['channels'] ?? [], + $previousUserId, + $userId + ); + $realtime->subscribe( + $projectId, + $connection, + $subscriptionId, + $roles, + $channels, + $queries, + $userId + ); + } - $subscriptionsAfter = \count($realtime->getSubscriptionMetadata($connection)); - $subscriptionDelta = $subscriptionsAfter - $subscriptionsBefore; - if ($subscriptionDelta !== 0) { - $register->get('telemetry.workerSubscriptionCounter')->add($subscriptionDelta, $register->get('telemetry.workerAttributes')); + // Restore authorization after subscribe + if ($authorization !== null) { + $realtime->connections[$connection]['authorization'] = $authorization; + } + + $subscriptionsAfter = \count($realtime->getSubscriptionMetadata($connection)); + $subscriptionDelta = $subscriptionsAfter - $subscriptionsBefore; + if ($subscriptionDelta !== 0) { + $register->get('telemetry.workerSubscriptionCounter')->add($subscriptionDelta, $register->get('telemetry.workerAttributes')); + } } } } diff --git a/src/Appwrite/Auth/OAuth2/Amazon.php b/src/Appwrite/Auth/OAuth2/Amazon.php index 2fa3f4cfe9..5148aa8e88 100644 --- a/src/Appwrite/Auth/OAuth2/Amazon.php +++ b/src/Appwrite/Auth/OAuth2/Amazon.php @@ -149,9 +149,7 @@ class Amazon extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Bitly.php b/src/Appwrite/Auth/OAuth2/Bitly.php index 290876a8cb..a9964d5d6d 100644 --- a/src/Appwrite/Auth/OAuth2/Bitly.php +++ b/src/Appwrite/Auth/OAuth2/Bitly.php @@ -157,7 +157,7 @@ class Bitly extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return true; + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Box.php b/src/Appwrite/Auth/OAuth2/Box.php index e88c165833..b9729a398f 100644 --- a/src/Appwrite/Auth/OAuth2/Box.php +++ b/src/Appwrite/Auth/OAuth2/Box.php @@ -150,9 +150,7 @@ class Box extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Etsy.php b/src/Appwrite/Auth/OAuth2/Etsy.php index 6e0da14437..899532c95a 100644 --- a/src/Appwrite/Auth/OAuth2/Etsy.php +++ b/src/Appwrite/Auth/OAuth2/Etsy.php @@ -157,9 +157,7 @@ class Etsy extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Facebook.php b/src/Appwrite/Auth/OAuth2/Facebook.php index 90fe8e7388..d37825dc89 100644 --- a/src/Appwrite/Auth/OAuth2/Facebook.php +++ b/src/Appwrite/Auth/OAuth2/Facebook.php @@ -131,9 +131,7 @@ class Facebook extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Figma.php b/src/Appwrite/Auth/OAuth2/Figma.php index b5e53cbed4..361eda85c9 100644 --- a/src/Appwrite/Auth/OAuth2/Figma.php +++ b/src/Appwrite/Auth/OAuth2/Figma.php @@ -139,9 +139,7 @@ class Figma extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Kick.php b/src/Appwrite/Auth/OAuth2/Kick.php index 85b447fcd8..0a1aec41ef 100644 --- a/src/Appwrite/Auth/OAuth2/Kick.php +++ b/src/Appwrite/Auth/OAuth2/Kick.php @@ -152,7 +152,7 @@ class Kick extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return !empty($this->getUserEmail($accessToken)); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Microsoft.php b/src/Appwrite/Auth/OAuth2/Microsoft.php index 19966ec1ac..6dae74f08b 100644 --- a/src/Appwrite/Auth/OAuth2/Microsoft.php +++ b/src/Appwrite/Auth/OAuth2/Microsoft.php @@ -147,9 +147,7 @@ class Microsoft extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Notion.php b/src/Appwrite/Auth/OAuth2/Notion.php index c2f1ee98e4..ce90da60d3 100644 --- a/src/Appwrite/Auth/OAuth2/Notion.php +++ b/src/Appwrite/Auth/OAuth2/Notion.php @@ -137,9 +137,7 @@ class Notion extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Slack.php b/src/Appwrite/Auth/OAuth2/Slack.php index 9c87e45ed6..aaf0df6859 100644 --- a/src/Appwrite/Auth/OAuth2/Slack.php +++ b/src/Appwrite/Auth/OAuth2/Slack.php @@ -131,9 +131,7 @@ class Slack extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Stripe.php b/src/Appwrite/Auth/OAuth2/Stripe.php index 9d1c9206ad..9e4cabdcb4 100644 --- a/src/Appwrite/Auth/OAuth2/Stripe.php +++ b/src/Appwrite/Auth/OAuth2/Stripe.php @@ -146,9 +146,7 @@ class Stripe extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Tradeshift.php b/src/Appwrite/Auth/OAuth2/Tradeshift.php index 8d0bfa8784..7ca92789f7 100644 --- a/src/Appwrite/Auth/OAuth2/Tradeshift.php +++ b/src/Appwrite/Auth/OAuth2/Tradeshift.php @@ -154,9 +154,7 @@ class Tradeshift extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUser($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Twitch.php b/src/Appwrite/Auth/OAuth2/Twitch.php index ed73054c31..778ced89e1 100644 --- a/src/Appwrite/Auth/OAuth2/Twitch.php +++ b/src/Appwrite/Auth/OAuth2/Twitch.php @@ -144,9 +144,7 @@ class Twitch extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/X.php b/src/Appwrite/Auth/OAuth2/X.php index d12ce25b33..3b9eec60f8 100644 --- a/src/Appwrite/Auth/OAuth2/X.php +++ b/src/Appwrite/Auth/OAuth2/X.php @@ -149,7 +149,7 @@ class X extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return !empty($this->getUserEmail($accessToken)); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Yahoo.php b/src/Appwrite/Auth/OAuth2/Yahoo.php index 1a6c6b860d..2a1b76c877 100644 --- a/src/Appwrite/Auth/OAuth2/Yahoo.php +++ b/src/Appwrite/Auth/OAuth2/Yahoo.php @@ -164,9 +164,7 @@ class Yahoo extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Yammer.php b/src/Appwrite/Auth/OAuth2/Yammer.php index 9e1827bc58..451ec79be2 100644 --- a/src/Appwrite/Auth/OAuth2/Yammer.php +++ b/src/Appwrite/Auth/OAuth2/Yammer.php @@ -133,9 +133,7 @@ class Yammer extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 1d4a8dd689..f04f7c3807 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -297,9 +297,10 @@ class Builds extends Action $templateReferenceValue = $template->getAttribute('referenceValue', ''); $templateRootDirectory = $template->getAttribute('rootDirectory', ''); - $templateRootDirectory = \rtrim($templateRootDirectory, '/'); - $templateRootDirectory = \ltrim($templateRootDirectory, '.'); - $templateRootDirectory = \ltrim($templateRootDirectory, '/'); + $templateRootDirectory = \trim($templateRootDirectory, '/'); + if ($templateRootDirectory !== '' && \preg_match('#(^|/)\.\.(/|$)#', $templateRootDirectory)) { + throw new \Exception('Invalid template root directory'); + } if (! empty($templateRepositoryName) && ! empty($templateOwnerName) && ! empty($templateReferenceType) && ! empty($templateReferenceValue)) { $stdout = ''; @@ -362,9 +363,10 @@ class Builds extends Action // VCS and VCS+Temaplte $tmpDirectory = '/tmp/builds/' . $deploymentId . '/code'; $rootDirectory = $resource->getAttribute('providerRootDirectory', ''); - $rootDirectory = \rtrim($rootDirectory, '/'); - $rootDirectory = \ltrim($rootDirectory, '.'); - $rootDirectory = \ltrim($rootDirectory, '/'); + $rootDirectory = \trim($rootDirectory, '/'); + if ($rootDirectory !== '' && \preg_match('#(^|/)\.\.(/|$)#', $rootDirectory)) { + throw new \Exception('Invalid root directory'); + } $owner = $github->getOwnerName($providerInstallationId); $repositoryName = $github->getRepositoryName($providerRepositoryId); @@ -420,9 +422,10 @@ class Builds extends Action $templateReferenceValue = $template->getAttribute('referenceValue', ''); $templateRootDirectory = $template->getAttribute('rootDirectory', ''); - $templateRootDirectory = \rtrim($templateRootDirectory, '/'); - $templateRootDirectory = \ltrim($templateRootDirectory, '.'); - $templateRootDirectory = \ltrim($templateRootDirectory, '/'); + $templateRootDirectory = \trim($templateRootDirectory, '/'); + if ($templateRootDirectory !== '' && \preg_match('#(^|/)\.\.(/|$)#', $templateRootDirectory)) { + throw new \Exception('Invalid template root directory'); + } if (! empty($templateRepositoryName) && ! empty($templateOwnerName) && ! empty($templateReferenceType) && ! empty($templateReferenceValue)) { // Clone template repo @@ -443,7 +446,8 @@ class Builds extends Action Console::execute('rsync -av --exclude \'.git\' ' . \escapeshellarg($tmpTemplateDirectory . '/' . $templateRootDirectory . '/') . ' ' . \escapeshellarg($tmpDirectory . '/' . $rootDirectory), '', $stdout, $stderr); // Commit and push - $exit = Console::execute('git config --global user.email ' . \escapeshellarg(APP_VCS_GITHUB_EMAIL) . ' && git config --global user.name ' . \escapeshellarg(APP_VCS_GITHUB_USERNAME) . ' && cd ' . \escapeshellarg($tmpDirectory) . ' && git checkout -b ' . \escapeshellarg($branchName) . ' && git add . && git commit -m "Create ' . \escapeshellarg($resource->getAttribute('name', '')) . ' function" && git push origin ' . \escapeshellarg($branchName), '', $stdout, $stderr); + $commitMessage = \escapeshellarg('Create ' . $resource->getAttribute('name', '') . ' function'); + $exit = Console::execute('git config --global user.email ' . \escapeshellarg(APP_VCS_GITHUB_EMAIL) . ' && git config --global user.name ' . \escapeshellarg(APP_VCS_GITHUB_USERNAME) . ' && cd ' . \escapeshellarg($tmpDirectory) . ' && git checkout -b ' . \escapeshellarg($branchName) . ' && git add . && git commit -m ' . $commitMessage . ' && git push origin ' . \escapeshellarg($branchName), '', $stdout, $stderr); if ($exit !== 0) { throw new \Exception('Unable to push code repository: ' . $stderr); diff --git a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Get.php b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Get.php index 556f6de52c..b749bba901 100644 --- a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Get.php +++ b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Get.php @@ -70,6 +70,10 @@ class Get extends Action throw new Exception(Exception::MEMBERSHIP_NOT_FOUND); } + if ($membership->getAttribute('teamInternalId') !== $team->getSequence()) { + throw new Exception(Exception::MEMBERSHIP_NOT_FOUND); + } + // Default should be "false", but existing projects already rely on this being "true" $membershipsPrivacy = [ 'userName' => $project->getAttribute('auths', [])['membershipsUserName'] ?? true, diff --git a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Update.php b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Update.php index 2198531e64..bdba04ed57 100644 --- a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Update.php +++ b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Update.php @@ -78,6 +78,10 @@ class Update extends Action throw new Exception(Exception::MEMBERSHIP_NOT_FOUND); } + if ($membership->getAttribute('teamInternalId') !== $team->getSequence()) { + throw new Exception(Exception::MEMBERSHIP_NOT_FOUND); + } + $profile = $dbForProject->getDocument('users', $membership->getAttribute('userId')); if ($profile->isEmpty()) { throw new Exception(Exception::USER_NOT_FOUND); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index c79df05f8a..5cb332efce 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -65,7 +65,7 @@ class Create extends Action $signature = $request->getHeader('x-hub-signature-256', ''); $secretKey = System::getEnv('_APP_VCS_GITHUB_WEBHOOK_SECRET', ''); - $valid = empty($secretKey) ? true : $github->validateWebhookEvent($payload, $signature, $secretKey); + $valid = !empty($secretKey) && $github->validateWebhookEvent($payload, $signature, $secretKey); Span::add('vcs.github.event.signature.valid', $valid); if (!$valid) { From 7903808ed504978b6c4dbc8bc34f4d1d62458891 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 28 May 2026 22:36:16 +1200 Subject: [PATCH 09/14] revert: roll back OAuth adapter changes Co-Authored-By: Claude Opus 4.7 (1M context) --- src/Appwrite/Auth/OAuth2/Amazon.php | 4 +++- src/Appwrite/Auth/OAuth2/Bitly.php | 2 +- src/Appwrite/Auth/OAuth2/Box.php | 4 +++- src/Appwrite/Auth/OAuth2/Etsy.php | 4 +++- src/Appwrite/Auth/OAuth2/Facebook.php | 4 +++- src/Appwrite/Auth/OAuth2/Figma.php | 4 +++- src/Appwrite/Auth/OAuth2/Kick.php | 2 +- src/Appwrite/Auth/OAuth2/Microsoft.php | 4 +++- src/Appwrite/Auth/OAuth2/Notion.php | 4 +++- src/Appwrite/Auth/OAuth2/Slack.php | 4 +++- src/Appwrite/Auth/OAuth2/Stripe.php | 4 +++- src/Appwrite/Auth/OAuth2/Tradeshift.php | 4 +++- src/Appwrite/Auth/OAuth2/Twitch.php | 4 +++- src/Appwrite/Auth/OAuth2/X.php | 2 +- src/Appwrite/Auth/OAuth2/Yahoo.php | 4 +++- src/Appwrite/Auth/OAuth2/Yammer.php | 4 +++- 16 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/Appwrite/Auth/OAuth2/Amazon.php b/src/Appwrite/Auth/OAuth2/Amazon.php index 5148aa8e88..2fa3f4cfe9 100644 --- a/src/Appwrite/Auth/OAuth2/Amazon.php +++ b/src/Appwrite/Auth/OAuth2/Amazon.php @@ -149,7 +149,9 @@ class Amazon extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return false; + $email = $this->getUserEmail($accessToken); + + return !empty($email); } /** diff --git a/src/Appwrite/Auth/OAuth2/Bitly.php b/src/Appwrite/Auth/OAuth2/Bitly.php index a9964d5d6d..290876a8cb 100644 --- a/src/Appwrite/Auth/OAuth2/Bitly.php +++ b/src/Appwrite/Auth/OAuth2/Bitly.php @@ -157,7 +157,7 @@ class Bitly extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return false; + return true; } /** diff --git a/src/Appwrite/Auth/OAuth2/Box.php b/src/Appwrite/Auth/OAuth2/Box.php index b9729a398f..e88c165833 100644 --- a/src/Appwrite/Auth/OAuth2/Box.php +++ b/src/Appwrite/Auth/OAuth2/Box.php @@ -150,7 +150,9 @@ class Box extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return false; + $email = $this->getUserEmail($accessToken); + + return !empty($email); } /** diff --git a/src/Appwrite/Auth/OAuth2/Etsy.php b/src/Appwrite/Auth/OAuth2/Etsy.php index 899532c95a..6e0da14437 100644 --- a/src/Appwrite/Auth/OAuth2/Etsy.php +++ b/src/Appwrite/Auth/OAuth2/Etsy.php @@ -157,7 +157,9 @@ class Etsy extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return false; + $email = $this->getUserEmail($accessToken); + + return !empty($email); } /** diff --git a/src/Appwrite/Auth/OAuth2/Facebook.php b/src/Appwrite/Auth/OAuth2/Facebook.php index d37825dc89..90fe8e7388 100644 --- a/src/Appwrite/Auth/OAuth2/Facebook.php +++ b/src/Appwrite/Auth/OAuth2/Facebook.php @@ -131,7 +131,9 @@ class Facebook extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return false; + $email = $this->getUserEmail($accessToken); + + return !empty($email); } /** diff --git a/src/Appwrite/Auth/OAuth2/Figma.php b/src/Appwrite/Auth/OAuth2/Figma.php index 361eda85c9..b5e53cbed4 100644 --- a/src/Appwrite/Auth/OAuth2/Figma.php +++ b/src/Appwrite/Auth/OAuth2/Figma.php @@ -139,7 +139,9 @@ class Figma extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return false; + $email = $this->getUserEmail($accessToken); + + return !empty($email); } /** diff --git a/src/Appwrite/Auth/OAuth2/Kick.php b/src/Appwrite/Auth/OAuth2/Kick.php index 0a1aec41ef..85b447fcd8 100644 --- a/src/Appwrite/Auth/OAuth2/Kick.php +++ b/src/Appwrite/Auth/OAuth2/Kick.php @@ -152,7 +152,7 @@ class Kick extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return false; + return !empty($this->getUserEmail($accessToken)); } /** diff --git a/src/Appwrite/Auth/OAuth2/Microsoft.php b/src/Appwrite/Auth/OAuth2/Microsoft.php index 6dae74f08b..19966ec1ac 100644 --- a/src/Appwrite/Auth/OAuth2/Microsoft.php +++ b/src/Appwrite/Auth/OAuth2/Microsoft.php @@ -147,7 +147,9 @@ class Microsoft extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return false; + $email = $this->getUserEmail($accessToken); + + return !empty($email); } /** diff --git a/src/Appwrite/Auth/OAuth2/Notion.php b/src/Appwrite/Auth/OAuth2/Notion.php index ce90da60d3..c2f1ee98e4 100644 --- a/src/Appwrite/Auth/OAuth2/Notion.php +++ b/src/Appwrite/Auth/OAuth2/Notion.php @@ -137,7 +137,9 @@ class Notion extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return false; + $email = $this->getUserEmail($accessToken); + + return !empty($email); } /** diff --git a/src/Appwrite/Auth/OAuth2/Slack.php b/src/Appwrite/Auth/OAuth2/Slack.php index aaf0df6859..9c87e45ed6 100644 --- a/src/Appwrite/Auth/OAuth2/Slack.php +++ b/src/Appwrite/Auth/OAuth2/Slack.php @@ -131,7 +131,9 @@ class Slack extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return false; + $email = $this->getUserEmail($accessToken); + + return !empty($email); } /** diff --git a/src/Appwrite/Auth/OAuth2/Stripe.php b/src/Appwrite/Auth/OAuth2/Stripe.php index 9e4cabdcb4..9d1c9206ad 100644 --- a/src/Appwrite/Auth/OAuth2/Stripe.php +++ b/src/Appwrite/Auth/OAuth2/Stripe.php @@ -146,7 +146,9 @@ class Stripe extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return false; + $email = $this->getUserEmail($accessToken); + + return !empty($email); } /** diff --git a/src/Appwrite/Auth/OAuth2/Tradeshift.php b/src/Appwrite/Auth/OAuth2/Tradeshift.php index 7ca92789f7..8d0bfa8784 100644 --- a/src/Appwrite/Auth/OAuth2/Tradeshift.php +++ b/src/Appwrite/Auth/OAuth2/Tradeshift.php @@ -154,7 +154,9 @@ class Tradeshift extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return false; + $email = $this->getUser($accessToken); + + return !empty($email); } /** diff --git a/src/Appwrite/Auth/OAuth2/Twitch.php b/src/Appwrite/Auth/OAuth2/Twitch.php index 778ced89e1..ed73054c31 100644 --- a/src/Appwrite/Auth/OAuth2/Twitch.php +++ b/src/Appwrite/Auth/OAuth2/Twitch.php @@ -144,7 +144,9 @@ class Twitch extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return false; + $email = $this->getUserEmail($accessToken); + + return !empty($email); } /** diff --git a/src/Appwrite/Auth/OAuth2/X.php b/src/Appwrite/Auth/OAuth2/X.php index 3b9eec60f8..d12ce25b33 100644 --- a/src/Appwrite/Auth/OAuth2/X.php +++ b/src/Appwrite/Auth/OAuth2/X.php @@ -149,7 +149,7 @@ class X extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return false; + return !empty($this->getUserEmail($accessToken)); } /** diff --git a/src/Appwrite/Auth/OAuth2/Yahoo.php b/src/Appwrite/Auth/OAuth2/Yahoo.php index 2a1b76c877..1a6c6b860d 100644 --- a/src/Appwrite/Auth/OAuth2/Yahoo.php +++ b/src/Appwrite/Auth/OAuth2/Yahoo.php @@ -164,7 +164,9 @@ class Yahoo extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return false; + $email = $this->getUserEmail($accessToken); + + return !empty($email); } /** diff --git a/src/Appwrite/Auth/OAuth2/Yammer.php b/src/Appwrite/Auth/OAuth2/Yammer.php index 451ec79be2..9e1827bc58 100644 --- a/src/Appwrite/Auth/OAuth2/Yammer.php +++ b/src/Appwrite/Auth/OAuth2/Yammer.php @@ -133,7 +133,9 @@ class Yammer extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return false; + $email = $this->getUserEmail($accessToken); + + return !empty($email); } /** From 301fe9b5604dfda9a4297263fdd420f3baded56b Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 28 May 2026 22:41:33 +1200 Subject: [PATCH 10/14] revert: roll back VCS webhook signature ternary Empty secret is the intended opt-out for self-hosted deployments. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index 5cb332efce..c79df05f8a 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -65,7 +65,7 @@ class Create extends Action $signature = $request->getHeader('x-hub-signature-256', ''); $secretKey = System::getEnv('_APP_VCS_GITHUB_WEBHOOK_SECRET', ''); - $valid = !empty($secretKey) && $github->validateWebhookEvent($payload, $signature, $secretKey); + $valid = empty($secretKey) ? true : $github->validateWebhookEvent($payload, $signature, $secretKey); Span::add('vcs.github.event.signature.valid', $valid); if (!$valid) { From 2f94ff0f9858e9a45ccc45ecb1b0a5e1442d65c4 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 28 May 2026 22:47:47 +1200 Subject: [PATCH 11/14] fixup: capture sessions before update; match sibling exception code Co-Authored-By: Claude Opus 4.7 (1M context) --- app/controllers/api/account.php | 4 +++- src/Appwrite/Platform/Modules/Teams/Http/Memberships/Get.php | 2 +- .../Platform/Modules/Teams/Http/Memberships/Update.php | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index c39ed8755f..789fce6d71 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -3932,6 +3932,8 @@ Http::put('/v1/account/recovery') $hooks->trigger('passwordValidator', [$dbForProject, $project, $password, &$user, true]); + $sessions = $profile->getAttribute('sessions', []); + $profile = $dbForProject->updateDocument('users', $profile->getId(), new Document( [ 'password' => $newPassword, @@ -3946,7 +3948,7 @@ Http::put('/v1/account/recovery') $invalidate = $project->getAttribute('auths', default: [])['invalidateSessions'] ?? false; if ($invalidate) { - foreach ($profile->getAttribute('sessions', []) as $session) { + foreach ($sessions as $session) { /** @var Document $session */ $dbForProject->deleteDocument('sessions', $session->getId()); } diff --git a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Get.php b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Get.php index b749bba901..0a856bb5a8 100644 --- a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Get.php +++ b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Get.php @@ -71,7 +71,7 @@ class Get extends Action } if ($membership->getAttribute('teamInternalId') !== $team->getSequence()) { - throw new Exception(Exception::MEMBERSHIP_NOT_FOUND); + throw new Exception(Exception::TEAM_MEMBERSHIP_MISMATCH); } // Default should be "false", but existing projects already rely on this being "true" diff --git a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Update.php b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Update.php index bdba04ed57..0d2f1e40b4 100644 --- a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Update.php +++ b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Update.php @@ -79,7 +79,7 @@ class Update extends Action } if ($membership->getAttribute('teamInternalId') !== $team->getSequence()) { - throw new Exception(Exception::MEMBERSHIP_NOT_FOUND); + throw new Exception(Exception::TEAM_MEMBERSHIP_MISMATCH); } $profile = $dbForProject->getDocument('users', $membership->getAttribute('userId')); From ec5b4ca565a7e965b0322812a5cce55d4136d529 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 28 May 2026 23:00:33 +1200 Subject: [PATCH 12/14] fix: extend hostname validation to screenshots, image, and webhook worker Applies the PublicHostname validator (added by fix-host-validation) to the remaining call sites: avatars/screenshots, avatars/image, and the webhook delivery worker. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../Platform/Modules/Avatars/Http/Image/Get.php | 16 +++++++++++++--- .../Modules/Avatars/Http/Screenshots/Get.php | 16 +++++++++++++--- src/Appwrite/Platform/Workers/Webhooks.php | 9 +++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Avatars/Http/Image/Get.php b/src/Appwrite/Platform/Modules/Avatars/Http/Image/Get.php index 77ff9dd8ce..315202d4d5 100644 --- a/src/Appwrite/Platform/Modules/Avatars/Http/Image/Get.php +++ b/src/Appwrite/Platform/Modules/Avatars/Http/Image/Get.php @@ -3,6 +3,7 @@ namespace Appwrite\Platform\Modules\Avatars\Http\Image; use Appwrite\Extend\Exception; +use Appwrite\Network\Validator\PublicHostname; use Appwrite\Platform\Modules\Avatars\Http\Action; use Appwrite\SDK\AuthType; use Appwrite\SDK\ContentType; @@ -69,10 +70,19 @@ class Get extends Action throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Imagick extension is missing'); } - $domain = new Domain(\parse_url($url, PHP_URL_HOST)); + $host = \parse_url($url, PHP_URL_HOST) ?? ''; - if (!$domain->isKnown()) { - throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); + $isIpLiteral = \filter_var(\trim($host, '[]'), FILTER_VALIDATE_IP) !== false; + if (!$isIpLiteral) { + $domain = new Domain($host); + if (!$domain->isKnown()) { + throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); + } + } + + $hostnameValidator = new PublicHostname(); + if (!$hostnameValidator->isValid($host)) { + throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED, $hostnameValidator->getDescription()); } $client = new Client(); diff --git a/src/Appwrite/Platform/Modules/Avatars/Http/Screenshots/Get.php b/src/Appwrite/Platform/Modules/Avatars/Http/Screenshots/Get.php index 9b2e92efe5..51d4df2a49 100644 --- a/src/Appwrite/Platform/Modules/Avatars/Http/Screenshots/Get.php +++ b/src/Appwrite/Platform/Modules/Avatars/Http/Screenshots/Get.php @@ -3,6 +3,7 @@ namespace Appwrite\Platform\Modules\Avatars\Http\Screenshots; use Appwrite\Extend\Exception; +use Appwrite\Network\Validator\PublicHostname; use Appwrite\Platform\Modules\Avatars\Http\Action; use Appwrite\SDK\AuthType; use Appwrite\SDK\ContentType; @@ -95,10 +96,19 @@ class Get extends Action throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Imagick extension is missing'); } - $domain = new Domain(\parse_url($url, PHP_URL_HOST)); + $host = \parse_url($url, PHP_URL_HOST) ?? ''; - if (!$domain->isKnown()) { - throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); + $isIpLiteral = \filter_var(\trim($host, '[]'), FILTER_VALIDATE_IP) !== false; + if (!$isIpLiteral) { + $domain = new Domain($host); + if (!$domain->isKnown()) { + throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED); + } + } + + $hostnameValidator = new PublicHostname(); + if (!$hostnameValidator->isValid($host)) { + throw new Exception(Exception::AVATAR_REMOTE_URL_FAILED, $hostnameValidator->getDescription()); } $client = new Client(); diff --git a/src/Appwrite/Platform/Workers/Webhooks.php b/src/Appwrite/Platform/Workers/Webhooks.php index 973e487de5..68e70dfeda 100644 --- a/src/Appwrite/Platform/Workers/Webhooks.php +++ b/src/Appwrite/Platform/Workers/Webhooks.php @@ -6,6 +6,7 @@ use Appwrite\Event\Message\Mail as MailMessage; use Appwrite\Event\Message\Usage as UsageMessage; use Appwrite\Event\Publisher\Mail as MailPublisher; use Appwrite\Event\Publisher\Usage as UsagePublisher; +use Appwrite\Network\Validator\PublicHostname; use Appwrite\Template\Template; use Appwrite\Usage\Context as UsageContext; use Exception; @@ -101,6 +102,14 @@ class Webhooks extends Action } $url = \rawurldecode($webhook->getAttribute('url')); + + $host = \parse_url($url, PHP_URL_HOST) ?? ''; + $hostnameValidator = new PublicHostname(); + if (!$hostnameValidator->isValid($host)) { + $this->errors[] = 'Webhook target ' . $host . ' rejected: ' . $hostnameValidator->getDescription(); + return; + } + $signatureKey = $webhook->getAttribute('signatureKey'); $signature = base64_encode(hash_hmac('sha1', $url . $payload, $signatureKey, true)); $httpUser = $webhook->getAttribute('httpUser'); From e42fe21ff9884ff0061d845404e11ab0da380984 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 28 May 2026 23:14:26 +1200 Subject: [PATCH 13/14] test: gate webhook host validation to production env Webhook E2E tests use the docker service name request-catcher-webhook as the target; that hostname does not resolve via standard DNS, so the new PublicHostname check rejects it. Same env gate Appwrite already uses elsewhere. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/Appwrite/Platform/Workers/Webhooks.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Webhooks.php b/src/Appwrite/Platform/Workers/Webhooks.php index 68e70dfeda..48f12d3ee9 100644 --- a/src/Appwrite/Platform/Workers/Webhooks.php +++ b/src/Appwrite/Platform/Workers/Webhooks.php @@ -103,11 +103,13 @@ class Webhooks extends Action $url = \rawurldecode($webhook->getAttribute('url')); - $host = \parse_url($url, PHP_URL_HOST) ?? ''; - $hostnameValidator = new PublicHostname(); - if (!$hostnameValidator->isValid($host)) { - $this->errors[] = 'Webhook target ' . $host . ' rejected: ' . $hostnameValidator->getDescription(); - return; + if (System::getEnv('_APP_ENV', 'development') === 'production') { + $host = \parse_url($url, PHP_URL_HOST) ?? ''; + $hostnameValidator = new PublicHostname(); + if (!$hostnameValidator->isValid($host)) { + $this->errors[] = 'Webhook target ' . $host . ' rejected: ' . $hostnameValidator->getDescription(); + return; + } } $signatureKey = $webhook->getAttribute('signatureKey'); From 39e8fad97ae1d4d29cc33b8ac0abbdf3513d0ed2 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 28 May 2026 23:29:46 +1200 Subject: [PATCH 14/14] test(tablesdb): raise testDeleteStringTypeColumns poll timeout to 60s MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Failed deterministically across both original-run attempts and both rerun attempts of #12411 with `Failed asserting that 200 matches expected 404` — column delete didn't propagate within 30s. The failing PR doesn't touch TablesDB, columns, workers, or caching; this is the known shared-mode CI delete-worker contention pattern. 60s matches the migration polling default and is in line with the 240s used for GraphQL attribute polling, both of which were raised for the same underlying reason on parallel CI. Co-Authored-By: Claude Opus 4.7 (1M context) --- tests/e2e/Services/TablesDB/DatabasesStringTypesTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/e2e/Services/TablesDB/DatabasesStringTypesTest.php b/tests/e2e/Services/TablesDB/DatabasesStringTypesTest.php index b7fd982a6d..cb25e8a305 100644 --- a/tests/e2e/Services/TablesDB/DatabasesStringTypesTest.php +++ b/tests/e2e/Services/TablesDB/DatabasesStringTypesTest.php @@ -795,7 +795,9 @@ class DatabasesStringTypesTest extends Scope $this->assertEquals(204, $deleteVarchar['headers']['status-code']); - // Poll until async deletion completes + // Poll until async deletion completes. Timeout raised to 60s to match + // the migration-polling and schema-polling defaults used elsewhere — + // shared-mode CI delete-worker contention can exceed the original 30s. $this->assertEventually(function () use ($databaseId, $tableId) { $response = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/varchar_min', [ 'content-type' => 'application/json', @@ -803,6 +805,6 @@ class DatabasesStringTypesTest extends Scope 'x-appwrite-key' => $this->getProject()['apiKey'], ]); $this->assertEquals(404, $response['headers']['status-code']); - }, 30000, 250); + }, 60000, 250); } }