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()); }