From 32884fd8675eaeb8797b3d459f0bc5382cf09136 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Mon, 27 Mar 2023 20:57:10 +0200 Subject: [PATCH] shorten public links to 8 characters Signed-off-by: dartcafe --- CHANGELOG.md | 1 + lib/Service/ShareService.php | 30 ++++++++++++++++++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31d31e8d1..215d35a53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. - Added qr code for public shares ### Changes - PHP 8.0 as minimum requirement + - Shorten public tokens to 8 characters (lower and upper characters and digits) ## [4.1.5] - 2023-02-25 ### Fix diff --git a/lib/Service/ShareService.php b/lib/Service/ShareService.php index c540c4be6..bb4fba6f9 100644 --- a/lib/Service/ShareService.php +++ b/lib/Service/ShareService.php @@ -422,12 +422,30 @@ class ShareService { string $timeZone = '' ): Share { $preventInvitation = $userGroup->getType() === UserBase::TYPE_PUBLIC ?: $preventInvitation; - $token = $this->secureRandom->generate( - 16, - ISecureRandom::CHAR_DIGITS . - ISecureRandom::CHAR_LOWER . - ISecureRandom::CHAR_UPPER - ); + + $token = null; + $loopCounter = 0; + // Generate a unique id + while (!$token) { + $loopCounter++; + $token = $this->secureRandom->generate( + 8, + ISecureRandom::CHAR_DIGITS . + ISecureRandom::CHAR_LOWER . + ISecureRandom::CHAR_UPPER + ); + try { + $this->shareMapper->findByToken($token); + // reset token, if it already exists + $token = null; + } catch (ShareNotFoundException) { + $loopCounter = 0; + } + if ($loopCounter > 10) { + // In case of uninspected situations, avoid an endless loop + throw new \Exception('Unexpected loop count while trying to create a token for a new share'); + } + } $this->share = new Share(); $this->share->setToken($token);