Skip to content

Commit 231cf02

Browse files
authored
Merge pull request #62955 from nextcloud/backport/62537/stable32
[stable32] fix: Use PHP_AUTH_PW for strict password confirmation
2 parents 11837fe + a900309 commit 231cf02

4 files changed

Lines changed: 40 additions & 3 deletions

File tree

lib/private/AppFramework/Http/Request.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ public function getHeader(string $name): string {
272272
case 'CONTENT_TYPE':
273273
case 'CONTENT_LENGTH':
274274
case 'REMOTE_ADDR':
275+
case 'PHP_AUTH_USER':
276+
case 'PHP_AUTH_PW':
275277
if (isset($this->server[$name])) {
276278
return $this->server[$name];
277279
}

lib/private/AppFramework/Middleware/Security/PasswordConfirmationMiddleware.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,12 @@ public function beforeController(Controller $controller, string $methodName) {
8282

8383
$reflectionMethod = new ReflectionMethod($controller, $methodName);
8484
if ($this->isPasswordConfirmationStrict($reflectionMethod)) {
85-
$authHeader = $this->request->getHeader('Authorization');
86-
if (!str_starts_with(strtolower($authHeader), 'basic ')) {
85+
$password = $this->request->getHeader('PHP_AUTH_PW');
86+
87+
if ($password === '') {
8788
throw new NotConfirmedException('Required authorization header missing');
8889
}
89-
[, $password] = explode(':', base64_decode(substr($authHeader, 6)), 2);
90+
9091
$loginName = $this->session->get('loginname');
9192
$loginResult = $this->userManager->checkPassword($loginName, $password);
9293
if ($loginResult === false) {

tests/lib/AppFramework/Middleware/Security/Mock/PasswordConfirmationMiddlewareController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,8 @@ public function testAttribute() {
3535
#[PasswordConfirmationRequired]
3636
public function testSSO() {
3737
}
38+
39+
#[PasswordConfirmationRequired(strict: true)]
40+
public function testAuthHeader() {
41+
}
3842
}

tests/lib/AppFramework/Middleware/Security/PasswordConfirmationMiddlewareTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,34 @@ public function testSSO(): void {
211211

212212
$this->assertSame(false, $thrown);
213213
}
214+
215+
public function testAuthHeader(): void {
216+
$this->reflector->reflect($this->controller, __FUNCTION__);
217+
218+
$this->user->method('getBackendClassName')
219+
->willReturn('fictional_backend');
220+
$this->userSession->method('getUser')
221+
->willReturn($this->user);
222+
223+
$this->session->method('get')
224+
->with('loginname')
225+
->willReturn('user');
226+
227+
$this->request->method('getHeader')
228+
->with('PHP_AUTH_PW')
229+
->willReturn('password');
230+
231+
$this->userManager->expects($this->once())
232+
->method('checkPassword')
233+
->with('user', 'password');
234+
235+
$thrown = false;
236+
try {
237+
$this->middleware->beforeController($this->controller, __FUNCTION__);
238+
} catch (NotConfirmedException) {
239+
$thrown = true;
240+
}
241+
242+
$this->assertSame(false, $thrown);
243+
}
214244
}

0 commit comments

Comments
 (0)