Skip to content

Commit 7f86dee

Browse files
committed
Improve parsing of base64-encoded strings
1 parent 23e7e89 commit 7f86dee

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

docs/simplesamlphp-customauth.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,11 @@ class MyAuth extends \SimpleSAML\Module\core\Auth\UserPassBase
335335
*/
336336
private function checkPassword($passwordHash, $password)
337337
{
338-
$passwordHash = base64_decode($passwordHash);
338+
$passwordHash = base64_decode($passwordHash, true);
339+
if (empty($passwordHash)) {
340+
throw new \InvalidArgumentException("Password hash is empty or not a valid base64 encoded string.");
341+
}
342+
339343
$digest = substr($passwordHash, 0, 20);
340344
$salt = substr($passwordHash, 20);
341345

modules/core/src/Controller/Redirection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function postredirect(Request $request): Response
5959
if ($redirId !== false) {
6060
$postId = $redirId;
6161
} elseif ($redirInfo !== false) {
62-
$encData = base64_decode($redirInfo);
62+
$encData = base64_decode($redirInfo, true);
6363

6464
if (empty($encData)) {
6565
throw new Error\BadRequest('Invalid RedirInfo data.');

src/SimpleSAML/Utils/Crypto.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,12 @@ public function pem2der(string $pem): string
337337
}
338338
unset($lines[$last]);
339339

340-
return base64_decode(implode($lines));
340+
$transform = base64_decode(implode($lines), true);
341+
if (empty($transform)) {
342+
throw new InvalidArgumentException("pem2der: input is empty or not a valid base64 encoded string.");
343+
}
344+
345+
return $transform;
341346
}
342347

343348

tests/src/SimpleSAML/Utils/CryptoTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function testAesDecrypt(): void
120120
uR2Yu0r4itInKx91D/l9y/08L5CIQyev9nAr27fh3Sshous4vbXRRcMcjqHDOrquD+2vqLyw7ygnbA9jA9TpB4hLZocvAWcTN8tyO82hiSY=
121121
CIPHER;
122122

123-
$decrypted = $this->cryptoUtils->aesDecrypt(base64_decode($ciphertext));
123+
$decrypted = $this->cryptoUtils->aesDecrypt(base64_decode($ciphertext, true));
124124
$this->assertEquals($plaintext, $decrypted);
125125
}
126126

0 commit comments

Comments
 (0)