-
-
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.
Signed-off-by: SebastianKrupinski <krupinskis05@gmail.com>
- Loading branch information
1 parent
974fd52
commit cce9558
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\CardDAV\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 CardDavValidatePlugin 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 | ||
$cardSizeLimit = (int) $this->config->getAppValue(Application::APP_ID, 'card_size_limit', '5242880'); | ||
if ((int) $request->getRawServerValue('CONTENT_LENGTH') > $cardSizeLimit) { | ||
throw new Forbidden("VCard object exceeds $cardSizeLimit 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/CardDAV/Validation/CardDavValidatePluginTest.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\CardDAV\Validation; | ||
|
||
use OCA\DAV\CardDAV\Validation\CardDavValidatePlugin; | ||
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 CardDavValidatePluginTest extends TestCase { | ||
|
||
private CardDavValidatePlugin $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 CardDavValidatePlugin( | ||
$this->config, | ||
); | ||
} | ||
|
||
public function testPutSizeLessThenLimit(): void { | ||
|
||
// construct method responses | ||
$this->config | ||
->method('getAppValue') | ||
->with('dav', 'card_size_limit', '5242880') | ||
->willReturn('5242880'); | ||
$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', 'card_size_limit', '5242880') | ||
->willReturn('5242880'); | ||
$this->request | ||
->method('getRawServerValue') | ||
->with('CONTENT_LENGTH') | ||
->willReturn('6242880'); | ||
$this->expectException(Forbidden::class); | ||
// test condition | ||
$this->plugin->beforePut($this->request, $this->response); | ||
|
||
} | ||
|
||
} |