From 7b4278607a8ed5ee672e253e383bcf8e3928afc9 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 16 Nov 2026 07:41:02 +0000 Subject: [PATCH 1/2] Add `wp maintenance dependencies audit` Checks the committed composer.lock of a repository, by default wp-cli/wp-cli-bundle, for packages with known security advisories and for abandoned packages. Runs `composer audit --locked` on composer.json and composer.lock as committed at a ref, without installing anything, so it agrees with the "Audit locked dependencies" step of the Code Quality workflow. Composer runs without the environment variables and the global configuration that would change what it audits, such as COMPOSER_NO_DEV or a global `audit.ignore`. Co-Authored-By: Claude Opus 5.5 --- .maintenance/bootstrap.php | 1 + .maintenance/src/Bundle.php | 16 +- .maintenance/src/Dependencies_Command.php | 175 ++++++++++++ .maintenance/src/Dependency_Audit.php | 331 ++++++++++++++++++++++ .maintenance/src/Doctor_Command.php | 2 +- .maintenance/src/GitHub.php | 53 ++++ .readme-partials/DEVELOPMENT.md | 2 + tests/DependenciesCommandTest.php | 206 ++++++++++++++ tests/DependencyAuditTest.php | 225 +++++++++++++++ tests/GitHubApiTest.php | 29 ++ tests/fixtures/composer/audit.json | 61 ++++ 11 files changed, 1091 insertions(+), 10 deletions(-) create mode 100644 .maintenance/src/Dependencies_Command.php create mode 100644 .maintenance/src/Dependency_Audit.php create mode 100644 tests/DependenciesCommandTest.php create mode 100644 tests/DependencyAuditTest.php create mode 100644 tests/fixtures/composer/audit.json diff --git a/.maintenance/bootstrap.php b/.maintenance/bootstrap.php index a85a49c..fc3868e 100644 --- a/.maintenance/bootstrap.php +++ b/.maintenance/bootstrap.php @@ -7,6 +7,7 @@ WP_CLI::add_command( 'maintenance', 'WP_CLI\Maintenance\Maintenance_Namespace' ); WP_CLI::add_command( 'maintenance contrib-list', 'WP_CLI\Maintenance\Contrib_List_Command' ); +WP_CLI::add_command( 'maintenance dependencies', 'WP_CLI\Maintenance\Dependencies_Command' ); WP_CLI::add_command( 'maintenance doctor', 'WP_CLI\Maintenance\Doctor_Command' ); WP_CLI::add_command( 'maintenance milestones-after', 'WP_CLI\Maintenance\Milestones_After_Command' ); WP_CLI::add_command( 'maintenance milestones-since', 'WP_CLI\Maintenance\Milestones_Since_Command' ); diff --git a/.maintenance/src/Bundle.php b/.maintenance/src/Bundle.php index a2983a5..efc1cb5 100644 --- a/.maintenance/src/Bundle.php +++ b/.maintenance/src/Bundle.php @@ -1,7 +1,6 @@ status_code && ! $missing_is_fatal ) { - return false; - } + if ( null === $lock ) { + if ( ! $missing_is_fatal ) { + return false; + } - if ( 200 !== (int) $response->status_code ) { - WP_CLI::error( sprintf( 'Could not fetch %s (HTTP code %d)', $url, $response->status_code ) ); + WP_CLI::error( sprintf( 'Could not fetch https://raw.githubusercontent.com/%s/%s/composer.lock (HTTP code 404)', self::REPO, $ref ) ); } - return json_decode( $response->body, true ); + return json_decode( $lock, true ); } /** diff --git a/.maintenance/src/Dependencies_Command.php b/.maintenance/src/Dependencies_Command.php new file mode 100644 index 0000000..830bd4c --- /dev/null +++ b/.maintenance/src/Dependencies_Command.php @@ -0,0 +1,175 @@ +...] + * : Name(s) of the repository to audit. 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. + * + * [--ref=] + * : Branch, tag or commit to audit, such as a release tag. Defaults to the + * default branch of each repository. + * + * [--[no-]dev] + * : Whether to audit the packages that are only required for development + * as well. Pass `--no-dev` to audit only what ships. + * --- + * default: true + * --- + * + * [--format=] + * : Render output in a specific format. Defaults to a table in a terminal + * and to JSON otherwise. The JSON also holds the commit that was audited, + * and the affected version ranges and links of the advisories. + * --- + * options: + * - table + * - json + * --- + * + * ## EXAMPLES + * + * $ wp maintenance dependencies audit + * +----------------------+----------------------+---------+-----+---------------------+-----------------+--------------------------------------+ + * | repo | package | version | dev | advisory | cve | title | + * +----------------------+----------------------+---------+-----+---------------------+-----------------+--------------------------------------+ + * | wp-cli/wp-cli-bundle | symfony/yaml | v5.4.54 | yes | PKSA-nktx-f1n1-b4yd | CVE-2026-123792 | Exponential and Quadratic Parse ... | + * | wp-cli/wp-cli-bundle | behat/transliterator | v1.5.0 | yes | abandoned | | Abandoned, no replacement suggested. | + * +----------------------+----------------------+---------+-----+---------------------+-----------------+--------------------------------------+ + * Error: Locked packages are affected by security advisories in wp-cli/wp-cli-bundle at main. + * + * # Audit only what ships in the Phar. + * $ wp maintenance dependencies audit --no-dev + * Success: No known vulnerabilities or abandoned packages in wp-cli/wp-cli-bundle at main. + * + * @when before_wp_load + */ + public function audit( $args, $assoc_args ) { + $format = Utils\get_flag_value( $assoc_args, 'format', Shell::isPiped() ? 'json' : 'table' ); + $ref = Utils\get_flag_value( $assoc_args, 'ref', null ); + $dev = (bool) Utils\get_flag_value( $assoc_args, 'dev', true ); + $repos = $args ? array_values( array_unique( array_map( [ GitHub::class, 'qualify_repo' ], $args ) ) ) : [ Bundle::REPO ]; + + $audits = []; + foreach ( $repos as $repo ) { + $audit = Dependency_Audit::fetch( $repo, $ref, $dev ); + + if ( null === $audit ) { + WP_CLI::warning( "{$repo} has no composer.lock" . ( $ref ? " at {$ref}" : '' ) . ', skipping it.' ); + continue; + } + + $audits[] = $audit; + } + + if ( ! $audits ) { + WP_CLI::error( 'None of the repositories has a composer.lock to audit.' ); + } + + if ( 'json' === $format ) { + WP_CLI::line( (string) json_encode( $audits, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) ); + } else { + $this->render_table( $audits ); + } + + $vulnerable = []; + foreach ( $audits as $audit ) { + if ( Dependency_Audit::VULNERABLE === $audit['status'] ) { + $vulnerable[] = "{$audit['repo']} at {$audit['ref']}"; + } + } + + if ( $vulnerable ) { + WP_CLI::error( 'Locked packages are affected by security advisories in ' . implode( ', ', $vulnerable ) . '.' ); + } + } + + private function render_table( array $audits ) { + $rows = []; + foreach ( $audits as $audit ) { + foreach ( $audit['advisories'] as $advisory ) { + $rows[] = $this->row( $audit['repo'], $advisory, $advisory['advisory'], $advisory['cve'], $advisory['title'] ); + } + + foreach ( $audit['ignored'] as $advisory ) { + $rows[] = $this->row( + $audit['repo'], + $advisory, + $advisory['advisory'], + $advisory['cve'], + 'Ignored' . ( $advisory['ignore_reason'] ? " ({$advisory['ignore_reason']})" : '' ) . ": {$advisory['title']}" + ); + } + + foreach ( $audit['abandoned'] as $abandoned ) { + $rows[] = $this->row( + $audit['repo'], + $abandoned, + 'abandoned', + null, + $abandoned['replacement'] + ? "Abandoned, use {$abandoned['replacement']} instead." + : 'Abandoned, no replacement suggested.' + ); + } + } + + if ( $rows ) { + Utils\format_items( 'table', $rows, [ 'repo', 'package', 'version', 'dev', 'advisory', 'cve', 'title' ] ); + } + + foreach ( $audits as $audit ) { + if ( Dependency_Audit::OK === $audit['status'] ) { + WP_CLI::success( "No known vulnerabilities or abandoned packages in {$audit['repo']} at {$audit['ref']}." ); + } + } + } + + private function row( $repo, array $package, $advisory, $cve, $title ) { + // A package the lock does not list may or may not ship. + $dev = ''; + if ( null !== $package['dev'] ) { + $dev = $package['dev'] ? 'yes' : 'no'; + } + + return [ + 'repo' => $repo, + 'package' => $package['package'], + 'version' => $package['version'], + 'dev' => $dev, + 'advisory' => $advisory, + 'cve' => $cve, + 'title' => $title, + ]; + } +} diff --git a/.maintenance/src/Dependency_Audit.php b/.maintenance/src/Dependency_Audit.php new file mode 100644 index 0000000..4dcbb5c --- /dev/null +++ b/.maintenance/src/Dependency_Audit.php @@ -0,0 +1,331 @@ + $replacement ) { + $abandoned[] = array_merge( + self::describe_package( (string) $package, $locked ), + [ 'replacement' => is_string( $replacement ) && '' !== $replacement ? $replacement : null ] + ); + } + + usort( $abandoned, [ __CLASS__, 'compare' ] ); + + if ( $advisories ) { + $status = self::VULNERABLE; + } elseif ( $abandoned ) { + $status = self::ABANDONED; + } else { + $status = self::OK; + } + + return [ + 'repo' => $repo, + 'ref' => $ref, + 'sha' => $sha, + 'dev' => (bool) $dev, + 'status' => $status, + 'advisories' => $advisories, + 'ignored' => $ignored, + 'abandoned' => $abandoned, + ]; + } + + /** + * Lists the packages of a lockfile with their versions, and whether they + * are only required for development. + * + * @param array $lock Decoded composer.lock. + * + * @return array + */ + public static function get_locked_packages( 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'], + 'dev' => $dev, + ]; + } + } + + 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. + * + * The files are written to a temporary directory and audited from the + * lock, without installing anything. Plugins and scripts stay disabled, + * so auditing a repository never runs its code. + * + * @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 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' ); + } + + // 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 ); + + 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 ) + ); + } + + return $report; + } + + /** + * Gets a section of the report. Composer encodes an empty one as `[]`. + * + * @param array $report Decoded report. + * @param string $key Section name. + * + * @return array + */ + private static function section( array $report, $key ) { + return isset( $report[ $key ] ) && is_array( $report[ $key ] ) ? $report[ $key ] : []; + } + + private static function describe_advisories( array $section, array $locked ) { + $advisories = []; + + foreach ( $section as $package => $package_advisories ) { + foreach ( (array) $package_advisories as $advisory ) { + $advisories[] = array_merge( + self::describe_package( (string) $package, $locked ), + [ + 'advisory' => isset( $advisory['advisoryId'] ) ? $advisory['advisoryId'] : null, + 'cve' => isset( $advisory['cve'] ) ? $advisory['cve'] : null, + 'title' => isset( $advisory['title'] ) ? $advisory['title'] : '', + 'link' => isset( $advisory['link'] ) ? $advisory['link'] : null, + 'affected_versions' => isset( $advisory['affectedVersions'] ) ? $advisory['affectedVersions'] : null, + 'severity' => isset( $advisory['severity'] ) ? $advisory['severity'] : null, + 'reported_at' => isset( $advisory['reportedAt'] ) ? $advisory['reportedAt'] : null, + 'ignore_reason' => isset( $advisory['ignoreReason'] ) ? $advisory['ignoreReason'] : null, + ] + ); + } + } + + usort( $advisories, [ __CLASS__, 'compare' ] ); + + return $advisories; + } + + /** + * Describes a package of the report. A package the lock does not list has + * no version, and whether it ships is unknown. + */ + private static function describe_package( $package, array $locked ) { + return [ + 'package' => $package, + 'version' => isset( $locked[ $package ] ) ? $locked[ $package ]['version'] : null, + 'dev' => isset( $locked[ $package ] ) ? $locked[ $package ]['dev'] : null, + ]; + } + + private static function compare( $a, $b ) { + $by_package = strcmp( $a['package'], $b['package'] ); + + if ( 0 !== $by_package || ! isset( $a['advisory'], $b['advisory'] ) ) { + return $by_package; + } + + 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/Doctor_Command.php b/.maintenance/src/Doctor_Command.php index eacd1f0..3808810 100644 --- a/.maintenance/src/Doctor_Command.php +++ b/.maintenance/src/Doctor_Command.php @@ -128,7 +128,7 @@ private function check_tools() { 'git' => [ 'error', 'Needed to work with the repositories.' ], 'gh' => [ 'warning', 'Optional; without it, credentials come from GH_TOKEN or GITHUB_TOKEN.' ], 'jq' => [ 'warning', 'Needed by the scripts that clone and update the repositories.' ], - 'composer' => [ 'warning', 'Needed to install the development environment.' ], + 'composer' => [ 'warning', 'Needed to install the development environment and to audit dependencies.' ], ]; $checks = []; diff --git a/.maintenance/src/GitHub.php b/.maintenance/src/GitHub.php index eefc7c7..3813104 100644 --- a/.maintenance/src/GitHub.php +++ b/.maintenance/src/GitHub.php @@ -438,6 +438,59 @@ public static function get_default_branch( $project ) { return $repository->default_branch; } + /** + * Resolves a branch, tag or commit of a repository to its commit SHA. + * + * @param string $project Repository name, including the org. + * @param string $ref Branch, tag or commit. + * + * @return string|null Commit SHA, or null when the repository has no such ref. + */ + public static function get_commit_sha( $project, $ref ) { + $result = self::request( + self::API_ROOT . "repos/{$project}/commits", + [ + 'sha' => $ref, + 'per_page' => 1, + ], + 'GET', + [ 404, 422 ] + ); + + if ( ! $result || empty( $result[0] ) ) { + return null; + } + + return $result[0][0]->sha; + } + + /** + * Gets the contents of a file of a repository at a ref. + * + * @param string $project Repository name, including the org. + * @param string $ref Branch, tag or commit. + * @param string $path Path of the file within the repository. + * + * @return string|null Contents, or null when the ref has no such file + * (HTTP 404). Any other failure is fatal, so that a + * rate limit or an outage never passes for a missing + * file. + */ + public static function get_raw_file( $project, $ref, $path ) { + $url = sprintf( 'https://raw.githubusercontent.com/%s/%s/%s', $project, $ref, $path ); + $response = Utils\http_request( 'GET', $url ); + + if ( 404 === (int) $response->status_code ) { + return null; + } + + if ( 200 !== (int) $response->status_code ) { + WP_CLI::error( sprintf( 'Could not fetch %s (HTTP code %d)', $url, $response->status_code ) ); + } + + return $response->body; + } + /** * Makes a GET request and follows the pagination to collect every item. * diff --git a/.readme-partials/DEVELOPMENT.md b/.readme-partials/DEVELOPMENT.md index 09b4c3d..67b84a4 100644 --- a/.readme-partials/DEVELOPMENT.md +++ b/.readme-partials/DEVELOPMENT.md @@ -14,3 +14,5 @@ Running `composer install` or `composer update` also fetches every clone and fas The `wp maintenance` commands don't need the full environment: any `wp` executable picks them up through `wp-cli.yml` when run from this folder. They authenticate with the GitHub CLI when it is logged in, and with the `GH_TOKEN` or `GITHUB_TOKEN` environment variable otherwise. Run `wp maintenance doctor` to check the setup. To find out which packages have unreleased changes, run `wp maintenance release plan --bundle`. It makes no changes, and for each package it says whether a milestone is ready for `wp maintenance release generate` or what a maintainer still needs to decide, such as which milestone a merged pull request belongs to. + +To check the locked dependencies of the bundle for security advisories, run `wp maintenance dependencies audit`. It runs `composer audit` against the composer.lock as committed, so it reports what the Code Quality workflow fails on, and marks the packages that are only needed for development, as they don't ship in the Phar. diff --git a/tests/DependenciesCommandTest.php b/tests/DependenciesCommandTest.php new file mode 100644 index 0000000..38eaec1 --- /dev/null +++ b/tests/DependenciesCommandTest.php @@ -0,0 +1,206 @@ + + */ + private $composer_runs = []; + + /** + * Report the fake `composer audit` returns. + * + * @var array + */ + private $report = [ + 'advisories' => [], + 'abandoned' => [], + ]; + + /** + * What the command printed to STDOUT. + * + * @var string + */ + private $stdout = ''; + + protected function set_up(): void { + parent::set_up(); + + Dependency_Audit::set_runner( + function ( $composer_json, $composer_lock, $dev ) { + $this->composer_runs[] = [ $composer_json, $composer_lock, $dev ]; + + return $this->report; + } + ); + } + + protected function tear_down(): void { + Dependency_Audit::set_runner( null ); + + parent::tear_down(); + } + + /** + * Sets up a repository whose ref resolves to SHA, with the given files at + * that commit. + * + * @param string $repo Repository name, including the org. + * @param string $ref Ref that is asked for. + * @param array|null $composer_lock Decoded lock, or null for none. + * @param bool $composer_json Whether there is a composer.json. + */ + private function repository( $repo, $ref, $composer_lock, $composer_json = true ) { + $this->respond( 'GET', "repos/{$repo}", $this->fixture_object( 'repository', null, [ 'default_branch' => 'main' ] ) ); + $this->respond( 'GET', "repos/{$repo}/commits?sha={$ref}", [ [ 'sha' => self::SHA ] ] ); + $this->respond( 'GET', "{$repo}/" . self::SHA . '/composer.lock', $composer_lock, null === $composer_lock ? 404 : 200 ); + $this->respond( 'GET', "{$repo}/" . self::SHA . '/composer.json', [ 'name' => $repo ], $composer_json ? 200 : 404 ); + } + + private function lock() { + return [ + 'packages' => [ + [ + 'name' => 'wp-cli/wp-cli', + 'version' => 'dev-main', + ], + ], + 'packages-dev' => [ + [ + 'name' => 'symfony/yaml', + 'version' => 'v5.4.54', + ], + ], + ]; + } + + /** + * Runs the command and returns its exit code, 0 when it did not halt. + */ + private function audit( array $args, array $assoc_args ) { + $exit_code = 0; + + // WP_CLI::line() writes to STDOUT directly rather than to the logger. + ob_start(); + try { + ( new Dependencies_Command() )->audit( $args, $assoc_args + [ 'format' => 'json' ] ); + } catch ( ExitException $exception ) { + $exit_code = $exception->getCode(); + } finally { + $this->stdout = (string) ob_get_clean(); + } + + return $exit_code; + } + + /** + * @return array Decoded JSON the command printed. + */ + private function printed_audits() { + return json_decode( $this->stdout, true ); + } + + public function test_audits_the_bundle_at_its_default_branch_by_default(): void { + $this->repository( 'wp-cli/wp-cli-bundle', 'main', $this->lock() ); + + $this->assertSame( 0, $this->audit( [], [] ) ); + + $this->assertCount( 1, $this->composer_runs ); + list( $composer_json, $composer_lock, $dev ) = $this->composer_runs[0]; + $this->assertSame( [ 'name' => 'wp-cli/wp-cli-bundle' ], json_decode( $composer_json, true ) ); + $this->assertSame( $this->lock(), json_decode( $composer_lock, true ) ); + $this->assertTrue( $dev ); + + $output = $this->printed_audits(); + $this->assertSame( 'wp-cli/wp-cli-bundle', $output[0]['repo'] ); + $this->assertSame( 'main', $output[0]['ref'] ); + $this->assertSame( self::SHA, $output[0]['sha'] ); + $this->assertTrue( $output[0]['dev'] ); + $this->assertSame( Dependency_Audit::OK, $output[0]['status'] ); + } + + public function test_an_advisory_fails_the_audit(): void { + $this->repository( 'wp-cli/wp-cli-bundle', 'main', $this->lock() ); + $this->report['advisories'] = [ 'symfony/yaml' => [ [ 'advisoryId' => 'PKSA-nktx-f1n1-b4yd' ] ] ]; + + $this->assertSame( 1, $this->audit( [], [] ) ); + + $this->assertSame( Dependency_Audit::VULNERABLE, $this->printed_audits()[0]['status'] ); + $this->assertStringContainsString( 'Locked packages are affected by security advisories in wp-cli/wp-cli-bundle at main.', $this->logger->stderr ); + } + + public function test_abandoned_packages_alone_do_not_fail_the_audit(): void { + $this->repository( 'wp-cli/wp-cli-bundle', 'main', $this->lock() ); + $this->report['abandoned'] = [ 'behat/transliterator' => null ]; + + $this->assertSame( 0, $this->audit( [], [] ) ); + + $this->assertSame( Dependency_Audit::ABANDONED, $this->printed_audits()[0]['status'] ); + } + + public function test_no_dev_leaves_out_the_dev_packages(): void { + $this->repository( 'wp-cli/wp-cli-bundle', 'v2.12.0', $this->lock() ); + + $this->assertSame( + 0, + $this->audit( + [ 'wp-cli-bundle' ], + [ + 'ref' => 'v2.12.0', + 'dev' => false, + ] + ) + ); + + $this->assertFalse( $this->composer_runs[0][2] ); + $this->assertFalse( $this->printed_audits()[0]['dev'] ); + $this->assertSame( 'v2.12.0', $this->printed_audits()[0]['ref'] ); + } + + public function test_repositories_without_a_lock_are_skipped(): void { + $this->repository( 'wp-cli/wp-cli', 'main', null ); + $this->repository( 'wp-cli/wp-cli-bundle', 'main', $this->lock() ); + + $this->assertSame( 0, $this->audit( [ 'wp-cli', 'wp-cli-bundle' ], [] ) ); + + $this->assertStringContainsString( 'Warning: wp-cli/wp-cli has no composer.lock, skipping it.', $this->logger->stderr ); + $this->assertSame( [ 'wp-cli/wp-cli-bundle' ], array_column( $this->printed_audits(), 'repo' ) ); + $this->assertCount( 1, $this->composer_runs ); + } + + public function test_nothing_to_audit_is_an_error(): void { + $this->repository( 'wp-cli/wp-cli', 'main', null ); + + $this->assertSame( 1, $this->audit( [ 'wp-cli' ], [] ) ); + + $this->assertStringContainsString( 'None of the repositories has a composer.lock to audit.', $this->logger->stderr ); + $this->assertSame( [], $this->composer_runs ); + } + + public function test_an_unknown_ref_is_an_error_rather_than_a_missing_lock(): void { + $this->respond( 'GET', 'repos/wp-cli/wp-cli-bundle/commits?sha=v9.9.9', [ 'message' => 'Not Found' ], 404 ); + + $this->assertSame( 1, $this->audit( [], [ 'ref' => 'v9.9.9' ] ) ); + + $this->assertStringContainsString( 'wp-cli/wp-cli-bundle has no branch, tag or commit v9.9.9.', $this->logger->stderr ); + $this->assertSame( [], $this->composer_runs ); + } + + public function test_a_lock_without_composer_json_is_an_error(): void { + $this->repository( 'wp-cli/wp-cli-bundle', 'main', $this->lock(), false ); + + $this->assertSame( 1, $this->audit( [], [] ) ); + + $this->assertStringContainsString( 'has a composer.lock but no composer.json at main', $this->logger->stderr ); + } +} diff --git a/tests/DependencyAuditTest.php b/tests/DependencyAuditTest.php new file mode 100644 index 0000000..aa73f75 --- /dev/null +++ b/tests/DependencyAuditTest.php @@ -0,0 +1,225 @@ + $packages Name => version. + * @param array $dev_packages Name => version. + * + * @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, + ]; + } + + return $entries; + }; + + return [ + 'packages' => $section( $packages ), + 'packages-dev' => $section( $dev_packages ), + ]; + } + + public function test_get_locked_packages_tells_dev_packages_apart(): void { + $locked = Dependency_Audit::get_locked_packages( + $this->lock( [ 'wp-cli/wp-cli' => 'dev-main' ], [ 'symfony/yaml' => 'v5.4.54' ] ) + ); + + $this->assertSame( + [ + 'wp-cli/wp-cli' => [ + 'version' => 'dev-main', + 'dev' => false, + ], + 'symfony/yaml' => [ + 'version' => 'v5.4.54', + 'dev' => true, + ], + ], + $locked + ); + } + + public function test_get_locked_packages_accepts_a_lock_without_dev_section(): void { + $this->assertSame( + [ + 'mustache/mustache' => [ + 'version' => 'v3.0.0', + 'dev' => false, + ], + ], + Dependency_Audit::get_locked_packages( [ 'packages' => [ $this->lock( [ 'mustache/mustache' => 'v3.0.0' ] )['packages'][0] ] ] ) + ); + } + + public function test_advisories_carry_the_locked_version_and_whether_they_ship(): void { + $lock = $this->lock( + [ 'symfony/filesystem' => 'v5.4.54' ], + [ + 'symfony/config' => 'v5.4.46', + 'symfony/yaml' => 'v5.4.54', + 'behat/transliterator' => 'v1.5.0', + ] + ); + + $audit = Dependency_Audit::assess( 'wp-cli/wp-cli-bundle', 'main', self::SHA, true, $this->report(), $lock ); + + $this->assertSame( Dependency_Audit::VULNERABLE, $audit['status'] ); + $this->assertSame( 'wp-cli/wp-cli-bundle', $audit['repo'] ); + $this->assertSame( 'main', $audit['ref'] ); + $this->assertSame( self::SHA, $audit['sha'] ); + $this->assertTrue( $audit['dev'] ); + $this->assertSame( [], $audit['ignored'] ); + $this->assertSame( [ 'symfony/config', 'symfony/filesystem', 'symfony/yaml' ], array_column( $audit['advisories'], 'package' ) ); + $this->assertSame( [ true, false, true ], array_column( $audit['advisories'], 'dev' ) ); + + $this->assertSame( + [ + 'package' => 'symfony/yaml', + 'version' => 'v5.4.54', + 'dev' => true, + 'advisory' => 'PKSA-nktx-f1n1-b4yd', + 'cve' => 'CVE-2026-123792', + 'title' => 'Exponential and Quadratic Parse Times in the YAML Parser Let a Single Document Hold a Process', + 'link' => 'https://github.com/symfony/symfony/security/advisories/GHSA-hwfg-qwvh-2wfx', + 'affected_versions' => '>=2.0.0,<3.0.0|>=3.0.0,<4.0.0|>=4.0.0,<5.0.0|>=5.0.0,<5.1.0|>=5.1.0,<5.2.0|>=5.2.0,<5.3.0|>=5.3.0,<5.4.0|>=5.4.0,<5.4.56|>=6.0.0,<6.1.0|>=6.1.0,<6.2.0|>=6.2.0,<6.3.0|>=6.3.0,<6.4.0|>=6.4.0,<6.4.50|>=7.0.0,<7.1.0|>=7.1.0,<7.2.0|>=7.2.0,<7.3.0|>=7.3.0,<7.4.0|>=7.4.0,<7.4.23|>=8.0.0,<8.1.0|>=8.1.0,<8.1.11', + 'severity' => null, + 'reported_at' => '2026-11-04T10:45:07+00:00', + 'ignore_reason' => null, + ], + $audit['advisories'][2] + ); + + // symfony/filesystem has no CVE. + $this->assertNull( $audit['advisories'][1]['cve'] ); + } + + public function test_abandoned_packages_name_their_replacement(): void { + $report = [ + 'advisories' => [], + 'abandoned' => [ + 'behat/transliterator' => null, + 'symfony/debug' => 'symfony/error-handler', + ], + ]; + $lock = $this->lock( [], [ 'symfony/debug' => 'v4.4.44' ] ); + + $audit = Dependency_Audit::assess( 'wp-cli/wp-cli-bundle', 'v2.12.0', self::SHA, true, $report, $lock ); + + $this->assertSame( Dependency_Audit::ABANDONED, $audit['status'] ); + $this->assertSame( [], $audit['advisories'] ); + $this->assertSame( + [ + [ + 'package' => 'behat/transliterator', + 'version' => null, + 'dev' => null, + 'replacement' => null, + ], + [ + 'package' => 'symfony/debug', + 'version' => 'v4.4.44', + 'dev' => true, + 'replacement' => 'symfony/error-handler', + ], + ], + $audit['abandoned'] + ); + } + + public function test_a_report_without_findings_is_ok(): void { + // Composer encodes empty sections as JSON arrays, not objects. + $report = json_decode( '{"advisories":[],"abandoned":[]}', true ); + + $audit = Dependency_Audit::assess( 'wp-cli/wp-cli-bundle', 'main', self::SHA, true, $report, $this->lock( [ 'wp-cli/wp-cli' => 'dev-main' ] ) ); + + $this->assertSame( Dependency_Audit::OK, $audit['status'] ); + $this->assertSame( [], $audit['advisories'] ); + $this->assertSame( [], $audit['abandoned'] ); + } + + public function test_a_report_from_a_composer_without_abandoned_section_is_read(): void { + $audit = Dependency_Audit::assess( 'wp-cli/wp-cli-bundle', 'main', self::SHA, false, [ 'advisories' => [] ], $this->lock( [] ) ); + + $this->assertSame( Dependency_Audit::OK, $audit['status'] ); + } + + public function test_ignored_advisories_are_reported_but_do_not_make_the_lock_vulnerable(): void { + $report = $this->report(); + $report['ignored-advisories'] = [ 'symfony/yaml' => $report['advisories']['symfony/yaml'] ]; + $report['ignored-advisories']['symfony/yaml'][0]['ignoreReason'] = 'Only parses our own YAML.'; + $report['advisories'] = []; + $report['abandoned'] = []; + + $audit = Dependency_Audit::assess( 'wp-cli/wp-cli-bundle', 'main', self::SHA, true, $report, $this->lock( [], [ 'symfony/yaml' => 'v5.4.54' ] ) ); + + $this->assertSame( Dependency_Audit::OK, $audit['status'] ); + $this->assertSame( [ 'PKSA-nktx-f1n1-b4yd' ], array_column( $audit['ignored'], 'advisory' ) ); + $this->assertSame( 'Only parses our own YAML.', $audit['ignored'][0]['ignore_reason'] ); + $this->assertSame( 'v5.4.54', $audit['ignored'][0]['version'] ); + } + + public function test_advisories_of_one_package_are_sorted_by_id(): void { + $report = [ + 'advisories' => [ + 'composer/composer' => [ + [ 'advisoryId' => 'PKSA-zzzz' ], + [ 'advisoryId' => 'PKSA-aaaa' ], + ], + 'behat/behat' => [ + [ 'advisoryId' => 'PKSA-mmmm' ], + ], + ], + ]; + + $audit = Dependency_Audit::assess( 'wp-cli/wp-cli-bundle', 'v2.12.0', self::SHA, true, $report, $this->lock( [] ) ); + + $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/GitHubApiTest.php b/tests/GitHubApiTest.php index 0e6b94d..d28b6ff 100644 --- a/tests/GitHubApiTest.php +++ b/tests/GitHubApiTest.php @@ -62,6 +62,35 @@ public function test_writes_are_sent_as_json(): void { $this->assertEquals( (object) [ 'state' => 'closed' ], $this->sent_data[0] ); } + public function test_a_raw_file_is_fetched_at_a_ref(): void { + $lock = [ 'packages' => [ [ 'name' => 'wp-cli/wp-cli' ] ] ]; + $this->respond( 'GET', 'wp-cli/wp-cli-bundle/v2.12.0/composer.lock', $lock ); + + $contents = GitHub::get_raw_file( 'wp-cli/wp-cli-bundle', 'v2.12.0', 'composer.lock' ); + + $this->assertSame( $lock, json_decode( (string) $contents, true ) ); + $this->assertSame( [ 'GET https://raw.githubusercontent.com/wp-cli/wp-cli-bundle/v2.12.0/composer.lock' ], $this->requests ); + } + + public function test_a_missing_raw_file_is_null(): void { + $this->respond( 'GET', 'wp-cli/wp-cli/main/composer.lock', '404: Not Found', 404 ); + + $this->assertNull( GitHub::get_raw_file( 'wp-cli/wp-cli', 'main', 'composer.lock' ) ); + } + + public function test_a_failed_raw_file_request_stops_with_an_error_code(): void { + $this->respond( 'GET', 'wp-cli/wp-cli-bundle/main/composer.lock', 'Too Many Requests', 429 ); + + try { + GitHub::get_raw_file( 'wp-cli/wp-cli-bundle', 'main', 'composer.lock' ); + $this->fail( 'The request should have failed.' ); + } catch ( ExitException $exception ) { + $this->assertSame( 1, $exception->getCode() ); + } + + $this->assertStringContainsString( 'Could not fetch https://raw.githubusercontent.com/wp-cli/wp-cli-bundle/main/composer.lock (HTTP code 429)', $this->logger->stderr ); + } + public function test_writes_need_credentials(): void { $auth = new ReflectionProperty( GitHub::class, 'auth' ); if ( PHP_VERSION_ID < 80100 ) { diff --git a/tests/fixtures/composer/audit.json b/tests/fixtures/composer/audit.json new file mode 100644 index 0000000..5a10368 --- /dev/null +++ b/tests/fixtures/composer/audit.json @@ -0,0 +1,61 @@ +{ + "advisories": { + "symfony/config": [ + { + "advisoryId": "PKSA-9myg-zzdn-22rg", + "packageName": "symfony/config", + "affectedVersions": ">=2.0.0,<3.0.0|>=3.0.0,<4.0.0|>=4.0.0,<5.0.0|>=5.0.0,<5.1.0|>=5.1.0,<5.2.0|>=5.2.0,<5.3.0|>=5.3.0,<5.4.0|>=5.4.0,<5.4.56|>=6.0.0,<6.1.0|>=6.1.0,<6.2.0|>=6.2.0,<6.3.0|>=6.3.0,<6.4.0|>=6.4.0,<6.4.50|>=7.0.0,<7.1.0|>=7.1.0,<7.2.0|>=7.2.0,<7.3.0|>=7.3.0,<7.4.0|>=7.4.0,<7.4.23|>=8.0.0,<8.1.0|>=8.1.0,<8.1.11", + "title": "XXE (Local File Read) in Config XmlUtils::parse() via validateOnParse = true", + "cve": "CVE-2026-123824", + "link": "https://github.com/symfony/symfony/security/advisories/GHSA-5jff-8h8q-r5qx", + "reportedAt": "2026-11-04T10:45:07+00:00", + "sources": [ + { + "name": "FriendsOfPHP/security-advisories", + "remoteId": "symfony/config/CVE-2026-123824.yaml" + } + ], + "severity": null + } + ], + "symfony/filesystem": [ + { + "advisoryId": "PKSA-g499-cm4q-v35y", + "packageName": "symfony/filesystem", + "affectedVersions": ">=5.4.0,<5.4.56|>=6.0.0,<6.1.0|>=6.1.0,<6.2.0|>=6.2.0,<6.3.0|>=6.3.0,<6.4.0|>=6.4.0,<6.4.50|>=7.0.0,<7.1.0|>=7.1.0,<7.2.0|>=7.2.0,<7.3.0|>=7.3.0,<7.4.0|>=7.4.0,<7.4.23|>=8.0.0,<8.1.0|>=8.1.0,<8.1.11", + "title": "Filesystem Path Treats Any Prefix Before \"://\" as a Root, Letting Paths Escape Their Base Directory", + "cve": null, + "link": "https://github.com/symfony/symfony/security/advisories/GHSA-6prc-h956-hxjq", + "reportedAt": "2026-11-04T00:00:00+00:00", + "sources": [ + { + "name": "FriendsOfPHP/security-advisories", + "remoteId": "symfony/filesystem/2026-11-04-18.yaml" + } + ], + "severity": null + } + ], + "symfony/yaml": [ + { + "advisoryId": "PKSA-nktx-f1n1-b4yd", + "packageName": "symfony/yaml", + "affectedVersions": ">=2.0.0,<3.0.0|>=3.0.0,<4.0.0|>=4.0.0,<5.0.0|>=5.0.0,<5.1.0|>=5.1.0,<5.2.0|>=5.2.0,<5.3.0|>=5.3.0,<5.4.0|>=5.4.0,<5.4.56|>=6.0.0,<6.1.0|>=6.1.0,<6.2.0|>=6.2.0,<6.3.0|>=6.3.0,<6.4.0|>=6.4.0,<6.4.50|>=7.0.0,<7.1.0|>=7.1.0,<7.2.0|>=7.2.0,<7.3.0|>=7.3.0,<7.4.0|>=7.4.0,<7.4.23|>=8.0.0,<8.1.0|>=8.1.0,<8.1.11", + "title": "Exponential and Quadratic Parse Times in the YAML Parser Let a Single Document Hold a Process", + "cve": "CVE-2026-123792", + "link": "https://github.com/symfony/symfony/security/advisories/GHSA-hwfg-qwvh-2wfx", + "reportedAt": "2026-11-04T10:45:07+00:00", + "sources": [ + { + "name": "FriendsOfPHP/security-advisories", + "remoteId": "symfony/yaml/CVE-2026-123792.yaml" + } + ], + "severity": null + } + ] + }, + "abandoned": { + "behat/transliterator": null + } +} From fdc6d6e589417c4689801eaab3c9760db845859a Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 16 Nov 2026 12:59:04 +0000 Subject: [PATCH 2/2] Accept a lock with nothing to audit `composer audit` skips a lock without packages, or with only dev packages under --no-dev, and writes no report, which looked like a failure. Such a lock is now reported as ok without running Composer. Co-Authored-By: Claude Opus 5.5 --- .maintenance/src/Dependency_Audit.php | 32 +++++++++++++++++++++++++-- tests/DependenciesCommandTest.php | 11 +++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/.maintenance/src/Dependency_Audit.php b/.maintenance/src/Dependency_Audit.php index 4dcbb5c..53c3402 100644 --- a/.maintenance/src/Dependency_Audit.php +++ b/.maintenance/src/Dependency_Audit.php @@ -78,12 +78,40 @@ public static function fetch( $repo, $ref = null, $dev = true ) { WP_CLI::error( "The composer.lock of {$repo} at {$ref} is not valid JSON." ); } - $runner = self::$runner ? self::$runner : [ __CLASS__, 'run_composer_audit' ]; - $report = call_user_func( $runner, $composer_json, $lock, $dev ); + $report = self::audit_lock( $composer_json, $lock, $decoded_lock, $dev ); return self::assess( $repo, $ref, $sha, $dev, $report, $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. + */ + 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']; + } + ); + } + + if ( ! $packages ) { + return [ + 'advisories' => [], + 'abandoned' => [], + ]; + } + + $runner = self::$runner ? self::$runner : [ __CLASS__, 'run_composer_audit' ]; + + return call_user_func( $runner, $composer_json, $composer_lock, $dev ); + } + /** * Replaces the `composer audit` run, for tests. * diff --git a/tests/DependenciesCommandTest.php b/tests/DependenciesCommandTest.php index 38eaec1..c8b46a6 100644 --- a/tests/DependenciesCommandTest.php +++ b/tests/DependenciesCommandTest.php @@ -167,6 +167,17 @@ public function test_no_dev_leaves_out_the_dev_packages(): void { $this->assertSame( 'v2.12.0', $this->printed_audits()[0]['ref'] ); } + public function test_a_lock_with_nothing_to_audit_is_ok_without_asking_composer(): void { + $lock = $this->lock(); + $this->repository( 'wp-cli/dev-only', 'main', [ 'packages' => [] ] + $lock ); + $this->repository( 'wp-cli/empty', 'main', [ 'packages' => [] ] ); + + $this->assertSame( 0, $this->audit( [ 'dev-only', 'empty' ], [ 'dev' => false ] ) ); + + $this->assertSame( [], $this->composer_runs ); + $this->assertSame( [ 'ok', 'ok' ], array_column( $this->printed_audits(), 'status' ) ); + } + public function test_repositories_without_a_lock_are_skipped(): void { $this->repository( 'wp-cli/wp-cli', 'main', null ); $this->repository( 'wp-cli/wp-cli-bundle', 'main', $this->lock() );