Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions apps/sharebymail/lib/ShareByMailProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace OCA\ShareByMail;

use DateTime;
use OC\Share20\DefaultShareProvider;
use OC\Share20\Exception\InvalidShare;
use OC\Share20\Share;
Expand Down Expand Up @@ -98,14 +99,10 @@

// if the admin enforces a password for all mail shares we create a
// random password and send it to the recipient
$password = $share->getPassword() ?: '';
$passwordEnforced = $this->shareManager->shareApiLinkEnforcePassword();
if ($passwordEnforced && empty($password)) {
$password = $share->getPassword();
if ($password === null && $this->shareManager->shareApiLinkEnforcePassword()) {
$password = $this->autoGeneratePassword($share);
}

if (!empty($password)) {
$share->setPassword($this->hasher->hash($password));
$share->setPasswordHash($this->hasher->hash($password));
}

$shareId = $this->createMailShare($share);
Expand All @@ -116,9 +113,7 @@
// Temporary set the clear password again to send it by mail
// This need to be done after the share was created in the database
// as the password is hashed in between.
if (!empty($password)) {
$data['password'] = $password;
}
$data['password'] = $password;

return $this->createShareObject($data);
}
Expand Down Expand Up @@ -227,6 +222,11 @@
if ($share->getToken() === '') {
$share->setToken($this->generateToken());
}

if ($share->getPassword() !== null && !$share->isPasswordHashed()) {
throw new RuntimeException('The password must be hashed already.');

Check failure on line 227 in apps/sharebymail/lib/ShareByMailProvider.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedClass

apps/sharebymail/lib/ShareByMailProvider.php:227:14: UndefinedClass: Class, interface or enum named OCA\ShareByMail\RuntimeException does not exist (see https://psalm.dev/019)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new RuntimeException('The password must be hashed already.');
throw new \RuntimeException('The password must be hashed already.');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is another instance of the same typo in the file

}

return $this->addShareToDB(
$share->getNodeId(),
$share->getNodeType(),
Expand All @@ -241,6 +241,7 @@
$share->getHideDownload(),
$share->getLabel(),
$share->getExpirationDate(),
$share->getShareTime(),
$share->getNote(),
$share->getAttributes(),
$share->getMailSend(),
Expand Down Expand Up @@ -699,6 +700,7 @@
?bool $hideDownload,
?string $label,
?\DateTimeInterface $expirationTime,
?DateTime $shareTime,
?string $note = '',
?IAttributes $attributes = null,
?bool $mailSend = true,
Expand All @@ -717,7 +719,7 @@
->setValue('password', $qb->createNamedParameter($password))
->setValue('password_expiration_time', $qb->createNamedParameter($passwordExpirationTime, IQueryBuilder::PARAM_DATETIME_MUTABLE))
->setValue('password_by_talk', $qb->createNamedParameter($sendPasswordByTalk, IQueryBuilder::PARAM_BOOL))
->setValue('stime', $qb->createNamedParameter(time()))
->setValue('stime', $qb->createNamedParameter($shareTime?->getTimestamp() ?? time()))
->setValue('hide_download', $qb->createNamedParameter((int)$hideDownload, IQueryBuilder::PARAM_INT))
->setValue('label', $qb->createNamedParameter($label))
->setValue('note', $qb->createNamedParameter($note))
Expand Down Expand Up @@ -767,6 +769,11 @@
$expiration = \DateTime::createFromInterface($expiration);
$expiration->setTimezone(new \DateTimeZone(date_default_timezone_get()));
}

if ($share->getPassword() !== null && !$share->isPasswordHashed()) {
throw new RuntimeException('The password must be hashed already.');

Check failure on line 774 in apps/sharebymail/lib/ShareByMailProvider.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedClass

apps/sharebymail/lib/ShareByMailProvider.php:774:14: UndefinedClass: Class, interface or enum named OCA\ShareByMail\RuntimeException does not exist (see https://psalm.dev/019)
}

$qb->update('share')
->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
->set('item_source', $qb->createNamedParameter($share->getNodeId()))
Expand Down Expand Up @@ -1052,7 +1059,9 @@
$shareTime->setTimestamp((int)$data['stime']);
$share->setShareTime($shareTime);
$share->setSharedWith($data['share_with'] ?? '');
$share->setPassword($data['password']);
if (($password = $data['password']) !== null) {
$share->setPasswordHash($password);
}
$passwordExpirationTime = \DateTime::createFromFormat('Y-m-d H:i:s', $data['password_expiration_time'] ?? '');
$share->setPasswordExpirationTime($passwordExpirationTime !== false ? $passwordExpirationTime : null);
$share->setLabel($data['label'] ?? '');
Expand Down
4 changes: 2 additions & 2 deletions apps/sharing/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
xsi:noNamespaceSchemaLocation="https://apps.nextcloud.com/schema/apps/info.xsd">
<id>sharing</id>
<name>Sharing</name>
<summary>TODO</summary>
<description>TODO</description>
<summary>This app provides APIs and occ commands to manage shares.</summary>
<description>This app provides APIs and occ commands to manage shares.</description>
<version>2.0.0-dev.3</version>
<licence>AGPL-3.0-or-later</licence>
<author>Kate Döen</author>
Expand Down
1 change: 0 additions & 1 deletion apps/sharing/lib/Command/AddShareRecipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

// TODO: Initiator missing.
final class AddShareRecipient extends SharingBase {
#[\Override]
public function configure(): void {
Expand Down
2 changes: 1 addition & 1 deletion apps/sharing/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"info": {
"title": "sharing",
"version": "0.0.1",
"description": "TODO",
"description": "This app provides APIs and occ commands to manage shares.",
"license": {
"name": "AGPL-3.0-or-later"
}
Expand Down
19 changes: 0 additions & 19 deletions build/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<file src="apps/cloud_federation_api/lib/Controller/RequestHandlerController.php">
<DeprecatedMethod>
<code><![CDATA[getFederationIdFromSharedSecret]]></code>
<code><![CDATA[getFederationIdFromSharedSecret]]></code>
</DeprecatedMethod>
</file>
<file src="apps/comments/lib/Activity/Listener.php">
Expand Down Expand Up @@ -201,32 +200,17 @@
<code><![CDATA[null]]></code>
</NullableReturnStatement>
<UndefinedMagicPropertyFetch>
<code><![CDATA[$component->CLASS]]></code>
<code><![CDATA[$component->CLASS]]></code>
<code><![CDATA[$component->CLASS]]></code>
<code><![CDATA[$component->DTEND]]></code>
<code><![CDATA[$component->DTEND]]></code>
<code><![CDATA[$component->DTEND]]></code>
<code><![CDATA[$component->DTSTART]]></code>
<code><![CDATA[$component->DTSTART]]></code>
<code><![CDATA[$component->DTSTART]]></code>
<code><![CDATA[$component->DUE]]></code>
<code><![CDATA[$component->DUE]]></code>
<code><![CDATA[$component->DUE]]></code>
<code><![CDATA[$component->DURATION]]></code>
<code><![CDATA[$component->DURATION]]></code>
<code><![CDATA[$component->DURATION]]></code>
<code><![CDATA[$component->RDATE]]></code>
<code><![CDATA[$component->RDATE]]></code>
<code><![CDATA[$component->RDATE]]></code>
<code><![CDATA[$component->RDATE]]></code>
<code><![CDATA[$component->RDATE]]></code>
<code><![CDATA[$component->RDATE]]></code>
<code><![CDATA[$component->RRULE]]></code>
<code><![CDATA[$component->RRULE]]></code>
<code><![CDATA[$component->RRULE]]></code>
<code><![CDATA[$component->UID]]></code>
<code><![CDATA[$component->UID]]></code>
<code><![CDATA[$component->UID]]></code>
</UndefinedMagicPropertyFetch>
</file>
Expand Down Expand Up @@ -2423,9 +2407,6 @@
<code><![CDATA[deleteAppValue]]></code>
<code><![CDATA[deleteAppValue]]></code>
<code><![CDATA[getAppValue]]></code>
<code><![CDATA[getAppValue]]></code>
<code><![CDATA[getAppValue]]></code>
<code><![CDATA[getAppValue]]></code>
<code><![CDATA[setAppValue]]></code>
<code><![CDATA[setAppValue]]></code>
</DeprecatedMethod>
Expand Down
6 changes: 3 additions & 3 deletions core/Sharing/Recipient/GroupShareRecipientType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\EventDispatcher\IEventListener;
use OCP\Group\Events\GroupDeletedEvent;
use OCP\Group\Events\BeforeGroupDeletedEvent;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\Interaction\InteractionReceiver;
Expand All @@ -30,7 +30,7 @@
use OCP\Share\IShare;

/**
* @template-implements IEventListener<GroupDeletedEvent>
* @template-implements IEventListener<BeforeGroupDeletedEvent>
*/
final class GroupShareRecipientType extends AShareRecipientTypeSearchCollaborator implements IEventListener {
public function __construct(
Expand All @@ -39,7 +39,7 @@ public function __construct(
private readonly IGroupManager $groupManager,
private readonly ISharingManager $manager,
) {
$eventDispatcher->addServiceListener(GroupDeletedEvent::class, self::class);
$eventDispatcher->addServiceListener(BeforeGroupDeletedEvent::class, self::class);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is that a before deletion listener?
If deletion fails you still deleted the data.
Should be listener on groupdeletedevent with a high priority, no?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same remark for the user listener

}

#[\Override]
Expand Down
6 changes: 3 additions & 3 deletions core/Sharing/Recipient/UserShareRecipientType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Share\IShare;
use OCP\User\Events\UserDeletedEvent;
use OCP\User\Events\BeforeUserDeletedEvent;

/**
* @template-implements IEventListener<UserDeletedEvent>
* @template-implements IEventListener<BeforeUserDeletedEvent>
*/
final class UserShareRecipientType extends AShareRecipientTypeSearchCollaborator implements IEventListener {

Expand All @@ -39,7 +39,7 @@ public function __construct(
private readonly IUserManager $userManager,
private readonly ISharingManager $manager,
) {
$eventDispatcher->addServiceListener(UserDeletedEvent::class, self::class);
$eventDispatcher->addServiceListener(BeforeUserDeletedEvent::class, self::class);
}

#[\Override]
Expand Down
1 change: 0 additions & 1 deletion lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2366,7 +2366,6 @@
'OC\\Share20\\UserRemovedListener' => $baseDir . '/lib/private/Share20/UserRemovedListener.php',
'OC\\Share\\Constants' => $baseDir . '/lib/private/Share/Constants.php',
'OC\\Sharing\\ClassMapper' => $baseDir . '/lib/private/Sharing/ClassMapper.php',
'OC\\Sharing\\ISharingLegacyBackend' => $baseDir . '/lib/private/Sharing/ISharingLegacyBackend.php',
'OC\\Sharing\\SharingBackend' => $baseDir . '/lib/private/Sharing/SharingBackend.php',
'OC\\Sharing\\SharingManager' => $baseDir . '/lib/private/Sharing/SharingManager.php',
'OC\\Sharing\\SharingRegistry' => $baseDir . '/lib/private/Sharing/SharingRegistry.php',
Expand Down
1 change: 0 additions & 1 deletion lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -2407,7 +2407,6 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Share20\\UserRemovedListener' => __DIR__ . '/../../..' . '/lib/private/Share20/UserRemovedListener.php',
'OC\\Share\\Constants' => __DIR__ . '/../../..' . '/lib/private/Share/Constants.php',
'OC\\Sharing\\ClassMapper' => __DIR__ . '/../../..' . '/lib/private/Sharing/ClassMapper.php',
'OC\\Sharing\\ISharingLegacyBackend' => __DIR__ . '/../../..' . '/lib/private/Sharing/ISharingLegacyBackend.php',
'OC\\Sharing\\SharingBackend' => __DIR__ . '/../../..' . '/lib/private/Sharing/SharingBackend.php',
'OC\\Sharing\\SharingManager' => __DIR__ . '/../../..' . '/lib/private/Sharing/SharingManager.php',
'OC\\Sharing\\SharingRegistry' => __DIR__ . '/../../..' . '/lib/private/Sharing/SharingRegistry.php',
Expand Down
5 changes: 0 additions & 5 deletions lib/private/Group/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ public function setDisplayName(string $displayName): bool {
return false;
}

/**
* get all users in the group
*
* @return array<string, IUser>
*/
#[\Override]
public function getUsers(): array {
if ($this->usersLoaded) {
Expand Down
30 changes: 21 additions & 9 deletions lib/private/Share20/DefaultShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
use OCP\Share\IShareProviderWithNotification;
use OCP\Util;
use Psr\Log\LoggerInterface;
use RuntimeException;
use function str_starts_with;
use function strlen;

Expand Down Expand Up @@ -110,7 +111,10 @@ public function create(IShare $share) {
if ($share->getShareType() === IShare::TYPE_USER) {
//Set the UID of the user we share with
$qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
$qb->setValue('accepted', $qb->createNamedParameter(IShare::STATUS_PENDING));
if ($share->getStatus() === null) {
$share->setStatus(IShare::STATUS_PENDING);
}
$qb->setValue('accepted', $qb->createNamedParameter($share->getStatus()));

//If an expiration date is set store it
if ($expirationDate !== null) {
Expand All @@ -133,8 +137,11 @@ public function create(IShare $share) {
$qb->setValue('token', $qb->createNamedParameter($share->getToken()));

//If a password is set store it
if ($share->getPassword() !== null) {
$qb->setValue('password', $qb->createNamedParameter($share->getPassword()));
if (($password = $share->getPassword()) !== null) {
if (!$share->isPasswordHashed()) {
throw new RuntimeException('The password must be hashed already.');
}
$qb->setValue('password', $qb->createNamedParameter($password));
}

$qb->setValue('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL));
Expand Down Expand Up @@ -185,9 +192,9 @@ public function create(IShare $share) {
$qb->setValue('note', $qb->createNamedParameter($share->getNote()));
}

// Set the time this share was created
$shareTime = $this->timeFactory->now();
$shareTime = $share->getShareTime() ?? \DateTime::createFromImmutable($this->timeFactory->now());
$qb->setValue('stime', $qb->createNamedParameter($shareTime->getTimestamp()));
$share->setShareTime($shareTime);

// insert the data and fetch the id of the share
$qb->executeStatement();
Expand All @@ -197,8 +204,6 @@ public function create(IShare $share) {
$share->setId((string)$id);
$share->setProviderId($this->identifier());

$share->setShareTime(\DateTime::createFromImmutable($shareTime));

$mailSendValue = $share->getMailSend();
$share->setMailSend(($mailSendValue === null) ? true : $mailSendValue);

Expand Down Expand Up @@ -287,10 +292,15 @@ public function update(IShare $share) {
->set('attributes', $qb->createNamedParameter($shareAttributes))
->executeStatement();
} elseif ($share->getShareType() === IShare::TYPE_LINK) {
$password = $share->getPassword();
if ($password !== null && !$share->isPasswordHashed()) {
throw new RuntimeException('The password must be hashed already.');
}

$qb = $this->dbConn->getQueryBuilder();
$qb->update('share')
->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
->set('password', $qb->createNamedParameter($share->getPassword()))
->set('password', $qb->createNamedParameter($password))
->set('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL))
->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
Expand Down Expand Up @@ -1129,7 +1139,9 @@ private function createShare($data): IShare {
$share->setSharedWith($data['share_with']);
$share->setSharedWithDisplayNameCallback(fn (IShare $share) => $this->groupManager->getDisplayName($share->getSharedWith()));
} elseif ($share->getShareType() === IShare::TYPE_LINK) {
$share->setPassword($data['password']);
if (($password = $data['password']) !== null) {
$share->setPasswordHash($password);
}
$share->setSendPasswordByTalk((bool)$data['password_by_talk']);
$share->setToken($data['token']);
}
Expand Down
22 changes: 14 additions & 8 deletions lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,8 @@ public function createShare(IShare $share): IShare {
$this->verifyPassword($share->getPassword());

// If a password is set. Hash it!
if ($share->getShareType() === IShare::TYPE_LINK
&& $share->getPassword() !== null) {
$share->setPassword($this->hasher->hash($share->getPassword()));
if (($share->getShareType() === IShare::TYPE_LINK || $share->getShareType() === IShare::TYPE_EMAIL) && $share->getPassword() !== null && !$share->isPasswordHashed()) {
$share->setPasswordHash($this->hasher->hash($share->getPassword()));
}
}

Expand All @@ -588,9 +587,11 @@ public function createShare(IShare $share): IShare {
}
}

$target = $shareFolder . '/' . $share->getNode()->getName();
$target = Filesystem::normalizePath($target);
$share->setTarget($target);
if ($share->getTarget() === null) {
$target = $shareFolder . '/' . $share->getNode()->getName();
$target = Filesystem::normalizePath($target);
$share->setTarget($target);
}

// Pre share event
$event = new BeforeShareCreatedEvent($share);
Expand Down Expand Up @@ -832,7 +833,7 @@ private function updateSharePasswordIfNeeded(IShare $share, IShare $originalShar

// If a password is set. Hash it!
if (!empty($share->getPassword())) {
$share->setPassword($this->hasher->hash($share->getPassword()));
$share->setPasswordHash($this->hasher->hash($share->getPassword()));
if ($share->getShareType() === IShare::TYPE_EMAIL) {
// Shares shared by email have temporary passwords
$this->setSharePasswordExpirationTime($share);
Expand All @@ -850,7 +851,12 @@ private function updateSharePasswordIfNeeded(IShare $share, IShare $originalShar
} else {
// Reset the password to the original one, as it is either the same
// as the "new" password or a hashed version of it.
$share->setPassword($originalShare->getPassword());
$password = $originalShare->getPassword();
if ($password !== null && $originalShare->isPasswordHashed()) {
$share->setPasswordHash($password);
} else {
$share->setPassword($password);
}
}

return false;
Expand Down
Loading
Loading