-
-
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 <[email protected]>
- Loading branch information
1 parent
7538d66
commit d9ab32c
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\IAppConfig; | ||
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 IAppConfig $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->getValue(Application::APP_ID, 'card_size_limit', '5242880'); | ||
Check failure Code scanning / Psalm UndefinedInterfaceMethod Error
Method OCP\IAppConfig::getValue does not exist
Check failure on line 32 in apps/dav/lib/CardDAV/Validation/CardDavValidatePlugin.php GitHub Actions / static-code-analysisUndefinedInterfaceMethod
|
||
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\IAppConfig; | ||
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 IAppConfig|MockObject $config; | ||
private RequestInterface|MockObject $request; | ||
private ResponseInterface|MockObject $response; | ||
|
||
protected function setUp(): void { | ||
parent::setUp(); | ||
// construct mock objects | ||
$this->config = $this->createMock(IAppConfig::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('getValue') | ||
->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('getValue') | ||
->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); | ||
|
||
} | ||
|
||
} |