Skip to content

Commit 1dbd72d

Browse files
Merge pull request #48379 from nextcloud/backport/48142/stable28
[stable28] feat(share): ensure unique share tokens with dynamic length adjustment
2 parents 80d5cde + 614210c commit 1dbd72d

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

lib/private/Share20/Manager.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -689,13 +689,25 @@ public function createShare(IShare $share) {
689689
$this->linkCreateChecks($share);
690690
$this->setLinkParent($share);
691691

692-
// For now ignore a set token.
693-
$share->setToken(
694-
$this->secureRandom->generate(
692+
for ($i = 0; $i <= 3; $i++) {
693+
$token = $this->secureRandom->generate(
695694
\OC\Share\Constants::TOKEN_LENGTH,
696695
\OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
697-
)
698-
);
696+
);
697+
698+
try {
699+
$this->getShareByToken($token);
700+
} catch (\OCP\Share\Exceptions\ShareNotFound $e) {
701+
// Set the unique token
702+
$share->setToken($token);
703+
break;
704+
}
705+
706+
// Abort after 3 failed attempts
707+
if ($i >= 3) {
708+
throw new \Exception('Unable to generate a unique share token after 3 attempts.');
709+
}
710+
}
699711

700712
// Verify the expiration date
701713
$share = $this->validateExpirationDateLink($share);

0 commit comments

Comments
 (0)