Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions apps/files_sharing/lib/Controller/ShareAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
*/
class ShareAPIController extends OCSController {

/** Maximum length of a custom share token, matching the oc_share.token database column. */
private const TOKEN_MAX_LENGTH = 32;

private ?Node $lockedNode = null;
/** @var array<bool> $trustedServerCache */
private array $trustedServerCache = [];
Expand Down Expand Up @@ -1363,7 +1366,7 @@ public function updateShare(
throw new OCSForbiddenException($this->l->t('Custom share link tokens have been disabled by the administrator'));
}
if (!$this->validateToken($token)) {
throw new OCSBadRequestException($this->l->t('Tokens must contain at least 1 character and may only contain letters, numbers, or a hyphen'));
throw new OCSBadRequestException($this->l->t('Tokens must be between 1 and %s characters long and may only contain letters, numbers, or a hyphen', [self::TOKEN_MAX_LENGTH]));
}
$share->setToken($token);
}
Expand Down Expand Up @@ -1402,7 +1405,7 @@ public function updateShare(
}

private function validateToken(string $token): bool {
if (mb_strlen($token) === 0) {
if (mb_strlen($token) === 0 || mb_strlen($token) > self::TOKEN_MAX_LENGTH) {
return false;
}
if (!preg_match('/^[a-z0-9-]+$/i', $token)) {
Expand Down
16 changes: 16 additions & 0 deletions apps/files_sharing/tests/Controller/ShareAPIControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5604,6 +5604,22 @@ public function testWrapperStorageUnwrapped(): void {
$this->invokePrivate($ocs, 'checkInheritedAttributes', [$share]);
}

public static function dataValidateToken(): array {
return [
'empty token' => ['', false],
'single character' => ['a', true],
'letters numbers and hyphen' => ['abc-123', true],
'invalid character' => ['abc_123', false],
'32 characters (oc_share.token column limit)' => [str_repeat('a', 32), true],
'33 characters (exceeds oc_share.token column limit)' => [str_repeat('a', 33), false],
];
}

#[DataProvider('dataValidateToken')]
public function testValidateToken(string $token, bool $expected): void {
$this->assertSame($expected, $this->invokePrivate($this->ocs, 'validateToken', [$token]));
}

/**
* Helper to allow testing Talk integration even if Talk
* is not available during tests.
Expand Down
Loading