Skip to content

Commit 725f5be

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

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
@@ -394,6 +394,12 @@ public function logClientIn($user,
394394
try {
395395
$dbToken = $this->getTokenFromPassword($password);
396396
$isTokenPassword = $dbToken !== null;
397+
if (($dbToken instanceof PublicKeyToken)
398+
&& !in_array($dbToken->getType(), [IToken::PERMANENT_TOKEN,IToken::ONETIME_TOKEN])
399+
) {
400+
// Refuse session tokens here, only app tokens and onetime tokens are handled
401+
return false;
402+
}
397403
} catch (ExpiredTokenException) {
398404
// Just return on an expired token no need to check further or record a failed login
399405
return false;
@@ -817,32 +823,39 @@ private function validateTokenLoginName(?string $loginName, IToken $token): bool
817823
*/
818824
public function tryTokenLogin(IRequest $request) {
819825
$authHeader = $request->getHeader('Authorization');
826+
$tokenFromCookie = false;
820827
if (str_starts_with($authHeader, 'Bearer ')) {
821828
$token = substr($authHeader, 7);
822829
} elseif ($request->getCookie($this->config->getSystemValueString('instanceid')) !== null) {
823830
// No auth header, let's try session id, but only if this is an existing
824831
// session and the request has a session cookie
825832
try {
826833
$token = $this->session->getId();
834+
$tokenFromCookie = true;
827835
} catch (SessionNotAvailableException $ex) {
828836
return false;
829837
}
830838
} else {
831839
return false;
832840
}
833841

834-
if (!$this->loginWithToken($token)) {
842+
try {
843+
$dbToken = $this->tokenProvider->getToken($token);
844+
} catch (InvalidTokenException $e) {
845+
// Can't really happen but better safe than sorry
835846
return false;
836847
}
837-
if (!$this->validateToken($token)) {
848+
849+
if ($dbToken instanceof PublicKeyToken && $dbToken->getType() === IToken::TEMPORARY_TOKEN && !$tokenFromCookie) {
850+
// Session token but from Bearer header, not allowed
838851
return false;
839852
}
840853

841-
try {
842-
$dbToken = $this->tokenProvider->getToken($token);
843-
} catch (InvalidTokenException $e) {
844-
// Can't really happen but better save than sorry
845-
return true;
854+
if (!$this->loginWithToken($token)) {
855+
return false;
856+
}
857+
if (!$this->validateToken($token)) {
858+
return false;
846859
}
847860

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

0 commit comments

Comments
 (0)