Skip to content

Commit 0a9cad0

Browse files
ernolfbackportbot[bot]
authored andcommitted
fix(share): Ensure unique share tokens
- 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>
1 parent 8446a1e commit 0a9cad0

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

lib/private/Share20/Manager.php

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

700-
// For now ignore a set token.
701-
$share->setToken(
702-
$this->secureRandom->generate(
700+
for ($i = 0; $i <= 3; $i++) {
701+
$token = $this->secureRandom->generate(
703702
\OC\Share\Constants::TOKEN_LENGTH,
704703
\OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
705-
)
706-
);
704+
);
705+
706+
try {
707+
$this->getShareByToken($token);
708+
} catch (\OCP\Share\Exceptions\ShareNotFound $e) {
709+
// Set the unique token
710+
$share->setToken($token);
711+
break;
712+
}
713+
714+
// Abort after 3 failed attempts
715+
if ($i >= 3) {
716+
throw new \Exception('Unable to generate a unique share token after 3 attempts.');
717+
}
718+
}
707719

708720
// Verify the expiration date
709721
$share = $this->validateExpirationDateLink($share);

0 commit comments

Comments
 (0)