Skip to content

Commit cff842c

Browse files
committed
Add logic for validating storage URLs to StorageUrlTrait.
1 parent c3c6a87 commit cff842c

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

solid/lib/Controller/GetStorageUrlTrait.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use OCA\Solid\ServerConfig;
66
use OCP\IURLGenerator;
7+
use Psr\Http\Message\RequestInterface;
78

89
trait GetStorageUrlTrait
910
{
@@ -55,6 +56,27 @@ public function getStorageUrl($userId) {
5556
return $storageUrl;
5657
}
5758

59+
public function validateUrl(RequestInterface $request): bool {
60+
$isValid = false;
61+
62+
$host = $request->getUri()->getHost();
63+
$path = $request->getUri()->getPath();
64+
$pathParts = explode('/', $path);
65+
66+
$pathUsers = array_filter($pathParts, static function ($value) {
67+
return str_starts_with($value, '@');
68+
});
69+
70+
if (count($pathUsers) === 1) {
71+
$pathUser = reset($pathUsers);
72+
$subDomainUser = explode('.', $host)[0];
73+
74+
$isValid = $pathUser === '@' . $subDomainUser;
75+
}
76+
77+
return $isValid;
78+
}
79+
5880
////////////////////////////// UTILITY METHODS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
5981

6082
private function build_url(array $parts) {

solid/tests/Unit/Controller/GetStorageUrlTraitTest.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
namespace OCA\Solid\Controller;
44

55
use Error;
6+
use Laminas\Diactoros\Request;
7+
use Laminas\Diactoros\Uri;
68
use OCA\Solid\ServerConfig;
79
use OCP\IURLGenerator;
810
use PHPUnit\Framework\MockObject\MockObject;
911
use PHPUnit\Framework\TestCase;
10-
use ReflectionObject;
12+
use Psr\Http\Message\RequestInterface;
1113

1214
/**
1315
* @coversDefaultClass \OCA\Solid\Controller\GetStorageUrlTrait
@@ -98,6 +100,20 @@ public function testGetStorageUrlWithUserSubDomainsEnabled($url, $userId, $expec
98100
$this->assertEquals($expected, $actual);
99101
}
100102

103+
/**
104+
* @testdox GetStorageUrlTrait should return expected validity when asked to validateUrl
105+
*
106+
* @covers ::validateUrl
107+
*
108+
* @dataProvider provideRequests
109+
*/
110+
public function testValidateUrl(RequestInterface $response, $expected)
111+
{
112+
$actual = $this->trait->validateUrl($response);
113+
114+
$this->assertEquals($expected, $actual);
115+
}
116+
101117
////////////////////////////// MOCKS AND STUBS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
102118

103119
public function getMockConfig($enabled = false): MockObject|ServerConfig
@@ -128,6 +144,22 @@ public function getMockUrlGenerator($url): MockObject|IURLGenerator
128144

129145
/////////////////////////////// DATAPROVIDERS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
130146

147+
public function provideRequests()
148+
{
149+
$request = new Request();
150+
151+
return [
152+
'invalid: invalid URL' => ['request' => $request->withUri(new Uri('!@#$%^&*()_')), 'expected' => false],
153+
'invalid: no domain user' => ['request' => $request->withUri(new Uri('https://example.com/@alice/profile/card#me')), 'expected' => false],
154+
'invalid: no path or domain user' => ['request' => $request->withUri(new Uri('https://example.com/')), 'expected' => false],
155+
'invalid: no path user' => ['request' => $request->withUri(new Uri('https://alice.example.com/profile/card#me')), 'expected' => false],
156+
'invalid: no URL' => ['request' => $request, 'expected' => false],
157+
'invalid: path and domain user mismatch' => ['request' => $request->withUri(new Uri('https://bob.example.com/@alice/profile/card#me')), 'expected' => false],
158+
'valid: minimal path and domain user match' => ['request' => $request->withUri(new Uri('https://alice.example.com/apps/@alice')), 'expected' => true],
159+
'valid: path and domain user match' => ['request' => $request->withUri(new Uri('https://alice.example.com/apps/solid/@alice/profile/card#me')), 'expected' => true],
160+
];
161+
}
162+
131163
public function provideSubDomainsDisabledUrls()
132164
{
133165
return [

0 commit comments

Comments
 (0)