Skip to content

Commit 4c65ee9

Browse files
committed
feat(share): make sharelink token length configurable
Signed-off-by: ernolf <raphael.gradenwitz@googlemail.com>
1 parent 601b3b1 commit 4c65ee9

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

lib/private/Share/Constants.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ class Constants {
6363

6464
public const RESPONSE_FORMAT = 'json'; // default response format for ocs calls
6565

66-
public const TOKEN_LENGTH = 15; // old (oc7) length is 32, keep token length in db at least that for compatibility
66+
public const MIN_TOKEN_LENGTH = 4; // 14,776,336 different possible variations
67+
public const DEFAULT_TOKEN_LENGTH = 15; // 768,909,704,948,766,668,552,634,368 different possible variations
68+
public const MAX_TOKEN_LENGTH = 32; // 2,272,657,884,496,751,345,355,241,563,627,544,170,162,852,933,518,655,225,856 different possible variations
69+
public const TOKEN_LENGTH = self::DEFAULT_TOKEN_LENGTH; // old (oc7) length is 32, keep token length in db at least that for compatibility
6770

6871
protected static $shareTypeUserAndGroups = -1;
6972
protected static $shareTypeGroupUserUnique = 2;

lib/private/Share/Helper.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,18 @@ public static function isSameUserOnSameServer($user1, $server1, $user2, $server2
126126

127127
return false;
128128
}
129+
130+
public static function getTokenLength(): int {
131+
$config = \OC::$server->getConfig();
132+
$tokenLength = (int)$config->getAppValue('core', 'shareapi_token_length', self::DEFAULT_TOKEN_LENGTH);
133+
134+
// Token length should be within the defined min and max limits
135+
if ($tokenLength < self::MIN_TOKEN_LENGTH) {
136+
$tokenLength = self::MIN_TOKEN_LENGTH;
137+
} elseif ($tokenLength > self::MAX_TOKEN_LENGTH) {
138+
$tokenLength = self::MAX_TOKEN_LENGTH;
139+
}
140+
141+
return $tokenLength;
142+
}
129143
}

lib/private/Share20/Manager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ public function createShare(IShare $share) {
667667
// For now ignore a set token.
668668
$share->setToken(
669669
$this->secureRandom->generate(
670-
\OC\Share\Constants::TOKEN_LENGTH,
670+
\OC\Share\Constants\Helper::getTokenLength(),
671671
\OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
672672
)
673673
);

0 commit comments

Comments
 (0)