Skip to content

Commit c6d5152

Browse files
authored
Merge pull request #62477 from nextcloud/fix/62362/password-confirmation-php-auth-pw
fix: Use PHP_AUTH_PW for strict password confirmation
2 parents 3541356 + 07e123e commit c6d5152

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
@@ -291,6 +291,8 @@ public function getHeader(string $name): string {
291291
'CONTENT_TYPE' => true,
292292
'CONTENT_LENGTH' => true,
293293
'REMOTE_ADDR' => true,
294+
'PHP_AUTH_USER' => true,
295+
'PHP_AUTH_PW' => true,
294296
];
295297

296298
if (isset($specialKeys[$elementName]) && isset($this->server[$elementName])) {

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

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

7878
$reflectionMethod = new ReflectionMethod($controller, $methodName);
7979
if ($this->isPasswordConfirmationStrict($reflectionMethod)) {
80-
$authHeader = $this->request->getHeader('Authorization');
81-
if (!str_starts_with(strtolower($authHeader), 'basic ')) {
80+
$password = $this->request->getHeader('PHP_AUTH_PW');
81+
82+
if ($password === '') {
8283
throw new NotConfirmedException('Required authorization header missing');
8384
}
84-
[, $password] = explode(':', base64_decode(substr($authHeader, 6)), 2);
85+
8586
$loginName = $this->session->get('loginname');
8687
$loginResult = $this->userManager->checkPassword($loginName, $password);
8788
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
@@ -205,4 +205,34 @@ public function testSSO(): void {
205205

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

0 commit comments

Comments
 (0)