Files
nextcloud-server-mirror/apps/dav/tests/unit/Connector/LegacyPublicAuthTest.php
Ferdinand Thiessen 745bd1d446 fix(dav): only store hashed share passwords in the session
Skip remembering the share in the session unless its password is hashed,
and compare the stored hash with hash_equals().

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
2026-11-03 13:52:01 +01:00

294 lines
9.5 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\DAV\Tests\unit\Connector;
use OCA\DAV\Connector\LegacyPublicAuth;
use OCP\IRequest;
use OCP\ISession;
use OCP\IUserSession;
use OCP\Security\Bruteforce\IThrottler;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use OCP\Share\IShare;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
#[Group(name: 'DB')]
class LegacyPublicAuthTest extends TestCase {
private ISession&MockObject $session;
private IRequest&MockObject $request;
private IManager&MockObject $shareManager;
private IThrottler&MockObject $throttler;
private IUserSession&MockObject $userSession;
private LegacyPublicAuth $auth;
private string|false $oldUser;
protected function setUp(): void {
parent::setUp();
$this->session = $this->createMock(ISession::class);
$this->request = $this->createMock(IRequest::class);
$this->shareManager = $this->createMock(IManager::class);
$this->throttler = $this->createMock(IThrottler::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->auth = new LegacyPublicAuth(
$this->request,
$this->shareManager,
$this->session,
$this->throttler,
$this->userSession,
);
// Store current user
$this->oldUser = \OC_User::getUser();
}
protected function tearDown(): void {
\OC_User::setIncognitoMode(false);
// Set old user
\OC_User::setUserId($this->oldUser ?: null);
if ($this->oldUser !== false) {
\OC_Util::setupFS($this->oldUser);
}
parent::tearDown();
}
public function testNoShare(): void {
$this->shareManager->expects($this->once())
->method('getShareByToken')
->willThrowException(new ShareNotFound());
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
$this->assertFalse($result);
}
public function testShareNoPassword(): void {
$share = $this->createMock(IShare::class);
$share->method('getPassword')->willReturn(null);
$share->method('isPasswordProtected')->willReturn(false);
$this->shareManager->expects($this->once())
->method('getShareByToken')
->willReturn($share);
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
$this->assertTrue($result);
}
public function testSharePasswordFancyShareType(): void {
$share = $this->createMock(IShare::class);
$share->method('getPassword')->willReturn('password');
$share->method('isPasswordProtected')->willReturn(true);
$share->method('isPasswordHashed')->willReturn(true);
$share->method('getShareType')->willReturn(42);
$this->shareManager->expects($this->once())
->method('getShareByToken')
->willReturn($share);
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
$this->assertFalse($result);
}
public function testSharePasswordRemote(): void {
$share = $this->createMock(IShare::class);
$share->method('getPassword')->willReturn('password');
$share->method('isPasswordProtected')->willReturn(true);
$share->method('isPasswordHashed')->willReturn(true);
$share->method('getShareType')->willReturn(IShare::TYPE_REMOTE);
$this->shareManager->expects($this->once())
->method('getShareByToken')
->willReturn($share);
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
$this->assertTrue($result);
}
public function testSharePasswordLinkValidPassword(): void {
$share = $this->createMock(IShare::class);
$share->method('getPassword')->willReturn('password');
$share->method('isPasswordProtected')->willReturn(true);
$share->method('isPasswordHashed')->willReturn(true);
$share->method('getShareType')->willReturn(IShare::TYPE_LINK);
$share->method('getToken')->willReturn('GX9HSGQrGE');
$this->shareManager->expects($this->once())
->method('getShareByToken')
->willReturn($share);
$this->shareManager->expects($this->once())
->method('checkPassword')->with(
$this->equalTo($share),
$this->equalTo('password')
)->willReturn(true);
$this->session->expects($this->once())
->method('set')
->with('public_link_authenticated_frontend', '{"GX9HSGQrGE":"password"}');
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
$this->assertTrue($result);
}
public function testSharePasswordNotHashedNotStoredInSession(): void {
$share = $this->createMock(IShare::class);
$share->method('getPassword')->willReturn('password');
$share->method('isPasswordProtected')->willReturn(true);
$share->method('isPasswordHashed')->willReturn(false);
$share->method('getShareType')->willReturn(IShare::TYPE_LINK);
$share->method('getToken')->willReturn('GX9HSGQrGE');
$this->shareManager->expects($this->once())
->method('getShareByToken')
->willReturn($share);
$this->shareManager->method('checkPassword')->willReturn(true);
$this->session->expects($this->never())->method('set');
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
$this->assertTrue($result);
}
public function testSharePasswordMailValidPassword(): void {
$share = $this->createMock(IShare::class);
$share->method('getToken')->willReturn('GX9HSGQrGE');
$share->method('getPassword')->willReturn('password');
$share->method('isPasswordProtected')->willReturn(true);
$share->method('isPasswordHashed')->willReturn(true);
$share->method('getShareType')->willReturn(IShare::TYPE_EMAIL);
$this->shareManager->expects($this->once())
->method('getShareByToken')
->willReturn($share);
$this->shareManager->expects($this->once())
->method('checkPassword')->with(
$this->equalTo($share),
$this->equalTo('password')
)->willReturn(true);
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
$this->assertTrue($result);
}
public function testInvalidSharePasswordLinkValidSession(): void {
$share = $this->createMock(IShare::class);
$share->method('getPassword')->willReturn('password');
$share->method('isPasswordProtected')->willReturn(true);
$share->method('isPasswordHashed')->willReturn(true);
$share->method('getShareType')->willReturn(IShare::TYPE_LINK);
$share->method('getToken')->willReturn('GX9HSGQrGE');
$this->shareManager->expects($this->once())
->method('getShareByToken')
->willReturn($share);
$this->shareManager->method('checkPassword')
->with(
$this->equalTo($share),
$this->equalTo('password')
)->willReturn(false);
$this->session->method('get')->with('public_link_authenticated_frontend')->willReturn('{"GX9HSGQrGE":"password"}');
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
$this->assertTrue($result);
}
public function testInvalidSharePasswordLinkSessionWithChangedPassword(): void {
$share = $this->createMock(IShare::class);
$share->method('getPassword')->willReturn('new-password');
$share->method('isPasswordProtected')->willReturn(true);
$share->method('isPasswordHashed')->willReturn(true);
$share->method('getShareType')->willReturn(IShare::TYPE_LINK);
$share->method('getToken')->willReturn('GX9HSGQrGE');
$this->shareManager->expects($this->once())
->method('getShareByToken')
->willReturn($share);
$this->shareManager->method('checkPassword')
->with(
$this->equalTo($share),
$this->equalTo('old-password')
)->willReturn(false);
$this->session->method('get')->with('public_link_authenticated_frontend')->willReturn('{"GX9HSGQrGE":"old-password"}');
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'old-password']);
$this->assertFalse($result);
}
public function testSharePasswordLinkInvalidSession(): void {
$share = $this->createMock(IShare::class);
$share->method('getPassword')->willReturn('password');
$share->method('isPasswordProtected')->willReturn(true);
$share->method('isPasswordHashed')->willReturn(true);
$share->method('getShareType')->willReturn(IShare::TYPE_LINK);
$share->method('getToken')->willReturn('GX9HSGQrGE');
$this->shareManager->expects($this->once())
->method('getShareByToken')
->willReturn($share);
$this->shareManager->method('checkPassword')
->with(
$this->equalTo($share),
$this->equalTo('password')
)->willReturn(false);
$this->session->method('get')->with('public_link_authenticated_frontend')->willReturn('{"other-token":"password"}');
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
$this->assertFalse($result);
}
public function testSharePasswordMailInvalidSession(): void {
$share = $this->createMock(IShare::class);
$share->method('getPassword')->willReturn('password');
$share->method('isPasswordProtected')->willReturn(true);
$share->method('isPasswordHashed')->willReturn(true);
$share->method('getShareType')->willReturn(IShare::TYPE_EMAIL);
$share->method('getToken')->willReturn('GX9HSGQrGE');
$this->shareManager->expects($this->once())
->method('getShareByToken')
->willReturn($share);
$this->shareManager->method('checkPassword')
->with(
$this->equalTo($share),
$this->equalTo('password')
)->willReturn(false);
$this->session->method('get')->with('public_link_authenticated_frontend')->willReturn('{"other-token":"password"}');
$result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
$this->assertFalse($result);
}
}