diff --git a/inc/sso/class-magic-link.php b/inc/sso/class-magic-link.php index 2c9c3be07..3953d7b15 100644 --- a/inc/sso/class-magic-link.php +++ b/inc/sso/class-magic-link.php @@ -805,7 +805,7 @@ protected function verify_user_site_access($user_id, $site_id) { return false; } - if (is_super_admin($user_id) || is_user_member_of_blog($user_id, $site_id)) { + if ((is_super_admin($user_id) || is_user_member_of_blog($user_id, $site_id)) && $this->is_site_host_token_allowed($user_id, $site_id)) { return true; } // Check if the site is the dashboard site in WP Frontend Admin which the user would not be a member of. @@ -815,6 +815,32 @@ protected function verify_user_site_access($user_id, $site_id) { return false; } + /** + * Apply the SSO host token policy to a site's host. + * + * Login tokens must not be minted for, or redeemed on, customer-controlled + * mapped domains for network administrators: whoever controls the domain's + * DNS could capture the token (GHSA-qh2w-5cww-9wxr). + * + * @since n.e.x.t + * + * @param int $user_id User ID. + * @param int $site_id Site ID. + * @return bool True when a token may be used for the site's host. + */ + protected function is_site_host_token_allowed($user_id, $site_id) { + + $site = wu_get_site($site_id); + + if ( ! $site) { + return false; + } + + $host = (string) wp_parse_url($site->get_active_site_url(), PHP_URL_HOST); + + return SSO::get_instance()->can_use_cookie_less_sso_token((int) $user_id, $host); + } + /** * Verify that a user owns a payable payment and its fixed checkout URL. * diff --git a/inc/sso/class-sso.php b/inc/sso/class-sso.php index 96cb2db7d..fa47d6b28 100644 --- a/inc/sso/class-sso.php +++ b/inc/sso/class-sso.php @@ -1187,7 +1187,7 @@ private function add_cookie_less_sso_token(string $url, int $user_id): string { * @param string $host Token audience host. * @return bool */ - private function can_use_cookie_less_sso_token(int $user_id, string $host): bool { + public function can_use_cookie_less_sso_token(int $user_id, string $host): bool { $host = strtolower(trim((string) preg_replace('/:\d+$/', '', $host))); diff --git a/tests/WP_Ultimo/SSO/SSO_Coverage_Test.php b/tests/WP_Ultimo/SSO/SSO_Coverage_Test.php index 6143e4382..7683d263d 100644 --- a/tests/WP_Ultimo/SSO/SSO_Coverage_Test.php +++ b/tests/WP_Ultimo/SSO/SSO_Coverage_Test.php @@ -2302,6 +2302,35 @@ private function create_customer_mapped_site(string $domain_name): array { return [$blog_id, $domain]; } + /** + * Network admins get no magic link access for customer-mapped domains; members keep it. + */ + public function test_magic_link_access_denied_for_super_admin_on_customer_mapped_domain(): void { + $admin_id = self::factory()->user->create(); + $member_id = self::factory()->user->create(); + grant_super_admin($admin_id); + + [$blog_id, $domain] = $this->create_customer_mapped_site('magic-customer.example.net'); + + add_user_to_blog($blog_id, $member_id, 'subscriber'); + add_user_to_blog($blog_id, $admin_id, 'subscriber'); + + $magic = Magic_Link::get_instance(); + $ref = new \ReflectionMethod($magic, 'verify_user_site_access'); + + if (PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } + + try { + $this->assertFalse($ref->invoke($magic, $admin_id, $blog_id)); + $this->assertTrue($ref->invoke($magic, $member_id, $blog_id)); + } finally { + revoke_super_admin($admin_id); + $this->delete_customer_mapped_site($domain); + } + } + /** * Deletes a mapped domain fixture and its lookup cache. *