AuditCheckTrait.php
1 year ago
CoreUserRoleOptionIntegrityCheck.php
1 year ago
EditableFileSystemCheck.php
7 months ago
ElevatedCoreRoleCheck.php
7 months ago
EmptyUnusedRoleCheck.php
7 months ago
HighPrivilegeContentModeratorCheck.php
7 months ago
HighPrivilegeOrElevatedUserCheck.php
7 months ago
HighPrivilegeRoleCheck.php
7 months ago
HighPrivilegeUserCountCheck.php
7 months ago
RestfulAutoDiscoverEndpointCheck.php
7 months ago
RoleCapabilityNamingConventionCheck.php
7 months ago
RoleIntegrityCheck.php
7 months ago
RoleTransparencyCheck.php
7 months ago
XmlRpcEndpointCheck.php
7 months ago
ElevatedCoreRoleCheck.php
214 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * ====================================================================== |
| 5 | * LICENSE: This file is subject to the terms and conditions defined in * |
| 6 | * file 'license.txt', which is part of this source code package. * |
| 7 | * ====================================================================== |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Detect WordPress core roles that got elevated access |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @version 7.0.0 |
| 15 | */ |
| 16 | class AAM_Audit_ElevatedCoreRoleCheck |
| 17 | { |
| 18 | |
| 19 | use AAM_Audit_AuditCheckTrait; |
| 20 | |
| 21 | /** |
| 22 | * Step ID |
| 23 | * |
| 24 | * @version 7.0.0 |
| 25 | */ |
| 26 | const ID = 'elevated_core_role_caps'; |
| 27 | |
| 28 | /** |
| 29 | * WordPress core roles to scan |
| 30 | * |
| 31 | * @version 7.0.0 |
| 32 | */ |
| 33 | const TARGETING_CORE_ROLES = [ |
| 34 | 'editor' => [ |
| 35 | 'name' => 'Editor', |
| 36 | 'capabilities' => [ |
| 37 | 'moderate_comments', |
| 38 | 'manage_categories', |
| 39 | 'manage_links', |
| 40 | 'upload_files', |
| 41 | 'unfiltered_html', |
| 42 | 'edit_posts', |
| 43 | 'edit_others_posts', |
| 44 | 'edit_published_posts', |
| 45 | 'publish_posts', |
| 46 | 'edit_pages', |
| 47 | 'read', |
| 48 | 'level_7', |
| 49 | 'level_6', |
| 50 | 'level_5', |
| 51 | 'level_4', |
| 52 | 'level_3', |
| 53 | 'level_2', |
| 54 | 'level_1', |
| 55 | 'level_0', |
| 56 | 'edit_others_pages', |
| 57 | 'edit_published_pages', |
| 58 | 'publish_pages', |
| 59 | 'delete_pages', |
| 60 | 'delete_others_pages', |
| 61 | 'delete_published_pages', |
| 62 | 'delete_posts', |
| 63 | 'delete_others_posts', |
| 64 | 'delete_published_posts', |
| 65 | 'delete_private_posts', |
| 66 | 'edit_private_posts', |
| 67 | 'read_private_posts', |
| 68 | 'delete_private_pages', |
| 69 | 'edit_private_pages', |
| 70 | 'read_private_pages' |
| 71 | ] |
| 72 | ], |
| 73 | 'author' => [ |
| 74 | 'name' => 'Author', |
| 75 | 'capabilities' => [ |
| 76 | 'upload_files', |
| 77 | 'edit_posts', |
| 78 | 'edit_published_posts', |
| 79 | 'publish_posts', |
| 80 | 'read', |
| 81 | 'level_2', |
| 82 | 'level_1', |
| 83 | 'level_0', |
| 84 | 'delete_posts', |
| 85 | 'delete_published_posts' |
| 86 | ] |
| 87 | ], |
| 88 | 'contributor' => [ |
| 89 | 'name' => 'Contributor', |
| 90 | 'capabilities' => [ |
| 91 | 'edit_posts', |
| 92 | 'read', |
| 93 | 'level_1', |
| 94 | 'level_0', |
| 95 | 'delete_posts' |
| 96 | ] |
| 97 | ], |
| 98 | 'subscriber' => [ |
| 99 | 'name' => 'Subscriber', |
| 100 | 'capabilities' => [ |
| 101 | 'read', |
| 102 | 'level_0' |
| 103 | ] |
| 104 | ] |
| 105 | ]; |
| 106 | |
| 107 | /** |
| 108 | * Run the check |
| 109 | * |
| 110 | * @return array |
| 111 | * |
| 112 | * @access public |
| 113 | * @static |
| 114 | * |
| 115 | * @version 7.0.0 |
| 116 | */ |
| 117 | public static function run() |
| 118 | { |
| 119 | $issues = []; |
| 120 | $response = [ 'is_completed' => true ]; |
| 121 | |
| 122 | try { |
| 123 | $db_roles = self::_read_role_key_option(); |
| 124 | |
| 125 | array_push( |
| 126 | $issues, |
| 127 | ...self::_detect_elevated_core_roles($db_roles) |
| 128 | ); |
| 129 | } catch (Exception $e) { |
| 130 | array_push($issues, self::_format_issue( |
| 131 | 'APPLICATION_ERROR', |
| 132 | [ |
| 133 | 'message' => $e->getMessage() |
| 134 | ], |
| 135 | 'error' |
| 136 | )); |
| 137 | } |
| 138 | |
| 139 | if (count($issues) > 0) { |
| 140 | $response['issues'] = $issues; |
| 141 | } |
| 142 | |
| 143 | // Determine final status for the check |
| 144 | self::_determine_check_status($response); |
| 145 | |
| 146 | return $response; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Get a collection of error messages for current step |
| 151 | * |
| 152 | * @return array |
| 153 | * @access private |
| 154 | * @static |
| 155 | * |
| 156 | * @version 7.0.0 |
| 157 | */ |
| 158 | private static function _get_message_templates() |
| 159 | { |
| 160 | return [ |
| 161 | 'ELEVATED_CORE_ROLE_CAPS' => __( |
| 162 | 'Detected WordPress core role %s (%s) with elevated caps: %s', |
| 163 | 'advanced-access-manager' |
| 164 | ) |
| 165 | ]; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Detect WordPress core roles that have elevated access controls |
| 170 | * |
| 171 | * @param array $db_roles |
| 172 | * |
| 173 | * @return array |
| 174 | * |
| 175 | * @access private |
| 176 | * @static |
| 177 | * |
| 178 | * @version 7.0.0 |
| 179 | */ |
| 180 | private static function _detect_elevated_core_roles($db_roles) |
| 181 | { |
| 182 | $response = []; |
| 183 | |
| 184 | foreach($db_roles as $role_id => $role) { |
| 185 | // Take into consideration only core roles |
| 186 | if (array_key_exists($role_id, self::TARGETING_CORE_ROLES)) { |
| 187 | $existing_caps = array_keys( |
| 188 | array_filter($role['capabilities'], function($v) { |
| 189 | return !empty($v); |
| 190 | }) |
| 191 | ); |
| 192 | $diff_caps = array_diff( |
| 193 | $existing_caps, |
| 194 | self::TARGETING_CORE_ROLES[$role_id]['capabilities'] |
| 195 | ); |
| 196 | |
| 197 | if (!empty($diff_caps)) { |
| 198 | array_push($response, self::_format_issue( |
| 199 | 'ELEVATED_CORE_ROLE_CAPS', |
| 200 | [ |
| 201 | 'name' => translate_user_role($role['name']), |
| 202 | 'slug' => $role_id, |
| 203 | 'caps' => $diff_caps |
| 204 | ], |
| 205 | 'warning' |
| 206 | )); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return $response; |
| 212 | } |
| 213 | |
| 214 | } |