Skip to content

Commit 7f07be8

Browse files
fixup! fix(dav): Rate limit calendar/subscription creation
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
1 parent a96b459 commit 7f07be8

4 files changed

Lines changed: 26 additions & 18 deletions

File tree

.htaccess

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,3 @@
110110

111111
AddDefaultCharset utf-8
112112
Options -Indexes
113-
#### DO NOT CHANGE ANYTHING ABOVE THIS LINE ####
114-
115-
ErrorDocument 403 /index.php/error/403
116-
ErrorDocument 404 /index.php/error/404

apps/dav/appinfo/v1/caldav.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use OC\KnownUser\KnownUserService;
3030
use OCA\DAV\CalDAV\CalDavBackend;
3131
use OCA\DAV\CalDAV\CalendarRoot;
32+
use OCA\DAV\CalDAV\Security\RateLimitingPlugin;
3233
use OCA\DAV\Connector\LegacyDAVACL;
3334
use OCA\DAV\Connector\Sabre\Auth;
3435
use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
@@ -116,6 +117,7 @@
116117
$server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class));
117118
}
118119
$server->addPlugin(new ExceptionLoggerPlugin('caldav', $logger));
120+
$server->addPlugin(\OCP\Server::get(RateLimitingPlugin::class));
119121

120122
// And off we go!
121123
$server->exec();

apps/dav/lib/CalDAV/Security/RateLimitingPlugin.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use OCP\IUserManager;
3434
use Psr\Log\LoggerInterface;
3535
use Sabre\DAV;
36+
use Sabre\DAV\Exception\Forbidden;
3637
use Sabre\DAV\ServerPlugin;
3738
use function count;
3839
use function explode;
@@ -81,15 +82,15 @@ public function beforeBind(string $path): void {
8182
try {
8283
$this->limiter->registerUserRequest(
8384
'caldav-create-calendar',
84-
10,
85-
3600,
85+
$this->config->getValueInt('dav', 'rateLimitCalendarCreation', 10),
86+
$this->config->getValueInt('dav', 'rateLimitPeriodCalendarCreation', 3600),
8687
$user
8788
);
8889
} catch (RateLimitExceededException $e) {
8990
throw new TooManyRequests('Too many calendars created', 0, $e);
9091
}
9192

92-
$calendarLimit = $this->config->getValueInt('dav', 'maximum_calendars', 30);
93+
$calendarLimit = $this->config->getValueInt('dav', 'maximumCalendarsSubscriptions', 30);
9394
if ($calendarLimit === -1) {
9495
return;
9596
}
@@ -102,7 +103,7 @@ public function beforeBind(string $path): void {
102103
'subscription' => $numSubscriptions,
103104
'limit' => $calendarLimit,
104105
]);
105-
throw new TooManyRequests('Calendar limit reached', 0);
106+
throw new Forbidden('Calendar limit reached', 0);
106107
}
107108
}
108109
}

apps/dav/tests/unit/CalDAV/Security/RateLimitingPluginTest.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use OCP\IUserManager;
3636
use PHPUnit\Framework\MockObject\MockObject;
3737
use Psr\Log\LoggerInterface;
38+
use Sabre\DAV\Exception\Forbidden;
3839
use Test\TestCase;
3940

4041
class RateLimitingPluginTest extends TestCase {
@@ -90,6 +91,10 @@ public function testRegisterCalendarCreation(): void {
9091
->method('get')
9192
->with($this->userId)
9293
->willReturn($user);
94+
$this->config
95+
->method('getValueInt')
96+
->with('dav')
97+
->willReturnArgument(2);
9398
$this->limiter->expects(self::once())
9499
->method('registerUserRequest')
95100
->with(
@@ -98,10 +103,6 @@ public function testRegisterCalendarCreation(): void {
98103
3600,
99104
$user,
100105
);
101-
$this->config->expects(self::once())
102-
->method('getValueInt')
103-
->with('dav', 'maximum_calendars', 30)
104-
->willReturn(12);
105106

106107
$this->plugin->beforeBind('calendars/foo/cal');
107108
}
@@ -112,6 +113,10 @@ public function testCalendarCreationRateLimitExceeded(): void {
112113
->method('get')
113114
->with($this->userId)
114115
->willReturn($user);
116+
$this->config
117+
->method('getValueInt')
118+
->with('dav')
119+
->willReturnArgument(2);
115120
$this->limiter->expects(self::once())
116121
->method('registerUserRequest')
117122
->with(
@@ -133,6 +138,10 @@ public function testCalendarLimitReached(): void {
133138
->with($this->userId)
134139
->willReturn($user);
135140
$user->method('getUID')->willReturn('user123');
141+
$this->config
142+
->method('getValueInt')
143+
->with('dav')
144+
->willReturnArgument(2);
136145
$this->limiter->expects(self::once())
137146
->method('registerUserRequest')
138147
->with(
@@ -141,15 +150,15 @@ public function testCalendarLimitReached(): void {
141150
3600,
142151
$user,
143152
);
144-
$this->config->expects(self::once())
145-
->method('getValueInt')
146-
->with('dav', 'maximum_calendars', 30)
147-
->willReturn(12);
148153
$this->caldavBackend->expects(self::once())
149154
->method('getCalendarsForUserCount')
150155
->with('principals/users/user123')
151-
->willReturn(12);
152-
$this->expectException(TooManyRequests::class);
156+
->willReturn(27);
157+
$this->caldavBackend->expects(self::once())
158+
->method('getSubscriptionsForUserCount')
159+
->with('principals/users/user123')
160+
->willReturn(3);
161+
$this->expectException(Forbidden::class);
153162

154163
$this->plugin->beforeBind('calendars/foo/cal');
155164
}

0 commit comments

Comments
 (0)