From 16ba05ef2f2ad5129d43238521c3b1e049dffea3 Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Tue, 22 Sep 2026 17:29:43 +0530 Subject: [PATCH 01/23] fix: authenticate cross-site user deletion requests --- inc/Modules/Core/Hooks.php | 32 +++++++++++++++++++ inc/Modules/Rest/Abstract_REST_Controller.php | 5 ++- .../Rest/Governing_Site_Controller.php | 15 +++++---- inc/Modules/Settings/Settings.php | 27 +++++++++++++--- 4 files changed, 66 insertions(+), 13 deletions(-) diff --git a/inc/Modules/Core/Hooks.php b/inc/Modules/Core/Hooks.php index 81b75b1..ded5d87 100644 --- a/inc/Modules/Core/Hooks.php +++ b/inc/Modules/Core/Hooks.php @@ -37,6 +37,9 @@ public function __construct() { * {@inheritDoc} */ public function register_hooks(): void { + // Needed on both site types, so it must come before the consumer check below. + add_filter( 'http_request_host_is_external', [ $this, 'allow_oneaccess_host' ], 10, 2 ); + // Early return if this is not a consumer site. if ( ! Settings::is_consumer_site() ) { return; @@ -63,4 +66,33 @@ public function register_hooks(): void { public function user_deduplication(): void { $this->actions_controller->send_users_for_deduplication(); } + + /** + * Allow outbound requests to the configured OneAccess sites. + * + * `wp_safe_remote_*()` rejects hosts that resolve to a private or loopback address, which + * blocks site-to-site traffic in local and internally routed environments. Only the hosts + * OneAccess is configured to talk to are allowed; everything else keeps the default check. + * + * @internal Hook callback + * + * @param bool $is_external Whether the host is considered external. + * @param string $host Host name of the request. + */ + public function allow_oneaccess_host( $is_external, $host ): bool { + if ( ! empty( $is_external ) ) { + return true; + } + + $urls = array_column( Settings::get_shared_sites(), 'url' ); + $urls[] = (string) Settings::get_parent_site_url(); + + foreach ( $urls as $url ) { + if ( ! empty( $url ) && wp_parse_url( $url, PHP_URL_HOST ) === $host ) { + return true; + } + } + + return false; + } } diff --git a/inc/Modules/Rest/Abstract_REST_Controller.php b/inc/Modules/Rest/Abstract_REST_Controller.php index 019e4b9..fdd2c08 100644 --- a/inc/Modules/Rest/Abstract_REST_Controller.php +++ b/inc/Modules/Rest/Abstract_REST_Controller.php @@ -113,7 +113,10 @@ public function check_api_permissions( $request ) { } // if token is valid and request is from different domain then check if it matches governing site url. - return self::is_same_domain( $governing_site_url, $request_origin ) || false !== strpos( $user_agent, $governing_site_url ); + // The user-agent carries home_url(), whose scheme can differ from the stored URL, so match on the host. + $governing_host = (string) wp_parse_url( $governing_site_url, PHP_URL_HOST ); + + return self::is_same_domain( $governing_site_url, $request_origin ) || ( '' !== $governing_host && false !== strpos( $user_agent, $governing_host ) ); } /** diff --git a/inc/Modules/Rest/Governing_Site_Controller.php b/inc/Modules/Rest/Governing_Site_Controller.php index a172109..6307317 100644 --- a/inc/Modules/Rest/Governing_Site_Controller.php +++ b/inc/Modules/Rest/Governing_Site_Controller.php @@ -417,11 +417,10 @@ public function delete_user_from_sites( \WP_REST_Request $request ): \WP_REST_Re ); } - $response_data = []; - $oneaccess_sites_info = Settings::get_shared_sites(); - $processed_sites = []; - $error_log = []; - $user_delete_results = []; + $response_data = []; + $processed_sites = []; + $error_log = []; + $user_delete_results = []; foreach ( $sites as $site ) { @@ -433,8 +432,9 @@ public function delete_user_from_sites( \WP_REST_Request $request ): \WP_REST_Re continue; } - $request_url = $site['site_url'] . '/wp-json/' . self::NAMESPACE . '/delete-user'; - $api_key = $oneaccess_sites_info[ $site['site_url'] ]['api_key'] ?? ''; + $site_info = Settings::get_shared_site_by_url( $site['site_url'] ); + $request_url = untrailingslashit( $site_info['url'] ?? $site['site_url'] ) . '/wp-json/' . self::NAMESPACE . '/delete-user'; + $api_key = $site_info['api_key'] ?? ''; $response = wp_safe_remote_request( $request_url, [ @@ -445,6 +445,7 @@ public function delete_user_from_sites( \WP_REST_Request $request ): \WP_REST_Re ], 'headers' => [ 'X-OneAccess-Token' => $api_key, + 'Origin' => get_site_url(), ], ] ); diff --git a/inc/Modules/Settings/Settings.php b/inc/Modules/Settings/Settings.php index 33c056d..73c861f 100644 --- a/inc/Modules/Settings/Settings.php +++ b/inc/Modules/Settings/Settings.php @@ -222,7 +222,7 @@ public static function sanitize_shared_sites( $input ): array { */ /** - * Get brand sites configured for this governing site, keyed by the (trailing-slash) URL. + * Get brand sites configured for this governing site, keyed by the site URL. * * @return array $brand['api_key'] ?? '', @@ -269,9 +268,27 @@ public static function get_shared_sites(): array { public static function get_shared_site_by_url( string $site_url ): ?array { $brand_sites = self::get_shared_sites(); - $normalized_url = trailingslashit( $site_url ); + $normalized_url = self::normalize_site_url( $site_url ); - return $brand_sites[ $normalized_url ] ?? null; + foreach ( $brand_sites as $site ) { + if ( self::normalize_site_url( $site['url'] ) === $normalized_url ) { + return $site; + } + } + + return null; + } + + /** + * Normalize a site URL for comparison. + * + * A site reports itself via `get_site_url()`, whose scheme and trailing slash can differ + * from the URL configured on the governing site, so strip both before comparing. + * + * @param string $site_url The site URL. + */ + private static function normalize_site_url( string $site_url ): string { + return (string) preg_replace( '#^https?://#i', '', untrailingslashit( $site_url ) ); } /** From a4da9cd0445c4404bc2f2c0fdb43b5493448ffe8 Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Tue, 22 Sep 2026 18:17:00 +0530 Subject: [PATCH 02/23] =?UTF-8?q?fix:=20defer=20brand=20admin=20hooks=20un?= =?UTF-8?q?til=20pluggable.php=20is=20loaded=20=E2=80=94=20Profile=5FReque?= =?UTF-8?q?st.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/Modules/User/Profile_Request.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/inc/Modules/User/Profile_Request.php b/inc/Modules/User/Profile_Request.php index 3a68d0a..3738d8a 100644 --- a/inc/Modules/User/Profile_Request.php +++ b/inc/Modules/User/Profile_Request.php @@ -31,6 +31,14 @@ public function register_hooks(): void { add_action( 'personal_options_update', [ $this, 'store_profile_update_request' ] ); add_action( 'edit_user_profile_update', [ $this, 'store_profile_update_request' ] ); + // the current user is not available until pluggable.php has loaded, so defer. + add_action( 'init', [ $this, 'register_brand_admin_hooks' ] ); + } + + /** + * Register the hooks that only apply to brand admins. + */ + public function register_brand_admin_hooks(): void { // get current user. $current_user = wp_get_current_user(); From 109d9748d997e6c779a524828259ed1cdc1d1d07 Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Tue, 22 Sep 2026 18:21:18 +0530 Subject: [PATCH 03/23] fix: preserve passwords and single-word names when creating users --- assets/src/components/CreateUser.tsx | 14 ++- .../Rest/Governing_Site_Controller.php | 92 +++++++++++++++++-- 2 files changed, 95 insertions(+), 11 deletions(-) diff --git a/assets/src/components/CreateUser.tsx b/assets/src/components/CreateUser.tsx index 223ee54..df57e7e 100644 --- a/assets/src/components/CreateUser.tsx +++ b/assets/src/components/CreateUser.tsx @@ -201,6 +201,7 @@ const CreateUser = ( { message?: string; data?: { response_data?: CreateUserResult[]; + error_log?: { site_name?: string; message?: string }[]; }; }; if ( ! data.success ) { @@ -216,7 +217,18 @@ const CreateUser = ( { return; } - const results = data?.data?.response_data || []; + // Per-site failures are only reported in `error_log`, so merge them in as well. + const results: CreateUserResult[] = [ + ...( data?.data?.response_data || [] ), + ...( data?.data?.error_log || [] ).map( ( failure ) => ( { + status: 'error' as const, + site: failure.site_name ?? '', + message: + failure.message ?? + __( 'Failed to create user.', 'oneaccess' ), + } ) ), + ]; + const newNotices = results.map( ( result: CreateUserResult, index: number ) => ( { id: `notice-${ Date.now() }-${ index }`, diff --git a/inc/Modules/Rest/Governing_Site_Controller.php b/inc/Modules/Rest/Governing_Site_Controller.php index 6307317..5b1d0c4 100644 --- a/inc/Modules/Rest/Governing_Site_Controller.php +++ b/inc/Modules/Rest/Governing_Site_Controller.php @@ -18,6 +18,13 @@ * Class Governing_Site_Controller */ class Governing_Site_Controller extends Abstract_REST_Controller { + /** + * Minimum number of characters accepted for a new user's password. + * + * Mirrors the floor the admin UI enforces via its strength meter. + */ + public const MIN_PASSWORD_LENGTH = 8; + /** * {@inheritDoc} */ @@ -224,9 +231,8 @@ public function register_routes(): void { 'sanitize_callback' => 'sanitize_text_field', ], 'password' => [ - 'required' => true, - 'type' => 'string', - 'sanitize_callback' => 'sanitize_text_field', + 'required' => true, + 'type' => 'string', ], 'sites' => [ 'required' => true, @@ -693,8 +699,9 @@ public function add_user_to_sites( \WP_REST_Request $request ): \WP_REST_Respons $email = sanitize_email( $request->get_param( 'email' ) ); $username = sanitize_text_field( $request->get_param( 'username' ) ); $full_name = sanitize_text_field( $request->get_param( 'fullName' ) ); - $password = sanitize_text_field( $request->get_param( 'password' ) ); - $sites = $request->get_param( 'sites' ); + // Passwords are hashed, never rendered or queried, so they must reach wp_create_user() verbatim. + $password = (string) $request->get_param( 'password' ); + $sites = $request->get_param( 'sites' ); if ( empty( $email ) || empty( $username ) || empty( $full_name ) || empty( $password ) || empty( $sites ) ) { return new \WP_REST_Response( @@ -706,6 +713,20 @@ public function add_user_to_sites( \WP_REST_Request $request ): \WP_REST_Respons ); } + if ( strlen( $password ) < self::MIN_PASSWORD_LENGTH ) { + return new \WP_REST_Response( + [ + 'success' => false, + 'message' => sprintf( + /* translators: %d is the minimum number of characters */ + __( 'Password must be at least %d characters long.', 'oneaccess' ), + self::MIN_PASSWORD_LENGTH + ), + ], + 400 + ); + } + // Validate sites. if ( ! is_array( $sites ) ) { return new \WP_REST_Response( @@ -1028,9 +1049,10 @@ public function update_user_roles_for_sites( \WP_REST_Request $request ): \WP_RE * @param \WP_REST_Request $request The REST request object. */ public function create_user( \WP_REST_Request $request ): \WP_REST_Response { - $username = sanitize_user( $request->get_param( 'username' ) ); - $email = sanitize_email( $request->get_param( 'email' ) ); - $password = sanitize_text_field( $request->get_param( 'password' ) ); + $username = sanitize_user( $request->get_param( 'username' ) ); + $email = sanitize_email( $request->get_param( 'email' ) ); + // Passwords are hashed, never rendered or queried, so they must reach wp_create_user() verbatim. + $password = (string) $request->get_param( 'password' ); $full_name = sanitize_text_field( $request->get_param( 'full_name' ) ); $role = sanitize_text_field( $request->get_param( 'role' ) ); @@ -1044,6 +1066,20 @@ public function create_user( \WP_REST_Request $request ): \WP_REST_Response { ); } + if ( strlen( $password ) < self::MIN_PASSWORD_LENGTH ) { + return new \WP_REST_Response( + [ + 'success' => false, + 'message' => sprintf( + /* translators: %d is the minimum number of characters */ + __( 'Password must be at least %d characters long.', 'oneaccess' ), + self::MIN_PASSWORD_LENGTH + ), + ], + 400 + ); + } + if ( ! is_email( $email ) ) { return new \WP_REST_Response( [ @@ -1094,14 +1130,17 @@ public function create_user( \WP_REST_Request $request ): \WP_REST_Response { $role = 'subscriber'; } + // Split on the first run of whitespace; a single-word name simply has no last name. + $name_parts = preg_split( '/\s+/', trim( $full_name ), 2 ) ?: []; + // Set the user's full name and role. wp_update_user( [ 'ID' => $user_id, 'display_name' => $full_name, 'user_nicename' => sanitize_title( $full_name ), - 'first_name' => explode( ' ', $full_name )[0] ?: '', - 'last_name' => explode( ' ', $full_name )[1] ?: '', + 'first_name' => $name_parts[0] ?? '', + 'last_name' => $name_parts[1] ?? '', 'role' => $role ?: 'subscriber', ] ); @@ -1365,6 +1404,19 @@ public function create_users_for_sites( \WP_REST_Request $request ): \WP_REST_Re 400 ); } + if ( strlen( (string) $userdata['password'] ) < self::MIN_PASSWORD_LENGTH ) { + return new \WP_REST_Response( + [ + 'success' => false, + 'message' => sprintf( + /* translators: %d is the minimum number of characters */ + __( 'Password must be at least %d characters long.', 'oneaccess' ), + self::MIN_PASSWORD_LENGTH + ), + ], + 400 + ); + } if ( ! isset( $userdata['fullName'] ) || empty( $userdata['fullName'] ) ) { return new \WP_REST_Response( [ @@ -1434,7 +1486,27 @@ public function create_users_for_sites( \WP_REST_Request $request ): \WP_REST_Re continue; } + $response_code = wp_remote_retrieve_response_code( $response ); $response_body = json_decode( wp_remote_retrieve_body( $response ), true ); + + /* + * A body that will not decode means something other than the REST response was written to + * the stream, typically a PHP notice raised after the user was already created. Report it + * separately so it is not mistaken for the site rejecting the request. + */ + if ( ! is_array( $response_body ) ) { + $error_log[] = [ + 'site_name' => $site_name, + 'message' => sprintf( + /* translators: 1: site name, 2: HTTP status code */ + __( 'Unreadable response from site %1$s (HTTP %2$d). The user may have been created there; please verify before retrying.', 'oneaccess' ), + $site_name, + (int) $response_code + ), + ]; + continue; + } + if ( empty( $response_body['success'] ) ) { $error_log[] = [ 'site_name' => $site_name, From 955992d30fbd578b5984a655243e616d912a2a36 Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Tue, 22 Sep 2026 19:30:37 +0530 Subject: [PATCH 04/23] fix: toast notification postion --- assets/src/css/admin.scss | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/assets/src/css/admin.scss b/assets/src/css/admin.scss index d6d0e5c..302553d 100644 --- a/assets/src/css/admin.scss +++ b/assets/src/css/admin.scss @@ -3,28 +3,27 @@ #oneaccess-settings-page, #oneaccess-manage-user { - &:has(.components-snackbar-list) { - - .components-snackbar-list { - position: fixed; - bottom: 20px; - right: 20px; - z-index: 1000000; - align-items: flex-end; - justify-content: flex-end; - display: flex; - flex-direction: column; - } + /* Pin toasts bottom-right, from a list or standalone. */ + .components-snackbar-list, + .components-snackbar { + position: fixed; + bottom: 20px; + right: 20px; + z-index: 1000000; + width: auto; } - &:not(:has(.components-snackbar-list)) { + .components-snackbar-list { + align-items: flex-end; + justify-content: flex-end; + display: flex; + flex-direction: column; + /* Snackbars in a list are laid out by the list itself. */ .components-snackbar { - position: fixed; - bottom: 20px; - right: 20px; - z-index: 1000000; - width: auto; + position: static; + bottom: auto; + right: auto; } } @@ -41,8 +40,6 @@ background-color: #e11d1d; color: #fff; } - - } .toplevel_page_oneaccess { @@ -52,7 +49,6 @@ } } - body { &.oneaccess-missing-brand-sites, From e7eb9a28d8ceef5583868aa19df854ed52b21b62 Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Tue, 22 Sep 2026 20:22:11 +0530 Subject: [PATCH 05/23] chore: minor refactoring of comments --- assets/src/css/admin.scss | 1 - inc/Modules/Core/Hooks.php | 4 ---- inc/Modules/Rest/Abstract_REST_Controller.php | 2 -- inc/Modules/Rest/Governing_Site_Controller.php | 8 -------- 4 files changed, 15 deletions(-) diff --git a/assets/src/css/admin.scss b/assets/src/css/admin.scss index 302553d..b429d31 100644 --- a/assets/src/css/admin.scss +++ b/assets/src/css/admin.scss @@ -3,7 +3,6 @@ #oneaccess-settings-page, #oneaccess-manage-user { - /* Pin toasts bottom-right, from a list or standalone. */ .components-snackbar-list, .components-snackbar { position: fixed; diff --git a/inc/Modules/Core/Hooks.php b/inc/Modules/Core/Hooks.php index ded5d87..ccb3fa4 100644 --- a/inc/Modules/Core/Hooks.php +++ b/inc/Modules/Core/Hooks.php @@ -70,10 +70,6 @@ public function user_deduplication(): void { /** * Allow outbound requests to the configured OneAccess sites. * - * `wp_safe_remote_*()` rejects hosts that resolve to a private or loopback address, which - * blocks site-to-site traffic in local and internally routed environments. Only the hosts - * OneAccess is configured to talk to are allowed; everything else keeps the default check. - * * @internal Hook callback * * @param bool $is_external Whether the host is considered external. diff --git a/inc/Modules/Rest/Abstract_REST_Controller.php b/inc/Modules/Rest/Abstract_REST_Controller.php index fdd2c08..a873a19 100644 --- a/inc/Modules/Rest/Abstract_REST_Controller.php +++ b/inc/Modules/Rest/Abstract_REST_Controller.php @@ -112,8 +112,6 @@ public function check_api_permissions( $request ) { return false; } - // if token is valid and request is from different domain then check if it matches governing site url. - // The user-agent carries home_url(), whose scheme can differ from the stored URL, so match on the host. $governing_host = (string) wp_parse_url( $governing_site_url, PHP_URL_HOST ); return self::is_same_domain( $governing_site_url, $request_origin ) || ( '' !== $governing_host && false !== strpos( $user_agent, $governing_host ) ); diff --git a/inc/Modules/Rest/Governing_Site_Controller.php b/inc/Modules/Rest/Governing_Site_Controller.php index 5b1d0c4..985562c 100644 --- a/inc/Modules/Rest/Governing_Site_Controller.php +++ b/inc/Modules/Rest/Governing_Site_Controller.php @@ -20,8 +20,6 @@ class Governing_Site_Controller extends Abstract_REST_Controller { /** * Minimum number of characters accepted for a new user's password. - * - * Mirrors the floor the admin UI enforces via its strength meter. */ public const MIN_PASSWORD_LENGTH = 8; @@ -1130,7 +1128,6 @@ public function create_user( \WP_REST_Request $request ): \WP_REST_Response { $role = 'subscriber'; } - // Split on the first run of whitespace; a single-word name simply has no last name. $name_parts = preg_split( '/\s+/', trim( $full_name ), 2 ) ?: []; // Set the user's full name and role. @@ -1489,11 +1486,6 @@ public function create_users_for_sites( \WP_REST_Request $request ): \WP_REST_Re $response_code = wp_remote_retrieve_response_code( $response ); $response_body = json_decode( wp_remote_retrieve_body( $response ), true ); - /* - * A body that will not decode means something other than the REST response was written to - * the stream, typically a PHP notice raised after the user was already created. Report it - * separately so it is not mistaken for the site rejecting the request. - */ if ( ! is_array( $response_body ) ) { $error_log[] = [ 'site_name' => $site_name, From 8d63dbef5466bcb919d335e037ddd7edfe022817 Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Tue, 22 Sep 2026 20:40:14 +0530 Subject: [PATCH 06/23] fix: removed unused parameter --- inc/Modules/Core/Hooks.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/inc/Modules/Core/Hooks.php b/inc/Modules/Core/Hooks.php index ccb3fa4..20bde40 100644 --- a/inc/Modules/Core/Hooks.php +++ b/inc/Modules/Core/Hooks.php @@ -76,9 +76,6 @@ public function user_deduplication(): void { * @param string $host Host name of the request. */ public function allow_oneaccess_host( $is_external, $host ): bool { - if ( ! empty( $is_external ) ) { - return true; - } $urls = array_column( Settings::get_shared_sites(), 'url' ); $urls[] = (string) Settings::get_parent_site_url(); From 8af9a3fdf403357a63e1322f45131b402f803ffe Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Tue, 22 Sep 2026 20:40:48 +0530 Subject: [PATCH 07/23] chore: formatting fix --- inc/Modules/Core/Hooks.php | 1 - 1 file changed, 1 deletion(-) diff --git a/inc/Modules/Core/Hooks.php b/inc/Modules/Core/Hooks.php index 20bde40..2e825c5 100644 --- a/inc/Modules/Core/Hooks.php +++ b/inc/Modules/Core/Hooks.php @@ -76,7 +76,6 @@ public function user_deduplication(): void { * @param string $host Host name of the request. */ public function allow_oneaccess_host( $is_external, $host ): bool { - $urls = array_column( Settings::get_shared_sites(), 'url' ); $urls[] = (string) Settings::get_parent_site_url(); From dd9b9db252fa4864aa1daf086a84b99120e6bced Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Wed, 23 Sep 2026 01:18:11 +0530 Subject: [PATCH 08/23] Potential fix for pull request finding 'Preserve existing filter result for non-OneAccess hosts' Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- inc/Modules/Core/Hooks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Modules/Core/Hooks.php b/inc/Modules/Core/Hooks.php index 2e825c5..f525f4a 100644 --- a/inc/Modules/Core/Hooks.php +++ b/inc/Modules/Core/Hooks.php @@ -85,6 +85,6 @@ public function allow_oneaccess_host( $is_external, $host ): bool { } } - return false; + return (bool) $is_external; } } From 1a55d3bebb6eb7b378636da750de8f40cd9962a7 Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Fri, 2 Oct 2026 00:57:36 +0530 Subject: [PATCH 09/23] fix: code quality --- assets/src/components/CreateUser.tsx | 27 +++++++++---------- .../Rest/Governing_Site_Controller.php | 10 +++---- inc/Modules/Settings/Settings.php | 3 --- 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/assets/src/components/CreateUser.tsx b/assets/src/components/CreateUser.tsx index df57e7e..89484bc 100644 --- a/assets/src/components/CreateUser.tsx +++ b/assets/src/components/CreateUser.tsx @@ -1,35 +1,35 @@ /** * WordPress dependencies */ -import { useState, useEffect, useCallback } from '@wordpress/element'; -import { __ } from '@wordpress/i18n'; import { + Button, Card, - CardHeader, CardBody, - TextControl, - SelectControl, - Button, - Modal, + CardHeader, CheckboxControl, - Notice, + Dashicon, __experimentalGrid as Grid, __experimentalHStack as HStack, - __experimentalVStack as VStack, - Dashicon, + Icon, + Modal, + Notice, + SelectControl, Snackbar, SnackbarList, - Icon, + TextControl, + __experimentalVStack as VStack, } from '@wordpress/components'; +import { useCallback, useEffect, useState } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; /** * Internal dependencies */ import { - isValidEmail, checkPasswordStrength, - strengthWidths, getStrengthColor, + isValidEmail, + strengthWidths, type StrengthLevel, } from '../js/utils'; @@ -217,7 +217,6 @@ const CreateUser = ( { return; } - // Per-site failures are only reported in `error_log`, so merge them in as well. const results: CreateUserResult[] = [ ...( data?.data?.response_data || [] ), ...( data?.data?.error_log || [] ).map( ( failure ) => ( { diff --git a/inc/Modules/Rest/Governing_Site_Controller.php b/inc/Modules/Rest/Governing_Site_Controller.php index 985562c..d985bd7 100644 --- a/inc/Modules/Rest/Governing_Site_Controller.php +++ b/inc/Modules/Rest/Governing_Site_Controller.php @@ -697,9 +697,8 @@ public function add_user_to_sites( \WP_REST_Request $request ): \WP_REST_Respons $email = sanitize_email( $request->get_param( 'email' ) ); $username = sanitize_text_field( $request->get_param( 'username' ) ); $full_name = sanitize_text_field( $request->get_param( 'fullName' ) ); - // Passwords are hashed, never rendered or queried, so they must reach wp_create_user() verbatim. - $password = (string) $request->get_param( 'password' ); - $sites = $request->get_param( 'sites' ); + $password = (string) $request->get_param( 'password' ); + $sites = $request->get_param( 'sites' ); if ( empty( $email ) || empty( $username ) || empty( $full_name ) || empty( $password ) || empty( $sites ) ) { return new \WP_REST_Response( @@ -1047,9 +1046,8 @@ public function update_user_roles_for_sites( \WP_REST_Request $request ): \WP_RE * @param \WP_REST_Request $request The REST request object. */ public function create_user( \WP_REST_Request $request ): \WP_REST_Response { - $username = sanitize_user( $request->get_param( 'username' ) ); - $email = sanitize_email( $request->get_param( 'email' ) ); - // Passwords are hashed, never rendered or queried, so they must reach wp_create_user() verbatim. + $username = sanitize_user( $request->get_param( 'username' ) ); + $email = sanitize_email( $request->get_param( 'email' ) ); $password = (string) $request->get_param( 'password' ); $full_name = sanitize_text_field( $request->get_param( 'full_name' ) ); $role = sanitize_text_field( $request->get_param( 'role' ) ); diff --git a/inc/Modules/Settings/Settings.php b/inc/Modules/Settings/Settings.php index 73c861f..b8cc905 100644 --- a/inc/Modules/Settings/Settings.php +++ b/inc/Modules/Settings/Settings.php @@ -282,9 +282,6 @@ public static function get_shared_site_by_url( string $site_url ): ?array { /** * Normalize a site URL for comparison. * - * A site reports itself via `get_site_url()`, whose scheme and trailing slash can differ - * from the URL configured on the governing site, so strip both before comparing. - * * @param string $site_url The site URL. */ private static function normalize_site_url( string $site_url ): string { From 24d995e1f47fe81cb3a5d38d22eb935fa84606ed Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Thu, 8 Oct 2026 21:09:29 +0530 Subject: [PATCH 10/23] fix: enhance API token validation and add site URL header for requests --- inc/Modules/Rest/Abstract_REST_Controller.php | 154 ++++++++++++++---- inc/Modules/Rest/Actions_Controller.php | 8 +- inc/Modules/Rest/Brand_Site_Controller.php | 6 +- .../Rest/Governing_Site_Controller.php | 9 +- 4 files changed, 135 insertions(+), 42 deletions(-) diff --git a/inc/Modules/Rest/Abstract_REST_Controller.php b/inc/Modules/Rest/Abstract_REST_Controller.php index a873a19..7af5d03 100644 --- a/inc/Modules/Rest/Abstract_REST_Controller.php +++ b/inc/Modules/Rest/Abstract_REST_Controller.php @@ -65,56 +65,142 @@ public static function permission_callback(): bool { * @todo this should be on a hook. * * @param \WP_REST_Request $request Request. - * @return bool */ - public function check_api_permissions( $request ) { - // check if the request is from same site. - if ( Settings::is_governing_site() ) { - return current_user_can( 'manage_options' ); - } + public function check_api_permissions( $request ): bool { + $origin = $this->parse_origin( $request->get_header( 'origin' ) ); + $request_origin = $origin['origin']; + $parsed_origin = $origin['parsed']; + $request_url = $origin['url']; + $origin_port = $origin['port']; - // See if the `X_ONEACCESS_TOKEN` header is present. - $token = $request->get_header( 'X_ONEACCESS_TOKEN' ); + /** + * Token-based auth takes priority over the Origin same-host check: cross-site + * requests from sub-directory multisite installs lose the path in Origin, so + * same-host detection can misfire on sibling sub-sites. Validating by key + * instead avoids that false match. + */ + $token = $request->get_header( 'X-OneAccess-Token' ); $token = ! empty( $token ) ? sanitize_text_field( wp_unslash( $token ) ) : ''; - // Bail if the token is missing or invalid. - if ( ! hash_equals( Settings::get_api_key(), $token ) ) { - return false; + if ( ! empty( $token ) ) { + /** + * Origin is absent for server-side requests, so fall back to the + * explicitly-sent site URL header so token auth can still proceed. + */ + if ( empty( $request_url ) ) { + $site_url_header = $request->get_header( 'X-OneAccess-Site-URL' ); + if ( ! empty( $site_url_header ) ) { + $origin = $this->parse_origin( $site_url_header ); + $request_origin = $origin['origin']; + $parsed_origin = $origin['parsed']; + $request_url = $origin['url']; + $origin_port = $origin['port']; + } + } + + if ( empty( $request_url ) ) { + return false; + } + + $stored_key = $this->get_stored_api_key( $request_url ); + if ( empty( $stored_key ) || ! hash_equals( $stored_key, $token ) ) { + return false; + } + + // Governing sites were checked by ::get_stored_api_key already. + if ( Settings::is_governing_site() ) { + return true; + } + + // Non-healthcheck requests must match the site already recorded as governing. + $governing_site_url = Settings::get_parent_site_url(); + if ( '/' . $this->namespace . '/health-check' !== $request->get_route() ) { + return ! empty( $governing_site_url ) ? $this->is_url_from_host( $governing_site_url, $parsed_origin['host'], $origin_port ) : false; + } + + // Health-checks bootstrap the governing-site relationship since none is recorded yet. + Settings::set_parent_site_url( $request_origin ); + return true; } - $request_origin = $request->get_header( 'origin' ); - $request_origin = ! empty( $request_origin ) ? esc_url_raw( wp_unslash( $request_origin ) ) : ''; - $user_agent = $request->get_header( 'user-agent' ); - $user_agent = ! empty( $user_agent ) ? sanitize_text_field( wp_unslash( $user_agent ) ) : ''; + // No token: fall back to same-domain logged-in user check. + if ( empty( $request_url ) || $this->is_url_from_host( get_site_url(), $parsed_origin['host'], $origin_port ) ) { + return current_user_can( 'manage_options' ); + } - /** - * If both origin and user-agent are missing, deny access. - * - * Here checking both because server side requests will not have origin header. - */ - if ( empty( $request_origin ) && empty( $user_agent ) ) { + return false; + } + + /** + * Parses a raw Origin or X-OneAccess-Site-URL header value into its components. + * + * @param ?string $raw Raw header value. + * + * @return array{origin: string, parsed: array, url: string, port: int|null} + */ + private function parse_origin( ?string $raw ): array { + $origin = ! empty( $raw ) ? esc_url_raw( wp_unslash( $raw ) ) : ''; + $parsed = wp_parse_url( $origin ); + $parsed = is_array( $parsed ) ? $parsed : []; + $url = ! empty( $parsed['scheme'] ) && ! empty( $parsed['host'] ) + ? untrailingslashit( trim( $origin ) ) + : ''; + $port = isset( $parsed['port'] ) ? (int) $parsed['port'] : null; + + return [ + 'origin' => $origin, + 'parsed' => $parsed, + 'url' => $url, + 'port' => $port, + ]; + } + + /** + * Check if two URLs belong to the same host. + * + * @param string $url The URL to check. + * @param string $host The host to compare against. + * @param int|null $port Optional. The port to compare against. + * + * @return bool True if both URLs belong to the same host (and port if specified), false otherwise. + */ + protected function is_url_from_host( string $url, string $host, ?int $port = null ): bool { + $parsed_url = wp_parse_url( $url ); + + // Compare both host and port to properly handle localhost with different ports. + if ( ! isset( $parsed_url['host'] ) || $parsed_url['host'] !== $host ) { return false; } - // If it's the same domain, we're good. - if ( self::is_same_domain( get_site_url(), $request_origin ) ) { - return true; + // If a port was provided, also compare ports. + if ( null !== $port ) { + $url_port = $parsed_url['port'] ?? 80; + return $url_port === $port; } - $governing_site_url = Settings::get_parent_site_url(); + return true; + } - // If it's a healthcheck with no governing site, allow it and set the governing site. - if ( empty( $governing_site_url ) ) { - if ( '/' . $this->namespace . '/health-check' === $request->get_route() ) { - Settings::set_parent_site_url( $request_origin ); - return true; - } - return false; + /** + * Gets the locally-stored API key for comparison. + * + * @param ?string $site_url Site URL. Only used for brand->governing site requests. + * + * @return string The stored API key. Empty string if not found. + */ + private function get_stored_api_key( ?string $site_url = null ): string { + if ( Settings::is_consumer_site() ) { + return Settings::get_api_key(); + } + + // If there's no brand site URL we cannot match the API key. + if ( ! isset( $site_url ) ) { + return ''; } - $governing_host = (string) wp_parse_url( $governing_site_url, PHP_URL_HOST ); + $shared_sites = Settings::get_shared_sites(); - return self::is_same_domain( $governing_site_url, $request_origin ) || ( '' !== $governing_host && false !== strpos( $user_agent, $governing_host ) ); + return ! empty( $shared_sites[ $site_url ]['api_key'] ) ? $shared_sites[ $site_url ]['api_key'] : ''; } /** diff --git a/inc/Modules/Rest/Actions_Controller.php b/inc/Modules/Rest/Actions_Controller.php index 4178b61..e20fd82 100644 --- a/inc/Modules/Rest/Actions_Controller.php +++ b/inc/Modules/Rest/Actions_Controller.php @@ -456,8 +456,9 @@ private function make_brand_site_request( string $site_url, string $api_key, arr $args = [ 'headers' => [ - 'X-OneAccess-Token' => $api_key, - 'Cache-Control' => 'no-cache', + 'X-OneAccess-Token' => $api_key, + 'X-OneAccess-Site-URL' => get_site_url(), + 'Cache-Control' => 'no-cache', ], 'timeout' => 30, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout -- profile requests fetching take time. ]; @@ -1000,7 +1001,8 @@ public function rebuild_deduplicated_users_index(): WP_REST_Response { trailingslashit( esc_url_raw( $site_url ) ) . 'wp-json/' . self::NAMESPACE . '/rebuild-brand-sites-index', [ 'headers' => [ - 'X-OneAccess-Token' => $api_key, + 'X-OneAccess-Token' => $api_key, + 'X-OneAccess-Site-URL' => get_site_url(), ], 'timeout' => 30, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout -- rebuilding index take time. ] diff --git a/inc/Modules/Rest/Brand_Site_Controller.php b/inc/Modules/Rest/Brand_Site_Controller.php index 1d40a19..9336d33 100644 --- a/inc/Modules/Rest/Brand_Site_Controller.php +++ b/inc/Modules/Rest/Brand_Site_Controller.php @@ -131,7 +131,8 @@ public function reject_profile_request( \WP_REST_Request $request ): \WP_REST_Re $site_url . '/wp-json/' . self::NAMESPACE . '/reject-profile', [ 'headers' => [ - 'X-OneAccess-Token' => $api_key, + 'X-OneAccess-Token' => $api_key, + 'X-OneAccess-Site-URL' => get_site_url(), ], 'body' => [ 'user_email' => $user_email, @@ -193,7 +194,8 @@ public function approve_profile_request( \WP_REST_Request $request ): \WP_REST_R $site_url . '/wp-json/' . self::NAMESPACE . '/approve-profile', [ 'headers' => [ - 'X-OneAccess-Token' => $api_key, + 'X-OneAccess-Token' => $api_key, + 'X-OneAccess-Site-URL' => get_site_url(), ], 'body' => [ 'user_id' => $user_id, diff --git a/inc/Modules/Rest/Governing_Site_Controller.php b/inc/Modules/Rest/Governing_Site_Controller.php index d985bd7..c0e86f3 100644 --- a/inc/Modules/Rest/Governing_Site_Controller.php +++ b/inc/Modules/Rest/Governing_Site_Controller.php @@ -778,7 +778,8 @@ public function add_user_to_sites( \WP_REST_Request $request ): \WP_REST_Respons 'role' => $user_role, ], 'headers' => [ - 'X-OneAccess-Token' => $api_key, + 'X-OneAccess-Token' => $api_key, + 'X-OneAccess-Site-URL' => get_site_url(), ], ] ); @@ -972,7 +973,8 @@ public function update_user_roles_for_sites( \WP_REST_Request $request ): \WP_RE 'role' => $new_role, ], 'headers' => [ - 'X-OneAccess-Token' => $api_key, + 'X-OneAccess-Token' => $api_key, + 'X-OneAccess-Site-URL' => get_site_url(), ], ] ); @@ -1464,7 +1466,8 @@ public function create_users_for_sites( \WP_REST_Request $request ): \WP_REST_Re 'role' => $userdata['role'] ?? 'subscriber', ], 'headers' => [ - 'X-OneAccess-Token' => $api_key, + 'X-OneAccess-Token' => $api_key, + 'X-OneAccess-Site-URL' => get_site_url(), ], ] ); From 3145b01251e1283469adfaf6d88087230576c1b2 Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Thu, 8 Oct 2026 21:20:23 +0530 Subject: [PATCH 11/23] fix: improve error handling for user creation and password validation --- assets/src/components/CreateUser.tsx | 15 +++- .../Rest/Governing_Site_Controller.php | 86 ++++++++++--------- 2 files changed, 58 insertions(+), 43 deletions(-) diff --git a/assets/src/components/CreateUser.tsx b/assets/src/components/CreateUser.tsx index 89484bc..1f595ed 100644 --- a/assets/src/components/CreateUser.tsx +++ b/assets/src/components/CreateUser.tsx @@ -186,12 +186,19 @@ const CreateUser = ( { ); if ( ! response.ok ) { + const errorData = ( await response + .json() + .catch( () => null ) ) as { + message?: string; + } | null; setNotice( { type: 'error', - message: __( - 'Failed to create user. Please try again later.', - 'oneaccess' - ), + message: + errorData?.message || + __( + 'Failed to create user. Please try again later.', + 'oneaccess' + ), } ); throw new Error( 'Failed to create user' ); } diff --git a/inc/Modules/Rest/Governing_Site_Controller.php b/inc/Modules/Rest/Governing_Site_Controller.php index c0e86f3..d0afb0b 100644 --- a/inc/Modules/Rest/Governing_Site_Controller.php +++ b/inc/Modules/Rest/Governing_Site_Controller.php @@ -481,7 +481,7 @@ public function delete_user_from_sites( \WP_REST_Request $request ): \WP_REST_Re } $response_body = json_decode( wp_remote_retrieve_body( $response ), true ); - if ( ! $response_body['success'] ) { + if ( ! is_array( $response_body ) || empty( $response_body['success'] ) ) { $error_log[] = [ 'site_name' => $site['site_url'] ?? '', 'message' => $response_body['message'] ?? __( 'Failed to delete user from site.', 'oneaccess' ), @@ -710,18 +710,9 @@ public function add_user_to_sites( \WP_REST_Request $request ): \WP_REST_Respons ); } - if ( strlen( $password ) < self::MIN_PASSWORD_LENGTH ) { - return new \WP_REST_Response( - [ - 'success' => false, - 'message' => sprintf( - /* translators: %d is the minimum number of characters */ - __( 'Password must be at least %d characters long.', 'oneaccess' ), - self::MIN_PASSWORD_LENGTH - ), - ], - 400 - ); + $password_error = self::validate_password( $password ); + if ( null !== $password_error ) { + return $password_error; } // Validate sites. @@ -812,7 +803,7 @@ public function add_user_to_sites( \WP_REST_Request $request ): \WP_REST_Respons $response_body = json_decode( wp_remote_retrieve_body( $response ), true ); - if ( ! $response_body['success'] ) { + if ( ! is_array( $response_body ) || empty( $response_body['success'] ) ) { $error_log[] = [ 'site_name' => $site_url['url'] ?? '', 'message' => $response_body['message'] ?? __( 'Failed to add user to site.', 'oneaccess' ), @@ -1007,7 +998,7 @@ public function update_user_roles_for_sites( \WP_REST_Request $request ): \WP_RE $response_body = json_decode( wp_remote_retrieve_body( $response ), true ); - if ( ! $response_body['success'] ) { + if ( ! is_array( $response_body ) || empty( $response_body['success'] ) ) { $error_log[] = [ 'site_name' => $site_url, 'message' => $response_body['message'] ?? __( 'Failed to update user role on site.', 'oneaccess' ), @@ -1064,18 +1055,9 @@ public function create_user( \WP_REST_Request $request ): \WP_REST_Response { ); } - if ( strlen( $password ) < self::MIN_PASSWORD_LENGTH ) { - return new \WP_REST_Response( - [ - 'success' => false, - 'message' => sprintf( - /* translators: %d is the minimum number of characters */ - __( 'Password must be at least %d characters long.', 'oneaccess' ), - self::MIN_PASSWORD_LENGTH - ), - ], - 400 - ); + $password_error = self::validate_password( $password ); + if ( null !== $password_error ) { + return $password_error; } if ( ! is_email( $email ) ) { @@ -1401,18 +1383,9 @@ public function create_users_for_sites( \WP_REST_Request $request ): \WP_REST_Re 400 ); } - if ( strlen( (string) $userdata['password'] ) < self::MIN_PASSWORD_LENGTH ) { - return new \WP_REST_Response( - [ - 'success' => false, - 'message' => sprintf( - /* translators: %d is the minimum number of characters */ - __( 'Password must be at least %d characters long.', 'oneaccess' ), - self::MIN_PASSWORD_LENGTH - ), - ], - 400 - ); + $password_error = self::validate_password( (string) $userdata['password'] ); + if ( null !== $password_error ) { + return $password_error; } if ( ! isset( $userdata['fullName'] ) || empty( $userdata['fullName'] ) ) { return new \WP_REST_Response( @@ -1531,4 +1504,39 @@ public function create_users_for_sites( \WP_REST_Request $request ): \WP_REST_Re 200 ); } + + /** + * Validate a new user's password. + * + * @param string $password The password to validate. + * + * @return ?\WP_REST_Response Error response if invalid, null otherwise. + */ + private static function validate_password( string $password ): ?\WP_REST_Response { + if ( mb_strlen( $password ) < self::MIN_PASSWORD_LENGTH ) { + return new \WP_REST_Response( + [ + 'success' => false, + 'message' => sprintf( + /* translators: %d is the minimum number of characters */ + __( 'Password must be at least %d characters long.', 'oneaccess' ), + self::MIN_PASSWORD_LENGTH + ), + ], + 400 + ); + } + + if ( str_contains( $password, '\\' ) ) { + return new \WP_REST_Response( + [ + 'success' => false, + 'message' => __( 'Passwords cannot contain the "\\" character.', 'oneaccess' ), + ], + 400 + ); + } + + return null; + } } From 6e60d4358b89718c1a80a065787fd51e325b0acb Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Thu, 8 Oct 2026 21:22:29 +0530 Subject: [PATCH 12/23] fix: update inc/Modules/Rest/Governing_Site_Controller.php Co-authored-by: Utsav Patel <75293077+up1512001@users.noreply.github.com> --- inc/Modules/Rest/Governing_Site_Controller.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/inc/Modules/Rest/Governing_Site_Controller.php b/inc/Modules/Rest/Governing_Site_Controller.php index d0afb0b..f81088a 100644 --- a/inc/Modules/Rest/Governing_Site_Controller.php +++ b/inc/Modules/Rest/Governing_Site_Controller.php @@ -436,9 +436,21 @@ public function delete_user_from_sites( \WP_REST_Request $request ): \WP_REST_Re continue; } - $site_info = Settings::get_shared_site_by_url( $site['site_url'] ); - $request_url = untrailingslashit( $site_info['url'] ?? $site['site_url'] ) . '/wp-json/' . self::NAMESPACE . '/delete-user'; - $api_key = $site_info['api_key'] ?? ''; + $site_info = Settings::get_shared_site_by_url( $site['site_url'] ); + if ( null === $site_info ) { + $error_log[] = [ + 'site_name' => $site['site_url'], + 'message' => sprintf( + /* translators: %s is the site URL */ + __( 'Site %s is not connected to OneAccess.', 'oneaccess' ), + esc_html( $site['site_url'] ) + ), + ]; + continue; + } + + $request_url = $site_info['url'] . '/wp-json/' . self::NAMESPACE . '/delete-user'; + $api_key = $site_info['api_key']; $response = wp_safe_remote_request( $request_url, [ From 73969bd226f9bfad56376244b6aa617f65f4ef4e Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Thu, 8 Oct 2026 21:29:04 +0530 Subject: [PATCH 13/23] fix: add site URL header to user deletion request --- inc/Modules/Rest/Governing_Site_Controller.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/Modules/Rest/Governing_Site_Controller.php b/inc/Modules/Rest/Governing_Site_Controller.php index f81088a..aeeeaba 100644 --- a/inc/Modules/Rest/Governing_Site_Controller.php +++ b/inc/Modules/Rest/Governing_Site_Controller.php @@ -460,8 +460,8 @@ public function delete_user_from_sites( \WP_REST_Request $request ): \WP_REST_Re 'email' => $email, ], 'headers' => [ - 'X-OneAccess-Token' => $api_key, - 'Origin' => get_site_url(), + 'X-OneAccess-Token' => $api_key, + 'X-OneAccess-Site-URL' => get_site_url(), ], ] ); From a31ab49ed289b78ee0236df6dc10ad66080a50a0 Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Thu, 8 Oct 2026 21:33:02 +0530 Subject: [PATCH 14/23] fix: sanitize password input when creating a user --- inc/Modules/Rest/Governing_Site_Controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Modules/Rest/Governing_Site_Controller.php b/inc/Modules/Rest/Governing_Site_Controller.php index aeeeaba..9008914 100644 --- a/inc/Modules/Rest/Governing_Site_Controller.php +++ b/inc/Modules/Rest/Governing_Site_Controller.php @@ -1105,7 +1105,7 @@ public function create_user( \WP_REST_Request $request ): \WP_REST_Response { } // Create the user. - $user_id = wp_create_user( $username, $password, $email ); + $user_id = wp_create_user( $username, wp_slash( $password ), $email ); if ( is_wp_error( $user_id ) ) { return new \WP_REST_Response( [ From e9fa5bed53519e1ac6380f64f0eb77f9df7113f1 Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Thu, 8 Oct 2026 21:34:54 +0530 Subject: [PATCH 15/23] fix: refactor name splitting logic in user creation and update methods --- .../Rest/Governing_Site_Controller.php | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/inc/Modules/Rest/Governing_Site_Controller.php b/inc/Modules/Rest/Governing_Site_Controller.php index 9008914..09993cc 100644 --- a/inc/Modules/Rest/Governing_Site_Controller.php +++ b/inc/Modules/Rest/Governing_Site_Controller.php @@ -829,14 +829,7 @@ public function add_user_to_sites( \WP_REST_Request $request ): \WP_REST_Respons 'message' => __( 'User added successfully.', 'oneaccess' ), ]; - $first_name = ''; - $last_name = ''; - - // split full name into first and last name. - $name_parts = explode( ' ', $full_name ); - - $first_name = $name_parts[0] ?? ''; - $last_name = isset( $name_parts[1] ) ? implode( ' ', array_slice( $name_parts, 1 ) ) : ''; + [ $first_name, $last_name ] = self::split_full_name( $full_name ); $user_id = isset( $response_body['data']['user_id'] ) ? absint( $response_body['data']['user_id'] ) : 0; $user_role = isset( $response_body['data']['role'] ) ? sanitize_text_field( $response_body['data']['role'] ) : 'subscriber'; @@ -1122,7 +1115,7 @@ public function create_user( \WP_REST_Request $request ): \WP_REST_Response { $role = 'subscriber'; } - $name_parts = preg_split( '/\s+/', trim( $full_name ), 2 ) ?: []; + [ $first_name, $last_name ] = self::split_full_name( $full_name ); // Set the user's full name and role. wp_update_user( @@ -1130,8 +1123,8 @@ public function create_user( \WP_REST_Request $request ): \WP_REST_Response { 'ID' => $user_id, 'display_name' => $full_name, 'user_nicename' => sanitize_title( $full_name ), - 'first_name' => $name_parts[0] ?? '', - 'last_name' => $name_parts[1] ?? '', + 'first_name' => $first_name, + 'last_name' => $last_name, 'role' => $role ?: 'subscriber', ] ); @@ -1551,4 +1544,17 @@ private static function validate_password( string $password ): ?\WP_REST_Respons return null; } + + /** + * Split a full name into first and last name on the first run of whitespace. + * + * @param string $full_name The full name. + * + * @return array{0: string, 1: string} First and last name. + */ + private static function split_full_name( string $full_name ): array { + $name_parts = preg_split( '/\s+/', trim( $full_name ), 2 ) ?: []; + + return [ $name_parts[0] ?? '', $name_parts[1] ?? '' ]; + } } From 2563ccbbf500a08c607cda86a48ade3f2f48b12a Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Thu, 8 Oct 2026 22:00:05 +0530 Subject: [PATCH 16/23] fix: enhance user deletion error handling and improve site name retrieval --- assets/src/components/SharedUsers.tsx | 56 +++++++++++++++---- .../Rest/Governing_Site_Controller.php | 2 +- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/assets/src/components/SharedUsers.tsx b/assets/src/components/SharedUsers.tsx index e12bfe9..cd18d17 100644 --- a/assets/src/components/SharedUsers.tsx +++ b/assets/src/components/SharedUsers.tsx @@ -100,6 +100,12 @@ interface GenericApiResponse { message?: string; } +interface DeleteUserResponse extends GenericApiResponse { + data?: { + error_log?: Array< { site_name?: string; message?: string } >; + }; +} + const SharedUsers = ( { availableSites, }: { @@ -384,30 +390,56 @@ const SharedUsers = ( { } ); - if ( ! response.ok ) { - throw new Error( 'Failed to delete user from sites' ); + const data = ( await response + .json() + .catch( () => null ) ) as DeleteUserResponse | null; + + if ( ! response.ok || ! data ) { + setNotice( { + type: 'error', + message: + data?.message || + __( 'Failed to delete user.', 'oneaccess' ), + } ); + return; } - const data = ( await response.json() ) as GenericApiResponse; - if ( ! data.success ) { - throw new Error( - data.message || 'Failed to delete user from sites' - ); + if ( data.success ) { + setNotice( { + type: 'success', + message: __( 'User deleted successfully.', 'oneaccess' ), + } ); + return; } + // Partial failure: list each site's error after the summary. + const siteErrors = ( data.data?.error_log || [] ) + .filter( ( failure ) => !! failure.message ) + .map( ( { site_name: siteName = '', message = '' } ) => + ! siteName || message.includes( siteName ) + ? message + : `${ siteName }: ${ message }` + ); + setNotice( { - type: 'success', - message: __( 'User deleted successfully.', 'oneaccess' ), + type: 'error', + message: [ + data.message || + __( + 'User could not be deleted from some sites.', + 'oneaccess' + ), + ...siteErrors, + ].join( ' ' ), } ); - - // Refresh users list - await fetchUsers(); } catch { setNotice( { type: 'error', message: __( 'Failed to delete user.', 'oneaccess' ), } ); } finally { + // Refresh even on failure, since some sites may have been deleted. + await fetchUsers(); setIsDeletingUser( false ); setShowUserDeletionModal( false ); setSelectedUser( null ); diff --git a/inc/Modules/Rest/Governing_Site_Controller.php b/inc/Modules/Rest/Governing_Site_Controller.php index 09993cc..3aaedb2 100644 --- a/inc/Modules/Rest/Governing_Site_Controller.php +++ b/inc/Modules/Rest/Governing_Site_Controller.php @@ -1417,7 +1417,7 @@ public function create_users_for_sites( \WP_REST_Request $request ): \WP_REST_Re $processed_sites[] = $site_url; $api_key = $oneaccess_sites_info[ $site_url ]['api_key'] ?? ''; - $site_name = $oneaccess_sites_info[ $site_url ]['name'] ?? ''; + $site_name = ! empty( $oneaccess_sites_info[ $site_url ]['name'] ) ? $oneaccess_sites_info[ $site_url ]['name'] : $site_url; if ( empty( $api_key ) ) { $error_log[] = [ 'site_name' => $site_name, From 016fd69ece00849bcf352c736b5c2c00f94665bf Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Thu, 8 Oct 2026 22:01:34 +0530 Subject: [PATCH 17/23] fix: linting --- assets/src/components/CreateUser.tsx | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/assets/src/components/CreateUser.tsx b/assets/src/components/CreateUser.tsx index 1f595ed..ff55c30 100644 --- a/assets/src/components/CreateUser.tsx +++ b/assets/src/components/CreateUser.tsx @@ -1,35 +1,35 @@ /** * WordPress dependencies */ +import { useState, useEffect, useCallback } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; import { - Button, Card, - CardBody, CardHeader, + CardBody, + TextControl, + SelectControl, + Button, + Modal, CheckboxControl, - Dashicon, + Notice, __experimentalGrid as Grid, __experimentalHStack as HStack, - Icon, - Modal, - Notice, - SelectControl, + __experimentalVStack as VStack, + Dashicon, Snackbar, SnackbarList, - TextControl, - __experimentalVStack as VStack, + Icon, } from '@wordpress/components'; -import { useCallback, useEffect, useState } from '@wordpress/element'; -import { __ } from '@wordpress/i18n'; /** * Internal dependencies */ import { - checkPasswordStrength, - getStrengthColor, isValidEmail, + checkPasswordStrength, strengthWidths, + getStrengthColor, type StrengthLevel, } from '../js/utils'; From 26996c49028d5f58e421c46c6a7925b0836f4449 Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Thu, 8 Oct 2026 22:05:44 +0530 Subject: [PATCH 18/23] fix: refactor Snackbar handling to improve notice display logic --- assets/src/components/CreateUser.tsx | 55 +++++++++++++++------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/assets/src/components/CreateUser.tsx b/assets/src/components/CreateUser.tsx index ff55c30..699567d 100644 --- a/assets/src/components/CreateUser.tsx +++ b/assets/src/components/CreateUser.tsx @@ -17,7 +17,6 @@ import { __experimentalHStack as HStack, __experimentalVStack as VStack, Dashicon, - Snackbar, SnackbarList, Icon, } from '@wordpress/components'; @@ -60,6 +59,9 @@ const initialFormState: UserFormData = { role: 'subscriber', }; +// Id of the general (non per-site) notice within the snackbar list. +const GENERAL_NOTICE_ID = 'create-user-notice'; + const CreateUser = ( { availableSites, }: { @@ -82,12 +84,7 @@ const CreateUser = ( { StrengthLevel | 'default' >( 'default' ); const [ userCreationNotices, setUserCreationNotices ] = useState< - Array< - Omit< React.ComponentProps< typeof Snackbar >, 'children' > & { - id: string; - content: string; - } - > + Array< { id: string; content: string; className: string } > >( [] ); const fetchStrongPassword = useCallback( async () => { @@ -515,26 +512,32 @@ const CreateUser = ( { - { notice && notice.message && ( - setNotice( null ) } - > - { notice.message } - - ) } - - { userCreationNotices.length > 0 && ( + { ( notice?.message || userCreationNotices.length > 0 ) && ( { - setTimeout( () => { - setUserCreationNotices( [] ); - }, 3000 ); + notices={ [ + ...( notice?.message + ? [ + { + id: GENERAL_NOTICE_ID, + content: notice.message, + className: + notice.type === 'error' + ? 'oneaccess-error-notice' + : 'oneaccess-success-notice', + }, + ] + : [] ), + ...userCreationNotices, + ] } + onRemove={ ( id: string ) => { + if ( GENERAL_NOTICE_ID === id ) { + setNotice( null ); + return; + } + + setUserCreationNotices( ( current ) => + current.filter( ( item ) => item.id !== id ) + ); } } /> ) } From f063edad041d10df692463ea75c71cb05cb38ddf Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Thu, 8 Oct 2026 22:15:51 +0530 Subject: [PATCH 19/23] fix: streamline user retrieval and enhance error logging during deletion process --- .../Rest/Governing_Site_Controller.php | 31 +++++++------------ 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/inc/Modules/Rest/Governing_Site_Controller.php b/inc/Modules/Rest/Governing_Site_Controller.php index 3aaedb2..a1782af 100644 --- a/inc/Modules/Rest/Governing_Site_Controller.php +++ b/inc/Modules/Rest/Governing_Site_Controller.php @@ -359,11 +359,8 @@ public function delete_user( \WP_REST_Request $request ): \WP_REST_Response { ); } - // Get the user by email or login. - $user = get_user_by( 'login', $username ); - if ( ! $user ) { - $user = get_user_by( 'email', $email ); - } + // Match by email, the identity used across OneAccess; usernames may differ between sites. + $user = get_user_by( 'email', $email ); if ( ! $user ) { return new \WP_REST_Response( [ @@ -430,11 +427,9 @@ public function delete_user_from_sites( \WP_REST_Request $request ): \WP_REST_Re // Skip duplicate or invalid sites. if ( empty( $site['site_url'] ) || in_array( $site['site_url'], $processed_sites, true ) ) { - if ( ! empty( $site['site_url'] ) ) { - $processed_sites[] = $site['site_url']; - } continue; } + $processed_sites[] = $site['site_url']; $site_info = Settings::get_shared_site_by_url( $site['site_url'] ); if ( null === $site_info ) { @@ -449,6 +444,7 @@ public function delete_user_from_sites( \WP_REST_Request $request ): \WP_REST_Re continue; } + $site_name = ! empty( $site_info['name'] ) ? $site_info['name'] : $site['site_url']; $request_url = $site_info['url'] . '/wp-json/' . self::NAMESPACE . '/delete-user'; $api_key = $site_info['api_key']; $response = wp_safe_remote_request( @@ -468,11 +464,11 @@ public function delete_user_from_sites( \WP_REST_Request $request ): \WP_REST_Re if ( is_wp_error( $response ) ) { $error_log[] = [ - 'site_name' => $site['site_url'] ?? '', + 'site_name' => $site_name, 'message' => sprintf( - /* translators: %s is the site URL */ + /* translators: %s is the site name */ __( 'Error deleting user from site %s.', 'oneaccess' ), - esc_html( $site['site_url'] ?? '' ) + esc_html( $site_name ) ), ]; continue; @@ -481,21 +477,20 @@ public function delete_user_from_sites( \WP_REST_Request $request ): \WP_REST_Re $response_code = wp_remote_retrieve_response_code( $response ); if ( 200 !== $response_code ) { $error_log[] = [ - 'site_name' => $site['site_url'] ?? '', + 'site_name' => $site_name, 'message' => sprintf( - /* translators: %s is the site URL */ + /* translators: %s is the site name */ __( 'Failed to delete user from site %s.', 'oneaccess' ), - esc_html( $site['site_url'] ?? '' ) + esc_html( $site_name ) ), ]; - $error_log[] = $response; continue; } $response_body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( ! is_array( $response_body ) || empty( $response_body['success'] ) ) { $error_log[] = [ - 'site_name' => $site['site_url'] ?? '', + 'site_name' => $site_name, 'message' => $response_body['message'] ?? __( 'Failed to delete user from site.', 'oneaccess' ), ]; continue; @@ -759,11 +754,9 @@ public function add_user_to_sites( \WP_REST_Request $request ): \WP_REST_Respons // Skip duplicate or invalid sites. if ( empty( $site_url['url'] ) || in_array( $site_url['url'], $processed_sites, true ) ) { - if ( ! empty( $site_url['url'] ) ) { - $processed_sites[] = $site_url['url']; - } continue; } + $processed_sites[] = $site_url['url']; $api_key = $oneaccess_sites_info[ $url ]['api_key'] ?? ''; $user_role = $site_url['role'] ?? 'subscriber'; From 05fed9dbd96bdf4b878b12cf46f122a5b0070126 Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Thu, 8 Oct 2026 22:30:33 +0530 Subject: [PATCH 20/23] fix: improve site URL handling and enhance error logging for unknown sites during user updates --- inc/Modules/Rest/Abstract_REST_Controller.php | 2 +- .../Rest/Governing_Site_Controller.php | 21 +++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/inc/Modules/Rest/Abstract_REST_Controller.php b/inc/Modules/Rest/Abstract_REST_Controller.php index 7af5d03..6c57c3b 100644 --- a/inc/Modules/Rest/Abstract_REST_Controller.php +++ b/inc/Modules/Rest/Abstract_REST_Controller.php @@ -213,7 +213,7 @@ private function get_stored_api_key( ?string $site_url = null ): string { * @return string Full API endpoint URL. */ protected function build_api_endpoint( string $site_url, string $endpoint, string $rest_namespace = self::NAMESPACE ): string { - return esc_url_raw( trailingslashit( $site_url ) ) . '/wp-json/' . $rest_namespace . '/' . ltrim( $endpoint, '/' ); + return untrailingslashit( esc_url_raw( $site_url ) ) . '/wp-json/' . $rest_namespace . '/' . ltrim( $endpoint, '/' ); } /** diff --git a/inc/Modules/Rest/Governing_Site_Controller.php b/inc/Modules/Rest/Governing_Site_Controller.php index a1782af..7d8d462 100644 --- a/inc/Modules/Rest/Governing_Site_Controller.php +++ b/inc/Modules/Rest/Governing_Site_Controller.php @@ -940,17 +940,30 @@ public function update_user_roles_for_sites( \WP_REST_Request $request ): \WP_RE foreach ( $roles as $key => $value ) { $site_key = untrailingslashit( $key ); $site = (array) ( $oneaccess_sites_info[ $site_key ] ?? [] ); - $site_url = ! empty( $site['url'] ) ? trailingslashit( $site['url'] ) : ''; + $site_url = ! empty( $site['url'] ) ? untrailingslashit( $site['url'] ) : ''; $api_key = $site['api_key'] ?? ''; $new_role = $value; - // Skip duplicate or invalid sites. - if ( empty( $site_url ) || in_array( $site_url, $processed_sites, true ) ) { + // Report unknown sites instead of silently skipping them. + if ( empty( $site_url ) ) { + $error_log[] = [ + 'site_name' => $site_key, + 'message' => sprintf( + /* translators: %s is the site URL */ + __( 'Site %s not found in OneAccess sites.', 'oneaccess' ), + esc_html( $site_key ) + ), + ]; + continue; + } + + // Skip duplicate sites. + if ( in_array( $site_url, $processed_sites, true ) ) { continue; } $processed_sites[] = $site_url; - $request_url = $site_url . '/wp-json/' . self::NAMESPACE . '/update-user'; + $request_url = $this->build_api_endpoint( $site_url, 'update-user' ); $response = wp_safe_remote_post( $request_url, From 0b604b738f3b088e031139b915862444738063f9 Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Thu, 8 Oct 2026 23:05:26 +0530 Subject: [PATCH 21/23] fix: remove unnecessary allow_oneaccess_host filter and related code from Hooks class --- inc/Modules/Core/Hooks.php | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/inc/Modules/Core/Hooks.php b/inc/Modules/Core/Hooks.php index f525f4a..81b75b1 100644 --- a/inc/Modules/Core/Hooks.php +++ b/inc/Modules/Core/Hooks.php @@ -37,9 +37,6 @@ public function __construct() { * {@inheritDoc} */ public function register_hooks(): void { - // Needed on both site types, so it must come before the consumer check below. - add_filter( 'http_request_host_is_external', [ $this, 'allow_oneaccess_host' ], 10, 2 ); - // Early return if this is not a consumer site. if ( ! Settings::is_consumer_site() ) { return; @@ -66,25 +63,4 @@ public function register_hooks(): void { public function user_deduplication(): void { $this->actions_controller->send_users_for_deduplication(); } - - /** - * Allow outbound requests to the configured OneAccess sites. - * - * @internal Hook callback - * - * @param bool $is_external Whether the host is considered external. - * @param string $host Host name of the request. - */ - public function allow_oneaccess_host( $is_external, $host ): bool { - $urls = array_column( Settings::get_shared_sites(), 'url' ); - $urls[] = (string) Settings::get_parent_site_url(); - - foreach ( $urls as $url ) { - if ( ! empty( $url ) && wp_parse_url( $url, PHP_URL_HOST ) === $host ) { - return true; - } - } - - return (bool) $is_external; - } } From 5ad22754f1cc11695c992103405883c9a5a3de37 Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Thu, 8 Oct 2026 23:30:23 +0530 Subject: [PATCH 22/23] fix: update site URL normalization and improve shared site retrieval logic --- inc/Modules/Settings/Settings.php | 19 ++----------------- inc/Modules/User/Profile_Request.php | 2 +- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/inc/Modules/Settings/Settings.php b/inc/Modules/Settings/Settings.php index b8cc905..e60f943 100644 --- a/inc/Modules/Settings/Settings.php +++ b/inc/Modules/Settings/Settings.php @@ -268,24 +268,9 @@ public static function get_shared_sites(): array { public static function get_shared_site_by_url( string $site_url ): ?array { $brand_sites = self::get_shared_sites(); - $normalized_url = self::normalize_site_url( $site_url ); + $normalized_url = untrailingslashit( trim( $site_url ) ); - foreach ( $brand_sites as $site ) { - if ( self::normalize_site_url( $site['url'] ) === $normalized_url ) { - return $site; - } - } - - return null; - } - - /** - * Normalize a site URL for comparison. - * - * @param string $site_url The site URL. - */ - private static function normalize_site_url( string $site_url ): string { - return (string) preg_replace( '#^https?://#i', '', untrailingslashit( $site_url ) ); + return $brand_sites[ $normalized_url ] ?? null; } /** diff --git a/inc/Modules/User/Profile_Request.php b/inc/Modules/User/Profile_Request.php index 3738d8a..262748d 100644 --- a/inc/Modules/User/Profile_Request.php +++ b/inc/Modules/User/Profile_Request.php @@ -32,7 +32,7 @@ public function register_hooks(): void { add_action( 'edit_user_profile_update', [ $this, 'store_profile_update_request' ] ); // the current user is not available until pluggable.php has loaded, so defer. - add_action( 'init', [ $this, 'register_brand_admin_hooks' ] ); + add_action( 'admin_init', [ $this, 'register_brand_admin_hooks' ] ); } /** From 6c87017e8281f8b29e5ace7e59e933dc3f292b70 Mon Sep 17 00:00:00 2001 From: Kallyan Singha Date: Thu, 8 Oct 2026 23:52:55 +0530 Subject: [PATCH 23/23] fix: add site URL handling and improve CORS headers for OneAccess requests --- assets/src/components/CreateUser.tsx | 2 +- assets/src/components/SiteModal.tsx | 2 ++ assets/src/types/global.d.ts | 1 + inc/Modules/Core/Assets.php | 1 + inc/Modules/Core/Rest.php | 15 ++++------ inc/Modules/Rest/Abstract_REST_Controller.php | 30 +++++++++---------- .../Integration/Modules/Core/AssetsTest.php | 1 + .../Integration/Modules/Core/RestTest.php | 14 ++++----- 8 files changed, 33 insertions(+), 33 deletions(-) diff --git a/assets/src/components/CreateUser.tsx b/assets/src/components/CreateUser.tsx index 699567d..a3cda25 100644 --- a/assets/src/components/CreateUser.tsx +++ b/assets/src/components/CreateUser.tsx @@ -197,7 +197,7 @@ const CreateUser = ( { 'oneaccess' ), } ); - throw new Error( 'Failed to create user' ); + return; } const data = ( await response.json() ) as { diff --git a/assets/src/components/SiteModal.tsx b/assets/src/components/SiteModal.tsx index c5143d3..42986a1 100644 --- a/assets/src/components/SiteModal.tsx +++ b/assets/src/components/SiteModal.tsx @@ -114,6 +114,8 @@ const SiteModal = ( { headers: { 'Content-Type': 'application/json', 'X-OneAccess-Token': formData.api_key, + 'X-OneAccess-Site-URL': + window.OneAccessSettings.siteUrl, }, } ); diff --git a/assets/src/types/global.d.ts b/assets/src/types/global.d.ts index 769bee1..91220b4 100644 --- a/assets/src/types/global.d.ts +++ b/assets/src/types/global.d.ts @@ -12,6 +12,7 @@ interface OneAccessSettings { api_key: string; setupUrl: string; siteType: SiteType; + siteUrl: string; } export interface OneAccessOnboarding { diff --git a/inc/Modules/Core/Assets.php b/inc/Modules/Core/Assets.php index e182496..225a50c 100644 --- a/inc/Modules/Core/Assets.php +++ b/inc/Modules/Core/Assets.php @@ -72,6 +72,7 @@ public static function get_localized_data(): array { 'restUrl' => esc_url( home_url( '/wp-json' ) ), 'setupUrl' => esc_url( admin_url( 'admin.php?page=oneaccess-settings' ) ), 'siteType' => Settings::get_site_type(), + 'siteUrl' => esc_url( get_site_url() ), ]; } diff --git a/inc/Modules/Core/Rest.php b/inc/Modules/Core/Rest.php index 77f5d9a..cb5efd6 100644 --- a/inc/Modules/Core/Rest.php +++ b/inc/Modules/Core/Rest.php @@ -30,16 +30,13 @@ public function register_hooks(): void { * @return array Modified headers. */ public function allowed_cors_headers( $headers ): array { - // Skip if the headers are already present. - if ( in_array( 'X-OneAccess-Token', $headers, true ) ) { - return $headers; + // Append only the headers not already present so the list stays de-duplicated. + foreach ( [ 'X-OneAccess-Token', 'X-OneAccess-Site-URL' ] as $header ) { + if ( ! in_array( $header, $headers, true ) ) { + $headers[] = $header; + } } - return array_merge( - $headers, - [ - 'X-OneAccess-Token', - ] - ); + return $headers; } } diff --git a/inc/Modules/Rest/Abstract_REST_Controller.php b/inc/Modules/Rest/Abstract_REST_Controller.php index 6c57c3b..5c0f3f3 100644 --- a/inc/Modules/Rest/Abstract_REST_Controller.php +++ b/inc/Modules/Rest/Abstract_REST_Controller.php @@ -83,19 +83,13 @@ public function check_api_permissions( $request ): bool { $token = ! empty( $token ) ? sanitize_text_field( wp_unslash( $token ) ) : ''; if ( ! empty( $token ) ) { - /** - * Origin is absent for server-side requests, so fall back to the - * explicitly-sent site URL header so token auth can still proceed. - */ - if ( empty( $request_url ) ) { - $site_url_header = $request->get_header( 'X-OneAccess-Site-URL' ); - if ( ! empty( $site_url_header ) ) { - $origin = $this->parse_origin( $site_url_header ); - $request_origin = $origin['origin']; - $parsed_origin = $origin['parsed']; - $request_url = $origin['url']; - $origin_port = $origin['port']; - } + $site_url_header = $this->parse_origin( $request->get_header( 'X-OneAccess-Site-URL' ) ); + if ( ! empty( $site_url_header['url'] ) ) { + $origin = $site_url_header; + $request_origin = $origin['origin']; + $parsed_origin = $origin['parsed']; + $request_url = $origin['url']; + $origin_port = $origin['port']; } if ( empty( $request_url ) ) { @@ -112,13 +106,17 @@ public function check_api_permissions( $request ): bool { return true; } - // Non-healthcheck requests must match the site already recorded as governing. + // Once paired, every request (health-checks included) must come from the recorded governing site. $governing_site_url = Settings::get_parent_site_url(); + if ( ! empty( $governing_site_url ) ) { + return $this->is_url_from_host( $governing_site_url, $parsed_origin['host'], $origin_port ); + } + + // Only a health-check may bootstrap the governing-site relationship. if ( '/' . $this->namespace . '/health-check' !== $request->get_route() ) { - return ! empty( $governing_site_url ) ? $this->is_url_from_host( $governing_site_url, $parsed_origin['host'], $origin_port ) : false; + return false; } - // Health-checks bootstrap the governing-site relationship since none is recorded yet. Settings::set_parent_site_url( $request_origin ); return true; } diff --git a/tests/phpunit/Integration/Modules/Core/AssetsTest.php b/tests/phpunit/Integration/Modules/Core/AssetsTest.php index 4577cab..1d2d891 100644 --- a/tests/phpunit/Integration/Modules/Core/AssetsTest.php +++ b/tests/phpunit/Integration/Modules/Core/AssetsTest.php @@ -77,5 +77,6 @@ public function test_get_localized_data_returns_expected_keys(): void { $this->assertArrayHasKey( 'restUrl', $data ); $this->assertArrayHasKey( 'setupUrl', $data ); $this->assertArrayHasKey( 'siteType', $data ); + $this->assertArrayHasKey( 'siteUrl', $data ); } } diff --git a/tests/phpunit/Integration/Modules/Core/RestTest.php b/tests/phpunit/Integration/Modules/Core/RestTest.php index 7510f75..c458f19 100644 --- a/tests/phpunit/Integration/Modules/Core/RestTest.php +++ b/tests/phpunit/Integration/Modules/Core/RestTest.php @@ -30,21 +30,21 @@ public function test_class_instantiation(): void { } /** - * Tests that the OneAccess token header is added once. + * Tests that the OneAccess headers are added once. */ - public function test_allowed_cors_headers_adds_OneAccess_token_once(): void { + public function test_allowed_cors_headers_adds_OneAccess_headers_once(): void { $rest = new Rest(); $this->assertSame( - [ 'X-WP-Nonce', 'X-OneAccess-Token' ], + [ 'X-WP-Nonce', 'X-OneAccess-Token', 'X-OneAccess-Site-URL' ], $rest->allowed_cors_headers( [ 'X-WP-Nonce' ] ), - 'Token should be added to headers' + 'Headers should be added' ); $this->assertSame( - [ 'X-OneAccess-Token' ], - $rest->allowed_cors_headers( [ 'X-OneAccess-Token' ] ), - 'Token should not be readded' + [ 'X-OneAccess-Token', 'X-OneAccess-Site-URL' ], + $rest->allowed_cors_headers( [ 'X-OneAccess-Token', 'X-OneAccess-Site-URL' ] ), + 'Headers should not be readded' ); } }