From 190cd202b0de076b027c0085388de4624114d958 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 16 Nov 2026 07:47:36 +0000 Subject: [PATCH 1/2] Add `wp maintenance dependencies update` Updates the locked packages that `dependencies audit` reports security advisories for, or the ones given with --packages, within the constraints of composer.json. Runs `composer update --no-install` against the files on GitHub, audits the new lock again, and commits it through the Git Data API on top of the commit it was built from. The pull request comes from a branch named after the update, so a security fix and a refresh of the framework never replace each other, and a rerun with the same result only refreshes the pull request. A branch with commits or changes the command did not make is never overwritten. Wildcards are resolved against the lock, as Composer would only update their matches as far as needed. Composer's security blocking is turned off for the update, so that an advisory composer.json does not allow fixing does not hold back the others; the audit afterwards reports it. A lock written by a newer Composer than the local one is not downgraded. Moves the temporary directory and environment handling of the audit into a Composer helper shared by both subcommands, and the confirmation prompt of `release generate` into a Prompt helper. Co-Authored-By: Claude Opus 5.5 --- .maintenance/src/Composer.php | 136 ++++++ .maintenance/src/Dependencies_Command.php | 161 ++++++- .maintenance/src/Dependency_Audit.php | 180 ++++---- .maintenance/src/Dependency_Update.php | 501 ++++++++++++++++++++++ .maintenance/src/GitHub.php | 177 ++++++++ .maintenance/src/Prompt.php | 30 ++ .maintenance/src/Release_Command.php | 15 +- .readme-partials/DEVELOPMENT.md | 2 +- tests/ComposerTest.php | 43 ++ tests/DependenciesUpdateCommandTest.php | 490 +++++++++++++++++++++ tests/DependencyAuditTest.php | 24 -- tests/DependencyUpdateTest.php | 231 ++++++++++ 12 files changed, 1850 insertions(+), 140 deletions(-) create mode 100644 .maintenance/src/Composer.php create mode 100644 .maintenance/src/Dependency_Update.php create mode 100644 .maintenance/src/Prompt.php create mode 100644 tests/ComposerTest.php create mode 100644 tests/DependenciesUpdateCommandTest.php create mode 100644 tests/DependencyUpdateTest.php diff --git a/.maintenance/src/Composer.php b/.maintenance/src/Composer.php new file mode 100644 index 0000000..68e13c9 --- /dev/null +++ b/.maintenance/src/Composer.php @@ -0,0 +1,136 @@ + $files File name => contents. + * @param string[] $read_back Files to read back afterwards. + * @param array $env Extra environment variables. + * + * @return array{return_code: int, stdout: string, stderr: string, files: array} + */ + public static function run( array $arguments, array $files, array $read_back = [], array $env = [] ) { + $dir = Utils\get_temp_dir() . uniqid( 'wp-cli-maintenance-composer-', true ); + + $written = mkdir( $dir, 0700 ) && mkdir( "{$dir}/home", 0700 ); + foreach ( $files as $name => $contents ) { + $written = $written && false !== file_put_contents( "{$dir}/{$name}", $contents ); + } + + if ( ! $written ) { + self::remove_directory( $dir ); + WP_CLI::error( "Could not write the files for Composer to the temporary directory {$dir}." ); + } + + $arguments = array_merge( + $arguments, + [ '--no-interaction', '--no-plugins', '--no-scripts', "--working-dir={$dir}" ] + ); + + $command = 'composer ' . implode( ' ', array_map( 'escapeshellarg', $arguments ) ); + $auth = GitHub::get_auth(); + $result = Process::create( $command, null, array_merge( self::get_env( (array) getenv(), "{$dir}/home", $auth['token'] ), $env ) )->run(); + + $contents = []; + foreach ( $read_back as $name ) { + $contents[ $name ] = is_file( "{$dir}/{$name}" ) ? (string) file_get_contents( "{$dir}/{$name}" ) : null; + } + + self::remove_directory( $dir ); + + if ( '' !== trim( $result->stderr ) ) { + WP_CLI::debug( trim( $result->stderr ), 'composer' ); + } + + return [ + 'return_code' => (int) $result->return_code, + 'stdout' => (string) $result->stdout, + 'stderr' => (string) $result->stderr, + 'files' => $contents, + ]; + } + + /** + * Builds the environment for Composer: the current one, without the + * variables that change its behavior, and with a Composer home of its + * own, so that no global configuration, such as `audit.ignore` or extra + * repositories, applies. + * + * @param array $env Current environment. + * @param string $composer_home Empty directory to use as + * Composer home. + * @param string|null $github_token Token to raise GitHub's API + * rate limit for Composer. + * + * @return array + */ + public static function get_env( array $env, $composer_home, $github_token = null ) { + foreach ( self::ENV_OVERRIDES as $variable ) { + unset( $env[ $variable ] ); + } + + $env['COMPOSER_HOME'] = $composer_home; + + if ( $github_token ) { + $env['COMPOSER_AUTH'] = (string) json_encode( [ 'github-oauth' => [ 'github.com' => $github_token ] ] ); + } + + return $env; + } + + private static function remove_directory( $dir ) { + if ( ! is_dir( $dir ) ) { + return; + } + + $entries = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator( $dir, \FilesystemIterator::SKIP_DOTS ), + \RecursiveIteratorIterator::CHILD_FIRST + ); + + foreach ( $entries as $entry ) { + if ( $entry->isDir() && ! $entry->isLink() ) { + rmdir( $entry->getPathname() ); + } else { + unlink( $entry->getPathname() ); + } + } + + rmdir( $dir ); + } +} diff --git a/.maintenance/src/Dependencies_Command.php b/.maintenance/src/Dependencies_Command.php index 830bd4c..c7a5890 100644 --- a/.maintenance/src/Dependencies_Command.php +++ b/.maintenance/src/Dependencies_Command.php @@ -5,7 +5,7 @@ use WP_CLI\Utils; /** - * Checks the locked dependencies of the repositories that commit a + * Checks and updates the locked dependencies of the repositories that commit a * composer.lock, such as wp-cli/wp-cli-bundle. */ final class Dependencies_Command { @@ -114,6 +114,165 @@ public function audit( $args, $assoc_args ) { } } + /** + * Updates locked dependencies and proposes the new composer.lock as a pull request. + * + * By default, updates the packages that `wp maintenance dependencies + * audit` reports security advisories for. The update runs within the + * constraints of composer.json, which stays unchanged, and moves the + * dependencies of the updated packages only as far as needed. + * + * The new composer.lock is audited again, then committed on top of the + * default branch, and a pull request is opened from that commit. The branch + * is named after the update: `update-dependencies-security` by default, + * and after the packages with --packages. A later run of the same update + * replaces that commit and refreshes the pull request, but never + * overwrites a branch that someone else pushed to. + * + * Without --packages, exits with an error code when advisories are left + * that the update cannot fix, as fixing them needs a change to + * composer.json. + * + * Needs Composer 2.7 or later. Works from the files on GitHub, so no clone + * of the repository is needed. + * + * ## OPTIONS + * + * [...] + * : Name(s) of the repository to update. If no user/org was provided, + * 'wp-cli' org is assumed. Defaults to wp-cli/wp-cli-bundle. Repositories + * without a composer.lock are skipped with a warning. + * + * [--packages=] + * : Comma-separated list of packages to update instead, with their + * dependencies. `*` matches any characters, so `symfony/*` updates all + * locked Symfony packages and `*` refreshes the whole lock. + * + * [--dry-run] + * : Show what the update changes, without pushing anything. + * + * [--yes] + * : Push and open the pull request without asking for confirmation. + * + * ## EXAMPLES + * + * # Fix what the audit reports. + * $ wp maintenance dependencies update --dry-run + * +--------------------+---------+---------+-----+ + * | package | from | to | dev | + * +--------------------+---------+---------+-----+ + * | symfony/config | v5.4.46 | v5.4.56 | yes | + * | symfony/filesystem | v5.4.54 | v5.4.56 | yes | + * | symfony/yaml | v5.4.54 | v5.4.56 | yes | + * +--------------------+---------+---------+-----+ + * Fixes 3 security advisories. + * Dry run: not pushing the new composer.lock of wp-cli/wp-cli-bundle. + * + * # Move the bundle to the latest commit of the framework. + * $ wp maintenance dependencies update --packages=wp-cli/wp-cli --yes + * + * @when before_wp_load + */ + public function update( $args, $assoc_args ) { + $dry_run = (bool) Utils\get_flag_value( $assoc_args, 'dry-run', false ); + $confirmed = (bool) Utils\get_flag_value( $assoc_args, 'yes', false ); + $packages = array_values( array_filter( array_map( 'trim', explode( ',', (string) Utils\get_flag_value( $assoc_args, 'packages', '' ) ) ), 'strlen' ) ); + $repos = $args ? array_values( array_unique( array_map( [ GitHub::class, 'qualify_repo' ], $args ) ) ) : [ Bundle::REPO ]; + + foreach ( $packages as $package ) { + if ( '-' === $package[0] ) { + WP_CLI::error( "'{$package}' is not a package name." ); + } + } + + $updated = false; + $still_found = []; + foreach ( $repos as $repo ) { + $plan = Dependency_Update::prepare( $repo, $packages ); + + if ( null === $plan ) { + WP_CLI::warning( "{$repo} has no composer.lock, skipping it." ); + continue; + } + + $updated = true; + + if ( $plan['remaining'] ) { + $still_found[] = $repo; + } + + foreach ( $plan['unmatched'] as $pattern ) { + WP_CLI::warning( "{$pattern} matches no package locked in {$repo}." ); + } + + if ( ! $plan['packages'] ) { + if ( ! $plan['requested'] ) { + WP_CLI::success( "No locked package of {$repo} has a security advisory, so there is nothing to update." ); + } + continue; + } + + if ( ! $plan['changes'] ) { + WP_CLI::warning( 'Updating ' . implode( ', ', $plan['packages'] ) . " changes no locked version of {$repo}." ); + $this->report_remaining( $plan ); + continue; + } + + Utils\format_items( + 'table', + array_map( + static function ( $change ) { + return [ + 'package' => $change['package'], + 'from' => null === $change['from'] ? '(new)' : $change['from'], + 'to' => null === $change['to'] ? '(removed)' : $change['to'], + 'dev' => $change['dev'] ? 'yes' : 'no', + ]; + }, + $plan['changes'] + ), + [ 'package', 'from', 'to', 'dev' ] + ); + + if ( $plan['fixed'] ) { + WP_CLI::log( sprintf( 'Fixes %d security %s.', count( $plan['fixed'] ), 1 === count( $plan['fixed'] ) ? 'advisory' : 'advisories' ) ); + } + $this->report_remaining( $plan ); + + if ( $dry_run ) { + WP_CLI::log( "Dry run: not pushing the new composer.lock of {$repo}." ); + continue; + } + + if ( ! $confirmed && ! Prompt::confirm( "Push the new composer.lock to the {$plan['head']} branch of {$repo} and open a pull request?" ) ) { + WP_CLI::log( "Skipping {$repo}." ); + continue; + } + + list( $pull_request, $pushed ) = Dependency_Update::publish( $plan ); + + if ( ! $pushed ) { + WP_CLI::log( "The {$plan['head']} branch already holds this composer.lock, so only the pull request was refreshed." ); + } + WP_CLI::success( "Pull request for {$repo}: {$pull_request->html_url}" ); + } + + if ( ! $updated ) { + WP_CLI::error( 'None of the repositories has a composer.lock to update.' ); + } + + // An explicit list of packages may leave advisories alone on purpose. + if ( $still_found && ! $packages ) { + WP_CLI::error( 'Locked packages are still affected by security advisories in ' . implode( ', ', $still_found ) . '. Fixing them needs a change to composer.json.' ); + } + } + + private function report_remaining( array $plan ) { + foreach ( $plan['remaining'] as $advisory ) { + WP_CLI::warning( "{$advisory['package']} {$advisory['version']} is still affected by {$advisory['advisory']}: {$advisory['title']}" ); + } + } + private function render_table( array $audits ) { $rows = []; foreach ( $audits as $audit ) { diff --git a/.maintenance/src/Dependency_Audit.php b/.maintenance/src/Dependency_Audit.php index 53c3402..00921c3 100644 --- a/.maintenance/src/Dependency_Audit.php +++ b/.maintenance/src/Dependency_Audit.php @@ -1,8 +1,6 @@ $ref, + 'sha' => $sha, + 'composer_json' => $composer_json, + 'composer_lock' => $lock, + 'lock' => $decoded_lock, + ]; } /** - * Runs `composer audit` on a lock, unless it has no packages to audit. - * Composer skips such a lock without writing a report, which would look - * like a failure. + * Runs `composer audit` against a composer.json and composer.lock, unless + * the lock has no packages to audit. Composer skips such a lock without + * writing a report, which would look like a failure. + * + * @param string $composer_json Contents of composer.json. + * @param string $composer_lock Contents of composer.lock. + * @param bool $dev Whether to audit the require-dev packages too. + * + * @return array Decoded JSON report. */ - private static function audit_lock( $composer_json, $composer_lock, array $lock, $dev ) { - $packages = self::get_locked_packages( $lock ); - - if ( ! $dev ) { - $packages = array_filter( - $packages, - static function ( $package ) { - return ! $package['dev']; - } - ); - } + public static function audit_files( $composer_json, $composer_lock, $dev = true ) { + $lock = json_decode( $composer_lock, true ); + + if ( is_array( $lock ) ) { + $packages = self::get_locked_packages( $lock ); + + if ( ! $dev ) { + $packages = array_filter( + $packages, + static function ( $package ) { + return ! $package['dev']; + } + ); + } - if ( ! $packages ) { - return [ - 'advisories' => [], - 'abandoned' => [], - ]; + if ( ! $packages ) { + return [ + 'advisories' => [], + 'abandoned' => [], + ]; + } } $runner = self::$runner ? self::$runner : [ __CLASS__, 'run_composer_audit' ]; @@ -203,28 +232,6 @@ public static function get_locked_packages( array $lock ) { return $packages; } - /** - * Builds the environment for `composer audit`: the current one, without - * the variables that change what it checks, and with a Composer home of - * its own, so that no global configuration, such as `audit.ignore`, hides - * an advisory. - * - * @param array $env Current environment. - * @param string $composer_home Empty directory to use as - * Composer home. - * - * @return array - */ - public static function get_composer_env( array $env, $composer_home ) { - foreach ( self::COMPOSER_ENV_OVERRIDES as $variable ) { - unset( $env[ $variable ] ); - } - - $env['COMPOSER_HOME'] = $composer_home; - - return $env; - } - /** * Runs `composer audit` against a composer.json and composer.lock. * @@ -239,37 +246,29 @@ public static function get_composer_env( array $env, $composer_home ) { * @return array Decoded JSON report. */ private static function run_composer_audit( $composer_json, $composer_lock, $dev ) { - $dir = Utils\get_temp_dir() . uniqid( 'wp-cli-dependency-audit-', true ); - - if ( ! mkdir( $dir, 0700 ) || ! mkdir( "{$dir}/home", 0700 ) - || false === file_put_contents( "{$dir}/composer.json", $composer_json ) - || false === file_put_contents( "{$dir}/composer.lock", $composer_lock ) ) { - self::remove_directory( $dir ); - WP_CLI::error( "Could not write the files to audit to the temporary directory {$dir}." ); - } - // --abandoned=report matches the Code Quality workflow, and keeps a // composer.json setting from hiding abandoned packages. - $command = 'composer audit --locked --format=json --abandoned=report --no-interaction --no-plugins --no-scripts' - . ( $dev ? '' : ' --no-dev' ) - . ' --working-dir=' . escapeshellarg( $dir ); - - $result = Process::create( $command, null, self::get_composer_env( (array) getenv(), "{$dir}/home" ) )->run(); - - self::remove_directory( $dir ); - - if ( '' !== trim( $result->stderr ) ) { - WP_CLI::debug( trim( $result->stderr ), 'dependencies' ); + $arguments = [ 'audit', '--locked', '--format=json', '--abandoned=report' ]; + if ( ! $dev ) { + $arguments[] = '--no-dev'; } + $result = Composer::run( + $arguments, + [ + 'composer.json' => $composer_json, + 'composer.lock' => $composer_lock, + ] + ); + // The exit code flags vulnerable and abandoned packages as well as // failures, so only a report that can be read tells them apart. - $report = json_decode( $result->stdout, true ); + $report = json_decode( $result['stdout'], true ); if ( ! is_array( $report ) || ! array_key_exists( 'advisories', $report ) ) { WP_CLI::error( - "`composer audit` failed (exit code {$result->return_code}). Composer 2.7 or later is needed.\n" - . trim( $result->stderr . "\n" . $result->stdout ) + "`composer audit` failed (exit code {$result['return_code']}). Composer 2.7 or later is needed.\n" + . trim( $result['stderr'] . "\n" . $result['stdout'] ) ); } @@ -335,25 +334,4 @@ private static function compare( $a, $b ) { return strcmp( $a['advisory'], $b['advisory'] ); } - - private static function remove_directory( $dir ) { - if ( ! is_dir( $dir ) ) { - return; - } - - $entries = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator( $dir, \FilesystemIterator::SKIP_DOTS ), - \RecursiveIteratorIterator::CHILD_FIRST - ); - - foreach ( $entries as $entry ) { - if ( $entry->isDir() && ! $entry->isLink() ) { - rmdir( $entry->getPathname() ); - } else { - unlink( $entry->getPathname() ); - } - } - - rmdir( $dir ); - } } diff --git a/.maintenance/src/Dependency_Update.php b/.maintenance/src/Dependency_Update.php new file mode 100644 index 0000000..63e8af7 --- /dev/null +++ b/.maintenance/src/Dependency_Update.php @@ -0,0 +1,501 @@ + $repo, + 'branch' => $files['ref'], + 'head' => self::get_branch_name( $packages ), + 'sha' => $files['sha'], + 'requested' => (bool) $packages, + 'packages' => [], + 'unmatched' => [], + 'changes' => [], + 'fixed' => [], + 'remaining' => $before['advisories'], + 'composer_lock' => null, + ]; + + if ( $packages ) { + list( $plan['packages'], $plan['unmatched'] ) = self::expand( $packages, array_keys( self::index( $files['lock'] ) ) ); + } else { + $plan['packages'] = array_values( array_unique( array_column( $before['advisories'], 'package' ) ) ); + } + + if ( ! $plan['packages'] ) { + return $plan; + } + + $packages = $plan['packages']; + + $runner = self::$runner ? self::$runner : [ __CLASS__, 'run_composer_update' ]; + $new_lock = (string) call_user_func( $runner, $files['composer_json'], $files['composer_lock'], $packages, $files['ref'] ); + $decoded = json_decode( $new_lock, true ); + + if ( ! is_array( $decoded ) ) { + WP_CLI::error( "`composer update` wrote a composer.lock for {$repo} that is not valid JSON." ); + } + + if ( isset( $files['lock']['plugin-api-version'], $decoded['plugin-api-version'] ) + && version_compare( $decoded['plugin-api-version'], $files['lock']['plugin-api-version'], '<' ) ) { + WP_CLI::error( + sprintf( + 'The composer.lock of %s was written by a newer Composer (plugin API %s) than the one installed here (plugin API %s), which would downgrade it. Run `composer self-update` first.', + $repo, + $files['lock']['plugin-api-version'], + $decoded['plugin-api-version'] + ) + ); + } + + $plan['changes'] = self::diff( $files['lock'], $decoded ); + + if ( ! $plan['changes'] ) { + return $plan; + } + + $after = self::audit( $repo, $files, $new_lock, $decoded ); + + $plan['fixed'] = self::subtract( $before['advisories'], $after['advisories'] ); + $plan['remaining'] = $after['advisories']; + $plan['composer_lock'] = $new_lock; + + return $plan; + } + + /** + * Resolves the packages to update against the locked ones. Composer + * itself would update the packages a wildcard matches only as far as + * needed, so the matches are named one by one. + * + * @param string[] $patterns Package names, `*` matching any characters. + * @param string[] $locked Names of the locked packages. + * + * @return array{0: string[], 1: string[]} Matched locked packages, and the + * patterns that match none. + */ + public static function expand( array $patterns, array $locked ) { + $matched = []; + $unmatched = []; + + foreach ( $patterns as $pattern ) { + $regex = '#^' . str_replace( '\\*', '.*', preg_quote( $pattern, '#' ) ) . '$#i'; + $found = preg_grep( $regex, $locked ); + + if ( $found ) { + $matched = array_merge( $matched, $found ); + } else { + $unmatched[] = $pattern; + } + } + + $matched = array_values( array_unique( $matched ) ); + sort( $matched ); + + return [ $matched, $unmatched ]; + } + + /** + * Names the branch for an update, after the packages that were asked for. + * + * @param string[] $packages Packages as given, or none for the packages + * with advisories. + * + * @return string + */ + public static function get_branch_name( array $packages ) { + if ( ! $packages ) { + return self::BRANCH_PREFIX . 'security'; + } + + $names = $packages; + sort( $names ); + $slug = trim( (string) preg_replace( '/[^a-z0-9]+/', '-', str_replace( '*', 'all', strtolower( implode( '-', $names ) ) ) ), '-' ); + + if ( strlen( $slug ) > 60 ) { + $slug = rtrim( substr( $slug, 0, 51 ), '-' ) . '-' . substr( md5( $slug ), 0, 8 ); + } + + return self::BRANCH_PREFIX . $slug; + } + + /** + * Lists how the locked packages differ between two lockfiles. + * + * @param array $before Decoded composer.lock before the update. + * @param array $after Decoded composer.lock after the update. + * + * @return array + * Changed packages sorted by name; `from` is null for an added + * package and `to` for a removed one. + */ + public static function diff( array $before, array $after ) { + $before = self::index( $before ); + $after = self::index( $after ); + $names = array_unique( array_merge( array_keys( $before ), array_keys( $after ) ) ); + sort( $names ); + + $changes = []; + foreach ( $names as $name ) { + $old = isset( $before[ $name ] ) ? $before[ $name ] : null; + $new = isset( $after[ $name ] ) ? $after[ $name ] : null; + + if ( $old && $new && $old['version'] === $new['version'] && $old['reference'] === $new['reference'] ) { + continue; + } + + // The same branch version moved to another commit, as for dev-main. + $show_reference = $old && $new && $old['version'] === $new['version']; + + $changes[] = [ + 'package' => $name, + 'from' => $old ? self::describe_version( $old, $show_reference ) : null, + 'to' => $new ? self::describe_version( $new, $show_reference ) : null, + 'dev' => $new ? $new['dev'] : $old['dev'], + ]; + } + + return $changes; + } + + /** + * Keeps the advisories of the first list that the second one no longer has. + * + * @param array $before Advisories before the update, see Dependency_Audit. + * @param array $after Advisories after the update. + * + * @return array + */ + public static function subtract( array $before, array $after ) { + $left = []; + foreach ( $after as $advisory ) { + $left[ "{$advisory['package']} {$advisory['advisory']}" ] = true; + } + + return array_values( + array_filter( + $before, + static function ( $advisory ) use ( $left ) { + return ! isset( $left[ "{$advisory['package']} {$advisory['advisory']}" ] ); + } + ) + ); + } + + /** + * Writes the title and description of the pull request for a plan. + * + * @param array $plan Plan, see prepare(). + * + * @return array{0: string, 1: string} Title and description. + */ + public static function describe_pull_request( array $plan ) { + $names = array_column( $plan['changes'], 'package' ); + + if ( count( $names ) > 3 ) { + $title = sprintf( 'Update %d locked dependencies', count( $names ) ); + } elseif ( count( $names ) > 1 ) { + $title = 'Update ' . implode( ', ', array_slice( $names, 0, -1 ) ) . ' and ' . end( $names ); + } else { + $title = 'Update ' . reset( $names ); + } + + $lines = [ + '**This is an automated pull request**, opened by `wp maintenance dependencies update`.', + '', + ( $plan['requested'] ? '' : 'Fixes security advisories. ' ) + . 'Updates `' . implode( '`, `', $plan['packages'] ) . '` in composer.lock, with the dependencies that had to move along. composer.json is unchanged.', + '', + '| Package | From | To | Dev |', + '| --- | --- | --- | --- |', + ]; + + foreach ( $plan['changes'] as $change ) { + $lines[] = sprintf( + '| %s | %s | %s | %s |', + $change['package'], + null === $change['from'] ? '(new)' : $change['from'], + null === $change['to'] ? '(removed)' : $change['to'], + $change['dev'] ? 'yes' : 'no' + ); + } + + if ( $plan['fixed'] ) { + $lines[] = ''; + $lines[] = '### Fixed security advisories'; + $lines[] = ''; + foreach ( $plan['fixed'] as $advisory ) { + $lines[] = self::describe_advisory( $advisory ); + } + } + + if ( $plan['remaining'] ) { + $lines[] = ''; + $lines[] = '### Still affected'; + $lines[] = ''; + $lines[] = $plan['requested'] + ? 'This update leaves these advisories as they are:' + : 'Fixing these needs a change to the constraints in composer.json:'; + $lines[] = ''; + foreach ( $plan['remaining'] as $advisory ) { + $lines[] = self::describe_advisory( $advisory ); + } + } + + return [ $title, implode( "\n", $lines ) . "\n" ]; + } + + /** + * Commits the new composer.lock of a plan to its branch, on top of the + * commit the plan was made from, and opens or refreshes the pull request + * for it. A branch that already holds the same lock on the same commit is + * left as it is, so that a rerun does not restart its checks. + * + * @param array $plan Plan with a new composer.lock, see prepare(). + * + * @return array{0: object, 1: bool} Pull request, and whether a new commit + * was pushed. + */ + public static function publish( array $plan ) { + $repo = $plan['repo']; + $head = $plan['head']; + $existing = GitHub::get_branch_sha( $repo, $head ); + $push = true; + + if ( $existing ) { + $comparison = GitHub::compare( $repo, $plan['branch'], $head ); + + if ( ! self::is_own_branch( $comparison ) ) { + WP_CLI::error( "The {$head} branch of {$repo} has commits that `wp maintenance dependencies update` did not make, so it is not overwritten. Merge or delete it first." ); + } + + $push = 1 !== (int) $comparison->ahead_by + || $comparison->merge_base_commit->sha !== $plan['sha'] + || GitHub::get_raw_file( $repo, $existing, 'composer.lock' ) !== $plan['composer_lock']; + } + + if ( $push ) { + $message = self::COMMIT_TITLE . "\n\n" . implode( + "\n", + array_map( + static function ( $change ) { + return sprintf( '- %s: %s => %s', $change['package'], $change['from'] ?: '(new)', $change['to'] ?: '(removed)' ); + }, + $plan['changes'] + ) + ); + + $sha = GitHub::commit_file( $repo, $plan['sha'], 'composer.lock', (string) $plan['composer_lock'], $message ); + GitHub::set_branch( $repo, $head, $sha, (bool) $existing ); + } + + list( $title, $body ) = self::describe_pull_request( $plan ); + + $pull_request = GitHub::get_open_pull_request( $repo, $head ); + + $pull_request = $pull_request + ? GitHub::update_pull_request( $repo, $pull_request->number, $title, $body ) + : GitHub::create_pull_request( $repo, $head, $plan['branch'], $title, $body ); + + // Also on a refresh, in case labelling failed when it was opened. + GitHub::add_label( $repo, $pull_request->number, self::LABEL ); + + return [ $pull_request, $push ]; + } + + /** + * Checks whether a branch holds nothing but a change of composer.lock by + * this command on top of the default branch. + * + * @param object $comparison Comparison of the default branch with the + * branch. + * + * @return bool + */ + public static function is_own_branch( $comparison ) { + if ( 0 === (int) $comparison->ahead_by ) { + return true; + } + + if ( 1 !== (int) $comparison->ahead_by ) { + return false; + } + + $commit = end( $comparison->commits ); + $title = strtok( $commit->commit->message, "\n" ); + $files = isset( $comparison->files ) ? array_column( (array) $comparison->files, 'filename' ) : []; + + return self::COMMIT_TITLE === $title && [ 'composer.lock' ] === $files; + } + + private static function audit( $repo, array $files, $composer_lock, array $lock ) { + return Dependency_Audit::assess( + $repo, + $files['ref'], + $files['sha'], + true, + Dependency_Audit::audit_files( $files['composer_json'], $composer_lock, true ), + $lock + ); + } + + /** + * Runs `composer update` for some packages and returns the new lock. + * + * Packages that depend on each other move together, but only as far as + * needed, and root requirements only when given. + */ + private static function run_composer_update( $composer_json, $composer_lock, array $packages, $branch ) { + $result = Composer::run( + array_merge( + [ 'update' ], + $packages, + [ '--with-dependencies', '--minimal-changes', '--no-install', '--no-audit' ] + ), + [ + 'composer.json' => $composer_json, + 'composer.lock' => $composer_lock, + ], + [ 'composer.lock' ], + [ + 'COMPOSER_ROOT_VERSION' => "dev-{$branch}", + // Composer 2.9+ would otherwise refuse to resolve at all when a + // package keeps an advisory that composer.json does not let it + // fix, and hold back the fixes for all others with it. What is + // left gets reported by the audit afterwards. + 'COMPOSER_NO_SECURITY_BLOCKING' => '1', + ] + ); + + if ( 0 !== $result['return_code'] || null === $result['files']['composer.lock'] ) { + WP_CLI::error( + "`composer update` failed (exit code {$result['return_code']}).\n" + . trim( $result['stderr'] . "\n" . $result['stdout'] ) + ); + } + + return $result['files']['composer.lock']; + } + + private static function index( array $lock ) { + $packages = []; + $sections = [ + 'packages' => false, + 'packages-dev' => true, + ]; + + foreach ( $sections as $section => $dev ) { + if ( empty( $lock[ $section ] ) ) { + continue; + } + + foreach ( $lock[ $section ] as $package ) { + $packages[ $package['name'] ] = [ + 'version' => $package['version'], + 'reference' => self::get_reference( $package ), + 'dev' => $dev, + ]; + } + } + + return $packages; + } + + private static function get_reference( array $package ) { + foreach ( [ 'source', 'dist' ] as $type ) { + if ( isset( $package[ $type ]['reference'] ) ) { + return $package[ $type ]['reference']; + } + } + + return null; + } + + private static function describe_version( array $package, $show_reference ) { + if ( ! $show_reference || null === $package['reference'] ) { + return $package['version']; + } + + return sprintf( '%s (%s)', $package['version'], substr( $package['reference'], 0, 7 ) ); + } + + private static function describe_advisory( array $advisory ) { + $id = $advisory['link'] ? "[{$advisory['advisory']}]({$advisory['link']})" : $advisory['advisory']; + + return sprintf( + '- %s %s: %s%s', + $advisory['package'], + $id, + $advisory['title'], + $advisory['cve'] ? " ({$advisory['cve']})" : '' + ); + } +} diff --git a/.maintenance/src/GitHub.php b/.maintenance/src/GitHub.php index 3813104..f823326 100644 --- a/.maintenance/src/GitHub.php +++ b/.maintenance/src/GitHub.php @@ -464,6 +464,183 @@ public static function get_commit_sha( $project, $ref ) { return $result[0][0]->sha; } + /** + * Compares two refs of a repository. + * + * @param string $project Repository name, including the org. + * @param string $base Base ref. + * @param string $head Head ref. + * + * @return object Comparison, with `ahead_by`, `behind_by` and `commits`. + */ + public static function compare( $project, $base, $head ) { + list( $comparison ) = self::request( + self::API_ROOT . "repos/{$project}/compare/" . rawurlencode( $base ) . '...' . rawurlencode( $head ) + ); + + return $comparison; + } + + /** + * Gets the commit a branch points at. + * + * @param string $project Repository name, including the org. + * @param string $branch Branch name. + * + * @return string|null Commit SHA, or null when there is no such branch. + */ + public static function get_branch_sha( $project, $branch ) { + $result = self::request( self::API_ROOT . "repos/{$project}/git/ref/heads/{$branch}", [], 'GET', [ 404 ] ); + + return $result ? $result[0]->object->sha : null; + } + + /** + * Commits a change to one file on top of a commit, without moving any + * branch. + * + * @param string $project Repository name, including the org. + * @param string $parent_sha SHA of the parent commit. + * @param string $path Path of the file within the repository. + * @param string $contents New contents of the file. + * @param string $message Commit message. + * + * @return string SHA of the new commit. + */ + public static function commit_file( $project, $parent_sha, $path, $contents, $message ) { + list( $parent_commit ) = self::request( self::API_ROOT . "repos/{$project}/git/commits/{$parent_sha}" ); + + list( $tree ) = self::request( + self::API_ROOT . "repos/{$project}/git/trees", + [ + 'base_tree' => $parent_commit->tree->sha, + 'tree' => [ + [ + 'path' => $path, + 'mode' => '100644', + 'type' => 'blob', + 'content' => $contents, + ], + ], + ], + 'POST' + ); + + list( $commit ) = self::request( + self::API_ROOT . "repos/{$project}/git/commits", + [ + 'message' => $message, + 'tree' => $tree->sha, + 'parents' => [ $parent_sha ], + ], + 'POST' + ); + + return $commit->sha; + } + + /** + * Points a branch at a commit, creating the branch if needed. An existing + * branch is moved even when the commit does not descend from it. + * + * @param string $project Repository name, including the org. + * @param string $branch Branch name. + * @param string $sha Commit SHA. + * @param bool $exists Whether the branch exists already. + */ + public static function set_branch( $project, $branch, $sha, $exists ) { + if ( $exists ) { + self::request( + self::API_ROOT . "repos/{$project}/git/refs/heads/{$branch}", + [ + 'sha' => $sha, + 'force' => true, + ], + 'PATCH' + ); + return; + } + + self::request( + self::API_ROOT . "repos/{$project}/git/refs", + [ + 'ref' => "refs/heads/{$branch}", + 'sha' => $sha, + ], + 'POST' + ); + } + + /** + * Gets the open pull request from a branch of the repository itself. + * + * @param string $project Repository name, including the org. + * @param string $branch Head branch. + * + * @return object|null + */ + public static function get_open_pull_request( $project, $branch ) { + list( $owner ) = explode( '/', $project ); + list( $pull_requests ) = self::request( + self::API_ROOT . "repos/{$project}/pulls", + [ + 'head' => "{$owner}:{$branch}", + 'state' => 'open', + ] + ); + + return $pull_requests ? $pull_requests[0] : null; + } + + /** + * Opens a pull request. + * + * @param string $project Repository name, including the org. + * @param string $head Branch with the changes. + * @param string $base Branch to merge them into. + * @param string $title Title. + * @param string $body Description. + * + * @return object Pull request. + */ + public static function create_pull_request( $project, $head, $base, $title, $body ) { + list( $pull_request ) = self::request( + self::API_ROOT . "repos/{$project}/pulls", + [ + 'head' => $head, + 'base' => $base, + 'title' => $title, + 'body' => $body, + ], + 'POST' + ); + + return $pull_request; + } + + /** + * Changes the title and description of a pull request. + * + * @param string $project Repository name, including the org. + * @param int $number Pull request number. + * @param string $title Title. + * @param string $body Description. + * + * @return object Pull request. + */ + public static function update_pull_request( $project, $number, $title, $body ) { + list( $pull_request ) = self::request( + self::API_ROOT . "repos/{$project}/pulls/{$number}", + [ + 'title' => $title, + 'body' => $body, + ], + 'PATCH' + ); + + return $pull_request; + } + /** * Gets the contents of a file of a repository at a ref. * diff --git a/.maintenance/src/Prompt.php b/.maintenance/src/Prompt.php new file mode 100644 index 0000000..3bed1c1 --- /dev/null +++ b/.maintenance/src/Prompt.php @@ -0,0 +1,30 @@ + '/usr/bin', + 'COMPOSER' => '/elsewhere/composer.json', + 'COMPOSER_NO_DEV' => '1', + 'COMPOSER_NO_AUDIT' => '1', + 'COMPOSER_AUDIT_ABANDONED' => 'ignore', + 'COMPOSER_PREFER_LOWEST' => '1', + 'COMPOSER_WITH_DEPENDENCIES' => '1', + 'COMPOSER_AUTH' => '{"http-basic":{}}', + 'COMPOSER_HOME' => '/home/me/.composer', + 'COMPOSER_CACHE_DIR' => '/home/me/.cache/composer', + ], + '/tmp/composer/home' + ); + + $this->assertSame( + [ + 'PATH' => '/usr/bin', + 'COMPOSER_HOME' => '/tmp/composer/home', + 'COMPOSER_CACHE_DIR' => '/home/me/.cache/composer', + ], + $env + ); + } + + public function test_the_github_token_is_handed_to_composer(): void { + $env = Composer::get_env( [], '/tmp/composer/home', 'test-token' ); + + $this->assertSame( + [ 'github-oauth' => [ 'github.com' => 'test-token' ] ], + json_decode( $env['COMPOSER_AUTH'], true ) + ); + } +} diff --git a/tests/DependenciesUpdateCommandTest.php b/tests/DependenciesUpdateCommandTest.php new file mode 100644 index 0000000..fcef55c --- /dev/null +++ b/tests/DependenciesUpdateCommandTest.php @@ -0,0 +1,490 @@ + + */ + private $updates = []; + + /** + * Whether the fake `composer update` fails to fix the advisory, as when + * composer.json does not allow a fixed version. + * + * @var bool + */ + private $unfixable = false; + + /** + * Plugin API version of the Composer the fake `composer update` stands + * for. + * + * @var string + */ + private $plugin_api = '2.9.0'; + + /** + * What the command printed to STDOUT. + * + * @var string + */ + private $stdout = ''; + + protected function set_up(): void { + parent::set_up(); + + // symfony/yaml v5.4.54 has an advisory; the update moves it to v5.4.56. + Dependency_Audit::set_runner( + static function ( $composer_json, $composer_lock ) { + $affected = false !== strpos( $composer_lock, '"v5.4.54"' ); + + return [ + 'advisories' => $affected ? [ + 'symfony/yaml' => [ + [ + 'advisoryId' => 'PKSA-nktx-f1n1-b4yd', + 'cve' => 'CVE-2026-123792', + 'title' => 'Exponential and Quadratic Parse Times in the YAML Parser', + 'link' => 'https://github.com/symfony/symfony/security/advisories/GHSA-hwfg-qwvh-2wfx', + ], + ], + ] : [], + 'abandoned' => [], + ]; + } + ); + + Dependency_Update::set_runner( + function ( $composer_json, $composer_lock, array $packages, $branch ) { + $this->updates[] = [ $composer_json, $composer_lock, $packages, $branch ]; + + return $this->unfixable ? $composer_lock : $this->lock_file( 'v5.4.56', $this->plugin_api ); + } + ); + } + + protected function tear_down(): void { + Dependency_Audit::set_runner( null ); + Dependency_Update::set_runner( null ); + + parent::tear_down(); + } + + private function lock_file( $yaml_version, $plugin_api = '2.9.0' ) { + return (string) json_encode( $this->lock( $yaml_version, $plugin_api ), JSON_PRETTY_PRINT ); + } + + private function lock( $yaml_version, $plugin_api = '2.9.0' ) { + return [ + 'plugin-api-version' => $plugin_api, + 'packages' => [ + [ + 'name' => 'wp-cli/wp-cli', + 'version' => 'dev-main', + 'source' => [ 'reference' => '5ec3c0545d30f0f93c49a40a06ba6c1fd0e9fd6c' ], + ], + ], + 'packages-dev' => [ + [ + 'name' => 'symfony/yaml', + 'version' => $yaml_version, + ], + ], + ]; + } + + /** + * Sets up the bundle with symfony/yaml locked at the given version. + */ + private function bundle( $yaml_version = 'v5.4.54' ) { + $this->respond( 'GET', self::REPO, $this->fixture_object( 'repository', null, [ 'default_branch' => 'main' ] ) ); + $this->respond( 'GET', self::REPO . '/commits?sha=main', [ [ 'sha' => self::SHA ] ] ); + $this->respond( 'GET', 'wp-cli/wp-cli-bundle/' . self::SHA . '/composer.lock', $this->lock( $yaml_version ) ); + $this->respond( 'GET', 'wp-cli/wp-cli-bundle/' . self::SHA . '/composer.json', [ 'name' => 'wp-cli/wp-cli-bundle' ] ); + } + + /** + * Sets up the requests that commit a file on top of SHA. + */ + private function commit() { + $this->respond( 'GET', self::REPO . '/git/commits/' . self::SHA, [ 'tree' => [ 'sha' => 'base-tree' ] ] ); + $this->respond( 'POST', self::REPO . '/git/trees', [ 'sha' => 'new-tree' ] ); + $this->respond( 'POST', self::REPO . '/git/commits', [ 'sha' => 'new-commit' ] ); + } + + /** + * Runs the command and returns its exit code, 0 when it did not halt. + */ + private function update( array $args, array $assoc_args ) { + $exit_code = 0; + + // Tables go to the output stream of php-cli-tools, which tests swap + // for one in memory. + $out = new ReflectionProperty( \cli\Streams::class, 'out' ); + if ( PHP_VERSION_ID < 80100 ) { + $out->setAccessible( true ); + } + $original = $out->getValue(); + $stream = fopen( 'php://memory', 'w+' ); + $out->setValue( null, $stream ); + + ob_start(); + try { + ( new Dependencies_Command() )->update( $args, $assoc_args ); + } catch ( ExitException $exception ) { + $exit_code = $exception->getCode(); + } finally { + $output = (string) ob_get_clean(); + $out->setValue( null, $original ); + rewind( $stream ); + $this->stdout = (string) stream_get_contents( $stream ) . $output; + fclose( $stream ); + } + + return $exit_code; + } + + /** + * Builds the comparison of the default branch with a branch holding one + * commit of this command. + * + * @param string $merge_base Commit the branch was made from. + * + * @return array + */ + private function comparison( $merge_base ) { + return [ + 'ahead_by' => 1, + 'merge_base_commit' => [ 'sha' => $merge_base ], + 'commits' => [ [ 'commit' => [ 'message' => "Update locked dependencies\n\n- symfony/yaml: v5.4.53 => v5.4.54" ] ] ], + 'files' => [ [ 'filename' => 'composer.lock' ] ], + ]; + } + + /** + * @return string[] Write requests made, as "METHOD URL". + */ + private function writes() { + return array_values( + array_filter( + $this->requests, + static function ( $request ) { + return 0 !== strpos( $request, 'GET ' ); + } + ) + ); + } + + /** + * @return mixed Decoded body of the write request to a URL. + */ + private function sent( $request ) { + $index = array_search( $request, $this->requests, true ); + $this->assertNotFalse( $index, "No request {$request}." ); + + return $this->sent_data[ $index ]; + } + + public function test_a_dry_run_shows_the_changes_without_writing(): void { + $this->bundle(); + + $this->assertSame( 0, $this->update( [], [ 'dry-run' => true ] ) ); + + $this->assertStringContainsString( "symfony/yaml\tv5.4.54\tv5.4.56\tyes", $this->stdout ); + $this->assertStringContainsString( 'Fixes 1 security advisory.', $this->logger->stdout ); + $this->assertStringContainsString( 'Dry run: not pushing the new composer.lock of wp-cli/wp-cli-bundle.', $this->logger->stdout ); + $this->assertSame( [ 'symfony/yaml' ], $this->updates[0][2] ); + $this->assertSame( 'main', $this->updates[0][3] ); + $this->assertSame( [], $this->writes() ); + } + + public function test_commits_the_new_lock_and_opens_a_pull_request(): void { + $this->bundle(); + $this->respond( 'GET', self::REPO . '/git/ref/heads/update-dependencies-security', [ 'message' => 'Not Found' ], 404 ); + $this->commit(); + $this->respond( 'POST', self::REPO . '/git/refs', [ 'ref' => 'refs/heads/update-dependencies-security' ] ); + $this->respond( 'GET', self::REPO . '/pulls?head=wp-cli:update-dependencies-security&state=open', [] ); + $this->respond( + 'POST', + self::REPO . '/pulls', + [ + 'number' => 7, + 'html_url' => 'https://github.com/wp-cli/wp-cli-bundle/pull/7', + ] + ); + $this->respond( 'POST', self::REPO . '/issues/7/labels', [] ); + + $this->assertSame( 0, $this->update( [], [ 'yes' => true ] ) ); + + $api = 'https://api.github.com/' . self::REPO; + $this->assertSame( + [ + "POST {$api}/git/trees", + "POST {$api}/git/commits", + "POST {$api}/git/refs", + "POST {$api}/pulls", + "POST {$api}/issues/7/labels", + ], + $this->writes() + ); + + $tree = $this->sent( "POST {$api}/git/trees" ); + $this->assertSame( 'base-tree', $tree->base_tree ); + $this->assertSame( 'composer.lock', $tree->tree[0]->path ); + $this->assertSame( $this->lock( 'v5.4.56' ), json_decode( $tree->tree[0]->content, true ) ); + + $commit = $this->sent( "POST {$api}/git/commits" ); + $this->assertSame( [ self::SHA ], $commit->parents ); + $this->assertSame( "Update locked dependencies\n\n- symfony/yaml: v5.4.54 => v5.4.56", $commit->message ); + + $this->assertEquals( + (object) [ + 'ref' => 'refs/heads/update-dependencies-security', + 'sha' => 'new-commit', + ], + $this->sent( "POST {$api}/git/refs" ) + ); + + $pull_request = $this->sent( "POST {$api}/pulls" ); + $this->assertSame( 'update-dependencies-security', $pull_request->head ); + $this->assertSame( 'main', $pull_request->base ); + $this->assertSame( 'Update symfony/yaml', $pull_request->title ); + $this->assertStringContainsString( 'PKSA-nktx-f1n1-b4yd', $pull_request->body ); + + $this->assertEquals( (object) [ 'labels' => [ 'scope:distribution' ] ], $this->sent( "POST {$api}/issues/7/labels" ) ); + $this->assertStringContainsString( 'Pull request for wp-cli/wp-cli-bundle: https://github.com/wp-cli/wp-cli-bundle/pull/7', $this->logger->stdout ); + } + + public function test_a_later_run_moves_its_own_branch_and_refreshes_the_pull_request(): void { + $this->bundle(); + $this->respond( 'GET', self::REPO . '/git/ref/heads/update-dependencies-security', [ 'object' => [ 'sha' => 'old-commit' ] ] ); + $this->respond( + 'GET', + self::REPO . '/compare/main...update-dependencies-security', + $this->comparison( 'older-default-branch-commit' ) + ); + $this->commit(); + $this->respond( 'PATCH', self::REPO . '/git/refs/heads/update-dependencies-security', [ 'ref' => 'refs/heads/update-dependencies-security' ] ); + $this->respond( 'GET', self::REPO . '/pulls?head=wp-cli:update-dependencies-security&state=open', [ [ 'number' => 7 ] ] ); + $this->respond( + 'PATCH', + self::REPO . '/pulls/7', + [ + 'number' => 7, + 'html_url' => 'https://github.com/wp-cli/wp-cli-bundle/pull/7', + ] + ); + + $this->respond( 'POST', self::REPO . '/issues/7/labels', [] ); + + $this->assertSame( 0, $this->update( [], [ 'yes' => true ] ) ); + + $api = 'https://api.github.com/' . self::REPO; + $this->assertSame( + [ + "POST {$api}/git/trees", + "POST {$api}/git/commits", + "PATCH {$api}/git/refs/heads/update-dependencies-security", + "PATCH {$api}/pulls/7", + "POST {$api}/issues/7/labels", + ], + $this->writes() + ); + $this->assertEquals( + (object) [ + 'sha' => 'new-commit', + 'force' => true, + ], + $this->sent( "PATCH {$api}/git/refs/heads/update-dependencies-security" ) + ); + } + + public function test_a_branch_someone_else_pushed_to_is_left_alone(): void { + $this->bundle(); + $this->respond( 'GET', self::REPO . '/git/ref/heads/update-dependencies-security', [ 'object' => [ 'sha' => 'old-commit' ] ] ); + $this->respond( + 'GET', + self::REPO . '/compare/main...update-dependencies-security', + [ + 'ahead_by' => 2, + 'merge_base_commit' => [ 'sha' => self::SHA ], + 'commits' => [ + [ 'commit' => [ 'message' => 'Update locked dependencies' ] ], + [ 'commit' => [ 'message' => 'Pin symfony/yaml for PHP 7.2' ] ], + ], + 'files' => [ [ 'filename' => 'composer.lock' ] ], + ] + ); + + $this->assertSame( 1, $this->update( [], [ 'yes' => true ] ) ); + + $this->assertStringContainsString( 'The update-dependencies-security branch of wp-cli/wp-cli-bundle has commits that `wp maintenance dependencies update` did not make', $this->logger->stderr ); + $this->assertSame( [], $this->writes() ); + } + + public function test_nothing_is_updated_without_advisories(): void { + $this->bundle( 'v5.4.56' ); + + $this->assertSame( 0, $this->update( [], [ 'yes' => true ] ) ); + + $this->assertStringContainsString( 'No locked package of wp-cli/wp-cli-bundle has a security advisory, so there is nothing to update.', $this->logger->stdout ); + $this->assertSame( [], $this->updates ); + $this->assertSame( [], $this->writes() ); + } + + public function test_advisories_the_update_cannot_fix_fail_the_run(): void { + $this->bundle(); + $this->unfixable = true; + + $this->assertSame( 1, $this->update( [], [ 'dry-run' => true ] ) ); + + $this->assertStringContainsString( 'Updating symfony/yaml changes no locked version of wp-cli/wp-cli-bundle.', $this->logger->stderr ); + $this->assertStringContainsString( 'symfony/yaml v5.4.54 is still affected by PKSA-nktx-f1n1-b4yd', $this->logger->stderr ); + $this->assertStringContainsString( 'Fixing them needs a change to composer.json.', $this->logger->stderr ); + } + + public function test_given_packages_are_updated_instead_and_may_leave_advisories(): void { + $this->bundle(); + $this->unfixable = true; + + $this->assertSame( + 0, + $this->update( + [], + [ + 'packages' => 'symfony/*, wp-cli/wp-cli', + 'dry-run' => true, + ] + ) + ); + + // Named one by one, so that Composer updates each of them fully. + $this->assertSame( [ 'symfony/yaml', 'wp-cli/wp-cli' ], $this->updates[0][2] ); + $this->assertStringContainsString( 'is still affected by PKSA-nktx-f1n1-b4yd', $this->logger->stderr ); + } + + public function test_an_option_is_not_taken_for_a_package(): void { + $this->assertSame( 1, $this->update( [], [ 'packages' => '--with-all-dependencies' ] ) ); + + $this->assertStringContainsString( "'--with-all-dependencies' is not a package name.", $this->logger->stderr ); + $this->assertSame( [], $this->requests ); + } + + public function test_a_rerun_with_the_same_result_only_refreshes_the_pull_request(): void { + $this->bundle(); + $this->respond( 'GET', self::REPO . '/git/ref/heads/update-dependencies-security', [ 'object' => [ 'sha' => 'old-commit' ] ] ); + $this->respond( 'GET', self::REPO . '/compare/main...update-dependencies-security', $this->comparison( self::SHA ) ); + $this->respond( 'GET', 'wp-cli/wp-cli-bundle/old-commit/composer.lock', $this->lock( 'v5.4.56' ) ); + $this->respond( 'GET', self::REPO . '/pulls?head=wp-cli:update-dependencies-security&state=open', [ [ 'number' => 7 ] ] ); + $this->respond( + 'PATCH', + self::REPO . '/pulls/7', + [ + 'number' => 7, + 'html_url' => 'https://github.com/wp-cli/wp-cli-bundle/pull/7', + ] + ); + $this->respond( 'POST', self::REPO . '/issues/7/labels', [] ); + + // The update writes exactly the lock that the branch holds already, as + // the fake GitHub API encodes it. + Dependency_Update::set_runner( + function () { + return (string) json_encode( $this->lock( 'v5.4.56' ) ); + } + ); + + $this->assertSame( 0, $this->update( [], [ 'yes' => true ] ) ); + + $api = 'https://api.github.com/' . self::REPO; + $this->assertSame( [ "PATCH {$api}/pulls/7", "POST {$api}/issues/7/labels" ], $this->writes() ); + $this->assertStringContainsString( 'The update-dependencies-security branch already holds this composer.lock, so only the pull request was refreshed.', $this->logger->stdout ); + } + + public function test_a_branch_with_changes_beyond_the_lock_is_left_alone(): void { + $this->bundle(); + $comparison = $this->comparison( self::SHA ); + $comparison['files'] = [ [ 'filename' => 'composer.json' ], [ 'filename' => 'composer.lock' ] ]; + $this->respond( 'GET', self::REPO . '/git/ref/heads/update-dependencies-security', [ 'object' => [ 'sha' => 'old-commit' ] ] ); + $this->respond( 'GET', self::REPO . '/compare/main...update-dependencies-security', $comparison ); + + $this->assertSame( 1, $this->update( [], [ 'yes' => true ] ) ); + + $this->assertSame( [], $this->writes() ); + } + + public function test_packages_get_a_branch_of_their_own(): void { + $this->bundle(); + $this->respond( 'GET', self::REPO . '/git/ref/heads/update-dependencies-wp-cli-wp-cli', [ 'message' => 'Not Found' ], 404 ); + $this->commit(); + $this->respond( 'POST', self::REPO . '/git/refs', [ 'ref' => 'refs/heads/update-dependencies-wp-cli-wp-cli' ] ); + $this->respond( 'GET', self::REPO . '/pulls?head=wp-cli:update-dependencies-wp-cli-wp-cli&state=open', [] ); + $this->respond( + 'POST', + self::REPO . '/pulls', + [ + 'number' => 8, + 'html_url' => 'https://github.com/wp-cli/wp-cli-bundle/pull/8', + ] + ); + $this->respond( 'POST', self::REPO . '/issues/8/labels', [] ); + + $this->assertSame( + 0, + $this->update( + [], + [ + 'packages' => 'wp-cli/wp-cli', + 'yes' => true, + ] + ) + ); + + $api = 'https://api.github.com/' . self::REPO; + $this->assertSame( 'refs/heads/update-dependencies-wp-cli-wp-cli', $this->sent( "POST {$api}/git/refs" )->ref ); + $body = $this->sent( "POST {$api}/pulls" )->body; + $this->assertStringContainsString( 'Updates `wp-cli/wp-cli` in composer.lock', $body ); + $this->assertStringNotContainsString( 'Fixes security advisories.', $body ); + } + + public function test_packages_that_match_nothing_locked_are_reported(): void { + $this->bundle(); + + $this->assertSame( + 0, + $this->update( + [], + [ + 'packages' => 'acme/*', + 'dry-run' => true, + ] + ) + ); + + $this->assertStringContainsString( 'acme/* matches no package locked in wp-cli/wp-cli-bundle.', $this->logger->stderr ); + $this->assertSame( [], $this->updates ); + } + + public function test_an_older_composer_does_not_downgrade_the_lock(): void { + $this->respond( 'GET', self::REPO, $this->fixture_object( 'repository', null, [ 'default_branch' => 'main' ] ) ); + $this->respond( 'GET', self::REPO . '/commits?sha=main', [ [ 'sha' => self::SHA ] ] ); + $this->respond( 'GET', 'wp-cli/wp-cli-bundle/' . self::SHA . '/composer.lock', $this->lock( 'v5.4.54', '2.11.0' ) ); + $this->respond( 'GET', 'wp-cli/wp-cli-bundle/' . self::SHA . '/composer.json', [ 'name' => 'wp-cli/wp-cli-bundle' ] ); + + $this->assertSame( 1, $this->update( [], [ 'dry-run' => true ] ) ); + + $this->assertStringContainsString( 'was written by a newer Composer (plugin API 2.11.0) than the one installed here (plugin API 2.9.0)', $this->logger->stderr ); + } +} diff --git a/tests/DependencyAuditTest.php b/tests/DependencyAuditTest.php index aa73f75..9d6f52c 100644 --- a/tests/DependencyAuditTest.php +++ b/tests/DependencyAuditTest.php @@ -198,28 +198,4 @@ public function test_advisories_of_one_package_are_sorted_by_id(): void { $this->assertSame( [ 'PKSA-mmmm', 'PKSA-aaaa', 'PKSA-zzzz' ], array_column( $audit['advisories'], 'advisory' ) ); } - - public function test_the_composer_environment_cannot_change_what_is_audited(): void { - $env = Dependency_Audit::get_composer_env( - [ - 'PATH' => '/usr/bin', - 'COMPOSER' => '/elsewhere/composer.json', - 'COMPOSER_NO_DEV' => '1', - 'COMPOSER_NO_AUDIT' => '1', - 'COMPOSER_AUDIT_ABANDONED' => 'ignore', - 'COMPOSER_HOME' => '/home/me/.composer', - 'COMPOSER_CACHE_DIR' => '/home/me/.cache/composer', - ], - '/tmp/audit/home' - ); - - $this->assertSame( - [ - 'PATH' => '/usr/bin', - 'COMPOSER_HOME' => '/tmp/audit/home', - 'COMPOSER_CACHE_DIR' => '/home/me/.cache/composer', - ], - $env - ); - } } diff --git a/tests/DependencyUpdateTest.php b/tests/DependencyUpdateTest.php new file mode 100644 index 0000000..0c416da --- /dev/null +++ b/tests/DependencyUpdateTest.php @@ -0,0 +1,231 @@ + $packages Name => [ version, reference ]. + * @param array $dev_packages Name => [ version, reference ]. + * + * @return array + */ + private function lock( array $packages, array $dev_packages = [] ) { + $section = static function ( array $versions ) { + $entries = []; + foreach ( $versions as $name => $version ) { + $entries[] = [ + 'name' => $name, + 'version' => $version[0], + 'source' => [ 'reference' => isset( $version[1] ) ? $version[1] : sha1( $name . $version[0] ) ], + ]; + } + + return $entries; + }; + + return [ + 'packages' => $section( $packages ), + 'packages-dev' => $section( $dev_packages ), + ]; + } + + private function advisory( $package, $id, $cve = null ) { + return [ + 'package' => $package, + 'version' => 'v5.4.54', + 'dev' => true, + 'advisory' => $id, + 'cve' => $cve, + 'title' => "Problem in {$package}", + 'link' => "https://example.com/{$id}", + ]; + } + + private function plan( array $changes, array $fixed = [], array $remaining = [] ) { + return [ + 'repo' => 'wp-cli/wp-cli-bundle', + 'branch' => 'main', + 'head' => 'update-dependencies-security', + 'sha' => 'cee1f64b6ba7df6fefe41d4e96cb20f53acf2e10', + 'requested' => false, + 'packages' => [ 'symfony/yaml' ], + 'unmatched' => [], + 'changes' => $changes, + 'fixed' => $fixed, + 'remaining' => $remaining, + 'composer_lock' => '{}', + ]; + } + + private function change( $package, $from = 'v5.4.54', $to = 'v5.4.56', $dev = true ) { + return [ + 'package' => $package, + 'from' => $from, + 'to' => $to, + 'dev' => $dev, + ]; + } + + public function test_diff_lists_changed_added_and_removed_packages(): void { + $before = $this->lock( + [ 'mustache/mustache' => [ 'v3.0.0' ] ], + [ + 'symfony/yaml' => [ 'v5.4.54' ], + 'behat/transliterator' => [ 'v1.5.0' ], + ] + ); + $after = $this->lock( + [ 'mustache/mustache' => [ 'v3.0.0' ] ], + [ + 'symfony/yaml' => [ 'v5.4.56' ], + 'symfony/polyfill' => [ 'v1.33.0' ], + ] + ); + + $this->assertSame( + [ + $this->change( 'behat/transliterator', 'v1.5.0', null ), + $this->change( 'symfony/polyfill', null, 'v1.33.0' ), + $this->change( 'symfony/yaml' ), + ], + Dependency_Update::diff( $before, $after ) + ); + } + + public function test_diff_shows_the_commit_a_branch_version_moved_to(): void { + $before = $this->lock( [ 'wp-cli/wp-cli' => [ 'dev-main', '5ec3c0545d30f0f93c49a40a06ba6c1fd0e9fd6c' ] ] ); + $after = $this->lock( [ 'wp-cli/wp-cli' => [ 'dev-main', '0123456789abcdef0123456789abcdef01234567' ] ] ); + + $this->assertSame( + [ $this->change( 'wp-cli/wp-cli', 'dev-main (5ec3c05)', 'dev-main (0123456)', false ) ], + Dependency_Update::diff( $before, $after ) + ); + } + + public function test_diff_of_the_same_lock_is_empty(): void { + $lock = $this->lock( [ 'wp-cli/wp-cli' => [ 'dev-main', '5ec3c05' ] ], [ 'symfony/yaml' => [ 'v5.4.54' ] ] ); + + $this->assertSame( [], Dependency_Update::diff( $lock, $lock ) ); + } + + public function test_subtract_keeps_the_advisories_the_update_fixed(): void { + $yaml = $this->advisory( 'symfony/yaml', 'PKSA-1' ); + $config = $this->advisory( 'symfony/config', 'PKSA-2' ); + $filesystem = $this->advisory( 'symfony/filesystem', 'PKSA-3' ); + + $this->assertSame( [ $yaml, $filesystem ], Dependency_Update::subtract( [ $yaml, $config, $filesystem ], [ $config ] ) ); + $this->assertSame( [], Dependency_Update::subtract( [ $yaml ], [ $yaml ] ) ); + } + + public function test_the_title_names_up_to_three_packages(): void { + $titles = array_map( + function ( array $names ) { + list( $title ) = Dependency_Update::describe_pull_request( $this->plan( array_map( [ $this, 'change' ], $names ) ) ); + return $title; + }, + [ + [ 'symfony/yaml' ], + [ 'symfony/config', 'symfony/yaml' ], + [ 'symfony/config', 'symfony/filesystem', 'symfony/yaml' ], + [ 'a/a', 'b/b', 'c/c', 'd/d' ], + ] + ); + + $this->assertSame( + [ + 'Update symfony/yaml', + 'Update symfony/config and symfony/yaml', + 'Update symfony/config, symfony/filesystem and symfony/yaml', + 'Update 4 locked dependencies', + ], + $titles + ); + } + + public function test_the_description_lists_changes_and_advisories(): void { + list( , $body ) = Dependency_Update::describe_pull_request( + $this->plan( + [ $this->change( 'symfony/yaml' ), $this->change( 'symfony/new', null, 'v1.0.0', false ) ], + [ $this->advisory( 'symfony/yaml', 'PKSA-1', 'CVE-2026-1' ) ], + [ $this->advisory( 'symfony/config', 'PKSA-2' ) ] + ) + ); + + $this->assertStringContainsString( '| symfony/yaml | v5.4.54 | v5.4.56 | yes |', $body ); + $this->assertStringContainsString( '| symfony/new | (new) | v1.0.0 | no |', $body ); + $this->assertStringContainsString( "### Fixed security advisories\n\n- symfony/yaml [PKSA-1](https://example.com/PKSA-1): Problem in symfony/yaml (CVE-2026-1)", $body ); + $this->assertStringContainsString( "### Still affected\n", $body ); + $this->assertStringContainsString( '- symfony/config [PKSA-2](https://example.com/PKSA-2): Problem in symfony/config', $body ); + } + + public function test_the_description_leaves_out_empty_sections(): void { + list( , $body ) = Dependency_Update::describe_pull_request( $this->plan( [ $this->change( 'wp-cli/wp-cli', 'dev-main (5ec3c05)', 'dev-main (0123456)', false ) ] ) ); + + $this->assertStringNotContainsString( '###', $body ); + } + + /** + * @param int $ahead_by Commits on the branch that the default branch lacks. + * @param string[] $messages Messages of those commits, oldest first. + */ + private function comparison( $ahead_by, array $messages = [], array $files = [ 'composer.lock' ] ) { + return (object) [ + 'ahead_by' => $ahead_by, + 'files' => array_map( + static function ( $file ) { + return (object) [ 'filename' => $file ]; + }, + $files + ), + 'commits' => array_map( + static function ( $message ) { + return (object) [ 'commit' => (object) [ 'message' => $message ] ]; + }, + $messages + ), + ]; + } + + public function test_only_a_branch_with_nothing_but_its_own_commit_is_overwritten(): void { + $this->assertTrue( Dependency_Update::is_own_branch( $this->comparison( 0 ) ) ); + $this->assertTrue( Dependency_Update::is_own_branch( $this->comparison( 1, [ "Update locked dependencies\n\n- symfony/yaml: v5.4.54 => v5.4.56" ] ) ) ); + $this->assertFalse( Dependency_Update::is_own_branch( $this->comparison( 1, [ 'Pin symfony/yaml' ] ) ) ); + $this->assertFalse( Dependency_Update::is_own_branch( $this->comparison( 2, [ 'Update locked dependencies', 'Fix the build' ] ) ) ); + $this->assertFalse( Dependency_Update::is_own_branch( $this->comparison( 1, [ 'Update locked dependencies' ], [ 'composer.json', 'composer.lock' ] ) ) ); + } + + public function test_expand_names_each_locked_package_a_pattern_matches(): void { + $locked = [ 'symfony/yaml', 'symfony/config', 'Symfony/Polyfill-PHP84', 'wp-cli/wp-cli', 'behat/behat' ]; + + $this->assertSame( + [ [ 'Symfony/Polyfill-PHP84', 'symfony/config', 'symfony/yaml', 'wp-cli/wp-cli' ], [ 'acme/*' ] ], + Dependency_Update::expand( [ 'symfony/*', 'WP-CLI/wp-cli', 'acme/*', 'symfony/yaml' ], $locked ) + ); + $this->assertSame( [ [ 'behat/behat', 'symfony/config', 'symfony/yaml', 'wp-cli/wp-cli' ], [] ], Dependency_Update::expand( [ '*' ], [ 'symfony/yaml', 'symfony/config', 'wp-cli/wp-cli', 'behat/behat' ] ) ); + $this->assertSame( [ [], [ 'symfony.yaml' ] ], Dependency_Update::expand( [ 'symfony.yaml' ], [ 'symfony/yaml' ] ) ); + } + + public function test_branch_names_follow_the_requested_packages(): void { + $this->assertSame( 'update-dependencies-security', Dependency_Update::get_branch_name( [] ) ); + $this->assertSame( 'update-dependencies-wp-cli-wp-cli', Dependency_Update::get_branch_name( [ 'wp-cli/wp-cli' ] ) ); + $this->assertSame( 'update-dependencies-symfony-all-wp-cli-wp-cli', Dependency_Update::get_branch_name( [ 'wp-cli/wp-cli', 'symfony/*' ] ) ); + $this->assertSame( 'update-dependencies-all', Dependency_Update::get_branch_name( [ '*' ] ) ); + + $long = Dependency_Update::get_branch_name( [ 'symfony/config', 'symfony/filesystem', 'symfony/yaml', 'symfony/process', 'symfony/finder' ] ); + $this->assertLessThanOrEqual( strlen( Dependency_Update::BRANCH_PREFIX ) + 60, strlen( $long ) ); + $this->assertNotSame( $long, Dependency_Update::get_branch_name( [ 'symfony/config', 'symfony/filesystem', 'symfony/yaml', 'symfony/process', 'symfony/console' ] ) ); + } + + public function test_the_description_of_a_requested_update_does_not_blame_composer_json(): void { + $plan = $this->plan( [ $this->change( 'wp-cli/wp-cli', 'dev-main (5ec3c05)', 'dev-main (0123456)', false ) ], [], [ $this->advisory( 'symfony/yaml', 'PKSA-1' ) ] ); + $plan['requested'] = true; + + list( , $body ) = Dependency_Update::describe_pull_request( $plan ); + + $this->assertStringContainsString( 'This update leaves these advisories as they are:', $body ); + $this->assertStringNotContainsString( 'composer.json:', $body ); + $this->assertStringNotContainsString( 'Fixes security advisories.', $body ); + } +} From 572825d414c7f54727206fb5d4619352624890e9 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 16 Nov 2026 11:50:47 +0000 Subject: [PATCH 2/2] Say what holds back the fix of an advisory the update leaves For each advisory that `wp maintenance dependencies update` cannot fix, work out the first version that fixes it from the affected ranges, and ask `composer why-not` what keeps that version out of the lock. Both go into the warnings and into the "Still affected" section of the pull request, so a maintainer knows which constraint in composer.json, or which package, to change. Co-Authored-By: Claude Opus 5.5 --- .maintenance/src/Dependencies_Command.php | 21 +- .maintenance/src/Dependency_Update.php | 225 +++++++++++++++++++++- tests/DependenciesUpdateCommandTest.php | 37 +++- tests/DependencyUpdateTest.php | 69 +++++++ 4 files changed, 345 insertions(+), 7 deletions(-) diff --git a/.maintenance/src/Dependencies_Command.php b/.maintenance/src/Dependencies_Command.php index c7a5890..592197e 100644 --- a/.maintenance/src/Dependencies_Command.php +++ b/.maintenance/src/Dependencies_Command.php @@ -131,7 +131,9 @@ public function audit( $args, $assoc_args ) { * * Without --packages, exits with an error code when advisories are left * that the update cannot fix, as fixing them needs a change to - * composer.json. + * composer.json. For each of them, it says which version fixes it, and + * what `composer why-not` says holds that version back, both here and in + * the pull request. * * Needs Composer 2.7 or later. Works from the files on GitHub, so no clone * of the repository is needed. @@ -269,7 +271,22 @@ static function ( $change ) { private function report_remaining( array $plan ) { foreach ( $plan['remaining'] as $advisory ) { - WP_CLI::warning( "{$advisory['package']} {$advisory['version']} is still affected by {$advisory['advisory']}: {$advisory['title']}" ); + $message = "{$advisory['package']} {$advisory['version']} is still affected by {$advisory['advisory']}: {$advisory['title']}"; + + if ( ! empty( $advisory['fixed_in'] ) ) { + $message .= "\nIt is fixed in {$advisory['fixed_in']}"; + + if ( ! empty( $advisory['held_back_by'] ) ) { + $message .= ', which is held back by:'; + foreach ( $advisory['held_back_by'] as $reason ) { + $message .= "\n {$reason}"; + } + } else { + $message .= '.'; + } + } + + WP_CLI::warning( $message ); } } diff --git a/.maintenance/src/Dependency_Update.php b/.maintenance/src/Dependency_Update.php index 63e8af7..586aae8 100644 --- a/.maintenance/src/Dependency_Update.php +++ b/.maintenance/src/Dependency_Update.php @@ -34,6 +34,13 @@ final class Dependency_Update { */ private static $runner; + /** + * Runs `composer why-not`, or null for the real one. Tests swap it out. + * + * @var callable|null + */ + private static $why_not_runner; + /** * Replaces the `composer update` run, for tests. * @@ -46,6 +53,19 @@ public static function set_runner( $runner ) { self::$runner = $runner; } + /** + * Replaces the `composer why-not` run, for tests. + * + * @param callable|null $runner Receives the contents of composer.json and + * composer.lock, a package, a version and the + * branch, and returns what holds that version + * back, as for get_held_back_by(). Null + * restores the real run. + */ + public static function set_why_not_runner( $runner ) { + self::$why_not_runner = $runner; + } + /** * Works out what an update of the default branch of a repository changes. * @@ -59,7 +79,10 @@ public static function set_runner( $runner ) { * `branch` is the default branch and `head` the branch for the * pull request. `unmatched` lists the given packages that match no * locked package. `composer_lock` holds the new lock, or null when - * the update changes no locked version. + * the update changes no locked version. Each of the `remaining` + * advisories also has a `fixed_in` version, the first one that + * fixes it, and `held_back_by`, what `composer why-not` says keeps + * that version out, see explain(). */ public static function prepare( $repo, array $packages = [] ) { $files = Dependency_Audit::fetch_files( $repo ); @@ -68,6 +91,165 @@ public static function prepare( $repo, array $packages = [] ) { return null; } + $plan = self::plan( $repo, $packages, $files ); + + $plan['remaining'] = self::explain( + $plan['remaining'], + $files['composer_json'], + null === $plan['composer_lock'] ? $files['composer_lock'] : $plan['composer_lock'], + $files['ref'] + ); + + return $plan; + } + + /** + * Adds to each advisory the first version that fixes it, and what holds + * that version back. + * + * @param array $advisories Advisories, see Dependency_Audit. + * @param string $composer_json Contents of composer.json. + * @param string $composer_lock Contents of the composer.lock they apply to. + * @param string $branch Branch the files are from. + * + * @return array The advisories, each with `fixed_in`, null when no fixed + * version is known, and `held_back_by`, the reasons + * `composer why-not` gives, an empty list when it finds none, + * or null when it was not asked. + */ + public static function explain( array $advisories, $composer_json, $composer_lock, $branch ) { + $runner = self::$why_not_runner ? self::$why_not_runner : [ __CLASS__, 'get_held_back_by' ]; + $reasons = []; + + foreach ( $advisories as $index => $advisory ) { + $fixed_in = self::get_first_fixed_version( isset( $advisory['affected_versions'] ) ? $advisory['affected_versions'] : null, $advisory['version'] ); + $held_back_by = null; + + if ( null !== $fixed_in ) { + $key = "{$advisory['package']} {$fixed_in}"; + + if ( ! array_key_exists( $key, $reasons ) ) { + $reasons[ $key ] = call_user_func( $runner, $composer_json, $composer_lock, $advisory['package'], $fixed_in, $branch ); + } + + $held_back_by = $reasons[ $key ]; + } + + $advisories[ $index ]['fixed_in'] = $fixed_in; + $advisories[ $index ]['held_back_by'] = $held_back_by; + } + + return $advisories; + } + + /** + * Finds the lowest version above a locked one that none of the affected + * ranges of an advisory covers. Advisories often list one range per + * minor version, as in `>=5.3.0,<5.4.0|>=5.4.0,<5.4.56`, so the upper + * bound of one range can be the start of the next. + * + * @param string|null $affected Affected versions, as Composer reports them. + * @param string|null $version Locked version. + * + * @return string|null The version, or null when none is known to be fixed + * or the ranges cannot be read. + */ + public static function get_first_fixed_version( $affected, $version ) { + if ( ! is_string( $affected ) || ! is_string( $version ) || ! preg_match( '/^v?(\d[\w.\-]*)$/i', $version, $match ) ) { + return null; + } + + $ranges = []; + foreach ( explode( '|', $affected ) as $constraint ) { + $range = [ + 'lower' => null, + 'upper' => null, + ]; + + foreach ( preg_split( '/[\s,]+/', trim( $constraint ), -1, PREG_SPLIT_NO_EMPTY ) as $part ) { + if ( ! preg_match( '/^(>=|>|<=|<|==|=)?v?(\d[\w.\-]*)$/i', $part, $bound ) ) { + return null; + } + + if ( '' === $bound[1] || '=' === $bound[1][0] ) { + $range['lower'] = [ $bound[2], true ]; + $range['upper'] = [ $bound[2], true ]; + } elseif ( '>' === $bound[1][0] ) { + $range['lower'] = [ $bound[2], '>=' === $bound[1] ]; + } else { + $range['upper'] = [ $bound[2], '<=' === $bound[1] ]; + } + } + + $ranges[] = $range; + } + + $candidate = $match[1]; + $passes = count( $ranges ) + 1; + + // Each pass but the last moves past a range, so this many will do. + for ( $pass = 0; $pass < $passes; $pass++ ) { + $containing = null; + foreach ( $ranges as $range ) { + if ( self::in_range( $candidate, $range ) ) { + $containing = $range; + break; + } + } + + if ( null === $containing ) { + return $candidate === $match[1] ? null : $candidate; + } + + if ( null === $containing['upper'] || $containing['upper'][1] ) { + return null; + } + + $candidate = $containing['upper'][0]; + } + + return null; + } + + /** + * Asks `composer why-not` what keeps a version of a package from being + * locked. + * + * @param string $composer_json Contents of composer.json. + * @param string $composer_lock Contents of composer.lock. + * @param string $package Package name. + * @param string $version Version that should be locked. + * @param string $branch Branch the files are from. + * + * @return string[]|null The reasons, one per line, such as + * `symfony/yaml v6.4.50 requires php (>=8.1 but 7.2.24 is installed)`; + * none when it finds nothing, and null when it fails. + */ + public static function get_held_back_by( $composer_json, $composer_lock, $package, $version, $branch ) { + $result = Composer::run( + [ 'why-not', $package, $version, '--locked' ], + [ + 'composer.json' => $composer_json, + 'composer.lock' => $composer_lock, + ], + [], + [ 'COMPOSER_ROOT_VERSION' => "dev-{$branch}" ] + ); + + // It exits with 1 when it finds something that holds the version back. + if ( 0 === $result['return_code'] ) { + return []; + } + + if ( 1 !== $result['return_code'] ) { + WP_CLI::debug( "`composer why-not {$package} {$version}` failed (exit code {$result['return_code']}).", 'composer' ); + return null; + } + + return array_values( array_filter( array_map( 'trim', explode( "\n", $result['stdout'] ) ), 'strlen' ) ); + } + + private static function plan( $repo, array $packages, array $files ) { $before = self::audit( $repo, $files, $files['composer_lock'], $files['lock'] ); $plan = [ @@ -307,6 +489,10 @@ public static function describe_pull_request( array $plan ) { $lines[] = ''; foreach ( $plan['remaining'] as $advisory ) { $lines[] = self::describe_advisory( $advisory ); + + foreach ( self::describe_held_back_by( $advisory ) as $line ) { + $lines[] = '' === $line ? '' : " {$line}"; + } } } @@ -487,6 +673,43 @@ private static function describe_version( array $package, $show_reference ) { return sprintf( '%s (%s)', $package['version'], substr( $package['reference'], 0, 7 ) ); } + /** + * Says what keeps the fix of an advisory out of the lock, as Markdown. + * + * @param array $advisory Advisory, see explain(). + * + * @return string[] Lines. + */ + private static function describe_held_back_by( array $advisory ) { + if ( empty( $advisory['fixed_in'] ) ) { + return []; + } + + $fixed_in = $advisory['fixed_in']; + + if ( null === $advisory['held_back_by'] ) { + return [ "Fixed in {$fixed_in}." ]; + } + + if ( ! $advisory['held_back_by'] ) { + return [ "Fixed in {$fixed_in}. `composer why-not` finds nothing that holds it back; `composer update \"{$advisory['package']}:{$fixed_in}\" --dry-run` may tell more." ]; + } + + return array_merge( + [ "Fixed in {$fixed_in}, which `composer why-not` says is held back by:", '', '```' ], + $advisory['held_back_by'], + [ '```' ] + ); + } + + private static function in_range( $version, array $range ) { + if ( null !== $range['lower'] && ! version_compare( $version, $range['lower'][0], $range['lower'][1] ? '>=' : '>' ) ) { + return false; + } + + return null === $range['upper'] || version_compare( $version, $range['upper'][0], $range['upper'][1] ? '<=' : '<' ); + } + private static function describe_advisory( array $advisory ) { $id = $advisory['link'] ? "[{$advisory['advisory']}]({$advisory['link']})" : $advisory['advisory']; diff --git a/tests/DependenciesUpdateCommandTest.php b/tests/DependenciesUpdateCommandTest.php index fcef55c..35c8359 100644 --- a/tests/DependenciesUpdateCommandTest.php +++ b/tests/DependenciesUpdateCommandTest.php @@ -35,6 +35,13 @@ class DependenciesUpdateCommandTest extends GitHub_TestCase { */ private $plugin_api = '2.9.0'; + /** + * Calls to the fake `composer why-not`: composer.lock, package, version. + * + * @var array + */ + private $why_nots = []; + /** * What the command printed to STDOUT. * @@ -54,10 +61,11 @@ static function ( $composer_json, $composer_lock ) { 'advisories' => $affected ? [ 'symfony/yaml' => [ [ - 'advisoryId' => 'PKSA-nktx-f1n1-b4yd', - 'cve' => 'CVE-2026-123792', - 'title' => 'Exponential and Quadratic Parse Times in the YAML Parser', - 'link' => 'https://github.com/symfony/symfony/security/advisories/GHSA-hwfg-qwvh-2wfx', + 'advisoryId' => 'PKSA-nktx-f1n1-b4yd', + 'cve' => 'CVE-2026-123792', + 'title' => 'Exponential and Quadratic Parse Times in the YAML Parser', + 'link' => 'https://github.com/symfony/symfony/security/advisories/GHSA-hwfg-qwvh-2wfx', + 'affectedVersions' => '>=5.0.0,<5.4.0|>=5.4.0,<5.4.56|>=6.0.0,<6.4.50', ], ], ] : [], @@ -73,11 +81,20 @@ function ( $composer_json, $composer_lock, array $packages, $branch ) { return $this->unfixable ? $composer_lock : $this->lock_file( 'v5.4.56', $this->plugin_api ); } ); + + Dependency_Update::set_why_not_runner( + function ( $composer_json, $composer_lock, $package, $version ) { + $this->why_nots[] = [ $composer_lock, $package, $version ]; + + return [ "wp-cli/wp-cli-bundle dev-main requires {$package} (5.4.54)" ]; + } + ); } protected function tear_down(): void { Dependency_Audit::set_runner( null ); Dependency_Update::set_runner( null ); + Dependency_Update::set_why_not_runner( null ); parent::tear_down(); } @@ -352,7 +369,19 @@ public function test_advisories_the_update_cannot_fix_fail_the_run(): void { $this->assertStringContainsString( 'Updating symfony/yaml changes no locked version of wp-cli/wp-cli-bundle.', $this->logger->stderr ); $this->assertStringContainsString( 'symfony/yaml v5.4.54 is still affected by PKSA-nktx-f1n1-b4yd', $this->logger->stderr ); + $this->assertStringContainsString( "It is fixed in 5.4.56, which is held back by:\n wp-cli/wp-cli-bundle dev-main requires symfony/yaml (5.4.54)", $this->logger->stderr ); $this->assertStringContainsString( 'Fixing them needs a change to composer.json.', $this->logger->stderr ); + $this->assertCount( 1, $this->why_nots ); + $this->assertSame( $this->lock( 'v5.4.54' ), json_decode( $this->why_nots[0][0], true ) ); + $this->assertSame( [ 'symfony/yaml', '5.4.56' ], array_slice( $this->why_nots[0], 1 ) ); + } + + public function test_fixed_advisories_are_not_explained(): void { + $this->bundle(); + + $this->assertSame( 0, $this->update( [], [ 'dry-run' => true ] ) ); + + $this->assertSame( [], $this->why_nots ); } public function test_given_packages_are_updated_instead_and_may_leave_advisories(): void { diff --git a/tests/DependencyUpdateTest.php b/tests/DependencyUpdateTest.php index 0c416da..9a10d9f 100644 --- a/tests/DependencyUpdateTest.php +++ b/tests/DependencyUpdateTest.php @@ -228,4 +228,73 @@ public function test_the_description_of_a_requested_update_does_not_blame_compos $this->assertStringNotContainsString( 'composer.json:', $body ); $this->assertStringNotContainsString( 'Fixes security advisories.', $body ); } + + public function test_the_first_fixed_version_skips_ranges_that_follow_on(): void { + $affected = '>=5.0.0,<5.1.0|>=5.1.0,<5.4.56|>=6.0.0,<6.4.50'; + + $this->assertSame( '5.4.56', Dependency_Update::get_first_fixed_version( $affected, 'v5.0.3' ) ); + $this->assertSame( '5.4.56', Dependency_Update::get_first_fixed_version( $affected, '5.4.54' ) ); + $this->assertSame( '6.4.50', Dependency_Update::get_first_fixed_version( $affected, 'v6.0.0' ) ); + $this->assertSame( '2.0.1', Dependency_Update::get_first_fixed_version( '<2.0.1', '1.9.0' ) ); + $this->assertSame( '1.2.4', Dependency_Update::get_first_fixed_version( '>1.0 <1.2.4', '1.2.3' ) ); + } + + public function test_no_first_fixed_version_is_made_up(): void { + // Open-ended, inclusive upper bound, unreadable, not affected, branch. + $this->assertNull( Dependency_Update::get_first_fixed_version( '>=1.0.0', '1.2.0' ) ); + $this->assertNull( Dependency_Update::get_first_fixed_version( '<=1.2.0', '1.2.0' ) ); + $this->assertNull( Dependency_Update::get_first_fixed_version( '^1.0', '1.2.0' ) ); + $this->assertNull( Dependency_Update::get_first_fixed_version( '<1.0.0', '1.2.0' ) ); + $this->assertNull( Dependency_Update::get_first_fixed_version( '<1.0.0', 'dev-main' ) ); + $this->assertNull( Dependency_Update::get_first_fixed_version( null, '1.2.0' ) ); + } + + public function test_explain_asks_composer_once_per_package_and_version(): void { + $calls = []; + Dependency_Update::set_why_not_runner( + static function ( $composer_json, $composer_lock, $package, $version, $branch ) use ( &$calls ) { + $calls[] = [ $package, $version, $branch ]; + + return [ "{$package} v{$version} requires php (>=8.1 but 7.2.24 is installed)" ]; + } + ); + + $advisories = [ $this->advisory( 'symfony/yaml', 'PKSA-1' ), $this->advisory( 'symfony/yaml', 'PKSA-2' ), $this->advisory( 'symfony/config', 'PKSA-3' ) ]; + $advisories[0]['affected_versions'] = '>=5.4.0,<5.4.56'; + $advisories[1]['affected_versions'] = '>=5.4.0,<5.4.56'; + + try { + $explained = Dependency_Update::explain( $advisories, '{}', '{}', 'main' ); + } finally { + Dependency_Update::set_why_not_runner( null ); + } + + $this->assertSame( [ [ 'symfony/yaml', '5.4.56', 'main' ] ], $calls ); + $this->assertSame( '5.4.56', $explained[1]['fixed_in'] ); + $this->assertSame( [ 'symfony/yaml v5.4.56 requires php (>=8.1 but 7.2.24 is installed)' ], $explained[1]['held_back_by'] ); + $this->assertNull( $explained[2]['fixed_in'] ); + $this->assertNull( $explained[2]['held_back_by'] ); + } + + public function test_the_description_says_what_holds_a_fix_back(): void { + $held_back = $this->advisory( 'symfony/yaml', 'PKSA-1' ); + $held_back['fixed_in'] = '6.4.50'; + $held_back['held_back_by'] = [ 'symfony/yaml v6.4.50 requires php (>=8.1 but 7.2.24 is installed)' ]; + $unclear = $this->advisory( 'symfony/config', 'PKSA-2' ); + $unclear['fixed_in'] = '5.4.56'; + $unclear['held_back_by'] = []; + + list( , $body ) = Dependency_Update::describe_pull_request( $this->plan( [ $this->change( 'symfony/filesystem' ) ], [], [ $held_back, $unclear ] ) ); + + $this->assertStringContainsString( + "- symfony/yaml [PKSA-1](https://example.com/PKSA-1): Problem in symfony/yaml\n" + . " Fixed in 6.4.50, which `composer why-not` says is held back by:\n" + . "\n" + . " ```\n" + . " symfony/yaml v6.4.50 requires php (>=8.1 but 7.2.24 is installed)\n" + . " ```\n", + $body + ); + $this->assertStringContainsString( ' Fixed in 5.4.56. `composer why-not` finds nothing that holds it back; `composer update "symfony/config:5.4.56" --dry-run` may tell more.', $body ); + } }