mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
feat: support capacitor:// as an allowed origin
Capacitor's iOS webview serves the app from `capacitor://<hostname>` (`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) <noreply@anthropic.com>
This commit is contained in:
1 parent
1d2a0dbaef
commit
b0885c2f6a
4 files changed
+24
No files matched your search
@@ -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)',
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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-<PROJECT_ID>`', $validator->getDescription());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user