Repository navigation
fix(dav): store webcal subscription credentials encrypted #65705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SebastianKrupinski
wants to merge
2
commits into
master
Choose a base branch
from
fix/subscription-credentials
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,76 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\DAV\Migration; | ||
|
|
||
| use Closure; | ||
| use OCA\DAV\CalDAV\CalDavBackend; | ||
| use OCP\DB\ISchemaWrapper; | ||
| use OCP\DB\QueryBuilder\IQueryBuilder; | ||
| use OCP\DB\Types; | ||
| use OCP\IDBConnection; | ||
| use OCP\Migration\Attributes\AddColumn; | ||
| use OCP\Migration\Attributes\ColumnType; | ||
| use OCP\Migration\IOutput; | ||
| use OCP\Migration\SimpleMigrationStep; | ||
| use Override; | ||
|
|
||
| #[AddColumn(table: 'calendarsubscriptions', name: 'credentials', type: ColumnType::TEXT, description: 'Encrypted credentials extracted from the source url')] | ||
| class Version3000Date20261019000000 extends SimpleMigrationStep { | ||
| public function __construct( | ||
| private readonly IDBConnection $connection, | ||
| ) { | ||
| } | ||
|
|
||
| #[Override] | ||
| public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
| /** @var ISchemaWrapper $schema */ | ||
| $schema = $schemaClosure(); | ||
|
|
||
| $table = $schema->getTable('calendarsubscriptions'); | ||
| if ($table->hasColumn('credentials')) { | ||
| return null; | ||
| } | ||
|
|
||
| $table->addColumn('credentials', Types::TEXT, [ | ||
| 'notnull' => false, | ||
| ]); | ||
|
|
||
| return $schema; | ||
| } | ||
|
|
||
| #[Override] | ||
| public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { | ||
| $select = $this->connection->getQueryBuilder(); | ||
| $select->select('id', 'source') | ||
| ->from('calendarsubscriptions') | ||
| ->where($select->expr()->like('source', $select->createNamedParameter('%@%'))) | ||
| ->andWhere($select->expr()->isNull('credentials')); | ||
| $result = $select->executeQuery(); | ||
|
|
||
| $update = $this->connection->getQueryBuilder(); | ||
| $update->update('calendarsubscriptions') | ||
| ->set('source', $update->createParameter('source')) | ||
| ->set('credentials', $update->createParameter('credentials')) | ||
| ->where($update->expr()->eq('id', $update->createParameter('id'))); | ||
|
|
||
| while ($row = $result->fetchAssociative()) { | ||
| [$source, $credentials] = CalDavBackend::extractSubscriptionCredentials((string)$row['source']); | ||
| if ($credentials === null) { | ||
| continue; | ||
| } | ||
|
|
||
| $update->setParameter('source', $source) | ||
| ->setParameter('credentials', $credentials) | ||
| ->setParameter('id', (int)$row['id'], IQueryBuilder::PARAM_INT) | ||
| ->executeStatement(); | ||
| } | ||
| $result->closeCursor(); | ||
| } | ||
| } | ||
61 changes: 61 additions & 0 deletions
61
apps/dav/tests/unit/CalDAV/CalDavBackendSubscriptionCredentialsTest.php
This file contains hidden or 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,61 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\DAV\Tests\unit\CalDAV; | ||
|
|
||
| use OCA\DAV\CalDAV\CalDavBackend; | ||
| use OCP\Security\ICrypto; | ||
| use OCP\Server; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Test\TestCase; | ||
|
|
||
| class CalDavBackendSubscriptionCredentialsTest extends TestCase { | ||
| public static function sourceWithCredentialsProvider(): array { | ||
| return [ | ||
| 'plain' => ['https://user:pass@example.com/cal.ics', 'https://example.com/cal.ics', 'user', 'pass'], | ||
| 'webcal' => ['webcal://user:pass@example.com/cal.ics', 'webcal://example.com/cal.ics', 'user', 'pass'], | ||
| 'port, query and fragment' => ['https://user:pass@example.com:8443/cal.ics?a=b@c#frag', 'https://example.com:8443/cal.ics?a=b@c#frag', 'user', 'pass'], | ||
| 'percent-encoded' => ['https://us%40er:p%23ss%3Aw%2Frd%25@example.com/cal.ics', 'https://example.com/cal.ics', 'us@er', 'p#ss:w/rd%'], | ||
| 'plus is literal' => ['https://user:pa+ss@example.com/cal.ics', 'https://example.com/cal.ics', 'user', 'pa+ss'], | ||
| 'utf-8' => ['https://user:p%C3%A4ss@example.com/cal.ics', 'https://example.com/cal.ics', 'user', 'päss'], | ||
| 'unencoded colon in password' => ['https://user:pa:ss@example.com/cal.ics', 'https://example.com/cal.ics', 'user', 'pa:ss'], | ||
| 'unencoded at sign in username' => ['https://user@domain.tld:pass@example.com/cal.ics', 'https://example.com/cal.ics', 'user@domain.tld', 'pass'], | ||
| 'username only' => ['https://user@example.com/cal.ics', 'https://example.com/cal.ics', 'user', ''], | ||
| 'password only' => ['https://:pass@example.com/cal.ics', 'https://example.com/cal.ics', '', 'pass'], | ||
| ]; | ||
| } | ||
|
|
||
| #[DataProvider('sourceWithCredentialsProvider')] | ||
| public function testExtractWithCredentials(string $source, string $expectedSource, string $expectedUsername, string $expectedPassword): void { | ||
| [$strippedSource, $credentials] = CalDavBackend::extractSubscriptionCredentials($source); | ||
|
|
||
| $this->assertSame($expectedSource, $strippedSource); | ||
| $this->assertIsString($credentials); | ||
| $this->assertSame( | ||
| ['username' => $expectedUsername, 'password' => $expectedPassword], | ||
| json_decode(Server::get(ICrypto::class)->decrypt($credentials), true), | ||
| ); | ||
| } | ||
|
|
||
| public static function sourceWithoutCredentialsProvider(): array { | ||
| return [ | ||
| 'no user info' => ['https://example.com/cal.ics', 'https://example.com/cal.ics'], | ||
| 'at sign in path' => ['https://example.com/user@domain/cal.ics', 'https://example.com/user@domain/cal.ics'], | ||
| 'at sign in query' => ['https://example.com/cal.ics?user=a@b', 'https://example.com/cal.ics?user=a@b'], | ||
| 'empty user info' => ['https://@example.com/cal.ics', 'https://example.com/cal.ics'], | ||
| 'empty username and password' => ['https://:@example.com/cal.ics', 'https://example.com/cal.ics'], | ||
| 'no scheme' => ['user:pass@example.com/cal.ics', 'user:pass@example.com/cal.ics'], | ||
| ]; | ||
| } | ||
|
|
||
| #[DataProvider('sourceWithoutCredentialsProvider')] | ||
| public function testExtractWithoutCredentials(string $source, string $expectedSource): void { | ||
| $this->assertSame([$expectedSource, null], CalDavBackend::extractSubscriptionCredentials($source)); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.