Skip to content

Commit d768d41

Browse files
authored
Merge pull request #62360 from nextcloud/fix/sharing/password-property-default-value-required
fix(PasswordSharePropertyType): Generate default password when passwords are required
2 parents e8afe36 + 948ed79 commit d768d41

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

core/Sharing/Property/PasswordSharePropertyType.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,29 @@
1010
namespace OC\Core\Sharing\Property;
1111

1212
use OC\Core\AppInfo\Application;
13+
use OCP\EventDispatcher\IEventDispatcher;
1314
use OCP\L10N\IFactory;
15+
use OCP\Security\Events\GenerateSecurePasswordEvent;
1416
use OCP\Security\IHasher;
17+
use OCP\Security\ISecureRandom;
18+
use OCP\Security\PasswordContext;
1519
use OCP\Share\IManager;
1620
use OCP\Sharing\Property\APasswordSharePropertyType;
1721
use OCP\Sharing\Property\ISharePropertyTypeFilter;
1822
use OCP\Sharing\Share;
1923
use OCP\Sharing\ShareAccessContext;
24+
use Random\Randomizer;
2025

2126
final class PasswordSharePropertyType extends APasswordSharePropertyType implements ISharePropertyTypeFilter {
2227

28+
private readonly Randomizer $randomizer;
29+
2330
public function __construct(
2431
private readonly IManager $legacyManager,
2532
private readonly IHasher $hasher,
33+
private readonly IEventDispatcher $eventDispatcher,
2634
) {
35+
$this->randomizer = new Randomizer();
2736
}
2837

2938
#[\Override]
@@ -54,7 +63,13 @@ public function isRequired(): bool {
5463

5564
#[\Override]
5665
public function getDefaultValue(): ?string {
57-
return null;
66+
if (!$this->isRequired()) {
67+
return null;
68+
}
69+
70+
$event = new GenerateSecurePasswordEvent(PasswordContext::SHARING);
71+
$this->eventDispatcher->dispatchTyped($event);
72+
return $event->getPassword() ?? $this->randomizer->getBytesFromString(ISecureRandom::CHAR_ALPHANUMERIC, 20);
5873
}
5974

6075
#[\Override]

tests/Core/Sharing/Property/PasswordSharePropertyTypeTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99

1010
namespace Tests\Core\Sharing\Property;
1111

12+
use OC\Core\AppInfo\Application;
13+
use OC\Core\AppInfo\ConfigLexicon;
1214
use OC\Core\Sharing\Property\PasswordSharePropertyType;
15+
use OCP\IAppConfig;
1316
use OCP\IUser;
1417
use OCP\IUserManager;
18+
use OCP\L10N\IFactory;
1519
use OCP\Security\IHasher;
1620
use OCP\Server;
1721
use OCP\Sharing\Property\ShareProperty;
@@ -64,6 +68,23 @@ private function createDummyShare(?ShareProperty $property): Share {
6468
);
6569
}
6670

71+
public function testGetDefaultValue(): void {
72+
$appConfig = Server::get(IAppConfig::class);
73+
$appConfig->deleteKey(Application::APP_ID, ConfigLexicon::SHARE_LINK_PASSWORD_ENFORCED);
74+
75+
$this->assertNull($this->propertyType->getDefaultValue());
76+
77+
$appConfig->setValueBool(Application::APP_ID, ConfigLexicon::SHARE_LINK_PASSWORD_ENFORCED, true);
78+
79+
$value = $this->propertyType->getDefaultValue();
80+
$this->assertNotNull($value);
81+
/** @psalm-suppress RedundantCastGivenDocblockType psalm:strict and rector:strict fight over the cast -_- */
82+
$this->assertGreaterThan(1, strlen((string)$value));
83+
$this->assertTrue($this->propertyType->validateValue(Server::get(IFactory::class), $value));
84+
85+
$appConfig->deleteKey(Application::APP_ID, ConfigLexicon::SHARE_LINK_PASSWORD_ENFORCED);
86+
}
87+
6788
public function testIsFiltered(): void {
6889
$this->assertFalse($this->propertyType->isFiltered(new ShareAccessContext(arguments: [$this->propertyType::class => '123']), $this->createDummyShare(new ShareProperty($this->propertyType::class, Server::get(IHasher::class)->hash('123')))));
6990
$this->assertFalse($this->propertyType->isFiltered(new ShareAccessContext(arguments: [$this->propertyType::class => '123']), $this->createDummyShare(new ShareProperty($this->propertyType::class, null))));

0 commit comments

Comments
 (0)