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: 2 additions & 5 deletions solid/lib/Controller/CalendarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Pdsinterop\Solid\Resources\Server as ResourceServer;

class CalendarController extends Controller {
use GetServerUriTrait;
use DpopFactoryTrait;

/* @var IURLGenerator */
Expand Down Expand Up @@ -61,11 +62,7 @@ private function getFileSystem($userId) {
// Create Formats objects
$formats = new \Pdsinterop\Rdf\Formats();

$serverParams = $this->rawRequest->getServerParams();
$scheme = $serverParams['REQUEST_SCHEME'];
$domain = $serverParams['SERVER_NAME'];
$path = $serverParams['REQUEST_URI'];
$serverUri = "{$scheme}://{$domain}{$path}"; // FIXME: doublecheck that this is the correct url;
$serverUri = $this->getServerUri($this->request);

// Create the RDF Adapter
$rdfAdapter = new \Pdsinterop\Rdf\Flysystem\Adapter\Rdf(
Expand Down
7 changes: 2 additions & 5 deletions solid/lib/Controller/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

class ContactsController extends Controller
{
use GetServerUriTrait;
use DpopFactoryTrait;

/* @var IURLGenerator */
Expand Down Expand Up @@ -62,11 +63,7 @@ private function getFileSystem($userId) {
// Create Formats objects
$formats = new \Pdsinterop\Rdf\Formats();

$serverParams = $this->rawRequest->getServerParams();
$scheme = $serverParams['REQUEST_SCHEME'];
$domain = $serverParams['SERVER_NAME'];
$path = $serverParams['REQUEST_URI'];
$serverUri = "{$scheme}://{$domain}{$path}"; // FIXME: doublecheck that this is the correct url;
$serverUri = $this->getServerUri($this->request);

// Create the RDF Adapter
$rdfAdapter = new \Pdsinterop\Rdf\Flysystem\Adapter\Rdf(
Expand Down
30 changes: 30 additions & 0 deletions solid/lib/Controller/GetServerUriTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace OCA\Solid\Controller;

use OCP\IRequest;

/**
* Builds the absolute URI of the current request, as seen by the client.
*
* The URI is used as the base for resolving relative IRIs in RDF documents.
* It must therefore be the public URI, not the one seen by the web server.
* Behind a reverse proxy that terminates TLS, `$_SERVER['REQUEST_SCHEME']`
* and `$_SERVER['SERVER_NAME']` describe the internal hop (for instance
* `http` and an internal host name). Nextcloud's IRequest honours the
* `overwriteprotocol`, `overwritehost` and `trusted_proxies` settings, so the
* scheme and host are taken from there.
*
* @see https://github.com/pdsinterop/solid-nextcloud/issues/225
*/
trait GetServerUriTrait
{
final public function getServerUri(IRequest $request): string
{
$scheme = $request->getServerProtocol();
$host = $request->getServerHost();
$path = $request->getRequestUri();

return "{$scheme}://{$host}{$path}";
}
}
7 changes: 2 additions & 5 deletions solid/lib/Controller/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Pdsinterop\Solid\Resources\Server as ResourceServer;

class ProfileController extends Controller {
use GetServerUriTrait;
use DpopFactoryTrait;
use GetStorageUrlTrait;

Expand Down Expand Up @@ -67,11 +68,7 @@ private function getFileSystem($userId) {
// Create Formats objects
$formats = new \Pdsinterop\Rdf\Formats();

$serverParams = $this->rawRequest->getServerParams();
$scheme = $serverParams['REQUEST_SCHEME'];
$domain = $serverParams['SERVER_NAME'];
$path = $serverParams['REQUEST_URI'];
$serverUri = "{$scheme}://{$domain}{$path}"; // FIXME: doublecheck that this is the correct url;
$serverUri = $this->getServerUri($this->request);

// Create the RDF Adapter
$rdfAdapter = new \Pdsinterop\Rdf\Flysystem\Adapter\Rdf(
Expand Down
3 changes: 2 additions & 1 deletion solid/lib/Controller/SolidWebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Pdsinterop\Solid\Auth\WAC as WAC;

class SolidWebhookController extends Controller {
use GetServerUriTrait;
use DpopFactoryTrait;
use GetStorageUrlTrait;

Expand Down Expand Up @@ -119,7 +120,7 @@ private function getFileSystem() {
// Create Formats objects
$formats = new \Pdsinterop\Rdf\Formats();

$serverUri = "https://" . $this->rawRequest->getServerParams()["SERVER_NAME"] . $this->rawRequest->getServerParams()["REQUEST_URI"]; // FIXME: doublecheck that this is the correct url;
$serverUri = $this->getServerUri($this->request);

// Create the RDF Adapter
$rdfAdapter = new \Pdsinterop\Rdf\Flysystem\Adapter\Rdf(
Expand Down
7 changes: 2 additions & 5 deletions solid/lib/Controller/StorageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

class StorageController extends Controller
{
use GetServerUriTrait;
use BearerFactoryTrait;
use DpopFactoryTrait;
use GetStorageUrlTrait;
Expand Down Expand Up @@ -64,11 +65,7 @@ private function getFileSystem() {
// Create Formats objects
$formats = new \Pdsinterop\Rdf\Formats();

$serverParams = $this->rawRequest->getServerParams();
$scheme = $serverParams['REQUEST_SCHEME'];
$domain = $serverParams['SERVER_NAME'];
$path = $serverParams['REQUEST_URI'];
$serverUri = "{$scheme}://{$domain}{$path}"; // FIXME: doublecheck that this is the correct url;
$serverUri = $this->getServerUri($this->request);

// Create the RDF Adapter
$rdfAdapter = new \Pdsinterop\Rdf\Flysystem\Adapter\Rdf(
Expand Down
84 changes: 84 additions & 0 deletions solid/tests/Unit/Controller/GetServerUriTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace OCA\Solid\Controller;

use OCP\IRequest;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @coversDefaultClass \OCA\Solid\Controller\GetServerUriTrait
*/
class GetServerUriTraitTest extends TestCase
{
////////////////////////////////// FIXTURES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

private $trait;

protected function setUp(): void
{
$this->trait = new class {
use GetServerUriTrait;
};
}

/////////////////////////////////// TESTS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

/**
* @testdox GetServerUriTrait should build the request URI from the protocol, host and path that Nextcloud reports
* @covers ::getServerUri
*/
public function testGetServerUri(): void
{
$request = $this->getMockRequest('https', 'cloud.example.com', '/apps/solid/~alice/storage/.acl');

$actual = $this->trait->getServerUri($request);

$this->assertEquals('https://cloud.example.com/apps/solid/~alice/storage/.acl', $actual);
}

/**
* @testdox GetServerUriTrait should keep a non-default port that is part of the server host
* @covers ::getServerUri
*/
public function testGetServerUriWithPort(): void
{
$request = $this->getMockRequest('https', 'cloud.example.com:8443', '/apps/solid/~alice/profile/card');

$actual = $this->trait->getServerUri($request);

$this->assertEquals('https://cloud.example.com:8443/apps/solid/~alice/profile/card', $actual);
}

/**
* @testdox GetServerUriTrait should not read the scheme or host from $_SERVER, which describe the internal hop behind a TLS-terminating proxy
* @covers ::getServerUri
*/
public function testGetServerUriIgnoresInternalServerVariables(): void
{
$backup = $_SERVER;
$_SERVER['REQUEST_SCHEME'] = 'http';
$_SERVER['SERVER_NAME'] = 'internal-container-name';

try {
$request = $this->getMockRequest('https', 'cloud.example.com', '/apps/solid/~alice/storage/');
$actual = $this->trait->getServerUri($request);
} finally {
$_SERVER = $backup;
}

$this->assertEquals('https://cloud.example.com/apps/solid/~alice/storage/', $actual);
}

////////////////////////////// MOCKS AND STUBS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

private function getMockRequest(string $protocol, string $host, string $path): IRequest|MockObject
{
$request = $this->createMock(IRequest::class);
$request->method('getServerProtocol')->willReturn($protocol);
$request->method('getServerHost')->willReturn($host);
$request->method('getRequestUri')->willReturn($path);

return $request;
}
}