From a431a3afbeb59dba4568f62d1bc88ca8794c29bc Mon Sep 17 00:00:00 2001 From: Cristian Scheid Date: Sun, 15 Nov 2026 08:52:54 -0300 Subject: [PATCH] feat(ocm): support new format of notfication payload Signed-off-by: Cristian Scheid --- .../Controller/RequestHandlerController.php | 88 ++++++++++---- apps/cloud_federation_api/openapi.json | 18 ++- .../RequestHandlerControllerTest.php | 112 +++++++++++++++++- lib/composer/composer/autoload_classmap.php | 3 + lib/composer/composer/autoload_static.php | 3 + ...DomainAwareCloudFederationNotification.php | 62 ++++++++++ lib/private/OCM/OCMDiscoveryService.php | 31 +++++ ...DomainAwareCloudFederationNotification.php | 65 ++++++++++ ...nderDomainAwareCloudFederationProvider.php | 45 +++++++ .../Events/OCMNotificationReceivedEvent.php | 4 + lib/public/OCM/IOCMDiscoveryService.php | 14 +++ openapi.json | 18 ++- .../OCM/DiscoveryServiceSignedRequestTest.php | 87 ++++++++++++++ 13 files changed, 516 insertions(+), 34 deletions(-) create mode 100644 lib/private/Federation/SenderDomainAwareCloudFederationNotification.php create mode 100644 lib/public/Federation/ISenderDomainAwareCloudFederationNotification.php create mode 100644 lib/public/Federation/ISenderDomainAwareCloudFederationProvider.php create mode 100644 tests/lib/OCM/DiscoveryServiceSignedRequestTest.php diff --git a/apps/cloud_federation_api/lib/Controller/RequestHandlerController.php b/apps/cloud_federation_api/lib/Controller/RequestHandlerController.php index 9ebb26c61ad88..1077bf7b27a7d 100644 --- a/apps/cloud_federation_api/lib/Controller/RequestHandlerController.php +++ b/apps/cloud_federation_api/lib/Controller/RequestHandlerController.php @@ -8,6 +8,7 @@ namespace OCA\CloudFederationAPI\Controller; use OC\AppFramework\Http\Attributes\FederationRateLimit; +use OC\Federation\SenderDomainAwareCloudFederationNotification; use OC\OCM\OCMSignatoryManager; use OCA\CloudFederationAPI\Config; use OCA\CloudFederationAPI\ResponseDefinitions; @@ -25,8 +26,10 @@ use OCP\Federation\Exceptions\ProviderCouldNotAddShareException; use OCP\Federation\Exceptions\ProviderDoesNotExistsException; use OCP\Federation\ICloudFederationFactory; +use OCP\Federation\ICloudFederationNotification; use OCP\Federation\ICloudFederationProviderManager; use OCP\Federation\ICloudIdManager; +use OCP\Federation\ISenderDomainAwareCloudFederationProvider; use OCP\Federation\ISignedCloudFederationProvider; use OCP\Federation\IValidationAwareCloudFederationProvider; use OCP\IAppConfig; @@ -283,31 +286,49 @@ public function addShare($shareWith, $name, $description, $providerId, $owner, $ } /** - * Send a notification about an existing share + * Send a notification about a known share or resource * * @param string $notificationType Notification type, e.g. SHARE_ACCEPTED - * @param string $resourceType calendar, file, contact,... + * @param string|null $resourceType calendar, file, contact,... * @param string|null $providerId ID of the share * @param array|null $notification The actual payload of the notification + * @param string|null $senderDomain Domain of the sending server + * @param string|null $shareType Type of the recipient the notification is about, e.g. user * * @return JSONResponse, array{}>|JSONResponse|JSONResponse * * 201: The notification was successfully received * 400: Bad request due to invalid parameters, e.g. when `type` is invalid or missing * 403: Getting resource is not allowed - * 501: The resource type is not supported + * 501: The resource type or the notification type is not supported */ #[NoCSRFRequired] #[PublicPage] #[BruteForceProtection(action: 'receiveFederatedShareNotification')] - public function receiveNotification($notificationType, $resourceType, $providerId, ?array $notification) { + public function receiveNotification($notificationType, $resourceType, $providerId, ?array $notification, $senderDomain, $shareType) { // check if all required parameters are set - if ( - $notificationType === null - || $resourceType === null - || $providerId === null - || !is_array($notification) - ) { + $missingArguments = false; + if ($senderDomain !== null) { + // new format notification (OCM 1.5+) + if ( + $notificationType === null + || $senderDomain === '' + || ($resourceType === null && $shareType === null) + ) { + $missingArguments = true; + } + } else { + // old format notification (before OCM 1.5) + if ( + $notificationType === null + || $resourceType === null + || $providerId === null + || !is_array($notification) + ) { + $missingArguments = true; + } + } + if ($missingArguments) { return new JSONResponse( [ 'message' => 'Missing arguments', @@ -316,11 +337,19 @@ public function receiveNotification($notificationType, $resourceType, $providerI Http::STATUS_BAD_REQUEST ); } + $notification ??= []; + $signedRequest = null; if (!$this->appConfig->getValueBool('core', OCMSignatoryManager::APPCONFIG_SIGN_DISABLED, lazy: true)) { try { - $identity = $this->resolveNotificationIdentity($resourceType, $notification); - $signedRequest = $this->ocmDiscoveryService->getIncomingSignedRequest($identity !== '' ? $identity : null); + if ($senderDomain !== null) { + $signedRequest = $this->ocmDiscoveryService->getIncomingSignedRequestFromDomain($senderDomain); + // sharedSecret is optional: when present, the host of the federation ID it resolves to must match senderDomain + $identity = $resourceType !== null && ($notification['sharedSecret'] ?? '') !== '' ? $this->resolveNotificationIdentity($resourceType, $notification) : ''; + } else { + $identity = $this->resolveNotificationIdentity($resourceType, $notification); + $signedRequest = $this->ocmDiscoveryService->getIncomingSignedRequest($identity !== '' ? $identity : null); + } if ($identity !== '') { $this->ocmDiscoveryService->confirmRequestOrigin($signedRequest?->getOrigin(), $identity); } @@ -330,16 +359,34 @@ public function receiveNotification($notificationType, $resourceType, $providerI } } + if ($senderDomain !== null && $signedRequest !== null) { + $notificationObject = new SenderDomainAwareCloudFederationNotification($signedRequest->getOrigin(), $shareType); + } else { + $notificationObject = $this->factory->getCloudFederationNotification(); + } + $notificationObject->setMessage($notificationType, $resourceType, $providerId, $notification); + try { $provider = $this->cloudFederationProviderManager->getCloudFederationProvider($resourceType); - $result = $provider->notificationReceived($notificationType, $providerId, $notification); + if ($senderDomain !== null) { + if (!$provider instanceof ISenderDomainAwareCloudFederationProvider || $signedRequest === null) { + throw new ActionNotSupportedException($notificationType); + } + $result = $provider->senderDomainAwareNotificationReceived($notificationType, $providerId, $notification, $signedRequest->getOrigin(), $shareType); + } else { + $result = $provider->notificationReceived($notificationType, $providerId, $notification); + } } catch (ProviderDoesNotExistsException $e) { + // new format notifications are dispatched even without provider, since resourceType is optional + if ($notificationObject instanceof SenderDomainAwareCloudFederationNotification) { + $this->dispatchNotificationReceived($notificationObject); + } return new JSONResponse( [ 'message' => $e->getMessage(), 'validationErrors' => [], ], - Http::STATUS_BAD_REQUEST + $senderDomain !== null ? Http::STATUS_NOT_IMPLEMENTED : Http::STATUS_BAD_REQUEST ); } catch (ShareNotFound $e) { $response = new JSONResponse( @@ -384,16 +431,17 @@ public function receiveNotification($notificationType, $resourceType, $providerI 'cloud_federation_api.notification.received', )); + $this->dispatchNotificationReceived($notificationObject); + + return new JSONResponse($result, Http::STATUS_CREATED); + } + + private function dispatchNotificationReceived(ICloudFederationNotification $notification): void { try { - $notificationObject = $this->factory->getCloudFederationNotification(); - $notificationObject->setMessage($notificationType, $resourceType, $providerId, $notification); - $notificationEvent = new OCMNotificationReceivedEvent($notificationObject); - $this->dispatcher->dispatchTyped($notificationEvent); + $this->dispatcher->dispatchTyped(new OCMNotificationReceivedEvent($notification)); } catch (\Exception $e) { $this->logger->warning('error while dispatching OCM notification received event', ['exception' => $e]); } - - return new JSONResponse($result, Http::STATUS_CREATED); } /** diff --git a/apps/cloud_federation_api/openapi.json b/apps/cloud_federation_api/openapi.json index fffbefe54162b..b10aa046b1c74 100644 --- a/apps/cloud_federation_api/openapi.json +++ b/apps/cloud_federation_api/openapi.json @@ -224,7 +224,7 @@ "/index.php/ocm/notifications": { "post": { "operationId": "request_handler-receive-notification", - "summary": "Send a notification about an existing share", + "summary": "Send a notification about a known share or resource", "tags": [ "request_handler" ], @@ -244,8 +244,7 @@ "schema": { "type": "object", "required": [ - "notificationType", - "resourceType" + "notificationType" ], "properties": { "notificationType": { @@ -254,6 +253,7 @@ }, "resourceType": { "type": "string", + "nullable": true, "description": "calendar, file, contact,..." }, "providerId": { @@ -268,6 +268,16 @@ "additionalProperties": { "type": "object" } + }, + "senderDomain": { + "type": "string", + "nullable": true, + "description": "Domain of the sending server" + }, + "shareType": { + "type": "string", + "nullable": true, + "description": "Type of the recipient the notification is about, e.g. user" } } } @@ -309,7 +319,7 @@ } }, "501": { - "description": "The resource type is not supported", + "description": "The resource type or the notification type is not supported", "content": { "application/json": { "schema": { diff --git a/apps/cloud_federation_api/tests/Controller/RequestHandlerControllerTest.php b/apps/cloud_federation_api/tests/Controller/RequestHandlerControllerTest.php index 76558fc73db31..9e0d1afdbdcfd 100644 --- a/apps/cloud_federation_api/tests/Controller/RequestHandlerControllerTest.php +++ b/apps/cloud_federation_api/tests/Controller/RequestHandlerControllerTest.php @@ -15,6 +15,7 @@ use OCP\AppFramework\Http; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventDispatcher; +use OCP\Federation\Exceptions\ProviderDoesNotExistsException; use OCP\Federation\ICloudFederationFactory; use OCP\Federation\ICloudFederationNotification; use OCP\Federation\ICloudFederationProvider; @@ -22,13 +23,19 @@ use OCP\Federation\ICloudFederationShare; use OCP\Federation\ICloudId; use OCP\Federation\ICloudIdManager; +use OCP\Federation\ISenderDomainAwareCloudFederationNotification; +use OCP\Federation\ISenderDomainAwareCloudFederationProvider; +use OCP\Federation\ISignedCloudFederationProvider; use OCP\IAppConfig; use OCP\IGroupManager; use OCP\IRequest; use OCP\IURLGenerator; use OCP\IUserManager; use OCP\Log\Audit\CriticalActionPerformedEvent; +use OCP\OCM\Events\OCMNotificationReceivedEvent; use OCP\OCM\IOCMDiscoveryService; +use OCP\Security\Signature\Exceptions\IncomingRequestException; +use OCP\Security\Signature\IIncomingSignedRequest; use OCP\Share\Exceptions\ShareNotFound; use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; @@ -38,10 +45,12 @@ class RequestHandlerControllerTest extends TestCase { private IUserManager&MockObject $userManager; private ICloudFederationProviderManager&MockObject $providerManager; private ICloudFederationProvider&MockObject $provider; + private SignedSenderDomainAwareCloudFederationProvider&MockObject $senderDomainAwareProvider; private Config&MockObject $config; private IEventDispatcher&MockObject $dispatcher; private ICloudFederationFactory&MockObject $factory; private ICloudIdManager&MockObject $cloudIdManager; + private IOCMDiscoveryService&MockObject $ocmDiscoveryService; private RequestHandlerController $controller; /** @var list */ @@ -54,17 +63,30 @@ protected function setUp(): void { $this->userManager = $this->createMock(IUserManager::class); $this->providerManager = $this->createMock(ICloudFederationProviderManager::class); $this->provider = $this->createMock(ICloudFederationProvider::class); + $this->senderDomainAwareProvider = $this->createMock(SignedSenderDomainAwareCloudFederationProvider::class); $this->config = $this->createMock(Config::class); $this->dispatcher = $this->createMock(IEventDispatcher::class); $this->factory = $this->createMock(ICloudFederationFactory::class); $this->cloudIdManager = $this->createMock(ICloudIdManager::class); + $this->ocmDiscoveryService = $this->createMock(IOCMDiscoveryService::class); $appConfig = $this->createMock(IAppConfig::class); $appConfig->method('getValueBool') ->with('core', OCMSignatoryManager::APPCONFIG_SIGN_DISABLED, false, true) - ->willReturn(true); + ->willReturn(false); + + $this->providerManager->method('getCloudFederationProvider') + ->willReturnCallback(fn (?string $resourceType): ICloudFederationProvider => match ($resourceType) { + 'file' => $this->provider, + 'circle' => $this->senderDomainAwareProvider, + default => throw new ProviderDoesNotExistsException($resourceType), + }); + $this->factory->method('getCloudFederationNotification')->willReturn($this->createMock(ICloudFederationNotification::class)); + + $signedRequest = $this->createMock(IIncomingSignedRequest::class); + $signedRequest->method('getOrigin')->willReturn('remote.example'); + $this->ocmDiscoveryService->method('getIncomingSignedRequestFromDomain')->with('remote.example')->willReturn($signedRequest); - $this->providerManager->method('getCloudFederationProvider')->with('file')->willReturn($this->provider); $this->dispatcher->method('dispatchTyped') ->willReturnCallback(function (Event $event): void { $this->dispatchedEvents[] = $event; @@ -83,7 +105,7 @@ protected function setUp(): void { $appConfig, $this->factory, $this->cloudIdManager, - $this->createMock(IOCMDiscoveryService::class), + $this->ocmDiscoveryService, ); } @@ -150,9 +172,8 @@ public function testReceiveNotificationLogsCriticalAction(): void { ->method('notificationReceived') ->with('SHARE_ACCEPTED', '12', ['sharedSecret' => 'secret']) ->willReturn([]); - $this->factory->method('getCloudFederationNotification')->willReturn($this->createMock(ICloudFederationNotification::class)); - $response = $this->controller->receiveNotification('SHARE_ACCEPTED', 'file', '12', ['sharedSecret' => 'secret']); + $response = $this->controller->receiveNotification('SHARE_ACCEPTED', 'file', '12', ['sharedSecret' => 'secret'], null, null); $this->assertSame(Http::STATUS_CREATED, $response->getStatus()); $auditEvents = $this->getAuditEvents(); @@ -169,9 +190,88 @@ public function testReceiveNotificationLogsCriticalAction(): void { public function testReceiveNotificationForUnknownShareDoesNotLog(): void { $this->provider->method('notificationReceived')->willThrowException(new ShareNotFound()); - $response = $this->controller->receiveNotification('SHARE_ACCEPTED', 'file', '12', ['sharedSecret' => 'secret']); + $response = $this->controller->receiveNotification('SHARE_ACCEPTED', 'file', '12', ['sharedSecret' => 'secret'], null, null); $this->assertSame(Http::STATUS_BAD_REQUEST, $response->getStatus()); $this->assertSame([], $this->getAuditEvents()); } + + /** + * @return list + */ + private function getOCMNotificationReceivedEvents(): array { + return array_values(array_filter( + $this->dispatchedEvents, + static fn (Event $event): bool => $event instanceof OCMNotificationReceivedEvent, + )); + } + + public function testReceiveNotificationWithSenderDomain(): void { + $this->senderDomainAwareProvider->expects($this->once()) + ->method('senderDomainAwareNotificationReceived') + ->with('CIRCLES_EVENT', null, ['event' => []], 'remote.example', null) + ->willReturn(['result' => []]); + + $response = $this->controller->receiveNotification('CIRCLES_EVENT', 'circle', null, ['event' => []], 'remote.example', null); + + $this->assertSame(Http::STATUS_CREATED, $response->getStatus()); + $this->assertSame(['result' => []], $response->getData()); + $events = $this->getOCMNotificationReceivedEvents(); + $this->assertCount(1, $events); + $notification = $events[0]->getNotification(); + $this->assertInstanceOf(ISenderDomainAwareCloudFederationNotification::class, $notification); + $this->assertSame('remote.example', $notification->getSenderDomain()); + } + + public function testReceiveNotificationWithSenderDomainConfirmsSharedSecretOrigin(): void { + $this->senderDomainAwareProvider->method('getFederationIdFromSharedSecret') + ->with('secret', ['sharedSecret' => 'secret']) + ->willReturn('bob@remote.example'); + $this->ocmDiscoveryService->expects($this->once()) + ->method('confirmRequestOrigin') + ->with('remote.example', 'bob@remote.example'); + $this->senderDomainAwareProvider->expects($this->once())->method('senderDomainAwareNotificationReceived')->willReturn([]); + + $response = $this->controller->receiveNotification('CIRCLES_EVENT', 'circle', null, ['sharedSecret' => 'secret'], 'remote.example', null); + + $this->assertSame(Http::STATUS_CREATED, $response->getStatus()); + } + + public function testReceiveNotificationWithSenderDomainRejectsSharedSecretFromOtherOrigin(): void { + $this->senderDomainAwareProvider->method('getFederationIdFromSharedSecret') + ->willReturn('bob@other.example'); + $this->ocmDiscoveryService->method('confirmRequestOrigin') + ->with('remote.example', 'bob@other.example') + ->willThrowException(new IncomingRequestException('claimed origin does not match signed origin')); + $this->senderDomainAwareProvider->expects($this->never())->method('senderDomainAwareNotificationReceived'); + + $response = $this->controller->receiveNotification('CIRCLES_EVENT', 'circle', null, ['sharedSecret' => 'secret'], 'remote.example', null); + + $this->assertSame(Http::STATUS_BAD_REQUEST, $response->getStatus()); + } + + public function testReceiveNotificationWithSenderDomainWithoutResourceTypeAndShareType(): void { + $response = $this->controller->receiveNotification('CIRCLES_EVENT', null, null, [], 'remote.example', null); + + $this->assertSame(Http::STATUS_BAD_REQUEST, $response->getStatus()); + } + + public function testReceiveNotificationWithSenderDomainForProviderNotSenderDomainAware(): void { + $this->provider->expects($this->never())->method('notificationReceived'); + + $response = $this->controller->receiveNotification('SHARE_ACCEPTED', 'file', null, [], 'remote.example', null); + + $this->assertSame(Http::STATUS_NOT_IMPLEMENTED, $response->getStatus()); + $this->assertSame([], $this->getOCMNotificationReceivedEvents()); + } + + public function testReceiveNotificationWithSenderDomainWithoutProvider(): void { + $response = $this->controller->receiveNotification('SHARE_ACCEPTED', null, null, [], 'remote.example', 'user'); + + $this->assertSame(Http::STATUS_NOT_IMPLEMENTED, $response->getStatus()); + $this->assertCount(1, $this->getOCMNotificationReceivedEvents()); + } +} + +interface SignedSenderDomainAwareCloudFederationProvider extends ISenderDomainAwareCloudFederationProvider, ISignedCloudFederationProvider { } diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 31c511db768f6..26b25ca40640f 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -474,6 +474,8 @@ 'OCP\\Federation\\ICloudId' => $baseDir . '/lib/public/Federation/ICloudId.php', 'OCP\\Federation\\ICloudIdManager' => $baseDir . '/lib/public/Federation/ICloudIdManager.php', 'OCP\\Federation\\ICloudIdResolver' => $baseDir . '/lib/public/Federation/ICloudIdResolver.php', + 'OCP\\Federation\\ISenderDomainAwareCloudFederationNotification' => $baseDir . '/lib/public/Federation/ISenderDomainAwareCloudFederationNotification.php', + 'OCP\\Federation\\ISenderDomainAwareCloudFederationProvider' => $baseDir . '/lib/public/Federation/ISenderDomainAwareCloudFederationProvider.php', 'OCP\\Federation\\ISignedCloudFederationProvider' => $baseDir . '/lib/public/Federation/ISignedCloudFederationProvider.php', 'OCP\\Federation\\IValidationAwareCloudFederationProvider' => $baseDir . '/lib/public/Federation/IValidationAwareCloudFederationProvider.php', 'OCP\\Files' => $baseDir . '/lib/public/Files.php', @@ -1908,6 +1910,7 @@ 'OC\\Federation\\CloudFederationShare' => $baseDir . '/lib/private/Federation/CloudFederationShare.php', 'OC\\Federation\\CloudId' => $baseDir . '/lib/private/Federation/CloudId.php', 'OC\\Federation\\CloudIdManager' => $baseDir . '/lib/private/Federation/CloudIdManager.php', + 'OC\\Federation\\SenderDomainAwareCloudFederationNotification' => $baseDir . '/lib/private/Federation/SenderDomainAwareCloudFederationNotification.php', 'OC\\FilesMetadata\\FilesMetadataManager' => $baseDir . '/lib/private/FilesMetadata/FilesMetadataManager.php', 'OC\\FilesMetadata\\Job\\UpdateSingleMetadata' => $baseDir . '/lib/private/FilesMetadata/Job/UpdateSingleMetadata.php', 'OC\\FilesMetadata\\Listener\\MetadataDelete' => $baseDir . '/lib/private/FilesMetadata/Listener/MetadataDelete.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index ccde6dae259de..e172ab14a7470 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -515,6 +515,8 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Federation\\ICloudId' => __DIR__ . '/../../..' . '/lib/public/Federation/ICloudId.php', 'OCP\\Federation\\ICloudIdManager' => __DIR__ . '/../../..' . '/lib/public/Federation/ICloudIdManager.php', 'OCP\\Federation\\ICloudIdResolver' => __DIR__ . '/../../..' . '/lib/public/Federation/ICloudIdResolver.php', + 'OCP\\Federation\\ISenderDomainAwareCloudFederationNotification' => __DIR__ . '/../../..' . '/lib/public/Federation/ISenderDomainAwareCloudFederationNotification.php', + 'OCP\\Federation\\ISenderDomainAwareCloudFederationProvider' => __DIR__ . '/../../..' . '/lib/public/Federation/ISenderDomainAwareCloudFederationProvider.php', 'OCP\\Federation\\ISignedCloudFederationProvider' => __DIR__ . '/../../..' . '/lib/public/Federation/ISignedCloudFederationProvider.php', 'OCP\\Federation\\IValidationAwareCloudFederationProvider' => __DIR__ . '/../../..' . '/lib/public/Federation/IValidationAwareCloudFederationProvider.php', 'OCP\\Files' => __DIR__ . '/../../..' . '/lib/public/Files.php', @@ -1949,6 +1951,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Federation\\CloudFederationShare' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationShare.php', 'OC\\Federation\\CloudId' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudId.php', 'OC\\Federation\\CloudIdManager' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudIdManager.php', + 'OC\\Federation\\SenderDomainAwareCloudFederationNotification' => __DIR__ . '/../../..' . '/lib/private/Federation/SenderDomainAwareCloudFederationNotification.php', 'OC\\FilesMetadata\\FilesMetadataManager' => __DIR__ . '/../../..' . '/lib/private/FilesMetadata/FilesMetadataManager.php', 'OC\\FilesMetadata\\Job\\UpdateSingleMetadata' => __DIR__ . '/../../..' . '/lib/private/FilesMetadata/Job/UpdateSingleMetadata.php', 'OC\\FilesMetadata\\Listener\\MetadataDelete' => __DIR__ . '/../../..' . '/lib/private/FilesMetadata/Listener/MetadataDelete.php', diff --git a/lib/private/Federation/SenderDomainAwareCloudFederationNotification.php b/lib/private/Federation/SenderDomainAwareCloudFederationNotification.php new file mode 100644 index 0000000000000..269590f84904d --- /dev/null +++ b/lib/private/Federation/SenderDomainAwareCloudFederationNotification.php @@ -0,0 +1,62 @@ +senderDomain; + if ($this->shareType !== null) { + $message['shareType'] = $this->shareType; + } + + return $message; + } + + #[\Override] + public function getNotificationType(): string { + return (string)($this->getMessage()['notificationType'] ?? ''); + } + + #[\Override] + public function getSenderDomain(): string { + return $this->senderDomain; + } + + #[\Override] + public function getResourceType(): ?string { + return $this->getMessage()['resourceType'] ?? null; + } + + #[\Override] + public function getShareType(): ?string { + return $this->shareType; + } + + #[\Override] + public function getProviderId(): ?string { + $providerId = $this->getMessage()['providerId'] ?? ''; + return $providerId === '' ? null : $providerId; + } + + #[\Override] + public function getNotification(): array { + return $this->getMessage()['notification'] ?? []; + } +} diff --git a/lib/private/OCM/OCMDiscoveryService.php b/lib/private/OCM/OCMDiscoveryService.php index 996ceaa22b553..29e87eb2eee2d 100644 --- a/lib/private/OCM/OCMDiscoveryService.php +++ b/lib/private/OCM/OCMDiscoveryService.php @@ -290,6 +290,37 @@ public function getIncomingSignedRequest(?string $ocmAddress = null): ?IIncoming return null; } + /** + * @inheritDoc + * + * @throws IncomingRequestException + * @since 36.0.0 + */ + #[\Override] + public function getIncomingSignedRequestFromDomain(string $senderDomain): IIncomingSignedRequest { + try { + $origin = $this->signatureManager->extractIdentityFromUri('https://' . $senderDomain); + $signedRequest = $this->signatureManager->getIncomingSignedRequest($this->signatoryManager, null, $origin); + } catch (IdentityNotFoundException $e) { + throw new IncomingRequestException('invalid sender domain: ' . $senderDomain, previous: $e); + } catch (SignatureNotFoundException|SignatoryNotFoundException $e) { + $this->logger->debug('remote does not support signed request', ['exception' => $e]); + // as per OCM spec, sender should make HTTP request using httpsig + throw new IncomingRequestException('Unsigned request', previous: $e); + } catch (SignatureException $e) { + $this->logger->warning('wrongly signed request', ['exception' => $e]); + throw new IncomingRequestException('Invalid signature'); + } + + if ($signedRequest->getOrigin() !== $origin) { + throw new IncomingRequestException( + 'sender domain ' . $origin . ' does not match signed origin ' . $signedRequest->getOrigin() + ); + } + + return $signedRequest; + } + /** * @inheritDoc * diff --git a/lib/public/Federation/ISenderDomainAwareCloudFederationNotification.php b/lib/public/Federation/ISenderDomainAwareCloudFederationNotification.php new file mode 100644 index 0000000000000..e42550f43e857 --- /dev/null +++ b/lib/public/Federation/ISenderDomainAwareCloudFederationNotification.php @@ -0,0 +1,65 @@ + + * @since 36.0.0 + */ + public function getNotification(): array; +} diff --git a/lib/public/Federation/ISenderDomainAwareCloudFederationProvider.php b/lib/public/Federation/ISenderDomainAwareCloudFederationProvider.php new file mode 100644 index 0000000000000..4ddd252696697 --- /dev/null +++ b/lib/public/Federation/ISenderDomainAwareCloudFederationProvider.php @@ -0,0 +1,45 @@ + $notification provider specific notification + * @param string $senderDomain domain of the sending server, verified against the request signature. + * Providers must still verify that the sender is allowed to act on the resource. + * @param string|null $shareType type of the recipient the notification is about, e.g. user + * @return array data sent back to the sender + * + * @throws ShareNotFound + * @throws ActionNotSupportedException + * @throws BadRequestException + * @throws AuthenticationFailedException + * + * @since 36.0.0 + */ + public function senderDomainAwareNotificationReceived(string $notificationType, ?string $providerId, array $notification, string $senderDomain, ?string $shareType): array; +} diff --git a/lib/public/OCM/Events/OCMNotificationReceivedEvent.php b/lib/public/OCM/Events/OCMNotificationReceivedEvent.php index 517ddf65b0922..fa826f18bfabd 100644 --- a/lib/public/OCM/Events/OCMNotificationReceivedEvent.php +++ b/lib/public/OCM/Events/OCMNotificationReceivedEvent.php @@ -11,6 +11,7 @@ use OCP\AppFramework\Attribute\Listenable; use OCP\EventDispatcher\Event; use OCP\Federation\ICloudFederationNotification; +use OCP\Federation\ISenderDomainAwareCloudFederationNotification; /** * @since 35.0.0 @@ -27,6 +28,9 @@ public function __construct( } /** + * Notifications with senderDomain field are + * {@see ISenderDomainAwareCloudFederationNotification} + * * @since 35.0.0 */ public function getNotification(): ICloudFederationNotification { diff --git a/lib/public/OCM/IOCMDiscoveryService.php b/lib/public/OCM/IOCMDiscoveryService.php index 92c705be41d2f..b08989c0cff67 100644 --- a/lib/public/OCM/IOCMDiscoveryService.php +++ b/lib/public/OCM/IOCMDiscoveryService.php @@ -68,6 +68,20 @@ public function getLocalOCMProvider(bool $fullDetails = true): IOCMProvider; */ public function getIncomingSignedRequest(?string $ocmAddress = null): ?IIncomingSignedRequest; + /** + * Returns incoming request signed by $senderDomain. + * + * throw an exception: + * - if the request is not signed + * - if the request is signed, but not by $senderDomain + * + * @param string $senderDomain domain of the sending server + * + * @throws IncomingRequestException + * @since 36.0.0 + */ + public function getIncomingSignedRequestFromDomain(string $senderDomain): IIncomingSignedRequest; + /** * Confirm that the host portion of $ocmAddress matches $signedOrigin * under the current local signing policy. diff --git a/openapi.json b/openapi.json index a1f9d5eb551c0..51b24b1822ca6 100644 --- a/openapi.json +++ b/openapi.json @@ -19483,7 +19483,7 @@ "/index.php/ocm/notifications": { "post": { "operationId": "cloud_federation_api-request_handler-receive-notification", - "summary": "Send a notification about an existing share", + "summary": "Send a notification about a known share or resource", "tags": [ "cloud_federation_api/request_handler" ], @@ -19503,8 +19503,7 @@ "schema": { "type": "object", "required": [ - "notificationType", - "resourceType" + "notificationType" ], "properties": { "notificationType": { @@ -19513,6 +19512,7 @@ }, "resourceType": { "type": "string", + "nullable": true, "description": "calendar, file, contact,..." }, "providerId": { @@ -19527,6 +19527,16 @@ "additionalProperties": { "type": "object" } + }, + "senderDomain": { + "type": "string", + "nullable": true, + "description": "Domain of the sending server" + }, + "shareType": { + "type": "string", + "nullable": true, + "description": "Type of the recipient the notification is about, e.g. user" } } } @@ -19568,7 +19578,7 @@ } }, "501": { - "description": "The resource type is not supported", + "description": "The resource type or the notification type is not supported", "content": { "application/json": { "schema": { diff --git a/tests/lib/OCM/DiscoveryServiceSignedRequestTest.php b/tests/lib/OCM/DiscoveryServiceSignedRequestTest.php new file mode 100644 index 0000000000000..a9e3cc2783e0e --- /dev/null +++ b/tests/lib/OCM/DiscoveryServiceSignedRequestTest.php @@ -0,0 +1,87 @@ +signatureManager = $this->createMock(ISignatureManager::class); + $this->signatureManager->method('extractIdentityFromUri') + ->with('https://remote.example') + ->willReturn('remote.example'); + $this->signatoryManager = $this->createMock(OCMSignatoryManager::class); + + $this->discoveryService = new OCMDiscoveryService( + $this->createMock(ICacheFactory::class), + $this->createMock(IClientService::class), + $this->createMock(IEventDispatcher::class), + $this->createMock(IConfig::class), + $this->createMock(IAppConfig::class), + $this->createMock(IURLGenerator::class), + $this->signatureManager, + $this->signatoryManager, + $this->createMock(ICloudIdManager::class), + $this->createMock(LoggerInterface::class), + ); + } + + private function mockSignedRequest(string $origin): IIncomingSignedRequest&MockObject { + $signedRequest = $this->createMock(IIncomingSignedRequest::class); + $signedRequest->method('getOrigin')->willReturn($origin); + return $signedRequest; + } + + public function testGetIncomingSignedRequestFromDomain(): void { + $signedRequest = $this->mockSignedRequest('remote.example'); + $this->signatureManager->expects($this->once()) + ->method('getIncomingSignedRequest') + ->with($this->signatoryManager, null, 'remote.example') + ->willReturn($signedRequest); + + $this->assertSame($signedRequest, $this->discoveryService->getIncomingSignedRequestFromDomain('remote.example')); + } + + public function testGetIncomingSignedRequestFromDomainRejectsOtherOrigin(): void { + $this->signatureManager->method('getIncomingSignedRequest')->willReturn($this->mockSignedRequest('other.example')); + + $this->expectException(IncomingRequestException::class); + $this->discoveryService->getIncomingSignedRequestFromDomain('remote.example'); + } + + public function testGetIncomingSignedRequestFromDomainRejectsUnsignedRequest(): void { + $this->signatureManager->method('getIncomingSignedRequest')->willThrowException(new SignatureNotFoundException()); + + $this->expectException(IncomingRequestException::class); + $this->discoveryService->getIncomingSignedRequestFromDomain('remote.example'); + } +}