]> source.dussan.org Git - nextcloud-server.git/commitdiff
fix(share): Ensure unique share tokens backport/48142/stable29 48380/head
authorernolf <raphael.gradenwitz@googlemail.com>
Tue, 20 Aug 2024 10:33:28 +0000 (12:33 +0200)
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>
Thu, 26 Sep 2024 10:38:40 +0000 (10:38 +0000)
- check for token collisions and retry up to three times.
- throw after 3 attempts without finding a unique token.

Signed-off-by: ernolf <raphael.gradenwitz@googlemail.com>
lib/private/Share20/Manager.php

index 16ada8c6c7a18ac6dadb0eab093905919da1c840..bd35729cb3c8e7f762597b093df507c6f39b23ca 100644 (file)
@@ -697,13 +697,25 @@ class Manager implements IManager {
                                $this->linkCreateChecks($share);
                                $this->setLinkParent($share);
 
-                               // For now ignore a set token.
-                               $share->setToken(
-                                       $this->secureRandom->generate(
+                               for ($i = 0; $i <= 3; $i++) {
+                                       $token = $this->secureRandom->generate(
                                                \OC\Share\Constants::TOKEN_LENGTH,
                                                \OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
-                                       )
-                               );
+                                       );
+
+                                       try {
+                                               $this->getShareByToken($token);
+                                       } catch (\OCP\Share\Exceptions\ShareNotFound $e) {
+                                               // Set the unique token
+                                               $share->setToken($token);
+                                               break;
+                                       }
+
+                                       // Abort after 3 failed attempts
+                                       if ($i >= 3) {
+                                               throw new \Exception('Unable to generate a unique share token after 3 attempts.');
+                                       }
+                               }
 
                                // Verify the expiration date
                                $share = $this->validateExpirationDateLink($share);