'.git', 'composer_manifest' => 'composer.json', 'composer_lock' => 'composer.lock', 'node_manifest' => 'package.json', 'npm_lock' => 'package-lock.json', 'yarn_lock' => 'yarn.lock', 'pnpm_lock' => 'pnpm-lock.yaml', 'github_workflows' => '.github/workflows', 'gitlab_pipeline' => '.gitlab-ci.yml', 'bitbucket_pipeline' => 'bitbucket-pipelines.yml', 'deploy_script' => 'deploy.php', 'documentation' => 'docs', ); private FindingFactory $findings; /** @var callable():array */ private $artifact_source; private string $hash_key; private WordPressPathProvider $paths; /** * @param callable():array|null $artifact_source Testable direct-metadata adapter. */ public function __construct( FindingFactory $findings, ?callable $artifact_source = null, ?string $hash_key = null, ?WordPressPathProvider $paths = null ) { $this->findings = $findings; $this->artifact_source = $artifact_source ?? array( $this, 'wordpress_artifacts' ); $this->hash_key = $hash_key ?? ( new LocalHashKey() )->derive(); $this->paths = $paths ?? new WordPressPathProvider(); } public function id(): string { return 'takeover.repository_build'; } /** * @return array */ public function execute( string $timestamp ): array { try { $roots = call_user_func( $this->artifact_source ); } catch ( Throwable $exception ) { return $this->error_finding( $timestamp ); } if ( ! is_array( $roots ) ) { return $this->error_finding( $timestamp ); } $path_hashes = array(); $masked_paths = array(); $signals = array(); $observed = 0; $invalid = 0; $valid_roots = 0; $truncated = count( $roots ) > self::ROOT_LIMIT; $seen_roots = array(); foreach ( array_slice( $roots, 0, self::ROOT_LIMIT ) as $root ) { if ( ! is_array( $root ) || ! isset( $root['type'], $root['path'], $root['accessible'], $root['artifacts'] ) || ! is_string( $root['type'] ) || 1 !== preg_match( '/^[a-z0-9_]{1,48}$/', $root['type'] ) || isset( $seen_roots[ $root['type'] ] ) || ! is_string( $root['path'] ) || 1 !== preg_match( '/^[^\x00-\x1F\x7F]{1,' . self::PATH_LIMIT . '}$/', $root['path'] ) || ! is_bool( $root['accessible'] ) || ! is_array( $root['artifacts'] ) ) { ++$invalid; continue; } $seen_roots[ $root['type'] ] = true; ++$valid_roots; $artifacts = array(); foreach ( array_keys( self::ARTIFACTS ) as $artifact ) { if ( isset( $root['artifacts'][ $artifact ] ) && is_bool( $root['artifacts'][ $artifact ] ) ) { $artifacts[] = array( 'artifact' => $artifact, 'present' => $root['artifacts'][ $artifact ], ); } } if ( $root['accessible'] ) { $observed += count( $artifacts ); } $reference = hash_hmac( 'sha256', $root['path'], $this->hash_key ); $path_hashes[] = array( 'type' => $root['type'], 'hash' => $reference, ); $masked_paths[] = array( 'type' => $root['type'], 'label' => '{' . str_replace( '_', '-', $root['type'] ) . '}', ); $signals[] = array( 'type' => $root['type'], 'accessible' => $root['accessible'], 'artifacts' => $artifacts, ); } $total = $valid_roots * count( self::ARTIFACTS ); if ( 0 === $observed ) { $status = 'not_tested'; $coverage = 'none'; } else { $status = 'info'; $coverage = $invalid > 0 || $truncated || $observed < $total ? 'partial' : '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( 'Only direct existence metadata for a fixed artifact allowlist was checked at the WordPress root and active-theme root. No manifest, lockfile, VCS metadata, workflow or documentation content was read.', 'A deployment can intentionally omit source and build artifacts. Presence does not prove that a repository is current, accessible, complete, reproducible or owned by the site operator.', ), ), $observed, $total, $coverage, $timestamp ); } /** * @return list}> */ private function wordpress_artifacts(): array { $roots = array( array( 'type' => 'wordpress_root', 'path' => $this->paths->wordpress_root(), ), ); $theme_root = get_stylesheet_directory(); if ( is_string( $theme_root ) && '' !== $theme_root && wp_normalize_path( $theme_root ) !== $this->paths->wordpress_root() ) { $roots[] = array( 'type' => 'active_theme', 'path' => $theme_root, ); } $observations = array(); foreach ( $roots as $root ) { $accessible = is_dir( $root['path'] ) && is_readable( $root['path'] ); $artifacts = array(); if ( $accessible ) { foreach ( self::ARTIFACTS as $type => $relative_path ) { $artifacts[ $type ] = file_exists( trailingslashit( $root['path'] ) . $relative_path ); } } $observations[] = array( 'type' => $root['type'], 'path' => wp_normalize_path( $root['path'] ), 'accessible' => $accessible, 'artifacts' => $artifacts, ); } return $observations; } /** * @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 direct artifact observer failed; no exception, raw path, directory listing, repository metadata or file content was stored.' ), ), 0, 0, 'error', $timestamp ); } }