Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Limit the length of app password names
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Mar 23, 2022
commit ea0be37e6984ab865d2be711130cad515a62a65a
8 changes: 8 additions & 0 deletions apps/settings/lib/Controller/AuthSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ public function create($name) {
return $this->getServiceNotAvailableResponse();
}

if (mb_strlen($name) > 128) {
$name = mb_substr($name, 0, 120) . '…';
}

$token = $this->generateRandomDeviceToken();
$deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $loginName, $password, $name, IToken::PERMANENT_TOKEN);
$tokenData = $deviceToken->jsonSerialize();
Expand Down Expand Up @@ -241,6 +245,10 @@ public function update($id, array $scope, string $name) {
$this->publishActivity($scope['filesystem'] ? Provider::APP_TOKEN_FILESYSTEM_GRANTED : Provider::APP_TOKEN_FILESYSTEM_REVOKED, $token->getId(), ['name' => $currentName]);
}

if (mb_strlen($name) > 128) {
$name = mb_substr($name, 0, 120) . '…';
}

if ($token instanceof INamedToken && $name !== $currentName) {
$token->setName($name);
$this->publishActivity(Provider::APP_TOKEN_RENAMED, $token->getId(), ['name' => $currentName, 'newName' => $name]);
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Authentication/Token/IProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ interface IProvider {
* @param string $uid
* @param string $loginName
* @param string|null $password
* @param string $name
* @param string $name Name will be trimmed to 120 chars when longer
* @param int $type token type
* @param int $remember whether the session token should be used for remember-me
* @return IToken
Expand Down
6 changes: 5 additions & 1 deletion lib/private/Authentication/Token/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(DefaultTokenProvider $defaultTokenProvider, PublicKe
* @param string $uid
* @param string $loginName
* @param string|null $password
* @param string $name
* @param string $name Name will be trimmed to 120 chars when longer
* @param int $type token type
* @param int $remember whether the session token should be used for remember-me
* @return IToken
Expand All @@ -65,6 +65,10 @@ public function generateToken(string $token,
string $name,
int $type = IToken::TEMPORARY_TOKEN,
int $remember = IToken::DO_NOT_REMEMBER): IToken {
if (mb_strlen($name) > 128) {
$name = mb_substr($name, 0, 120) . '…';
}

try {
return $this->publicKeyTokenProvider->generateToken(
$token,
Expand Down
4 changes: 4 additions & 0 deletions lib/private/Authentication/Token/PublicKeyTokenProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public function generateToken(string $token,
string $name,
int $type = IToken::TEMPORARY_TOKEN,
int $remember = IToken::DO_NOT_REMEMBER): IToken {
if (mb_strlen($name) > 128) {
throw new InvalidTokenException('The given name is too long');
}

$dbToken = $this->newToken($token, $uid, $loginName, $password, $name, $type, $remember);
$this->mapper->insert($dbToken);

Expand Down
31 changes: 31 additions & 0 deletions tests/lib/Authentication/Token/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,37 @@ public function testGenerateConflictingToken() {
$this->assertSame($token, $actual);
}

public function testGenerateTokenTooLongName() {
$token = $this->createMock(IToken::class);
$token->method('getName')
->willReturn(str_repeat('a', 120) . '…');


$this->publicKeyTokenProvider->expects($this->once())
->method('generateToken')
->with(
'token',
'uid',
'loginName',
'password',
str_repeat('a', 120) . '…',
IToken::TEMPORARY_TOKEN,
IToken::REMEMBER
)->willReturn($token);

$actual = $this->manager->generateToken(
'token',
'uid',
'loginName',
'password',
str_repeat('a', 200),
IToken::TEMPORARY_TOKEN,
IToken::REMEMBER
);

$this->assertSame(121, mb_strlen($actual->getName()));
}

public function tokenData(): array {
return [
[new DefaultToken()],
Expand Down