Recover malformed JWT public key end marker

This commit is contained in:
2026-05-21 13:39:42 -05:00
parent 99f35c64ad
commit 6c54a438dd
3 changed files with 34 additions and 0 deletions

View File

@@ -166,6 +166,18 @@ class JwtAuthMiddleware
. "-----END {$type}-----\n"; . "-----END {$type}-----\n";
} }
if (preg_match('/-----BEGIN ([A-Z ]*PUBLIC KEY)-----(.*)/s', $publicKey, $matches)) {
$type = $matches[1];
$bodySource = preg_split('/-----END|END\s+(?:RSA\s+)?PUBLIC\s+KEY/i', $matches[2], 2)[0];
$body = preg_replace('/[^A-Za-z0-9+\/=]/', '', $bodySource);
if (strlen($body) > 100) {
return "-----BEGIN {$type}-----\n"
. chunk_split($body, 64, "\n")
. "-----END {$type}-----\n";
}
}
if (!str_contains($publicKey, '-----BEGIN')) { if (!str_contains($publicKey, '-----BEGIN')) {
$body = preg_replace('/[^A-Za-z0-9+\/=]/', '', $publicKey); $body = preg_replace('/[^A-Za-z0-9+\/=]/', '', $publicKey);

View File

@@ -50,6 +50,17 @@ Route::get('/health-check-key', function () {
$formattedPublicKey = "-----BEGIN {$pemType}-----\n" $formattedPublicKey = "-----BEGIN {$pemType}-----\n"
. chunk_split($body, 64, "\n") . chunk_split($body, 64, "\n")
. "-----END {$pemType}-----\n"; . "-----END {$pemType}-----\n";
} elseif (preg_match('/-----BEGIN ([A-Z ]*PUBLIC KEY)-----(.*)/s', $formattedPublicKey, $matches)) {
$pemType = $matches[1];
$bodySource = preg_split('/-----END|END\s+(?:RSA\s+)?PUBLIC\s+KEY/i', $matches[2], 2)[0];
$body = preg_replace('/[^A-Za-z0-9+\/=]/', '', $bodySource);
$bodyLength = strlen($body);
if ($bodyLength > 100) {
$formattedPublicKey = "-----BEGIN {$pemType}-----\n"
. chunk_split($body, 64, "\n")
. "-----END {$pemType}-----\n";
}
} elseif (!str_contains($formattedPublicKey, '-----BEGIN')) { } elseif (!str_contains($formattedPublicKey, '-----BEGIN')) {
$body = preg_replace('/[^A-Za-z0-9+\/=]/', '', $formattedPublicKey); $body = preg_replace('/[^A-Za-z0-9+\/=]/', '', $formattedPublicKey);
$bodyLength = strlen($body); $bodyLength = strlen($body);

View File

@@ -37,6 +37,17 @@ Route::get('/health-check-key', function () {
$formattedPublicKey = "-----BEGIN {$pemType}-----\n" $formattedPublicKey = "-----BEGIN {$pemType}-----\n"
. chunk_split($body, 64, "\n") . chunk_split($body, 64, "\n")
. "-----END {$pemType}-----\n"; . "-----END {$pemType}-----\n";
} elseif (preg_match('/-----BEGIN ([A-Z ]*PUBLIC KEY)-----(.*)/s', $formattedPublicKey, $matches)) {
$pemType = $matches[1];
$bodySource = preg_split('/-----END|END\s+(?:RSA\s+)?PUBLIC\s+KEY/i', $matches[2], 2)[0];
$body = preg_replace('/[^A-Za-z0-9+\/=]/', '', $bodySource);
$bodyLength = strlen($body);
if ($bodyLength > 100) {
$formattedPublicKey = "-----BEGIN {$pemType}-----\n"
. chunk_split($body, 64, "\n")
. "-----END {$pemType}-----\n";
}
} elseif (!str_contains($formattedPublicKey, '-----BEGIN')) { } elseif (!str_contains($formattedPublicKey, '-----BEGIN')) {
$body = preg_replace('/[^A-Za-z0-9+\/=]/', '', $formattedPublicKey); $body = preg_replace('/[^A-Za-z0-9+\/=]/', '', $formattedPublicKey);
$bodyLength = strlen($body); $bodyLength = strlen($body);