Skip to content

Commit ddeeccf

Browse files
authored
Merge pull request #66270 from nextcloud/backport/66269/stable27
[stable27] [stable28] [stable32] fix: Reject login with NUL bytes rather than converting
2 parents 8767d96 + cc43909 commit ddeeccf

2 files changed

Lines changed: 33 additions & 12 deletions

File tree

‎lib/private/User/Manager.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,12 @@ public function checkPassword($loginName, $password) {
255255
* @return IUser|false the User object on success, false otherwise
256256
*/
257257
public function checkPasswordNoLogging($loginName, $password) {
258-
$loginName = str_replace("\0", '', $loginName);
258+
// A NUL byte in the login name must not turn it into a second name for an
259+
// account: the checks done before the password check (e.g. 2FA enforcement
260+
// in Session::logClientIn()) see the name as sent, so the name has to stay the same
261+
if (str_contains($loginName, "\0")) {
262+
return false;
263+
}
259264
$password = str_replace("\0", '', $password);
260265

261266
$cachedBackend = $this->cache->get($loginName);

‎tests/lib/User/ManagerTest.php‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ public function testGetBackends() {
6262
$this->assertEquals([$userDummyBackend, $dummyDatabaseBackend], $manager->getBackends());
6363
}
6464

65-
66-
public function testUserExistsSingleBackendExists() {
65+
public function testUserExistsSingleBackendExists(): void {
6766
/**
6867
* @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
6968
*/
@@ -196,7 +195,28 @@ public function testCheckPasswordNotSupported() {
196195
$this->assertFalse($manager->checkPassword('foo', 'bar'));
197196
}
198197

199-
public function testGetOneBackendExists() {
198+
public function testCheckPasswordNullByteInLoginName(): void {
199+
$backend = $this->createMock(\Test\Util\User\Dummy::class);
200+
$backend->expects($this->never())
201+
->method('checkPassword');
202+
203+
$backend->expects($this->any())
204+
->method('implementsActions')
205+
->willReturnCallback(function ($actions) {
206+
if ($actions === BACKEND::CHECK_PASSWORD) {
207+
return true;
208+
} else {
209+
return false;
210+
}
211+
});
212+
213+
$manager = new Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
214+
$manager->registerBackend($backend);
215+
216+
$this->assertFalse($manager->checkPassword("foo\0", 'bar'));
217+
}
218+
219+
public function testGetOneBackendExists(): void {
200220
/**
201221
* @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
202222
*/
@@ -347,8 +367,7 @@ public function testCreateUserInvalid($uid, $password, $exception) {
347367
->with(\OC\User\Backend::CREATE_USER)
348368
->willReturn(true);
349369

350-
351-
$manager = new \OC\User\Manager($this->config, $this->oldDispatcher, $this->cacheFactory, $this->eventDispatcher);
370+
$manager = new Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
352371
$manager->registerBackend($backend);
353372

354373
$this->expectException(\InvalidArgumentException::class, $exception);
@@ -382,8 +401,7 @@ public function testCreateUserSingleBackendNotExists() {
382401
$this->assertEquals('foo', $user->getUID());
383402
}
384403

385-
386-
public function testCreateUserSingleBackendExists() {
404+
public function testCreateUserSingleBackendExists(): void {
387405
$this->expectException(\Exception::class);
388406

389407
/**
@@ -435,8 +453,7 @@ public function testCreateUserNoBackends() {
435453
$this->assertFalse($manager->createUser('foo', 'bar'));
436454
}
437455

438-
439-
public function testCreateUserFromBackendWithBackendError() {
456+
public function testCreateUserFromBackendWithBackendError(): void {
440457
$this->expectException(\InvalidArgumentException::class);
441458
$this->expectExceptionMessage('Could not create user');
442459

@@ -454,8 +471,7 @@ public function testCreateUserFromBackendWithBackendError() {
454471
$manager->createUserFromBackend('MyUid', 'MyPassword', $backend);
455472
}
456473

457-
458-
public function testCreateUserTwoBackendExists() {
474+
public function testCreateUserTwoBackendExists(): void {
459475
$this->expectException(\Exception::class);
460476

461477
/**

0 commit comments

Comments
 (0)