Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions solid/lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
35 changes: 35 additions & 0 deletions solid/tests/Unit/Controller/PageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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'],
];
}
}