Skip to content

Commit

Permalink
feat(share): make sharelink token length configurable
Browse files Browse the repository at this point in the history
Signed-off-by: ernolf <raphael.gradenwitz@googlemail.com>
  • Loading branch information
ernolf committed Aug 15, 2024
1 parent 601b3b1 commit 4b10b32
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/private/Share/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ class Constants {

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

public const TOKEN_LENGTH = 15; // old (oc7) length is 32, keep token length in db at least that for compatibility
public const MIN_TOKEN_LENGTH = 4; // 14,776,336 different possible variations
public const DEFAULT_TOKEN_LENGTH = 15; // 768,909,704,948,766,668,552,634,368 different possible variations
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
public const TOKEN_LENGTH = self::DEFAULT_TOKEN_LENGTH; // old (oc7) length is 32, keep token length in db at least that for compatibility

protected static $shareTypeUserAndGroups = -1;
protected static $shareTypeGroupUserUnique = 2;
Expand Down
14 changes: 14 additions & 0 deletions lib/private/Share/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,18 @@ public static function isSameUserOnSameServer($user1, $server1, $user2, $server2

return false;
}

public static function getTokenLength(): int {
$config = \OC::$server->getConfig();
$tokenLength = (int)$config->getAppValue('core', 'shareapi_token_length', self::DEFAULT_TOKEN_LENGTH);

Check failure on line 132 in lib/private/Share/Helper.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidArgument

lib/private/Share/Helper.php:132:77: InvalidArgument: Argument 3 of OCP\IConfig::getAppValue expects string, but 15 provided (see https://psalm.dev/004)

Check failure

Code scanning / Psalm

InvalidArgument Error

Argument 3 of OCP\IConfig::getAppValue expects string, but 15 provided

// Token length should be within the defined min and max limits
if ($tokenLength < self::MIN_TOKEN_LENGTH) {
$tokenLength = self::MIN_TOKEN_LENGTH;
} elseif ($tokenLength > self::MAX_TOKEN_LENGTH) {
$tokenLength = self::MAX_TOKEN_LENGTH;
}

return $tokenLength;
}
}
2 changes: 1 addition & 1 deletion lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ public function createShare(IShare $share) {
// For now ignore a set token.
$share->setToken(
$this->secureRandom->generate(
\OC\Share\Constants::TOKEN_LENGTH,
\OC\Share\Helper::getTokenLength(),
\OCP\Security\ISecureRandom::CHAR_HUMAN_READABLE
)
);
Expand Down

0 comments on commit 4b10b32

Please sign in to comment.