Skip to content

Commit 41a3008

Browse files
committed
feat: Allow access to E2EE folder when x-e2ee-supported header is set
Signed-off-by: Louis Chemineau <[email protected]>
1 parent 83d894e commit 41a3008

File tree

5 files changed

+35
-54
lines changed

5 files changed

+35
-54
lines changed

lib/Middleware/UserAgentCheckMiddleware.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ public function beforeController($controller, $methodName): void {
4949
return;
5050
}
5151

52-
if ($this->request->getHeader('x-e2ee-supported') === 'true') {
53-
return;
54-
}
55-
5652
throw new OCSForbiddenException('Client "' . $userAgent . '" is not allowed to access end-to-end encrypted content.');
5753
}
5854
}

lib/UserAgentManager.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace OCA\EndToEndEncryption;
1111

1212
use OCP\IConfig;
13+
use OCP\IRequest;
1314

1415
class UserAgentManager {
1516

@@ -21,7 +22,10 @@ class UserAgentManager {
2122
*/
2223
private array $supportedUserAgents;
2324

24-
public function __construct(IConfig $config) {
25+
public function __construct(
26+
IConfig $config,
27+
private IRequest $request,
28+
) {
2529
$this->supportedUserAgents = $config->getSystemValue('end_to_end_encryption.supported-user-agents', [
2630
'/^Mozilla\/5\.0 \(Android\) Nextcloud\-android\/(?<version>(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)).*$/' => '3.13.0',
2731
'/^Mozilla\/5\.0 \([A-Za-z ]+\) (mirall|csyncoC)\/(?<version>(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)).*$/' => '3.0.0',
@@ -51,11 +55,15 @@ public function supportsEndToEndEncryption(string $client): bool {
5155
return (version_compare($matches['version'], $minVersion) > -1);
5256
}
5357

58+
if ($this->request->getHeader('x-e2ee-supported') === 'true') {
59+
return true;
60+
}
61+
5462
return false;
5563
}
5664

5765
/**
58-
* @return string[]
66+
* @return array<string, string>
5967
*/
6068
protected function getSupportedUserAgents(): array {
6169
return $this->supportedUserAgents;

tests/Unit/Controller/KeyControllerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ public function testGetPublicServerKeyException(): void {
524524

525525
$this->logger->expects($this->once())
526526
->method('critical')
527-
->willReturn($exception->getMessage());
527+
->with($exception->getMessage());
528528

529529
$this->expectException(OCSBadRequestException::class);
530530
$this->expectExceptionMessage('Internal error');

tests/Unit/Middleware/UserAgentCheckMiddlewareTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected function setUp(): void {
4848
* @dataProvider beforeControllerDataProvider
4949
*/
5050
public function testBeforeController(bool $hasAnnotation, bool $supportsE2E, bool $expectException, bool $forceSupport) {
51-
$this->request->expects($hasAnnotation ? $this->exactly($supportsE2E ? 1 : 2) : $this->never())
51+
$this->request->expects($hasAnnotation ? $this->once() : $this->never())
5252
->method('getHeader')
5353
->willReturnMap([
5454
['user-agent', 'user-agent-string'],

tests/Unit/UserAgentManagerTest.php

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use OCA\EndToEndEncryption\UserAgentManager;
1313
use OCP\IConfig;
14+
use OCP\IRequest;
1415
use Test\TestCase;
1516

1617
class UserAgentManagerTest extends TestCase {
@@ -21,14 +22,13 @@ class UserAgentManagerTest extends TestCase {
2122
*
2223
* @dataProvider supportsEndToEndEncryptionDataProvider
2324
*/
24-
public function testSupportsEndToEndEncryption(string $client,
25-
bool $expected): void {
26-
$supportedUAs = $this->getSupportedUserAgents();
27-
$userAgentManager = $this->getUserAgentManager(['getSupportedUserAgents']);
28-
$userAgentManager->expects($this->once())
29-
->method('getSupportedUserAgents')
30-
->willReturn($supportedUAs);
31-
25+
public function testSupportsEndToEndEncryption(string $client, bool $expected): void {
26+
/** @var IRequest&\PHPUnit\Framework\MockObject\MockObject */
27+
$request = $this->createMock(IRequest::class);
28+
$request->expects($this->any())
29+
->method('getHeader')
30+
->willReturn('');
31+
$userAgentManager = new UserAgentManager(\OCP\Server::get(IConfig::class), $request);
3232
$actual = $userAgentManager->supportsEndToEndEncryption($client);
3333
$this->assertEquals($expected, $actual);
3434
}
@@ -42,9 +42,12 @@ public function supportsEndToEndEncryptionDataProvider(): array {
4242
['Mozilla/5.0 (Android) Nextcloud-android/1.9.9', false],
4343
['Mozilla/5.0 (Android) Nextcloud-android/2.1.3', false],
4444
['Mozilla/5.0 (Android) Nextcloud-android/2.3.3', false],
45-
['Mozilla/5.0 (Android) Nextcloud-android/2.3.4', true],
46-
['Mozilla/5.0 (Android) Nextcloud-android/2.4.9', true],
47-
['Mozilla/5.0 (Android) Nextcloud-android/3.0.0', true],
45+
['Mozilla/5.0 (Android) Nextcloud-android/2.3.4', false],
46+
['Mozilla/5.0 (Android) Nextcloud-android/2.4.9', false],
47+
['Mozilla/5.0 (Android) Nextcloud-android/3.0.0', false],
48+
['Mozilla/5.0 (Android) Nextcloud-android/3.13.0', true],
49+
['Mozilla/5.0 (Android) Nextcloud-android/3.13.1', true],
50+
['Mozilla/5.0 (Android) Nextcloud-android/3.14.0', true],
4851
// Android without version
4952
['Mozilla/5.0 (Android) Nextcloud-android/beta', false],
5053
['Mozilla/5.0 (Android) Nextcloud-android/', false],
@@ -53,9 +56,11 @@ public function supportsEndToEndEncryptionDataProvider(): array {
5356
['Mozilla/5.0 (iOS) Nextcloud-iOS/1.9.9', false],
5457
['Mozilla/5.0 (iOS) Nextcloud-iOS/2.1.3', false],
5558
['Mozilla/5.0 (iOS) Nextcloud-iOS/2.3.3', false],
56-
['Mozilla/5.0 (iOS) Nextcloud-iOS/2.3.4', true],
57-
['Mozilla/5.0 (iOS) Nextcloud-iOS/2.4.9', true],
58-
['Mozilla/5.0 (iOS) Nextcloud-iOS/3.0.0', true],
59+
['Mozilla/5.0 (iOS) Nextcloud-iOS/2.3.4', false],
60+
['Mozilla/5.0 (iOS) Nextcloud-iOS/2.4.9', false],
61+
['Mozilla/5.0 (iOS) Nextcloud-iOS/3.0.0', false],
62+
['Mozilla/5.0 (iOS) Nextcloud-iOS/3.0.5', true],
63+
['Mozilla/5.0 (iOS) Nextcloud-iOS/3.1.0', true],
5964
// iOS without version
6065
['Mozilla/5.0 (iOS) Nextcloud-iOS/beta', false],
6166
['Mozilla/5.0 (iOS) Nextcloud-iOS/', false],
@@ -64,43 +69,15 @@ public function supportsEndToEndEncryptionDataProvider(): array {
6469
['Mozilla/5.0 (Macintosh) mirall/1.9.9stable (build 20200303) (Nextcloud)', false],
6570
['Mozilla/5.0 (Macintosh) mirall/2.1.3rc (build 20200303)', false],
6671
['Mozilla/5.0 (Macintosh) mirall/2.3.3', false],
67-
['Mozilla/5.0 (Linux) mirall/2.3.4', true],
68-
['Mozilla/5.0 (Macintosh) csyncoC/2.4.9RC (build 20200303) (Nextcloud)', true],
72+
['Mozilla/5.0 (Linux) mirall/2.3.4', false],
73+
['Mozilla/5.0 (Macintosh) csyncoC/2.4.9RC (build 20200303) (Nextcloud)', false],
6974
['Mozilla/5.0 (Macintosh) mirall/3.0.0 (build 20200303)', true],
75+
['Mozilla/5.0 (Macintosh) mirall/3.0.1 (build 20200303)', true],
76+
['Mozilla/5.0 (Macintosh) mirall/3.1.1 (build 20200303)', true],
7077
// Desktop without version
7178
['Mozilla/5.0 (Macintosh) mirall/ (build 20200303)', false],
7279
['Mozilla/5.0 (Macintosh) mirall/', false],
7380
['Mozilla/5.0 (Macintosh) mirall', false],
7481
];
7582
}
76-
77-
private function getUserAgentManager(array $mockedMethods = []) {
78-
if (empty($mockedMethods)) {
79-
return new UserAgentManager(\OCP\Server::get(IConfig::class));
80-
}
81-
82-
return $this
83-
->getMockBuilder(UserAgentManager::class)
84-
->setMethods($mockedMethods)
85-
->disableOriginalConstructor()
86-
->getMock();
87-
}
88-
89-
/**
90-
* This function returns the user agents to test against
91-
* It keeps the original regex, but replaces the exact version
92-
* so this test suite doesn't break on a simple version bump
93-
*
94-
* @return array
95-
*/
96-
private function getSupportedUserAgents(): array {
97-
$userAgentManager = new UserAgentManager(\OCP\Server::get(IConfig::class));
98-
$originalRules = self::invokePrivate($userAgentManager, 'getSupportedUserAgents');
99-
100-
foreach ($originalRules as $regex => $version) {
101-
$originalRules[$regex] = '2.3.4';
102-
}
103-
104-
return $originalRules;
105-
}
10683
}

0 commit comments

Comments
 (0)