Skip to content

fix(federation): remove background jobs when removing trusted servers #53760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/federation/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'OCA\\Federation\\DAV\\FedAuth' => $baseDir . '/../lib/DAV/FedAuth.php',
'OCA\\Federation\\DbHandler' => $baseDir . '/../lib/DbHandler.php',
'OCA\\Federation\\Listener\\SabrePluginAuthInitListener' => $baseDir . '/../lib/Listener/SabrePluginAuthInitListener.php',
'OCA\\Federation\\Listener\\TrustedServerRemovedListener' => $baseDir . '/../lib/Listener/TrustedServerRemovedListener.php',
'OCA\\Federation\\Migration\\Version1010Date20200630191302' => $baseDir . '/../lib/Migration/Version1010Date20200630191302.php',
'OCA\\Federation\\Settings\\Admin' => $baseDir . '/../lib/Settings/Admin.php',
'OCA\\Federation\\SyncFederationAddressBooks' => $baseDir . '/../lib/SyncFederationAddressBooks.php',
Expand Down
1 change: 1 addition & 0 deletions apps/federation/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ComposerStaticInitFederation
'OCA\\Federation\\DAV\\FedAuth' => __DIR__ . '/..' . '/../lib/DAV/FedAuth.php',
'OCA\\Federation\\DbHandler' => __DIR__ . '/..' . '/../lib/DbHandler.php',
'OCA\\Federation\\Listener\\SabrePluginAuthInitListener' => __DIR__ . '/..' . '/../lib/Listener/SabrePluginAuthInitListener.php',
'OCA\\Federation\\Listener\\TrustedServerRemovedListener' => __DIR__ . '/..' . '/../lib/Listener/TrustedServerRemovedListener.php',
'OCA\\Federation\\Migration\\Version1010Date20200630191302' => __DIR__ . '/..' . '/../lib/Migration/Version1010Date20200630191302.php',
'OCA\\Federation\\Settings\\Admin' => __DIR__ . '/..' . '/../lib/Settings/Admin.php',
'OCA\\Federation\\SyncFederationAddressBooks' => __DIR__ . '/..' . '/../lib/SyncFederationAddressBooks.php',
Expand Down
3 changes: 3 additions & 0 deletions apps/federation/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

use OCA\DAV\Events\SabrePluginAuthInitEvent;
use OCA\Federation\Listener\SabrePluginAuthInitListener;
use OCA\Federation\Listener\TrustedServerRemovedListener;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Federation\Events\TrustedServerRemovedEvent;

class Application extends App implements IBootstrap {

Expand All @@ -25,6 +27,7 @@ public function __construct($urlParams = []) {

public function register(IRegistrationContext $context): void {
$context->registerEventListener(SabrePluginAuthInitEvent::class, SabrePluginAuthInitListener::class);
$context->registerEventListener(TrustedServerRemovedEvent::class, TrustedServerRemovedListener::class);
}

public function boot(IBootContext $context): void {
Expand Down
53 changes: 53 additions & 0 deletions apps/federation/lib/Listener/TrustedServerRemovedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Federation\Listener;

use OCA\Federation\BackgroundJob\GetSharedSecret;
use OCA\Federation\BackgroundJob\RequestSharedSecret;
use OCP\BackgroundJob\IJobList;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Federation\Events\TrustedServerRemovedEvent;

/** @template-implements IEventListener<TrustedServerRemovedEvent> */
class TrustedServerRemovedListener implements IEventListener {
public function __construct(
private readonly IJobList $jobList,
) {
}

public function handle(Event $event): void {
if (!$event instanceof TrustedServerRemovedEvent) {
return;
}

$this->removeJobsByUrl(RequestSharedSecret::class, $event->getUrl());
$this->removeJobsByUrl(GetSharedSecret::class, $event->getUrl());
}

/**
* Remove RequestSharedSecret or GetSharedSecret jobs from the job list by their URL.
* The jobs are scheduled with url, token, and created as arguments.
* Thus, we have to loop over the jobs here and cannot use IJobList.remove.
*/
private function removeJobsByUrl(string $class, string $url): void {
foreach ($this->jobList->getJobsIterator($class, null, 0) as $job) {
$arguments = $job->getArgument();
if (isset($arguments['url']) && $arguments['url'] === $url) {
try {
$this->jobList->removeById($job->getId());
} catch (\Exception) {
// Removing the background jobs is optional because they will expire sometime.
// Therefore, we are using catch and ignore.
}
}
}
}
}
2 changes: 1 addition & 1 deletion apps/federation/lib/TrustedServers.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function addSharedSecret(string $url, string $sharedSecret): void {
public function removeServer(int $id): void {
$server = $this->dbHandler->getServerById($id);
$this->dbHandler->removeServer($id);
$this->dispatcher->dispatchTyped(new TrustedServerRemovedEvent($server['url_hash']));
$this->dispatcher->dispatchTyped(new TrustedServerRemovedEvent($server['url'], $server['url_hash']));

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Federation\Tests\Listener;

use OC\AppFramework\Utility\TimeFactory;
use OCA\Federation\BackgroundJob\GetSharedSecret;
use OCA\Federation\Listener\TrustedServerRemovedListener;
use OCA\Federation\TrustedServers;
use OCP\BackgroundJob\IJobList;
use OCP\Federation\Events\TrustedServerRemovedEvent;
use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\IURLGenerator;
use OCP\OCS\IDiscoveryService;
use Psr\Log\NullLogger;
use Test\BackgroundJob\DummyJobList;
use Test\TestCase;

class TrustedServerRemovedListenerTest extends TestCase {

private IJobList $jobList;
private TrustedServerRemovedListener $listener;

protected function setUp(): void {
parent::setUp();

$this->jobList = new DummyJobList();
$this->listener = new TrustedServerRemovedListener($this->jobList);
}

public function testHandle(): void {
// Arrange
$url = 'https://example.com';
$event = new TrustedServerRemovedEvent($url, md5($url)); // we are using a different hashing in the tests.
$job1 = $this->createGetSharedSecretMock();
$job2 = $this->createGetSharedSecretMock();
$job3 = $this->createGetSharedSecretMock();
$job4 = $this->createGetSharedSecretMock();
$this->jobList->add($job1, ['url' => 'https://example.org', 'token' => 'nei0dooX', 'created' => 0]);
$this->jobList->add($job2, ['url' => 'https://example.net', 'token' => 'ci6Shah7', 'created' => 0]);
$this->jobList->add($job3, ['url' => $url, 'token' => 'ieXie6Me', 'created' => 0]);
$this->jobList->add($job4, ['url' => $url, 'token' => 'thoQu8th', 'created' => 0]);

// Act
$this->listener->handle($event);
$jobs = iterator_to_array($this->jobList->getJobsIterator(GetSharedSecret::class, null, 0), false);

// Assert
$this->assertCount(2, $jobs);
}

private function createGetSharedSecretMock(): GetSharedSecret {
return new GetSharedSecret(
$this->createMock(IClientService::class),
$this->createMock(IURLGenerator::class),
$this->jobList,
$this->createMock(TrustedServers::class),
new NullLogger(),
$this->createMock(IDiscoveryService::class),
new TimeFactory(),
$this->createMock(IConfig::class),
);
}
}
6 changes: 3 additions & 3 deletions apps/federation/tests/TrustedServersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ public function testGetSharedSecret(): void {

public function testRemoveServer(): void {
$id = 42;
$server = ['url_hash' => 'url_hash'];
$server = ['url' => 'url', 'url_hash' => 'url_hash'];
$this->dbHandler->expects($this->once())->method('removeServer')->with($id);
$this->dbHandler->expects($this->once())->method('getServerById')->with($id)
->willReturn($server);
$this->dispatcher->expects($this->once())->method('dispatchTyped')
->willReturnCallback(
function ($event): void {
$this->assertSame(get_class($event), TrustedServerRemovedEvent::class);
/** @var \OCP\Federated\Events\TrustedServerRemovedEvent $event */
$this->assertInstanceOf(TrustedServerRemovedEvent::class, $event);
$this->assertSame('url', $event->getUrl());
$this->assertSame('url_hash', $event->getUrlHash());
}
);
Expand Down
15 changes: 12 additions & 3 deletions lib/public/Federation/Events/TrustedServerRemovedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@
* @since 25.0.0
*/
class TrustedServerRemovedEvent extends Event {
private string $urlHash;

/**
* @since 25.0.0
* @since 32.0.0 Added $url argument
*/
public function __construct(string $urlHash) {
public function __construct(
private readonly string $url,
private readonly string $urlHash,
) {
Comment on lines +22 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Breaking change needs to be documented :)
Ideally the url should have been appended at the end to avoid breaking existing listeners (if any)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I will change that.

parent::__construct();
$this->urlHash = $urlHash;
}

/**
* @since 32.0.0
*/
public function getUrl(): string {
return $this->url;
}

/**
Expand Down
10 changes: 7 additions & 3 deletions tests/lib/BackgroundJob/DummyJobList.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Test\BackgroundJob;

use ArrayIterator;
use OC\BackgroundJob\JobList;
use OCP\BackgroundJob\IJob;
use OCP\BackgroundJob\Job;
Expand Down Expand Up @@ -98,20 +99,23 @@ public function getAll(): array {
return $this->jobs;
}

public function getJobsIterator($job, ?int $limit, int $offset): array {
public function getJobsIterator($job, ?int $limit, int $offset): iterable {
if ($job instanceof IJob) {
$jobClass = get_class($job);
} else {
$jobClass = $job;
}
return array_slice(

$jobs = array_slice(
array_filter(
$this->jobs,
fn ($job) => ($jobClass === null) || (get_class($job) == $jobClass)
fn ($job) => ($jobClass === null) || (get_class($job) === $jobClass)
),
$offset,
$limit
);

return new ArrayIterator($jobs);
}

/**
Expand Down
Loading