Skip to content

Commit d033768

Browse files
Merge pull request #42607 from nextcloud/tokenAnonReq
fix(session): Avoid useless authtoken DB queries for anonymous requests
2 parents ad12af8 + 72e0618 commit d033768

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

lib/private/User/Session.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -842,13 +842,16 @@ public function tryTokenLogin(IRequest $request) {
842842
$authHeader = $request->getHeader('Authorization');
843843
if (str_starts_with($authHeader, 'Bearer ')) {
844844
$token = substr($authHeader, 7);
845-
} else {
846-
// No auth header, let's try session id
845+
} elseif ($request->getCookie($this->config->getSystemValueString('instanceid')) !== null) {
846+
// No auth header, let's try session id, but only if this is an existing
847+
// session and the request has a session cookie
847848
try {
848849
$token = $this->session->getId();
849850
} catch (SessionNotAvailableException $ex) {
850851
return false;
851852
}
853+
} else {
854+
return false;
852855
}
853856

854857
if (!$this->loginWithToken($token)) {

tests/lib/User/SessionTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,56 @@ public function testLogClientInNoTokenPasswordNo2fa() {
479479
$userSession->logClientIn('john', 'doe', $request, $this->throttler);
480480
}
481481

482+
public function testTryTokenLoginNoHeaderNoSessionCookie(): void {
483+
$request = $this->createMock(IRequest::class);
484+
$this->config->expects(self::once())
485+
->method('getSystemValueString')
486+
->with('instanceid')
487+
->willReturn('abc123');
488+
$request->method('getHeader')->with('Authorization')->willReturn('');
489+
$request->method('getCookie')->with('abc123')->willReturn(null);
490+
$this->tokenProvider->expects(self::never())
491+
->method('getToken');
492+
493+
$loginResult = $this->userSession->tryTokenLogin($request);
494+
495+
self::assertFalse($loginResult);
496+
}
497+
498+
public function testTryTokenLoginAuthorizationHeaderTokenNotFound(): void {
499+
$request = $this->createMock(IRequest::class);
500+
$request->method('getHeader')->with('Authorization')->willReturn('Bearer abcde-12345');
501+
$this->tokenProvider->expects(self::once())
502+
->method('getToken')
503+
->with('abcde-12345')
504+
->willThrowException(new InvalidTokenException());
505+
506+
$loginResult = $this->userSession->tryTokenLogin($request);
507+
508+
self::assertFalse($loginResult);
509+
}
510+
511+
public function testTryTokenLoginSessionIdTokenNotFound(): void {
512+
$request = $this->createMock(IRequest::class);
513+
$this->config->expects(self::once())
514+
->method('getSystemValueString')
515+
->with('instanceid')
516+
->willReturn('abc123');
517+
$request->method('getHeader')->with('Authorization')->willReturn('');
518+
$request->method('getCookie')->with('abc123')->willReturn('abcde12345');
519+
$this->session->expects(self::once())
520+
->method('getId')
521+
->willReturn('abcde12345');
522+
$this->tokenProvider->expects(self::once())
523+
->method('getToken')
524+
->with('abcde12345')
525+
->willThrowException(new InvalidTokenException());
526+
527+
$loginResult = $this->userSession->tryTokenLogin($request);
528+
529+
self::assertFalse($loginResult);
530+
}
531+
482532
public function testRememberLoginValidToken() {
483533
$session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock();
484534
$managerMethods = get_class_methods(Manager::class);

0 commit comments

Comments
 (0)