*/ private $feature_source; /** * @param callable():array|null $feature_source Testable local feature adapter. */ public function __construct( FindingFactory $findings, ?callable $feature_source = null ) { $this->findings = $findings; $this->feature_source = $feature_source ?? array( $this, 'wordpress_features' ); } public function id(): string { // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText -- Stable registry machine ID. return 'wordpress.public_features'; } /** * @return array */ public function execute( string $timestamp ): array { try { $features = call_user_func( $this->feature_source ); } catch ( Throwable $exception ) { return $this->error_finding( $timestamp ); } if ( ! is_array( $features ) ) { return $this->error_finding( $timestamp ); } $observations = array(); foreach ( self::FIELDS as $field ) { if ( isset( $features[ $field ] ) && is_bool( $features[ $field ] ) ) { $observations[] = array( 'feature' => $field, 'enabled' => $features[ $field ], ); } } $observed = count( $observations ); $total = count( self::FIELDS ); $registration = isset( $features['user_registration'] ) && true === $features['user_registration']; $role_known = isset( $features['default_role_known'] ) && true === $features['default_role_known']; $privileged = isset( $features['default_role_privileged'] ) && true === $features['default_role_privileged']; if ( 0 === $observed ) { $status = 'not_tested'; $coverage = 'none'; } elseif ( $registration && $role_known && $privileged ) { $status = 'fail'; $coverage = $observed === $total ? 'complete' : 'partial'; } elseif ( $observed < $total || ( $registration && ! $role_known ) ) { $status = 'not_tested'; $coverage = 'partial'; } else { $status = 'pass'; $coverage = 'complete'; } return $this->findings->build( $this->id(), $status, array( 'observations' => $observations, 'method' => 'wordpress_option_and_code_presence_allowlist', 'limitations' => array( 'Enabled public features are not treated as vulnerabilities by themselves. Endpoint reachability, filters, users, credentials and request contents were not inspected.' ), ), $observed, $total, $coverage, $timestamp ); } /** * @return array */ private function wordpress_features(): array { $registration = (bool) get_option( 'users_can_register', false ); $default_role = get_option( 'default_role', 'subscriber' ); $role = is_string( $default_role ) ? get_role( $default_role ) : null; $privileged = false; if ( $role instanceof WP_Role ) { foreach ( array( 'manage_options', 'create_users', 'edit_users', 'install_plugins', 'edit_theme_options' ) as $capability ) { if ( $role->has_cap( $capability ) ) { $privileged = true; break; } } } return array( 'user_registration' => $registration, 'default_role_known' => $role instanceof WP_Role, 'default_role_privileged' => $privileged, 'default_comments' => 'open' === get_option( 'default_comment_status', 'closed' ), 'default_pingbacks' => 'open' === get_option( 'default_ping_status', 'closed' ), 'xmlrpc_code' => file_exists( ( new WordPressPathProvider() )->core_file( 'xmlrpc.php' ) ), 'application_password_api' => function_exists( 'wp_is_application_passwords_supported' ), ); } /** * @return array */ private function error_finding( string $timestamp ): array { return $this->findings->build( $this->id(), 'error', array( 'observations' => array(), 'method' => 'wordpress_option_and_code_presence_allowlist', 'limitations' => array( 'The local public-feature adapter failed; no exception, path, role or user value was stored.' ), ), 0, count( self::FIELDS ), 'error', $timestamp ); } }