Skip to content

Commit 9459352

Browse files
committed
fix(CalDAV): check birthday calendar owner
Signed-off-by: Anna Larch <[email protected]>
1 parent aa8094a commit 9459352

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

apps/dav/lib/CalDAV/BirthdayCalendar/EnablePlugin.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use OCA\DAV\CalDAV\BirthdayService;
2828
use OCA\DAV\CalDAV\CalendarHome;
2929
use OCP\IConfig;
30+
use OCP\IUser;
3031
use Sabre\DAV\Server;
3132
use Sabre\DAV\ServerPlugin;
3233
use Sabre\HTTP\RequestInterface;
@@ -56,15 +57,20 @@ class EnablePlugin extends ServerPlugin {
5657
*/
5758
protected $server;
5859

60+
/** @var IUser */
61+
private $user;
62+
5963
/**
6064
* PublishPlugin constructor.
6165
*
6266
* @param IConfig $config
6367
* @param BirthdayService $birthdayService
68+
* @param IUser $user
6469
*/
65-
public function __construct(IConfig $config, BirthdayService $birthdayService) {
70+
public function __construct(IConfig $config, BirthdayService $birthdayService, IUser $user) {
6671
$this->config = $config;
6772
$this->birthdayService = $birthdayService;
73+
$this->user = $user;
6874
}
6975

7076
/**
@@ -127,11 +133,14 @@ public function httpPost(RequestInterface $request, ResponseInterface $response)
127133
return;
128134
}
129135

130-
$principalUri = $node->getOwner();
131-
$userId = substr($principalUri, 17);
136+
$owner = substr($node->getOwner(), 17);
137+
if($owner !== $this->user->getUID()) {
138+
$this->server->httpResponse->setStatus(403);
139+
return false;
140+
}
132141

133-
$this->config->setUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes');
134-
$this->birthdayService->syncUser($userId);
142+
$this->config->setUserValue($this->user->getUID(), 'dav', 'generateBirthdayCalendar', 'yes');
143+
$this->birthdayService->syncUser($this->user->getUID());
135144

136145
$this->server->httpResponse->setStatus(204);
137146

apps/dav/lib/Server.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ public function __construct(IRequest $request, string $baseUri) {
325325
}
326326
$this->server->addPlugin(new \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin(
327327
\OC::$server->getConfig(),
328-
\OC::$server->query(BirthdayService::class)
328+
\OC::$server->query(BirthdayService::class),
329+
$user
329330
));
330331
$this->server->addPlugin(new AppleProvisioningPlugin(
331332
\OC::$server->getUserSession(),

apps/dav/tests/unit/CalDAV/BirthdayCalendar/EnablePluginTest.php

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use OCA\DAV\CalDAV\Calendar;
3232
use OCA\DAV\CalDAV\CalendarHome;
3333
use OCP\IConfig;
34+
use OCP\IUser;
3435
use Test\TestCase;
3536

3637
class EnablePluginTest extends TestCase {
@@ -44,6 +45,9 @@ class EnablePluginTest extends TestCase {
4445
/** @var BirthdayService |\PHPUnit\Framework\MockObject\MockObject */
4546
protected $birthdayService;
4647

48+
/** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
49+
protected $user;
50+
4751
/** @var \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin $plugin */
4852
protected $plugin;
4953

@@ -61,8 +65,9 @@ protected function setUp(): void {
6165

6266
$this->config = $this->createMock(IConfig::class);
6367
$this->birthdayService = $this->createMock(BirthdayService::class);
68+
$this->user = $this->createMock(IUser::class);
6469

65-
$this->plugin = new EnablePlugin($this->config, $this->birthdayService);
70+
$this->plugin = new EnablePlugin($this->config, $this->birthdayService, $this->user);
6671
$this->plugin->initialize($this->server);
6772

6873
$this->request = $this->createMock(\Sabre\HTTP\RequestInterface::class);
@@ -80,7 +85,7 @@ public function testGetName(): void {
8085
public function testInitialize(): void {
8186
$server = $this->createMock(\Sabre\DAV\Server::class);
8287

83-
$plugin = new EnablePlugin($this->config, $this->birthdayService);
88+
$plugin = new EnablePlugin($this->config, $this->birthdayService, $this->user);
8489

8590
$server->expects($this->once())
8691
->method('on')
@@ -143,6 +148,55 @@ public function testHttpPostWrongRequest(): void {
143148
$this->plugin->httpPost($this->request, $this->response);
144149
}
145150

151+
public function testHttpPostNotAuthorized(): void {
152+
$calendarHome = $this->createMock(CalendarHome::class);
153+
154+
$this->server->expects($this->once())
155+
->method('getRequestUri')
156+
->willReturn('/bar/foo');
157+
$this->server->tree->expects($this->once())
158+
->method('getNodeForPath')
159+
->with('/bar/foo')
160+
->willReturn($calendarHome);
161+
162+
$calendarHome->expects($this->once())
163+
->method('getOwner')
164+
->willReturn('principals/users/BlaBlub');
165+
166+
$this->request->expects($this->once())
167+
->method('getBodyAsString')
168+
->willReturn('<nc:enable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>');
169+
170+
$this->request->expects($this->once())
171+
->method('getUrl')
172+
->willReturn('url_abc');
173+
174+
$this->server->xml->expects($this->once())
175+
->method('parse')
176+
->willReturnCallback(function ($requestBody, $url, &$documentType): void {
177+
$documentType = '{http://nextcloud.com/ns}enable-birthday-calendar';
178+
});
179+
180+
$this->user->expects(self::once())
181+
->method('getUID')
182+
->willReturn('admin');
183+
184+
$this->server->httpResponse->expects($this->once())
185+
->method('setStatus')
186+
->with(403);
187+
188+
$this->config->expects($this->never())
189+
->method('setUserValue');
190+
191+
$this->birthdayService->expects($this->never())
192+
->method('syncUser');
193+
194+
195+
$result = $this->plugin->httpPost($this->request, $this->response);
196+
197+
$this->assertEquals(false, $result);
198+
}
199+
146200
public function testHttpPost(): void {
147201
$calendarHome = $this->createMock(CalendarHome::class);
148202

@@ -172,6 +226,10 @@ public function testHttpPost(): void {
172226
$documentType = '{http://nextcloud.com/ns}enable-birthday-calendar';
173227
});
174228

229+
$this->user->expects(self::exactly(3))
230+
->method('getUID')
231+
->willReturn('BlaBlub');
232+
175233
$this->config->expects($this->once())
176234
->method('setUserValue')
177235
->with('BlaBlub', 'dav', 'generateBirthdayCalendar', 'yes');

0 commit comments

Comments
 (0)