Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve key detection in site health tests #105

Merged
merged 3 commits into from
Nov 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions src/SiteStatusTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ public function register_site_status_tests( $tests ): array {
}

public function site_status_test_public_key(): array {
if ( ! defined( 'OIDC_PUBLIC_KEY' ) ) {
$key_is_defined = defined( 'OIDC_PUBLIC_KEY' );
$key_has_valid_pem_headers = (bool) preg_match(
'/^-----BEGIN\s.*PUBLIC KEY-----.*-----END\s.*PUBLIC KEY-----$/s',
OIDC_PUBLIC_KEY
);

if ( ! $key_is_defined ) {
$label = __( 'The public key constant OIDC_PUBLIC_KEY is not defined.', 'openid-connect-server' );
$status = 'critical';
$badge = 'red';
} elseif (
0 === strpos( OIDC_PUBLIC_KEY, '-----BEGIN PUBLIC KEY-----' )
&& '-----END PUBLIC KEY-----' === substr( OIDC_PUBLIC_KEY, - strlen( '-----END PUBLIC KEY-----' ) )
&& strlen( OIDC_PUBLIC_KEY ) > 50
) {
} elseif ( $key_has_valid_pem_headers ) {
$label = __( 'The public key is defined and in the right format', 'openid-connect-server' );
$status = 'good';
$badge = 'green';
Expand All @@ -60,7 +62,7 @@ public function site_status_test_public_key(): array {
sprintf(
// Translators: %s is a URL.
__( "Please see the <a href=%s>plugin's readme file</a> for details.", 'openid-connect-server' ),
'"https://github.com/Automattic/wp-openid-connect-server/blob/trunk/README.md"'
'"https://github.com/Automattic/wp-openid-connect-server/blob/main/README.md"'
)
) .
'</p>',
Expand All @@ -69,15 +71,17 @@ public function site_status_test_public_key(): array {
}

public function site_status_test_private_key(): array {
if ( ! defined( 'OIDC_PRIVATE_KEY' ) ) {
$key_is_defined = defined( 'OIDC_PRIVATE_KEY' );
$key_has_valid_pem_headers = (bool) preg_match(
'/^-----BEGIN\s.*PRIVATE KEY-----.*-----END\s.*PRIVATE KEY-----$/s',
OIDC_PRIVATE_KEY
);

if ( ! $key_is_defined ) {
$label = __( 'The private key constant OIDC_PRIVATE_KEY is not defined.', 'openid-connect-server' );
$status = 'critical';
$badge = 'red';
} elseif (
0 === strpos( OIDC_PRIVATE_KEY, '-----BEGIN RSA PRIVATE KEY-----' )
&& '-----END RSA PRIVATE KEY-----' === substr( OIDC_PRIVATE_KEY, - strlen( '-----END RSA PRIVATE KEY-----' ) )
&& strlen( OIDC_PRIVATE_KEY ) > 70
) {
} elseif ( $key_has_valid_pem_headers ) {
$label = __( 'The private key is defined and in the right format', 'openid-connect-server' );
$status = 'good';
$badge = 'green';
Expand Down
Loading