From 041a5d770d8ba165a0d93cdce7e822fc3301fdee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orc=CC=A7un=20Candan?= Date: Sat, 3 Oct 2026 09:41:33 +0300 Subject: [PATCH 1/2] Fix #20470: Treat invalid base64 or non UTF-8 Basic auth credentials as absent `Request::getAuthCredentials()` decoded the `Authorization: Basic` token with non-strict `base64_decode()`, so a malformed header such as `Basic foo:bar` produced arbitrary binary bytes as the username. Returning those bytes later breaks JSON encoding with a "Malformed UTF-8 characters" error (HTTP 500). Decode in strict mode and return `[null, null]` when decoding fails or the decoded value is not valid UTF-8. Co-Authored-By: Claude Opus 5.5 --- framework/CHANGELOG.md | 1 + framework/web/Request.php | 9 ++++++++- tests/framework/web/RequestTest.php | 5 ++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 657ed1169e8..9ee41f285f5 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -39,6 +39,7 @@ Yii Framework 2 Change Log - Enh #21079: Add the missing `@property` tags (mspirkov) - Bug #21094: Log a warning instead of silently ignoring `$isolationLevel` when `yii\db\Transaction::begin()` is called for a nested transaction (terabytesoftw) - Bug #21086: Allow integer and string keys in the `yii\base\Model::rules()` return annotation (terabytesoftw) +- Bug #20470: Fix `yii\web\Request::getAuthCredentials()` returning malformed credentials for an invalid base64 or non UTF-8 `Authorization: Basic` header (orcnd) 2.0.55 May 09, 2026 ------------------- diff --git a/framework/web/Request.php b/framework/web/Request.php index 3a68d1c72cc..5cb1a9b5b4d 100644 --- a/framework/web/Request.php +++ b/framework/web/Request.php @@ -1407,9 +1407,16 @@ public function getAuthCredentials() $auth_token = $this->getHeaders()->get('Authorization'); if ($auth_token !== null && strncasecmp($auth_token, 'basic', 5) === 0) { + $decoded = base64_decode(mb_substr($auth_token, 6), true); + + // malformed base64 or non UTF-8 credentials are treated as not given + if ($decoded === false || !mb_check_encoding($decoded, 'UTF-8')) { + return [null, null]; + } + $parts = array_map(function ($value) { return strlen($value) === 0 ? null : $value; - }, explode(':', base64_decode(mb_substr($auth_token, 6)), 2)); + }, explode(':', $decoded, 2)); if (count($parts) < 2) { return [$parts[0], null]; diff --git a/tests/framework/web/RequestTest.php b/tests/framework/web/RequestTest.php index bfc61cf69c5..19bffdd6a7b 100644 --- a/tests/framework/web/RequestTest.php +++ b/tests/framework/web/RequestTest.php @@ -1093,7 +1093,10 @@ public function testGetOrigin(): void public static function httpAuthorizationHeadersProvider(): array { return [ - ['not a base64 at all', [base64_decode('not a base64 at all'), null]], + 'not a base64 at all' => ['not a base64 at all', [null, null]], + 'invalid base64 characters' => ['foo:bar', [null, null]], + 'decoded value is not valid UTF-8' => [base64_encode("\xff\xfe:pw"), [null, null]], + 'decoded UTF-8 credentials' => [base64_encode('üser:pässwörd'), ['üser', 'pässwörd']], [base64_encode('user:'), ['user', null]], [base64_encode('user'), ['user', null]], [base64_encode('user:pw'), ['user', 'pw']], From 818926c6f0854d371d3b526688146626568c40f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orc=CC=A7un=20Candan?= Date: Sun, 15 Nov 2026 11:05:35 +0300 Subject: [PATCH 2/2] Add test isolating strict base64 decoding of Basic auth credentials --- tests/framework/web/RequestTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/framework/web/RequestTest.php b/tests/framework/web/RequestTest.php index 19bffdd6a7b..4937e3f3dea 100644 --- a/tests/framework/web/RequestTest.php +++ b/tests/framework/web/RequestTest.php @@ -1095,6 +1095,7 @@ public static function httpAuthorizationHeadersProvider(): array return [ 'not a base64 at all' => ['not a base64 at all', [null, null]], 'invalid base64 characters' => ['foo:bar', [null, null]], + 'invalid character after credentials' => [base64_encode('user:pw') . '!', [null, null]], 'decoded value is not valid UTF-8' => [base64_encode("\xff\xfe:pw"), [null, null]], 'decoded UTF-8 credentials' => [base64_encode('üser:pässwörd'), ['üser', 'pässwörd']], [base64_encode('user:'), ['user', null]],