*/ private $inventory_source; private string $hash_key; private WordPressPathProvider $paths; /** * @param callable():array|null $inventory_source Testable bounded filesystem adapter. */ public function __construct( FindingFactory $findings, ?callable $inventory_source = null, ?string $hash_key = null, ?WordPressPathProvider $paths = null ) { $this->findings = $findings; $this->inventory_source = $inventory_source ?? array( $this, 'filesystem_inventory' ); $this->hash_key = $hash_key ?? ( new LocalHashKey() )->derive(); $this->paths = $paths ?? new WordPressPathProvider(); } public function id(): string { return 'security.writable_php'; } /** * @return array */ public function execute( string $timestamp ): array { try { $inventory = call_user_func( $this->inventory_source ); } catch ( Throwable $exception ) { return $this->error_finding( $timestamp ); } if ( ! is_array( $inventory ) || ! isset( $inventory['scopes'], $inventory['truncated'] ) || ! is_array( $inventory['scopes'] ) || ! is_bool( $inventory['truncated'] ) ) { return $this->error_finding( $timestamp ); } $path_hashes = array(); $masked_paths = array(); $signals = array(); $observed = 0; $invalid = 0; $process_writes = 0; $world_writes = 0; $truncated = $inventory['truncated'] || count( $inventory['scopes'] ) > self::SCOPE_LIMIT; $seen_scopes = array(); foreach ( array_slice( $inventory['scopes'], 0, self::SCOPE_LIMIT ) as $scope ) { if ( ! $this->valid_scope( $scope ) || isset( $seen_scopes[ $scope['type'] ] ) ) { ++$invalid; continue; } $seen_scopes[ $scope['type'] ] = true; if ( $scope['accessible'] ) { ++$observed; } $process_writes += $scope['process_writable']; $world_writes += $scope['world_writable']; $signals[] = array( 'scope' => $scope['type'], 'accessible' => $scope['accessible'], 'items_observed' => $scope['items'], 'executable_files' => $scope['executable'], 'process_writable_files' => $scope['process_writable'], 'world_writable_files' => $scope['world_writable'], ); if ( count( $scope['issues'] ) > self::EVIDENCE_LIMIT ) { $truncated = true; } foreach ( array_slice( $scope['issues'], 0, self::EVIDENCE_LIMIT ) as $issue ) { if ( count( $path_hashes ) >= self::EVIDENCE_LIMIT ) { $truncated = true; break; } if ( ! is_array( $issue ) || ! isset( $issue['path'], $issue['writability'] ) || ! is_string( $issue['path'] ) || 1 !== preg_match( '/^[^\x00-\x1F\x7F]{1,' . self::PATH_LIMIT . '}$/', $issue['path'] ) || ! in_array( $issue['writability'], array( 'process_writable', 'world_writable' ), true ) ) { ++$invalid; continue; } $path_hashes[] = array( 'type' => $scope['type'], 'hash' => hash_hmac( 'sha256', $issue['path'], $this->hash_key ), 'writability' => $issue['writability'], ); $masked_paths[] = array( 'type' => $scope['type'], 'label' => '{' . str_replace( '_', '-', $scope['type'] ) . '}/…/{executable-file}', ); } } $total = min( count( $inventory['scopes'] ), self::SCOPE_LIMIT ); if ( 0 === $observed ) { $status = 'not_tested'; $coverage = 'none'; } elseif ( $world_writes > 0 ) { $status = 'fail'; $coverage = $truncated || $invalid > 0 || $observed < $total ? 'partial' : 'complete'; } elseif ( $process_writes > 0 ) { $status = 'info'; $coverage = $truncated || $invalid > 0 || $observed < $total ? 'partial' : 'complete'; } elseif ( $truncated || $invalid > 0 || $observed < $total ) { $status = 'not_tested'; $coverage = 'partial'; } else { $status = 'pass'; $coverage = 'complete'; } return $this->findings->build( $this->id(), $status, array( 'path_hashes' => $path_hashes, 'masked_paths' => $masked_paths, 'signal_types' => $signals, 'observed_count' => $observed, 'truncated' => $truncated, 'limitations' => array( 'The scan reads directory and permission metadata for at most 20,000 entries and ten seconds; it never reads file contents or follows symbolic-link entries encountered inside the selected roots.', 'Process writability can be expected on some managed or suexec hosting. World writability is a stronger signal, but ownership, ACLs, web-server execution rules and exploitability still require server-level review.', ), ), $observed, $total, $coverage, $timestamp ); } /** * @param mixed $scope Raw scope observation. */ private function valid_scope( $scope ): bool { return is_array( $scope ) && isset( $scope['type'], $scope['accessible'], $scope['items'], $scope['executable'], $scope['process_writable'], $scope['world_writable'], $scope['issues'] ) && is_string( $scope['type'] ) && 1 === preg_match( '/^[a-z0-9_]{1,48}$/', $scope['type'] ) && is_bool( $scope['accessible'] ) && is_int( $scope['items'] ) && $scope['items'] >= 0 && $scope['items'] <= self::ITEM_LIMIT && is_int( $scope['executable'] ) && $scope['executable'] >= 0 && $scope['executable'] <= $scope['items'] && is_int( $scope['process_writable'] ) && $scope['process_writable'] >= 0 && is_int( $scope['world_writable'] ) && $scope['world_writable'] >= 0 && $scope['world_writable'] <= $scope['process_writable'] && $scope['process_writable'] <= $scope['executable'] && is_array( $scope['issues'] ); } /** * @return array{scopes:list>,truncated:bool} */ private function filesystem_inventory(): array { $roots = array( array( 'type' => 'wordpress_root', 'path' => $this->paths->wordpress_root(), 'recursive' => false, ), array( 'type' => 'wp_admin', 'path' => $this->paths->core_file( 'wp-admin' ), 'recursive' => true, ), array( 'type' => 'wp_includes', 'path' => $this->paths->core_file( 'wp-includes' ), 'recursive' => true, ), array( 'type' => 'plugins', 'path' => $this->paths->plugins_root(), 'recursive' => true, ), array( 'type' => 'themes', 'path' => $this->paths->theme_roots()[0] ?? '', 'recursive' => true, ), ); $mu_plugins_root = $this->paths->mu_plugins_root(); if ( '' !== $mu_plugins_root ) { $roots[] = array( 'type' => 'mu_plugins', 'path' => $mu_plugins_root, 'recursive' => true, ); } $uploads_root = $this->paths->uploads_root(); if ( '' !== $uploads_root ) { $roots[] = array( 'type' => 'uploads', 'path' => $uploads_root, 'recursive' => true, ); } $started = microtime( true ); $items = 0; $truncated = false; $scopes = array(); $seen = array(); foreach ( $roots as $root ) { $normalized = wp_normalize_path( $root['path'] ); if ( isset( $seen[ $normalized ] ) ) { continue; } $seen[ $normalized ] = true; $scopes[] = $this->scan_scope( $root['type'], $root['path'], $root['recursive'], $started, $items, $truncated ); if ( $truncated ) { break; } } return array( 'scopes' => $scopes, 'truncated' => $truncated, ); } /** * @return array{type:string,accessible:bool,items:int,executable:int,process_writable:int,world_writable:int,issues:list} */ private function scan_scope( string $type, string $path, bool $recursive, float $started, int &$global_items, bool &$truncated ): array { $scope = array( 'type' => $type, 'accessible' => is_dir( $path ) && is_readable( $path ), 'items' => 0, 'executable' => 0, 'process_writable' => 0, 'world_writable' => 0, 'issues' => array(), ); if ( ! $scope['accessible'] ) { return $scope; } $directories = array( $path ); try { while ( array() !== $directories ) { $directory = array_pop( $directories ); if ( ! is_string( $directory ) ) { continue; } $iterator = new FilesystemIterator( $directory, FilesystemIterator::SKIP_DOTS ); foreach ( $iterator as $entry ) { if ( $truncated || $global_items >= self::ITEM_LIMIT || microtime( true ) - $started >= self::TIME_LIMIT ) { $truncated = true; break 2; } ++$global_items; ++$scope['items']; if ( $entry->isLink() ) { continue; } if ( $recursive && $entry->isDir() ) { $next_directory = $entry->getPathname(); if ( strlen( $next_directory ) > self::PATH_LIMIT || count( $directories ) >= self::DIRECTORY_LIMIT ) { $truncated = true; continue; } $directories[] = $next_directory; continue; } if ( ! $entry->isFile() || ! in_array( strtolower( $entry->getExtension() ), self::EXTENSIONS, true ) ) { continue; } ++$scope['executable']; $pathname = $entry->getPathname(); // Direct permission metadata inspection is read-only and does not request filesystem credentials. // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_is_writable $writable = is_writable( $pathname ); // Suppression prevents a race with deletion from logging a raw path. // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged $mode = @fileperms( $pathname ); $world = is_int( $mode ) && 0 !== ( $mode & 0002 ); if ( $writable || $world ) { ++$scope['process_writable']; } if ( $world ) { ++$scope['world_writable']; } if ( ( $writable || $world ) && count( $scope['issues'] ) <= self::EVIDENCE_LIMIT ) { $scope['issues'][] = array( 'path' => wp_normalize_path( $pathname ), 'writability' => $world ? 'world_writable' : 'process_writable', ); } } } } catch ( Throwable $exception ) { $truncated = true; } return $scope; } /** * @return array */ private function error_finding( string $timestamp ): array { return $this->findings->build( $this->id(), 'error', array( 'path_hashes' => array(), 'masked_paths' => array(), 'signal_types' => array(), 'observed_count' => 0, 'truncated' => false, 'limitations' => array( 'The bounded filesystem adapter failed; no exception, raw path, permission mode, directory listing or file content was stored.' ), ), 0, 0, 'error', $timestamp ); } }