Skip to content

Commit

Permalink
Merge pull request #40317 from nextcloud/backport/40292/stable24
Browse files Browse the repository at this point in the history
[stable24] fix(CalDAV): check birthday calendar owner
  • Loading branch information
AndyScherzinger committed Sep 7, 2023
2 parents fa9c7e2 + 5072edf commit 82ec950
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 13 deletions.
19 changes: 14 additions & 5 deletions apps/dav/lib/CalDAV/BirthdayCalendar/EnablePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OCA\DAV\CalDAV\BirthdayService;
use OCA\DAV\CalDAV\CalendarHome;
use OCP\IConfig;
use OCP\IUser;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
use Sabre\HTTP\RequestInterface;
Expand Down Expand Up @@ -56,15 +57,20 @@ class EnablePlugin extends ServerPlugin {
*/
protected $server;

/** @var IUser */
private $user;

/**
* PublishPlugin constructor.
*
* @param IConfig $config
* @param BirthdayService $birthdayService
* @param IUser $user
*/
public function __construct(IConfig $config, BirthdayService $birthdayService) {
public function __construct(IConfig $config, BirthdayService $birthdayService, IUser $user) {
$this->config = $config;
$this->birthdayService = $birthdayService;
$this->user = $user;
}

/**
Expand Down Expand Up @@ -127,11 +133,14 @@ public function httpPost(RequestInterface $request, ResponseInterface $response)
return;
}

$principalUri = $node->getOwner();
$userId = substr($principalUri, 17);
$owner = substr($node->getOwner(), 17);
if($owner !== $this->user->getUID()) {
$this->server->httpResponse->setStatus(403);
return false;
}

$this->config->setUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes');
$this->birthdayService->syncUser($userId);
$this->config->setUserValue($this->user->getUID(), 'dav', 'generateBirthdayCalendar', 'yes');
$this->birthdayService->syncUser($this->user->getUID());

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

Expand Down
3 changes: 2 additions & 1 deletion apps/dav/lib/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ public function __construct(IRequest $request, string $baseUri) {
}
$this->server->addPlugin(new \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin(
\OC::$server->getConfig(),
\OC::$server->query(BirthdayService::class)
\OC::$server->query(BirthdayService::class),
$user
));
$this->server->addPlugin(new AppleProvisioningPlugin(
\OC::$server->getUserSession(),
Expand Down
72 changes: 65 additions & 7 deletions apps/dav/tests/unit/CalDAV/BirthdayCalendar/EnablePluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCA\DAV\CalDAV\Calendar;
use OCA\DAV\CalDAV\CalendarHome;
use OCP\IConfig;
use OCP\IUser;
use Test\TestCase;

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

/** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
protected $user;

/** @var \OCA\DAV\CalDAV\BirthdayCalendar\EnablePlugin $plugin */
protected $plugin;

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

$this->config = $this->createMock(IConfig::class);
$this->birthdayService = $this->createMock(BirthdayService::class);
$this->user = $this->createMock(IUser::class);

$this->plugin = new EnablePlugin($this->config, $this->birthdayService);
$this->plugin = new EnablePlugin($this->config, $this->birthdayService, $this->user);
$this->plugin->initialize($this->server);

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

$plugin = new EnablePlugin($this->config, $this->birthdayService);
$plugin = new EnablePlugin($this->config, $this->birthdayService, $this->user);

$server->expects($this->once())
->method('on')
Expand All @@ -89,7 +94,7 @@ public function testInitialize() {
$plugin->initialize($server);
}

public function testHttpPostNoCalendarHome() {
public function testHttpPostNoCalendarHome(): void {
$calendar = $this->createMock(Calendar::class);

$this->server->expects($this->once())
Expand All @@ -109,7 +114,7 @@ public function testHttpPostNoCalendarHome() {
$this->plugin->httpPost($this->request, $this->response);
}

public function testHttpPostWrongRequest() {
public function testHttpPostWrongRequest(): void {
$calendarHome = $this->createMock(CalendarHome::class);

$this->server->expects($this->once())
Expand All @@ -130,7 +135,7 @@ public function testHttpPostWrongRequest() {

$this->server->xml->expects($this->once())
->method('parse')
->willReturnCallback(function ($requestBody, $url, &$documentType) {
->willReturnCallback(function ($requestBody, $url, &$documentType): void {
$documentType = '{http://nextcloud.com/ns}disable-birthday-calendar';
});

Expand All @@ -143,7 +148,7 @@ public function testHttpPostWrongRequest() {
$this->plugin->httpPost($this->request, $this->response);
}

public function testHttpPost() {
public function testHttpPostNotAuthorized(): void {
$calendarHome = $this->createMock(CalendarHome::class);

$this->server->expects($this->once())
Expand All @@ -168,10 +173,63 @@ public function testHttpPost() {

$this->server->xml->expects($this->once())
->method('parse')
->willReturnCallback(function ($requestBody, $url, &$documentType) {
->willReturnCallback(function ($requestBody, $url, &$documentType): void {
$documentType = '{http://nextcloud.com/ns}enable-birthday-calendar';
});

$this->user->expects(self::once())
->method('getUID')
->willReturn('admin');

$this->server->httpResponse->expects($this->once())
->method('setStatus')
->with(403);

$this->config->expects($this->never())
->method('setUserValue');

$this->birthdayService->expects($this->never())
->method('syncUser');


$result = $this->plugin->httpPost($this->request, $this->response);

$this->assertEquals(false, $result);
}

public function testHttpPost(): void {
$calendarHome = $this->createMock(CalendarHome::class);

$this->server->expects($this->once())
->method('getRequestUri')
->willReturn('/bar/foo');
$this->server->tree->expects($this->once())
->method('getNodeForPath')
->with('/bar/foo')
->willReturn($calendarHome);

$calendarHome->expects($this->once())
->method('getOwner')
->willReturn('principals/users/BlaBlub');

$this->request->expects($this->once())
->method('getBodyAsString')
->willReturn('<nc:enable-birthday-calendar xmlns:nc="http://nextcloud.com/ns"/>');

$this->request->expects($this->once())
->method('getUrl')
->willReturn('url_abc');

$this->server->xml->expects($this->once())
->method('parse')
->willReturnCallback(function ($requestBody, $url, &$documentType): void {
$documentType = '{http://nextcloud.com/ns}enable-birthday-calendar';
});

$this->user->expects(self::exactly(3))
->method('getUID')
->willReturn('BlaBlub');

$this->config->expects($this->once())
->method('setUserValue')
->with('BlaBlub', 'dav', 'generateBirthdayCalendar', 'yes');
Expand Down

0 comments on commit 82ec950

Please sign in to comment.