Skip to content

Commit

Permalink
refactor: simplify token length logic based on suggestions from nickv…
Browse files Browse the repository at this point in the history
…ergessen

- restrict tokenLength to integers only
- consolidate tokenLength boundary checks using min/max

Signed-off-by: ernolf <raphael.gradenwitz@googlemail.com>
  • Loading branch information
ernolf committed Sep 26, 2024
1 parent c2a5406 commit ee24fdd
Showing 1 changed file with 4 additions and 20 deletions.
24 changes: 4 additions & 20 deletions lib/private/Share/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,27 +128,11 @@ public static function isSameUserOnSameServer($user1, $server1, $user2, $server2
}

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

// Check for 'min', 'max', or non-numeric input
if (strtolower($tokenLengthValue) === 'min') {
$tokenLength = self::MIN_TOKEN_LENGTH;
} elseif (strtolower($tokenLengthValue) === 'max') {
$tokenLength = self::MAX_TOKEN_LENGTH;
} elseif (!is_numeric($tokenLengthValue) || (int)$tokenLengthValue === 0) {
$tokenLength = self::DEFAULT_TOKEN_LENGTH;
} else {
$tokenLength = (int)$tokenLengthValue;
}
$config = \OCP\Server::get(\OCP\IAppConfig::class);
$tokenLength = $config->getValueInt('core', 'shareapi_token_length', self::DEFAULT_TOKEN_LENGTH);
$tokenLength = $tokenLength ?: self::DEFAULT_TOKEN_LENGTH;

// 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;
return max(self::MIN_TOKEN_LENGTH, min($tokenLength, self::MAX_TOKEN_LENGTH));
}
}

0 comments on commit ee24fdd

Please sign in to comment.