Skip to content

Commit 4ce151b

Browse files
committed
fix: Reduce the mixups between apptokens and session ids
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent c126084 commit 4ce151b

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

lib/private/User/Session.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,12 @@ public function logClientIn($user,
391391
try {
392392
$dbToken = $this->getTokenFromPassword($password);
393393
$isTokenPassword = $dbToken !== null;
394+
if (($dbToken instanceof PublicKeyToken)
395+
&& !in_array($dbToken->getType(), [IToken::PERMANENT_TOKEN,IToken::ONETIME_TOKEN])
396+
) {
397+
// Refuse session tokens here, only app tokens and onetime tokens are handled
398+
return false;
399+
}
394400
} catch (ExpiredTokenException) {
395401
// Just return on an expired token no need to check further or record a failed login
396402
return false;
@@ -814,32 +820,39 @@ private function validateTokenLoginName(?string $loginName, IToken $token): bool
814820
*/
815821
public function tryTokenLogin(IRequest $request) {
816822
$authHeader = $request->getHeader('Authorization');
823+
$tokenFromCookie = false;
817824
if (str_starts_with($authHeader, 'Bearer ')) {
818825
$token = substr($authHeader, 7);
819826
} elseif ($request->getCookie($this->config->getSystemValueString('instanceid')) !== null) {
820827
// No auth header, let's try session id, but only if this is an existing
821828
// session and the request has a session cookie
822829
try {
823830
$token = $this->session->getId();
831+
$tokenFromCookie = true;
824832
} catch (SessionNotAvailableException $ex) {
825833
return false;
826834
}
827835
} else {
828836
return false;
829837
}
830838

831-
if (!$this->loginWithToken($token)) {
839+
try {
840+
$dbToken = $this->tokenProvider->getToken($token);
841+
} catch (InvalidTokenException $e) {
842+
// Can't really happen but better safe than sorry
832843
return false;
833844
}
834-
if (!$this->validateToken($token)) {
845+
846+
if ($dbToken instanceof PublicKeyToken && $dbToken->getType() === IToken::TEMPORARY_TOKEN && !$tokenFromCookie) {
847+
// Session token but from Bearer header, not allowed
835848
return false;
836849
}
837850

838-
try {
839-
$dbToken = $this->tokenProvider->getToken($token);
840-
} catch (InvalidTokenException $e) {
841-
// Can't really happen but better save than sorry
842-
return true;
851+
if (!$this->loginWithToken($token)) {
852+
return false;
853+
}
854+
if (!$this->validateToken($token)) {
855+
return false;
843856
}
844857

845858
// Set the session variable so we know this is an app password

0 commit comments

Comments
 (0)