From b0885c2f6af7738362b863499df74f88ef195928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 7 Sep 2026 18:45:08 +0200 Subject: [PATCH] feat: support capacitor:// as an allowed origin Capacitor's iOS webview serves the app from `capacitor://` (`capacitor://localhost` by default, or the configured `server.hostname`, e.g. `capacitor://app.tycko.cz`). Those requests were rejected with an unknown-origin error because `capacitor` was not a recognised scheme, so the validator fell through to the custom-scheme allow list. Treat it like the other webview/extension schemes: match the origin's host against the project's registered Web platform hostnames, so `capacitor://app.tycko.cz` is accepted once `app.tycko.cz` is registered and nothing else is. Follows the same shape as `tauri://`. Co-Authored-By: Claude Opus 5 (1M context) --- src/Appwrite/Network/Platform.php | 2 ++ src/Appwrite/Network/Validator/Origin.php | 1 + tests/unit/Network/CorsTest.php | 16 ++++++++++++++++ tests/unit/Network/Validators/OriginTest.php | 5 +++++ 4 files changed, 24 insertions(+) diff --git a/src/Appwrite/Network/Platform.php b/src/Appwrite/Network/Platform.php index c20c012862..7c52e2fb14 100644 --- a/src/Appwrite/Network/Platform.php +++ b/src/Appwrite/Network/Platform.php @@ -26,6 +26,7 @@ class Platform public const SCHEME_WINDOWS = 'appwrite-windows'; public const SCHEME_LINUX = 'appwrite-linux'; public const SCHEME_TAURI = 'tauri'; + public const SCHEME_CAPACITOR = 'capacitor'; public const string LOOPBACK_HOSTNAME = 'localhost'; @@ -70,6 +71,7 @@ class Platform self::SCHEME_SAFARI_EXTENSION => 'Web (Safari Extension)', self::SCHEME_EDGE_EXTENSION => 'Web (Edge Extension)', self::SCHEME_TAURI => 'Web (Tauri)', + self::SCHEME_CAPACITOR => 'Web (Capacitor)', ]; /** diff --git a/src/Appwrite/Network/Validator/Origin.php b/src/Appwrite/Network/Validator/Origin.php index 1d4cd79ea1..e018284608 100644 --- a/src/Appwrite/Network/Validator/Origin.php +++ b/src/Appwrite/Network/Validator/Origin.php @@ -70,6 +70,7 @@ class Origin extends Validator Platform::SCHEME_SAFARI_EXTENSION, Platform::SCHEME_EDGE_EXTENSION, Platform::SCHEME_TAURI, + Platform::SCHEME_CAPACITOR, ]; if (in_array($this->scheme, $webPlatforms, true)) { $validator = new Hostname($this->allowedHostnames); diff --git a/tests/unit/Network/CorsTest.php b/tests/unit/Network/CorsTest.php index ab94ba5bbd..0772df8c96 100644 --- a/tests/unit/Network/CorsTest.php +++ b/tests/unit/Network/CorsTest.php @@ -115,6 +115,22 @@ final class CorsTest extends TestCase $this->assertSame('https://example.com', $result[Cors::HEADER_ALLOW_ORIGIN]); } + public function testNonHttpSchemeOriginIsMatchedByHost(): void + { + $cors = new Cors( + allowedHosts: ['app.example.com'], + allowedMethods: ['POST'], + allowedHeaders: ['X-Test'], + exposedHeaders: [], + allowCredentials: true + ); + + /* Webview origins such as Capacitor's carry a custom scheme, see Platform::SCHEME_CAPACITOR */ + $result = $cors->headers('capacitor://app.example.com'); + + $this->assertSame('capacitor://app.example.com', $result[Cors::HEADER_ALLOW_ORIGIN]); + } + public function testOriginIsLowercasedForMatching(): void { $cors = new Cors( diff --git a/tests/unit/Network/Validators/OriginTest.php b/tests/unit/Network/Validators/OriginTest.php index ececb2294d..5b8b4c7bb1 100644 --- a/tests/unit/Network/Validators/OriginTest.php +++ b/tests/unit/Network/Validators/OriginTest.php @@ -79,6 +79,11 @@ final class OriginTest extends TestCase $this->assertEquals(false, $validator->isValid('tauri://example.com')); $this->assertSame('Invalid Origin. Register your new client (example.com) as a new Web (Tauri) platform on your project console dashboard', $validator->getDescription()); + $this->assertEquals(true, $validator->isValid('capacitor://localhost')); + $this->assertEquals(true, $validator->isValid('capacitor://appwrite.io')); + $this->assertEquals(false, $validator->isValid('capacitor://example.com')); + $this->assertSame('Invalid Origin. Register your new client (example.com) as a new Web (Capacitor) platform on your project console dashboard', $validator->getDescription()); + $this->assertEquals(false, $validator->isValid('random-scheme://localhost')); $this->assertSame('Invalid Scheme. The scheme used (random-scheme) in the Origin (random-scheme://localhost) is not supported. If you are using a custom scheme, please change it to `appwrite-callback-`', $validator->getDescription()); }