diff --git a/solid/lib/Controller/PageController.php b/solid/lib/Controller/PageController.php index baf8d0cc..351b7deb 100644 --- a/solid/lib/Controller/PageController.php +++ b/solid/lib/Controller/PageController.php @@ -102,6 +102,13 @@ public function approval($clientId) { $parsedOrigin = parse_url($clientRegistration['redirect_uris'][0]); $origin = $parsedOrigin['host']; if ($origin) { + // The browser follows the redirects after the form POST (back to the + // authorize endpoint, then to the client's redirect URI), and + // `form-action` applies to every hop. Keep the port, so clients on a + // non-default port (such as loopback redirects from RFC 8252) work. + if (isset($parsedOrigin['port'])) { + $origin .= ':' . $parsedOrigin['port']; + } $policy->addAllowedFormActionDomain($parsedOrigin['scheme'] . "://" . $origin); $templateResponse->setContentSecurityPolicy($policy); } diff --git a/solid/tests/Unit/Controller/PageControllerTest.php b/solid/tests/Unit/Controller/PageControllerTest.php index b15ba6d1..530dbd18 100644 --- a/solid/tests/Unit/Controller/PageControllerTest.php +++ b/solid/tests/Unit/Controller/PageControllerTest.php @@ -16,11 +16,14 @@ class PageControllerTest extends TestCase { private const MOCK_USER_ID = 'mock-user'; + private const MOCK_CLIENT_ID = 'mock-client-id'; private $controller; + private $mockConfig; public function setUp(): void { $mockConfig = $this->getMockBuilder(IConfig::class)->getMock(); + $this->mockConfig = $mockConfig; $mockRequest = $this->getMockBuilder(IRequest::class)->getMock(); $mockUrlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock(); $mockUserManager = $this->getMockBuilder(IUserManager::class)->getMock(); @@ -48,4 +51,36 @@ public function testIndex() $this->assertTrue($result instanceof TemplateResponse); } + /** + * @testdox PageController should allow the client's redirect origin, including a non-default port, as a form action on the approval page + * @covers ::approval + * @dataProvider provideRedirectUris + * @uses \OCA\Solid\BaseServerConfig + * @uses \OCA\Solid\ServerConfig::__construct + */ + public function testApprovalAllowsRedirectOriginAsFormAction(string $redirectUri, string $expectedOrigin) + { + $this->mockConfig->method('getAppValue')->willReturnMap([ + ['solid', 'client-' . self::MOCK_CLIENT_ID, '{}', json_encode([ + 'client_name' => 'Mock Client', + 'redirect_uris' => [$redirectUri], + ])], + ]); + $_GET['returnUrl'] = '/apps/solid/authorize?client_id=' . self::MOCK_CLIENT_ID; + + $result = $this->controller->approval(self::MOCK_CLIENT_ID); + + $policy = $result->getContentSecurityPolicy()->buildPolicy(); + + $this->assertStringContainsString("form-action 'self' {$expectedOrigin};", $policy . ';'); + } + + public static function provideRedirectUris(): array + { + return [ + 'default port' => ['https://app.example/callback', 'https://app.example'], + 'non-default port' => ['https://app.example:8443/callback', 'https://app.example:8443'], + 'loopback redirect' => ['http://localhost:3999/cb', 'http://localhost:3999'], + ]; + } }