-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46426 from nextcloud/backport/46315/stable27-2
[stable27] fix(caldav): limit vevent size
- Loading branch information
Showing
6 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
namespace OCA\DAV\CalDAV\Validation; | ||
|
||
use OCA\DAV\AppInfo\Application; | ||
use OCP\IConfig; | ||
use Sabre\DAV\Exception\Forbidden; | ||
use Sabre\DAV\Server; | ||
use Sabre\DAV\ServerPlugin; | ||
use Sabre\HTTP\RequestInterface; | ||
use Sabre\HTTP\ResponseInterface; | ||
|
||
class CalDavValidatePlugin extends ServerPlugin { | ||
|
||
public function __construct( | ||
private IConfig $config | ||
) { | ||
} | ||
|
||
public function initialize(Server $server): void { | ||
$server->on('beforeMethod:PUT', [$this, 'beforePut']); | ||
} | ||
|
||
public function beforePut(RequestInterface $request, ResponseInterface $response): bool { | ||
// evaluate if card size exceeds defined limit | ||
$eventSizeLimit = $this->config->getAppValue(Application::APP_ID, 'event_size_limit', '10485760'); | ||
if ((int) $request->getRawServerValue('CONTENT_LENGTH') > $eventSizeLimit) { | ||
throw new Forbidden("VEvent or VTodo object exceeds $eventSizeLimit bytes"); | ||
} | ||
// all tests passed return true | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
apps/dav/tests/unit/CalDAV/Validation/CalDavValidatePluginTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\DAV\Tests\unit\CalDAV\Validation; | ||
|
||
use OCA\DAV\CalDAV\Validation\CalDavValidatePlugin; | ||
use OCP\IConfig; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Sabre\DAV\Exception\Forbidden; | ||
use Sabre\HTTP\RequestInterface; | ||
use Sabre\HTTP\ResponseInterface; | ||
use Test\TestCase; | ||
|
||
class CalDavValidatePluginTest extends TestCase { | ||
|
||
private CalDavValidatePlugin $plugin; | ||
private IConfig|MockObject $config; | ||
private RequestInterface|MockObject $request; | ||
private ResponseInterface|MockObject $response; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
// construct mock objects | ||
$this->config = $this->createMock(IConfig::class); | ||
$this->request = $this->createMock(RequestInterface::class); | ||
$this->response = $this->createMock(ResponseInterface::class); | ||
$this->plugin = new CalDavValidatePlugin( | ||
$this->config, | ||
); | ||
} | ||
|
||
public function testPutSizeLessThenLimit(): void { | ||
|
||
// construct method responses | ||
$this->config | ||
->method('getAppValue') | ||
->with('dav', 'event_size_limit', '10485760') | ||
->willReturn(10485760); | ||
$this->request | ||
->method('getRawServerValue') | ||
->with('CONTENT_LENGTH') | ||
->willReturn('1024'); | ||
// test condition | ||
$this->assertTrue( | ||
$this->plugin->beforePut($this->request, $this->response) | ||
); | ||
|
||
} | ||
|
||
public function testPutSizeMoreThenLimit(): void { | ||
|
||
// construct method responses | ||
$this->config | ||
->method('getAppValue') | ||
->with('dav', 'event_size_limit', '10485760') | ||
->willReturn(10485760); | ||
$this->request | ||
->method('getRawServerValue') | ||
->with('CONTENT_LENGTH') | ||
->willReturn('16242880'); | ||
$this->expectException(Forbidden::class); | ||
// test condition | ||
$this->plugin->beforePut($this->request, $this->response); | ||
|
||
} | ||
|
||
} |