Skip to content

Commit 91dbf74

Browse files
fix: align and cleanup auth header normalization in OC::handleAuthHeaders()
Signed-off-by: Josh <josh.t.richards@gmail.com>
1 parent 94561a1 commit 91dbf74

1 file changed

Lines changed: 35 additions & 17 deletions

File tree

lib/base.php

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,27 +1249,45 @@ public static function handleLogin(OCP\IRequest $request): bool {
12491249
return false;
12501250
}
12511251

1252+
/**
1253+
* Normalizes authorization headers exposed through different $_SERVER keys
1254+
* during early bootstrap and extracts PHP Basic auth credentials when present.
1255+
*
1256+
* This runs before IRequest is available, so it operates directly on $_SERVER.
1257+
*
1258+
* The PHP execution path and web server configuration must pass the
1259+
* Authorization header through via one of the supported server variables.
1260+
*
1261+
* For Apache + PHP (any mode), this is handled by the distributed .htaccess file.
1262+
*/
12521263
protected static function handleAuthHeaders(): void {
1253-
//copy http auth headers for apache+php-fcgid work around
1254-
if (isset($_SERVER['HTTP_XAUTHORIZATION']) && !isset($_SERVER['HTTP_AUTHORIZATION'])) {
1255-
$_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['HTTP_XAUTHORIZATION'];
1264+
$authHeaderValue = $_SERVER['HTTP_AUTHORIZATION']
1265+
?? $_SERVER['REDIRECT_HTTP_AUTHORIZATION']
1266+
?? $_SERVER['HTTP_XAUTHORIZATION']
1267+
?? null;
1268+
1269+
if (!is_string($authHeaderValue) || $authHeaderValue === '') {
1270+
return;
12561271
}
12571272

1258-
// Extract PHP_AUTH_USER/PHP_AUTH_PW from other headers if necessary.
1259-
$vars = [
1260-
'HTTP_AUTHORIZATION', // apache+php-cgi work around
1261-
'REDIRECT_HTTP_AUTHORIZATION', // apache+php-cgi alternative
1262-
];
1263-
foreach ($vars as $var) {
1264-
if (isset($_SERVER[$var]) && is_string($_SERVER[$var]) && preg_match('/Basic\s+(.*)$/i', $_SERVER[$var], $matches)) {
1265-
$credentials = explode(':', base64_decode($matches[1]), 2);
1266-
if (count($credentials) === 2) {
1267-
$_SERVER['PHP_AUTH_USER'] = $credentials[0];
1268-
$_SERVER['PHP_AUTH_PW'] = $credentials[1];
1269-
break;
1270-
}
1271-
}
1273+
$_SERVER['HTTP_AUTHORIZATION'] ??= $authHeaderValue;
1274+
1275+
if (stripos($authHeaderValue, 'Basic ') !== 0) {
1276+
// Silently ignore non-Basic authentication requests.
1277+
return;
12721278
}
1279+
1280+
$decodedCredentials = base64_decode(substr($authHeaderValue, 6), true);
1281+
1282+
if ($decodedCredentials === false || !str_contains($decodedCredentials, ':')) {
1283+
// Silently ignore malformed Basic auth credentials.
1284+
return;
1285+
}
1286+
1287+
[$user, $pw] = explode(':', $decodedCredentials, 2);
1288+
1289+
$_SERVER['PHP_AUTH_USER'] = $user;
1290+
$_SERVER['PHP_AUTH_PW'] = $pw;
12731291
}
12741292

12751293
protected static function tryAppAPILogin(OCP\IRequest $request): bool {

0 commit comments

Comments
 (0)