From 0a35ef264a5adb16bae39e0cbabc790ac10acc5a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 11 Oct 2024 10:12:05 +0200 Subject: [PATCH] fix(push): Use fallback keys that look like a real one Signed-off-by: Joas Schilling --- lib/Push.php | 13 ++++++++++++- tests/Unit/PushTest.php | 9 ++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/Push.php b/lib/Push.php index 9c9f72f0..b27fae54 100644 --- a/lib/Push.php +++ b/lib/Push.php @@ -45,6 +45,7 @@ use OCP\L10N\IFactory; use OCP\Notification\IManager as INotificationManager; use OCP\Notification\INotification; +use OCP\Security\ISecureRandom; use OCP\UserStatus\IManager as IUserStatusManager; use OCP\UserStatus\IUserStatus; use OCP\Util; @@ -131,6 +132,7 @@ public function __construct( IUserStatusManager $userStatusManager, IFactory $l10nFactory, protected ITimeFactory $timeFactory, + protected ISecureRandom $random, LoggerInterface $log, ) { $this->db = $connection; @@ -486,7 +488,11 @@ protected function sendNotificationsToProxies(): void { if ($subscriptionAwareServer === 'https://push-notifications.nextcloud.com') { $subscriptionKey = $this->config->getAppValue('support', 'subscription_key'); } else { - $subscriptionKey = $this->config->getSystemValueString('instanceid'); + $subscriptionKey = $this->config->getAppValue(Application::APP_ID, 'push_subscription_key'); + if ($subscriptionKey === '') { + $subscriptionKey = $this->createPushSubscriptionKey(); + $this->config->setAppValue(Application::APP_ID, 'push_subscription_key', $subscriptionKey); + } } $client = $this->clientService->newClient(); @@ -785,4 +791,9 @@ protected function deletePushTokenByDeviceIdentifier(string $deviceIdentifier): protected function createFakeUserObject(string $userId): IUser { return new FakeUser($userId); } + + protected function createPushSubscriptionKey(): string { + $key = $this->random->generate(25, ISecureRandom::CHAR_ALPHANUMERIC); + return implode('-', str_split($key, 5)); + } } diff --git a/tests/Unit/PushTest.php b/tests/Unit/PushTest.php index c5e8afd2..91ece39a 100644 --- a/tests/Unit/PushTest.php +++ b/tests/Unit/PushTest.php @@ -42,6 +42,7 @@ use OCP\L10N\IFactory; use OCP\Notification\IManager as INotificationManager; use OCP\Notification\INotification; +use OCP\Security\ISecureRandom; use OCP\UserStatus\IManager as IUserStatusManager; use PHPUnit\Framework\MockObject\MockObject; use Psr\Http\Message\ResponseInterface; @@ -77,6 +78,8 @@ class PushTest extends TestCase { protected $l10nFactory; /** @var ITimeFactory|MockObject */ protected $timeFactory; + /** @var ISecureRandom|MockObject */ + protected $random; /** @var LoggerInterface|MockObject */ protected $logger; @@ -94,6 +97,7 @@ protected function setUp(): void { $this->userStatusManager = $this->createMock(IUserStatusManager::class); $this->l10nFactory = $this->createMock(IFactory::class); $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->random = $this->createMock(ISecureRandom::class); $this->logger = $this->createMock(LoggerInterface::class); $this->cacheFactory->method('createDistributed') @@ -119,6 +123,7 @@ protected function getPush(array $methods = []) { $this->userStatusManager, $this->l10nFactory, $this->timeFactory, + $this->random, $this->logger, ]) ->setMethods($methods) @@ -135,7 +140,9 @@ protected function getPush(array $methods = []) { $this->cacheFactory, $this->userStatusManager, $this->l10nFactory, - $this->logger + $this->timeFactory, + $this->random, + $this->logger, ); }