From e364f4f224d0a6106e4a151c3f8946ad950082a3 Mon Sep 17 00:00:00 2001 From: daggerhart Date: Fri, 20 Nov 2026 06:35:33 -0500 Subject: [PATCH 1/2] Rename login cookie so managed hosts pass it to PHP Hosts such as Pantheon only pass cookies with WordPress-style names through to PHP, so the login cookie never reached WordPress and every login failed with invalid-state (#702). Rename it to wp-openid_connect_generic_browser, add the openid-connect-generic-browser-cookie-name filter, and log why a state check failed. --- includes/openid-connect-generic-client.php | 45 +++++- .../openid-connect-generic-client_test.php | 131 +++++++++++++++++- 2 files changed, 166 insertions(+), 10 deletions(-) diff --git a/includes/openid-connect-generic-client.php b/includes/openid-connect-generic-client.php index 8ad83aae..06ecafda 100644 --- a/includes/openid-connect-generic-client.php +++ b/includes/openid-connect-generic-client.php @@ -128,11 +128,13 @@ class OpenID_Connect_Generic_Client { private $state_time_limit = 180; /** - * Cookie that identifies the browser that started a login. + * Default name of the cookie that identifies the browser that started a login. + * + * Managed hosts such as Pantheon only pass cookies with WordPress-style names to PHP. * * @var string */ - const BROWSER_COOKIE = 'openid-connect-generic-browser'; + const BROWSER_COOKIE = 'wp-openid_connect_generic_browser'; /** * Allow HTTP requests to internal/private network endpoints. @@ -501,6 +503,7 @@ public function check_state( $state ) { if ( ! is_string( $state ) || ! preg_match( '/^[a-f0-9]{32}$/', $state ) ) { do_action( 'openid-connect-generic-state-not-found', $state ); + $this->logger->log( 'State is malformed.', 'invalid-state' ); return false; } @@ -518,14 +521,25 @@ public function check_state( $state ) { } if ( ! $valid ) { + $this->logger->log( 'State not found or expired.', 'invalid-state' ); return false; } // The state must come back to the browser that started the login, and only once. $binding = get_transient( 'openid-connect-generic-state--' . $state . '--browser' ); $browser_id = $this->get_browser_id(); - if ( empty( $binding ) || empty( $browser_id ) || ! hash_equals( $binding, $this->hash_browser_id( $browser_id ) ) ) { + $reason = ''; + if ( empty( $binding ) ) { + $reason = 'State is not linked to a browser.'; + } elseif ( empty( $browser_id ) ) { + $reason = 'Browser cookie missing from the request.'; + } elseif ( ! hash_equals( $binding, $this->hash_browser_id( $browser_id ) ) ) { + $reason = 'Browser cookie does not match the state.'; + } + + if ( ! empty( $reason ) ) { do_action( 'openid-connect-generic-state-not-found', $state ); + $this->logger->log( $reason, 'invalid-state' ); return false; } @@ -676,17 +690,34 @@ public function delete_state( $state ) { delete_transient( 'openid-connect-generic-state--' . $state . '--pkce' ); } + /** + * Get the name of the browser cookie. + * + * @return string + */ + public function get_browser_cookie_name() { + /** + * Filters the name of the cookie that identifies the browser that started a login. + * + * @param string $name The cookie name. + */ + $name = apply_filters( 'openid-connect-generic-browser-cookie-name', self::BROWSER_COOKIE ); + + return is_string( $name ) && preg_match( '/^[A-Za-z0-9_-]+$/', $name ) ? $name : self::BROWSER_COOKIE; + } + /** * Get the browser identifier from the browser cookie. * * @return string Empty when the cookie is missing or malformed. */ public function get_browser_id() { - if ( empty( $_COOKIE[ self::BROWSER_COOKIE ] ) || ! is_string( $_COOKIE[ self::BROWSER_COOKIE ] ) ) { + $name = $this->get_browser_cookie_name(); + if ( empty( $_COOKIE[ $name ] ) || ! is_string( $_COOKIE[ $name ] ) ) { return ''; } - $browser_id = sanitize_text_field( wp_unslash( $_COOKIE[ self::BROWSER_COOKIE ] ) ); + $browser_id = sanitize_text_field( wp_unslash( $_COOKIE[ $name ] ) ); return preg_match( '/^[a-f0-9]{32}$/', $browser_id ) ? $browser_id : ''; } @@ -708,7 +739,7 @@ public function ensure_browser_id() { $browser_id = bin2hex( random_bytes( 16 ) ); $this->send_browser_cookie( $browser_id ); - $_COOKIE[ self::BROWSER_COOKIE ] = $browser_id; + $_COOKIE[ $this->get_browser_cookie_name() ] = $browser_id; return $browser_id; } @@ -731,7 +762,7 @@ protected function headers_sent() { */ protected function send_browser_cookie( $browser_id ) { setcookie( - self::BROWSER_COOKIE, + $this->get_browser_cookie_name(), $browser_id, array( 'expires' => 0, diff --git a/tests/phpunit/includes/openid-connect-generic-client_test.php b/tests/phpunit/includes/openid-connect-generic-client_test.php index 33f512fa..e507a325 100644 --- a/tests/phpunit/includes/openid-connect-generic-client_test.php +++ b/tests/phpunit/includes/openid-connect-generic-client_test.php @@ -341,12 +341,15 @@ private function create_client( $settings = array() ) { /** * Create a client whose header and cookie output can be controlled. * - * @param bool $headers_sent Whether headers count as already sent. + * @param bool $headers_sent Whether headers count as already sent. + * @param OpenID_Connect_Generic_Option_Logger|null $logger Logger, or null for a mock. * * @return OpenID_Connect_Generic_Client */ - private function create_browser_client( bool $headers_sent = false ): OpenID_Connect_Generic_Client { - $logger = $this->createMock( OpenID_Connect_Generic_Option_Logger::class ); + private function create_browser_client( bool $headers_sent = false, $logger = null ): OpenID_Connect_Generic_Client { + if ( null === $logger ) { + $logger = $this->createMock( OpenID_Connect_Generic_Option_Logger::class ); + } return new class( $headers_sent, $logger ) extends OpenID_Connect_Generic_Client { /** @@ -460,6 +463,128 @@ public function test_check_state_refuses_malformed_state() { $this->assertFalse( $client->check_state( array( 'a' ) ) ); } + /** + * The default browser cookie name uses a WordPress prefix that managed hosts pass to PHP. + * + * @group ClientTests + * @group StateTests + */ + public function test_browser_cookie_name_default() { + $client = $this->create_browser_client(); + + $this->assertSame( 'wp-openid_connect_generic_browser', $client->get_browser_cookie_name() ); + $this->assertMatchesRegularExpression( '/^wp-[A-Za-z0-9_]+$/', $client->get_browser_cookie_name() ); + } + + /** + * The browser cookie name can be changed with a filter, and the login uses the new name. + * + * @group ClientTests + * @group StateTests + */ + public function test_browser_cookie_name_filter() { + $filter = function () { + return 'custom_oidc_browser'; + }; + add_filter( 'openid-connect-generic-browser-cookie-name', $filter ); + + $client = $this->create_browser_client(); + $state = $this->start_bound_state( $client ); + + $this->assertSame( 'custom_oidc_browser', $client->get_browser_cookie_name() ); + $this->assertArrayHasKey( 'custom_oidc_browser', $_COOKIE ); + $this->assertArrayNotHasKey( OpenID_Connect_Generic_Client::BROWSER_COOKIE, $_COOKIE ); + $this->assertTrue( $client->check_state( $state ) ); + + unset( $_COOKIE['custom_oidc_browser'] ); + remove_filter( 'openid-connect-generic-browser-cookie-name', $filter ); + } + + /** + * Invalid filtered cookie names fall back to the default. + * + * @dataProvider invalid_browser_cookie_name_provider + * @group ClientTests + * @group StateTests + * + * @param mixed $name The filtered name. + */ + public function test_browser_cookie_name_invalid_filter( $name ) { + $filter = function () use ( $name ) { + return $name; + }; + add_filter( 'openid-connect-generic-browser-cookie-name', $filter ); + + $client = $this->create_browser_client(); + $this->assertSame( OpenID_Connect_Generic_Client::BROWSER_COOKIE, $client->get_browser_cookie_name() ); + + remove_filter( 'openid-connect-generic-browser-cookie-name', $filter ); + } + + /** + * Invalid cookie names. + * + * @return array + */ + public function invalid_browser_cookie_name_provider() { + return array( + 'empty' => array( '' ), + 'space' => array( 'has space' ), + 'dot' => array( 'has.dot' ), + 'semicolon' => array( 'a;b' ), + 'array' => array( array( 'a' ) ), + ); + } + + /** + * A failed state check logs which part of the check failed. + * + * @dataProvider failed_state_reason_provider + * @group ClientTests + * @group StateTests + * + * @param string $scenario The failure to set up. + * @param string $reason The expected log message. + */ + public function test_check_state_logs_failure_reason( $scenario, $reason ) { + $this->clear_browser_cookie(); + $logger = $this->createMock( OpenID_Connect_Generic_Option_Logger::class ); + $logger->expects( $this->once() ) + ->method( 'log' ) + ->with( $reason, 'invalid-state' ); + + $client = $this->create_browser_client( false, $logger ); + $state = $client->new_state( home_url() ); + + if ( 'missing-state' === $scenario ) { + $state = str_repeat( 'c', 32 ); + } elseif ( 'missing-cookie' === $scenario ) { + $client->bind_state( $state, $client->ensure_browser_id() ); + $this->clear_browser_cookie(); + } elseif ( 'other-browser' === $scenario ) { + $client->bind_state( $state, $client->ensure_browser_id() ); + $_COOKIE[ OpenID_Connect_Generic_Client::BROWSER_COOKIE ] = str_repeat( 'a', 32 ); + } + + $this->assertFalse( $client->check_state( $state ) ); + + $this->clear_browser_cookie(); + } + + /** + * State check failures and their log messages. + * + * @return array + */ + public function failed_state_reason_provider() { + return array( + 'state not found' => array( 'missing-state', 'State not found or expired.' ), + 'state not bound' => array( 'unbound', 'State is not linked to a browser.' ), + 'browser cookie lost' => array( 'missing-cookie', 'Browser cookie missing from the request.' ), + 'other browser cookie' => array( 'other-browser', 'Browser cookie does not match the state.' ), + ); + } + /** * The browser cookie is created only while headers can still be sent, and reused after. * From 7bdec067a69541dbfead45f6b36a5c49d7282af5 Mon Sep 17 00:00:00 2001 From: daggerhart Date: Fri, 20 Nov 2026 06:36:10 -0500 Subject: [PATCH 2/2] Release version 3.11.5 --- CHANGELOG.md | 5 +++ README.md | 11 +++++- languages/openid-connect-generic.pot | 58 ++++++++++++++-------------- openid-connect-generic.php | 4 +- package-lock.json | 4 +- package.json | 2 +- readme.txt | 11 +++++- 7 files changed, 59 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4126f829..81bbbcdd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # OpenId Connect Generic Changelog +**3.11.5** + +* Fix: Logins failed with "Invalid state" on hosts that only pass WordPress-style cookie names to PHP, such as Pantheon. The login cookie is now named `wp-openid_connect_generic_browser`, and the new `openid-connect-generic-browser-cookie-name` filter can change it. +* Improvement: When logging is enabled, a failed state check logs which part of the check failed. + **3.11.4** * Security: Login state is now tied to the browser that started the login and can only be used once. diff --git a/README.md b/README.md index 7c7491e6..703a831b 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ **Tags:** security, login, oauth2, openidconnect, apps, authentication, autologin, sso **Requires at least:** 5.0 **Tested up to:** 7.1 -**Stable tag:** 3.11.4 +**Stable tag:** 3.11.5 **Requires PHP:** 8.0 **License:** GPLv2 or later **License URI:** http://www.gnu.org/licenses/gpl-2.0.html @@ -49,6 +49,10 @@ On the settings page for this plugin (Dashboard > Settings > OpenID Connect Gene ## Upgrade Notice ## +### 3.11.5 ### + +Fixes logins failing with "Invalid state" on Pantheon and some other managed hosts after updating to 3.11.4. + ### 3.11.4 ### SECURITY UPDATE: Hardens login and account linking. Requires PHP 8.0. If you use Link Existing Users, review the new Link Users Without Verified Email setting after updating. @@ -59,6 +63,11 @@ SECURITY UPDATE: 3.11.x branch - Fixes authentication vulnerabilities including ## Changelog ## +### 3.11.5 ### + +* Fix: Logins failed with "Invalid state" on hosts that only pass WordPress-style cookie names to PHP, such as Pantheon. The login cookie is now named `wp-openid_connect_generic_browser`, and the new `openid-connect-generic-browser-cookie-name` filter can change it. +* Improvement: When logging is enabled, a failed state check logs which part of the check failed. + ### 3.11.4 ### * Security: Login state is now tied to the browser that started the login and can only be used once. diff --git a/languages/openid-connect-generic.pot b/languages/openid-connect-generic.pot index 299c6fa0..90b24570 100644 --- a/languages/openid-connect-generic.pot +++ b/languages/openid-connect-generic.pot @@ -2,10 +2,10 @@ # This file is distributed under the GPL-2.0+. msgid "" msgstr "" -"Project-Id-Version: OpenID Connect Generic 3.11.4\n" +"Project-Id-Version: OpenID Connect Generic 3.11.5\n" "Report-Msgid-Bugs-To: " "https://github.com/oidc-wp/openid-connect-generic/issues\n" -"POT-Creation-Date: 2026-11-15 13:25:51+00:00\n" +"POT-Creation-Date: 2026-11-20 11:35:59+00:00\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" @@ -26,7 +26,7 @@ msgstr "" "X-Generator: grunt-wp-i18n 1.0.3\n" #: includes/openid-connect-generic-client-wrapper.php:295 -#: includes/openid-connect-generic-client.php:281 +#: includes/openid-connect-generic-client.php:283 msgid "Invalid state." msgstr "" @@ -74,113 +74,113 @@ msgid "" "administrator." msgstr "" -#: includes/openid-connect-generic-client.php:277 +#: includes/openid-connect-generic-client.php:279 msgid "Missing state." msgstr "" -#: includes/openid-connect-generic-client.php:296 +#: includes/openid-connect-generic-client.php:298 msgid "Missing authentication code." msgstr "" -#: includes/openid-connect-generic-client.php:346 +#: includes/openid-connect-generic-client.php:348 msgid "Request for authentication token failed." msgstr "" -#: includes/openid-connect-generic-client.php:379 +#: includes/openid-connect-generic-client.php:381 msgid "Refresh token failed." msgstr "" -#: includes/openid-connect-generic-client.php:394 +#: includes/openid-connect-generic-client.php:396 msgid "Missing token body." msgstr "" -#: includes/openid-connect-generic-client.php:402 +#: includes/openid-connect-generic-client.php:404 msgid "Invalid token." msgstr "" -#: includes/openid-connect-generic-client.php:463 +#: includes/openid-connect-generic-client.php:465 msgid "Request for userinfo failed." msgstr "" -#: includes/openid-connect-generic-client.php:581 +#: includes/openid-connect-generic-client.php:595 msgid "Invalid nonce." msgstr "" -#: includes/openid-connect-generic-client.php:767 +#: includes/openid-connect-generic-client.php:798 msgid "Missing authentication state." msgstr "" -#: includes/openid-connect-generic-client.php:804 +#: includes/openid-connect-generic-client.php:835 msgid "No identity token." msgstr "" -#: includes/openid-connect-generic-client.php:844 +#: includes/openid-connect-generic-client.php:875 msgid "Missing identity token." msgstr "" -#: includes/openid-connect-generic-client.php:903 +#: includes/openid-connect-generic-client.php:934 msgid "Bad ID token claim." msgstr "" -#: includes/openid-connect-generic-client.php:908 +#: includes/openid-connect-generic-client.php:939 msgid "No subject identity." msgstr "" -#: includes/openid-connect-generic-client.php:913 +#: includes/openid-connect-generic-client.php:944 #: includes/openid-connect-generic-jwt-validator.php:184 msgid "Token missing expiration claim." msgstr "" -#: includes/openid-connect-generic-client.php:916 +#: includes/openid-connect-generic-client.php:947 msgid "Token has expired." msgstr "" -#: includes/openid-connect-generic-client.php:921 +#: includes/openid-connect-generic-client.php:952 #: includes/openid-connect-generic-jwt-validator.php:192 msgid "Token missing issued at claim." msgstr "" -#: includes/openid-connect-generic-client.php:926 +#: includes/openid-connect-generic-client.php:957 #: includes/openid-connect-generic-jwt-validator.php:200 msgid "Token missing audience claim." msgstr "" -#: includes/openid-connect-generic-client.php:939 +#: includes/openid-connect-generic-client.php:970 #: includes/openid-connect-generic-jwt-validator.php:217 msgid "Token audience does not match client." msgstr "" -#: includes/openid-connect-generic-client.php:949 +#: includes/openid-connect-generic-client.php:980 #: includes/openid-connect-generic-jwt-validator.php:226 msgid "Token missing issuer claim." msgstr "" -#: includes/openid-connect-generic-client.php:964 +#: includes/openid-connect-generic-client.php:995 #: includes/openid-connect-generic-jwt-validator.php:241 msgid "Token issuer does not match expected issuer." msgstr "" -#: includes/openid-connect-generic-client.php:974 +#: includes/openid-connect-generic-client.php:1005 msgid "No matching acr values." msgstr "" -#: includes/openid-connect-generic-client.php:994 +#: includes/openid-connect-generic-client.php:1025 msgid "Bad user claim." msgstr "" -#: includes/openid-connect-generic-client.php:1014 +#: includes/openid-connect-generic-client.php:1045 msgid "Invalid user claim." msgstr "" -#: includes/openid-connect-generic-client.php:1019 +#: includes/openid-connect-generic-client.php:1050 msgid "Error from the IDP." msgstr "" -#: includes/openid-connect-generic-client.php:1028 +#: includes/openid-connect-generic-client.php:1059 msgid "Incorrect user claim." msgstr "" -#: includes/openid-connect-generic-client.php:1035 +#: includes/openid-connect-generic-client.php:1066 msgid "Unauthorized access." msgstr "" diff --git a/openid-connect-generic.php b/openid-connect-generic.php index 9c527710..fd144d6a 100644 --- a/openid-connect-generic.php +++ b/openid-connect-generic.php @@ -16,7 +16,7 @@ * Plugin Name: OpenID Connect Generic * Plugin URI: https://github.com/oidc-wp/openid-connect-generic * Description: Connect to an OpenID Connect identity provider using Authorization Code Flow. - * Version: 3.11.4 + * Version: 3.11.5 * Requires at least: 5.0 * Requires PHP: 8.0 * Author: daggerhart @@ -93,7 +93,7 @@ class OpenID_Connect_Generic { * * @var string */ - const VERSION = '3.11.4'; + const VERSION = '3.11.5'; /** * Option flagging the admin notice about linking users without a verified email. diff --git a/package-lock.json b/package-lock.json index f6866cfa..0bbed59a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "openid-connect-generic", - "version": "3.11.4", + "version": "3.11.5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "openid-connect-generic", - "version": "3.11.4", + "version": "3.11.5", "license": "GPL-2.0-only", "dependencies": { "dev-require": "^0.1.0" diff --git a/package.json b/package.json index 9e9e834e..d3555f75 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "openid-connect-generic", - "version": "3.11.4", + "version": "3.11.5", "description": "OpenID Connect generic WordPress plugin.", "main": "Gruntfile.js", "repository": { diff --git a/readme.txt b/readme.txt index 5d0c309b..bdc220dd 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: daggerhart, tnolte Tags: security, login, oauth2, openidconnect, apps, authentication, autologin, sso Requires at least: 5.0 Tested up to: 7.1 -Stable tag: 3.11.4 +Stable tag: 3.11.5 Requires PHP: 8.0 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -49,6 +49,10 @@ On the settings page for this plugin (Dashboard > Settings > OpenID Connect Gene == Upgrade Notice == += 3.11.5 = + +Fixes logins failing with "Invalid state" on Pantheon and some other managed hosts after updating to 3.11.4. + = 3.11.4 = SECURITY UPDATE: Hardens login and account linking. Requires PHP 8.0. If you use Link Existing Users, review the new Link Users Without Verified Email setting after updating. @@ -59,6 +63,11 @@ SECURITY UPDATE: 3.11.x branch - Fixes authentication vulnerabilities including == Changelog == += 3.11.5 = + +* Fix: Logins failed with "Invalid state" on hosts that only pass WordPress-style cookie names to PHP, such as Pantheon. The login cookie is now named `wp-openid_connect_generic_browser`, and the new `openid-connect-generic-browser-cookie-name` filter can change it. +* Improvement: When logging is enabled, a failed state check logs which part of the check failed. + = 3.11.4 = * Security: Login state is now tied to the browser that started the login and can only be used once.