From aa17c3e42b42d93a00ebe6d354f984502dacc6d6 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 10 Oct 2024 17:11:18 +0200 Subject: [PATCH 1/2] fix(push): Make testing the push server easier Signed-off-by: Joas Schilling --- lib/Push.php | 14 +++++++++----- tests/Unit/PushTest.php | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/Push.php b/lib/Push.php index 64b62943..2d563785 100644 --- a/lib/Push.php +++ b/lib/Push.php @@ -470,6 +470,13 @@ protected function sendNotificationsToProxies(): void { return; } + $subscriptionAwareServer = rtrim($this->config->getAppValue(Application::APP_ID, 'subscription_aware_server', 'https://push-notifications.nextcloud.com'), '/'); + if ($subscriptionAwareServer === 'https://push-notifications.nextcloud.com') { + $subscriptionKey = $this->config->getAppValue('support', 'subscription_key'); + } else { + $subscriptionKey = $this->config->getSystemValueString('instanceid'); + } + $client = $this->clientService->newClient(); foreach ($pushNotifications as $proxyServer => $notifications) { try { @@ -479,11 +486,8 @@ protected function sendNotificationsToProxies(): void { ], ]; - if ($proxyServer === 'https://push-notifications.nextcloud.com') { - $subscriptionKey = $this->config->getAppValue('support', 'subscription_key'); - if ($subscriptionKey) { - $requestData['headers']['X-Nextcloud-Subscription-Key'] = $subscriptionKey; - } + if ($subscriptionKey !== '' && $proxyServer === $subscriptionAwareServer) { + $requestData['headers']['X-Nextcloud-Subscription-Key'] = $subscriptionKey; } $response = $client->post($proxyServer . '/notifications', $requestData); diff --git a/tests/Unit/PushTest.php b/tests/Unit/PushTest.php index 6de50ba7..7a841c33 100644 --- a/tests/Unit/PushTest.php +++ b/tests/Unit/PushTest.php @@ -497,6 +497,13 @@ public function testPushToDeviceSending($isDebug) { ->with('debug', false) ->willReturn($isDebug); + $this->config + ->method('getAppValue') + ->willReturnMap([ + ['notifications', 'subscription_aware_server', 'https://push-notifications.nextcloud.com', 'https://push-notifications.nextcloud.com'], + ['support', 'subscription_key', '', ''], + ]); + $this->l10nFactory ->method('getUserLanguage') ->with($user) @@ -805,6 +812,13 @@ public function testPushToDeviceTalkNotification(array $deviceTypes, $isTalkNoti ->with('has_internet_connection', true) ->willReturn(true); + $this->config + ->method('getAppValue') + ->willReturnMap([ + ['notifications', 'subscription_aware_server', 'https://push-notifications.nextcloud.com', 'https://push-notifications.nextcloud.com'], + ['support', 'subscription_key', '', ''], + ]); + $this->notificationManager->method('isFairUseOfFreePushService') ->willReturn(true); From 7fb10118b3e89652f3119789158ccc5192e5fa1a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 11 Oct 2024 10:12:05 +0200 Subject: [PATCH 2/2] 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 2d563785..bf89e1e0 100644 --- a/lib/Push.php +++ b/lib/Push.php @@ -30,6 +30,7 @@ use OCP\Notification\IManager as INotificationManager; use OCP\Notification\IncompleteParsedNotificationException; use OCP\Notification\INotification; +use OCP\Security\ISecureRandom; use OCP\UserStatus\IManager as IUserStatusManager; use OCP\UserStatus\IUserStatus; use OCP\Util; @@ -116,6 +117,7 @@ public function __construct( IUserStatusManager $userStatusManager, IFactory $l10nFactory, protected ITimeFactory $timeFactory, + protected ISecureRandom $random, LoggerInterface $log, ) { $this->db = $connection; @@ -474,7 +476,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(); @@ -781,4 +787,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 7a841c33..475db3b4 100644 --- a/tests/Unit/PushTest.php +++ b/tests/Unit/PushTest.php @@ -26,6 +26,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; @@ -61,6 +62,8 @@ class PushTest extends TestCase { protected $l10nFactory; /** @var ITimeFactory|MockObject */ protected $timeFactory; + /** @var ISecureRandom|MockObject */ + protected $random; /** @var LoggerInterface|MockObject */ protected $logger; @@ -78,6 +81,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') @@ -103,6 +107,7 @@ protected function getPush(array $methods = []) { $this->userStatusManager, $this->l10nFactory, $this->timeFactory, + $this->random, $this->logger, ]) ->setMethods($methods) @@ -119,7 +124,9 @@ protected function getPush(array $methods = []) { $this->cacheFactory, $this->userStatusManager, $this->l10nFactory, - $this->logger + $this->timeFactory, + $this->random, + $this->logger, ); }