Skip to content

Commit 3ea058d

Browse files
icewind1991backportbot[bot]
authored andcommitted
feat: add rate limiting for creating federated shares for non-trusted servers
feat: add rate limiting for creating federated shares for non-trusted servers Signed-off-by: Robin Appelman <robin@icewind.nl> [skip ci]
1 parent 6979f54 commit 3ea058d

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

apps/cloud_federation_api/lib/Controller/RequestHandlerController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function __construct(
7979
#[PublicPage]
8080
#[NoCSRFRequired]
8181
#[BruteForceProtection(action: 'receiveFederatedShare')]
82+
#[FederationRateLimit(limit: 5, period: 1200)]
8283
public function addShare($shareWith, $name, $description, $providerId, $owner, $ownerDisplayName, $sharedBy, $sharedByDisplayName, $protocol, $shareType, $resourceType) {
8384
// check if all required parameters are set
8485
if ($shareWith === null ||

apps/federatedfilesharing/lib/Controller/RequestHandlerController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public function __construct(string $appName,
120120
*/
121121
#[NoCSRFRequired]
122122
#[PublicPage]
123+
#[FederationRateLimit(limit: 5, period: 1200)]
123124
public function createShare(
124125
?string $remote = null,
125126
?string $token = null,
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OC\AppFramework\Http\Attributes;
11+
12+
use Attribute;
13+
use OC\OCM\OCMDiscoveryService;
14+
use OCA\Federation\TrustedServers;
15+
use OCP\AppFramework\Http\Attribute\AnonRateLimit;
16+
use OCP\IRequest;
17+
use OCP\Server;
18+
19+
/**
20+
* Attribute for controller methods that want to limit the times a not logged-in
21+
* guest can call the endpoint in a given time period.
22+
*
23+
* Unlike regular AnonRateLimit, signed requests from trusted servers are excluded from the rate limit.
24+
*/
25+
#[Attribute(Attribute::TARGET_METHOD)]
26+
class FederationRateLimit extends AnonRateLimit {
27+
private readonly OCMDiscoveryService $discoveryService;
28+
private readonly ?TrustedServers $trustedServers;
29+
30+
public function __construct(int $limit, int $period) {
31+
parent::__construct($limit, $period);
32+
33+
$this->discoveryService = Server::get(OCMDiscoveryService::class);
34+
$this->trustedServers = Server::get(TrustedServers::class);
35+
}
36+
37+
#[\Override]
38+
public function shouldApply(IRequest $request): bool {
39+
if ($this->trustedServers === null) {
40+
return true;
41+
}
42+
43+
try {
44+
$signedRequest = $this->discoveryService->getIncomingSignedRequest();
45+
if (!$signedRequest) {
46+
return true;
47+
}
48+
$signedRequest->verify();
49+
return !$this->trustedServers->isTrustedServer($signedRequest->getOrigin());
50+
} catch (\Exception) {
51+
// no or invalid signature
52+
return true;
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)