Skip to content

Commit 2e679c4

Browse files
deepenchlihsaa591
andauthored
Fix - Registration form status and role validation not enforced (#1419)
* Fix - Registration form status and role validation not enforced * Fix - Harden role check against malformed filters and role-name capability lookup * Fix stale wpeverest/.github reference to themegrill/.github --------- Co-authored-by: Aashil Bijukshe <81949431+lihsaa591@users.noreply.github.com>
1 parent 66b74c7 commit 2e679c4

3 files changed

Lines changed: 80 additions & 4 deletions

File tree

includes/class-ur-form-handler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ public function get_form( $id = '', $args = array() ) {
10421042
$the_post->post_content = str_replace( '"noopener"', "'noopener'", $the_post->post_content );
10431043

10441044
if ( isset( $args['publish'] ) ) {
1045-
if ( ( $args['publish'] && 'publish' === $the_post->post_type ) || ( ! $args['publish'] && 'publish' !== $the_post->post_type ) ) {
1045+
if ( ( $args['publish'] && 'publish' !== $the_post->post_status ) || ( ! $args['publish'] && 'publish' === $the_post->post_status ) ) {
10461046
return array();
10471047
}
10481048
}

includes/frontend/class-ur-frontend-form-handler.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,23 @@ public static function handle_form( $form_data, $form_id ) {
6565
self::$valid_form_data = array();
6666

6767
self::$form_id = $form_id;
68-
$post_content_array = ( $form_id ) ? UR()->form->get_form( $form_id, array( 'content_only' => true ) ) : array();
68+
$post_content_array = ( $form_id ) ? UR()->form->get_form( $form_id, array( 'content_only' => true, 'publish' => true ) ) : array();
69+
70+
if ( empty( $post_content_array ) ) {
71+
$logger->error(
72+
sprintf( '[Form #%d] Form could not be loaded for submission (missing, unpublished, or trashed).', $form_id ),
73+
array(
74+
'source' => 'form-submission',
75+
'form_id' => $form_id,
76+
)
77+
);
78+
79+
wp_send_json_error(
80+
array(
81+
'message' => __( 'This form is currently unavailable.', 'user-registration' ),
82+
)
83+
);
84+
}
6985

7086
if ( gettype( $form_data ) != 'array' && gettype( $form_data ) != 'object' ) {
7187
$form_data = array();
@@ -134,8 +150,13 @@ public static function handle_form( $form_data, $form_id ) {
134150
// $logger->info( __( 'Response received', 'user-registration' ), array( 'source' => 'form-submission' ) );
135151

136152
if ( count( self::$response_array ) === 0 ) {
137-
$user_role = ! in_array( ur_get_form_setting_by_key( $form_id, 'user_registration_form_setting_default_user_role' ), array_keys( ur_get_default_admin_roles() ) ) ? 'subscriber' : ur_get_form_setting_by_key( $form_id, 'user_registration_form_setting_default_user_role' );
138-
$user_role = apply_filters( 'user_registration_user_role', $user_role, self::$valid_form_data, $form_id );
153+
$configured_user_role = ur_get_form_setting_by_key( $form_id, 'user_registration_form_setting_default_user_role' );
154+
$is_known_role = in_array( $configured_user_role, array_keys( ur_get_default_admin_roles() ) );
155+
$current_user_capability = apply_filters( 'ur_registration_user_capability', 'create_users' );
156+
// A logged-in submitter already passed this same capability check in ur_process_registration(); an anonymous one never has.
157+
$is_trusted_submitter = is_user_logged_in() && ( current_user_can( 'manage_options' ) || current_user_can( $current_user_capability ) );
158+
$user_role = ( $is_known_role && ( $is_trusted_submitter || ! ur_registration_role_is_privileged( $configured_user_role ) ) ) ? $configured_user_role : 'subscriber';
159+
$user_role = apply_filters( 'user_registration_user_role', $user_role, self::$valid_form_data, $form_id );
139160
$user_registered_date = apply_filters( 'user_registration_user_registered_date', current_time( 'Y-m-d H:i:s' ) );
140161
$userdata = array(
141162
'user_login' => isset( self::$valid_form_data['user_login'] ) ? self::$valid_form_data['user_login']->value : '',

includes/functions-ur-core.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,61 @@ function ur_get_default_admin_roles() {
13731373
return apply_filters( 'user_registration_user_default_roles', $all_roles );
13741374
}
13751375

1376+
if ( ! function_exists( 'ur_registration_role_is_privileged' ) ) {
1377+
/**
1378+
* Whether a role carries administrator-tier capabilities.
1379+
*
1380+
* @since 5.2.8
1381+
*
1382+
* @param string $role Role key.
1383+
* @return bool True if the role must never be auto-assigned to an anonymous registration.
1384+
*/
1385+
function ur_registration_role_is_privileged( $role ) {
1386+
$wp_role = get_role( $role );
1387+
1388+
// Fail closed: an unrecognized role is never safe to auto-assign.
1389+
if ( ! $wp_role ) {
1390+
return true;
1391+
}
1392+
1393+
$default_restricted_caps = array(
1394+
'manage_options',
1395+
'edit_others_posts',
1396+
'edit_users',
1397+
'delete_users',
1398+
'create_users',
1399+
'promote_users',
1400+
'install_plugins',
1401+
'edit_plugins',
1402+
'delete_plugins',
1403+
'edit_theme_options',
1404+
'unfiltered_html',
1405+
'update_core',
1406+
);
1407+
1408+
/**
1409+
* Filters the capabilities that mark a role too privileged to be
1410+
* auto-assigned to an anonymous public registration.
1411+
*
1412+
* @param array $restricted_caps Capability keys.
1413+
*/
1414+
$restricted_caps = apply_filters( 'user_registration_restricted_registration_capabilities', $default_restricted_caps );
1415+
1416+
// Fail closed: an invalid filter return must not silently disable the check.
1417+
if ( ! is_array( $restricted_caps ) ) {
1418+
$restricted_caps = $default_restricted_caps;
1419+
}
1420+
1421+
foreach ( $restricted_caps as $cap ) {
1422+
if ( ! empty( $wp_role->capabilities[ $cap ] ) ) {
1423+
return true;
1424+
}
1425+
}
1426+
1427+
return false;
1428+
}
1429+
}
1430+
13761431

13771432
/**
13781433
* Random number generated by time()

0 commit comments

Comments
 (0)