Skip to content

Commit ca8b04f

Browse files
committed
fix(encryption): Add the script to test encryption status only on logged in pages
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent e3edf43 commit ca8b04f

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

apps/encryption/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
'OCA\\Encryption\\Exceptions\\PrivateKeyMissingException' => $baseDir . '/../lib/Exceptions/PrivateKeyMissingException.php',
2828
'OCA\\Encryption\\Exceptions\\PublicKeyMissingException' => $baseDir . '/../lib/Exceptions/PublicKeyMissingException.php',
2929
'OCA\\Encryption\\KeyManager' => $baseDir . '/../lib/KeyManager.php',
30+
'OCA\\Encryption\\Listeners\\BeforeTemplateRenderedListener' => $baseDir . '/../lib/Listeners/BeforeTemplateRenderedListener.php',
3031
'OCA\\Encryption\\Listeners\\UserEventsListener' => $baseDir . '/../lib/Listeners/UserEventsListener.php',
3132
'OCA\\Encryption\\Migration\\SetMasterKeyStatus' => $baseDir . '/../lib/Migration/SetMasterKeyStatus.php',
3233
'OCA\\Encryption\\Recovery' => $baseDir . '/../lib/Recovery.php',

apps/encryption/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class ComposerStaticInitEncryption
4242
'OCA\\Encryption\\Exceptions\\PrivateKeyMissingException' => __DIR__ . '/..' . '/../lib/Exceptions/PrivateKeyMissingException.php',
4343
'OCA\\Encryption\\Exceptions\\PublicKeyMissingException' => __DIR__ . '/..' . '/../lib/Exceptions/PublicKeyMissingException.php',
4444
'OCA\\Encryption\\KeyManager' => __DIR__ . '/..' . '/../lib/KeyManager.php',
45+
'OCA\\Encryption\\Listeners\\BeforeTemplateRenderedListener' => __DIR__ . '/..' . '/../lib/Listeners/BeforeTemplateRenderedListener.php',
4546
'OCA\\Encryption\\Listeners\\UserEventsListener' => __DIR__ . '/..' . '/../lib/Listeners/UserEventsListener.php',
4647
'OCA\\Encryption\\Migration\\SetMasterKeyStatus' => __DIR__ . '/..' . '/../lib/Migration/SetMasterKeyStatus.php',
4748
'OCA\\Encryption\\Recovery' => __DIR__ . '/..' . '/../lib/Recovery.php',

apps/encryption/lib/AppInfo/Application.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OCA\Encryption\Crypto\EncryptAll;
1515
use OCA\Encryption\Crypto\Encryption;
1616
use OCA\Encryption\KeyManager;
17+
use OCA\Encryption\Listeners\BeforeTemplateRenderedListener;
1718
use OCA\Encryption\Listeners\UserEventsListener;
1819
use OCA\Encryption\Session;
1920
use OCA\Encryption\Users\Setup;
@@ -22,6 +23,7 @@
2223
use OCP\AppFramework\Bootstrap\IBootContext;
2324
use OCP\AppFramework\Bootstrap\IBootstrap;
2425
use OCP\AppFramework\Bootstrap\IRegistrationContext;
26+
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
2527
use OCP\Encryption\IManager;
2628
use OCP\EventDispatcher\IEventDispatcher;
2729
use OCP\IConfig;
@@ -47,8 +49,6 @@ public function register(IRegistrationContext $context): void {
4749
}
4850

4951
public function boot(IBootContext $context): void {
50-
\OCP\Util::addScript(self::APP_ID, 'encryption');
51-
5252
$context->injectFn(function (IManager $encryptionManager) use ($context): void {
5353
if (!($encryptionManager instanceof \OC\Encryption\Manager)) {
5454
return;
@@ -89,6 +89,7 @@ public function registerEventListeners(
8989
}
9090

9191
// No maintenance so register all events
92+
$eventDispatcher->addServiceListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
9293
$eventDispatcher->addServiceListener(UserLoggedInEvent::class, UserEventsListener::class);
9394
$eventDispatcher->addServiceListener(UserLoggedInWithCookieEvent::class, UserEventsListener::class);
9495
$eventDispatcher->addServiceListener(UserLoggedOutEvent::class, UserEventsListener::class);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Encryption\Listeners;
11+
12+
use OCA\Encryption\AppInfo\Application;
13+
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
14+
use OCP\AppFramework\Http\TemplateResponse;
15+
use OCP\EventDispatcher\Event;
16+
use OCP\EventDispatcher\IEventListener;
17+
use OCP\Util;
18+
19+
/** @template-implements IEventListener<BeforeTemplateRenderedEvent> */
20+
class BeforeTemplateRenderedListener implements IEventListener {
21+
public function handle(Event $event): void {
22+
if (!($event instanceof BeforeTemplateRenderedEvent)) {
23+
// Unrelated
24+
return;
25+
}
26+
27+
if ($event->getResponse()->getRenderAs() !== TemplateResponse::RENDER_AS_USER) {
28+
// Do not test encryption status on public or login pages
29+
return;
30+
}
31+
32+
Util::addScript(Application::APP_ID, 'encryption');
33+
}
34+
}

0 commit comments

Comments
 (0)