Skip to content

Commit

Permalink
Limit the length of app password names
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Mar 23, 2022
1 parent 0fa17f8 commit a0c7798
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
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
3 changes: 3 additions & 0 deletions core/Controller/AppPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public function getAppPassword(): DataResponse {
}

$userAgent = $this->request->getHeader('USER_AGENT');
if (mb_strlen($userAgent) > 128) {
$userAgent = mb_substr($userAgent, 0, 120) . '';
}

$token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);

Expand Down
4 changes: 4 additions & 0 deletions core/Controller/ClientFlowLoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ public function generateAppPassword($stateToken,
$clientName = $client->getName();
}

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

$token = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS);
$uid = $this->userSession->getUser()->getUID();
$generatedToken = $this->tokenProvider->generateToken(
Expand Down
4 changes: 4 additions & 0 deletions lib/private/Authentication/Token/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,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');
}

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) {

This comment has been minimized.

Copy link
@eastyu

eastyu May 7, 2022

As I understand, the $name variable woule be the user agent sent by browser in cases. I tested with new chromium edge browser from windows 11. The user agent length was 134 and the exception was thrown out. Would you please consider to extend this value?

This comment has been minimized.

Copy link
@nickvergessen

nickvergessen May 7, 2022

Author Member

The level before this code should automatically trim it to a shorter value.
If this is not happening can you please raise a new issue?

This comment has been minimized.

Copy link
@eastyu

eastyu May 8, 2022

thanks for response quickly, I opened a issue for this. #32302

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

0 comments on commit a0c7798

Please sign in to comment.