|
| 1 | +<?php namespace WP_CLI\Maintenance; |
| 2 | + |
| 3 | +use cli\Shell; |
| 4 | +use WP_CLI; |
| 5 | +use WP_CLI\Utils; |
| 6 | + |
| 7 | +/** |
| 8 | + * Checks the locked dependencies of the repositories that commit a |
| 9 | + * composer.lock, such as wp-cli/wp-cli-bundle. |
| 10 | + */ |
| 11 | +final class Dependencies_Command { |
| 12 | + |
| 13 | + /** |
| 14 | + * Checks locked dependencies for known security advisories. |
| 15 | + * |
| 16 | + * Runs `composer audit` against the composer.lock of each repository, as |
| 17 | + * committed at the given ref, without installing anything. This is the |
| 18 | + * check the Code Quality workflow runs, so a repository it reports as |
| 19 | + * vulnerable also fails there. |
| 20 | + * |
| 21 | + * Packages only required for development are marked as such, as they do |
| 22 | + * not ship in the Phar. Abandoned packages are reported too, and so are |
| 23 | + * advisories the composer.json of the repository ignores. |
| 24 | + * |
| 25 | + * Exits with an error code when any locked package is affected by an |
| 26 | + * advisory that is not ignored. Abandoned packages alone do not fail the |
| 27 | + * audit. |
| 28 | + * |
| 29 | + * Needs Composer 2.7 or later. Its global configuration and environment |
| 30 | + * variables such as COMPOSER_NO_DEV are left out of the audit. |
| 31 | + * |
| 32 | + * ## OPTIONS |
| 33 | + * |
| 34 | + * [<repo>...] |
| 35 | + * : Name(s) of the repository to audit. If no user/org was provided, |
| 36 | + * 'wp-cli' org is assumed. Defaults to wp-cli/wp-cli-bundle. Repositories |
| 37 | + * without a composer.lock are skipped with a warning. |
| 38 | + * |
| 39 | + * [--ref=<ref>] |
| 40 | + * : Branch, tag or commit to audit, such as a release tag. Defaults to the |
| 41 | + * default branch of each repository. |
| 42 | + * |
| 43 | + * [--[no-]dev] |
| 44 | + * : Whether to audit the packages that are only required for development |
| 45 | + * as well. Pass `--no-dev` to audit only what ships. |
| 46 | + * --- |
| 47 | + * default: true |
| 48 | + * --- |
| 49 | + * |
| 50 | + * [--format=<format>] |
| 51 | + * : Render output in a specific format. Defaults to a table in a terminal |
| 52 | + * and to JSON otherwise. The JSON also holds the commit that was audited, |
| 53 | + * and the affected version ranges and links of the advisories. |
| 54 | + * --- |
| 55 | + * options: |
| 56 | + * - table |
| 57 | + * - json |
| 58 | + * --- |
| 59 | + * |
| 60 | + * ## EXAMPLES |
| 61 | + * |
| 62 | + * $ wp maintenance dependencies audit |
| 63 | + * +----------------------+----------------------+---------+-----+---------------------+-----------------+--------------------------------------+ |
| 64 | + * | repo | package | version | dev | advisory | cve | title | |
| 65 | + * +----------------------+----------------------+---------+-----+---------------------+-----------------+--------------------------------------+ |
| 66 | + * | wp-cli/wp-cli-bundle | symfony/yaml | v5.4.54 | yes | PKSA-nktx-f1n1-b4yd | CVE-2026-123792 | Exponential and Quadratic Parse ... | |
| 67 | + * | wp-cli/wp-cli-bundle | behat/transliterator | v1.5.0 | yes | abandoned | | Abandoned, no replacement suggested. | |
| 68 | + * +----------------------+----------------------+---------+-----+---------------------+-----------------+--------------------------------------+ |
| 69 | + * Error: Locked packages are affected by security advisories in wp-cli/wp-cli-bundle at main. |
| 70 | + * |
| 71 | + * # Audit only what ships in the Phar. |
| 72 | + * $ wp maintenance dependencies audit --no-dev |
| 73 | + * Success: No known vulnerabilities or abandoned packages in wp-cli/wp-cli-bundle at main. |
| 74 | + * |
| 75 | + * @when before_wp_load |
| 76 | + */ |
| 77 | + public function audit( $args, $assoc_args ) { |
| 78 | + $format = Utils\get_flag_value( $assoc_args, 'format', Shell::isPiped() ? 'json' : 'table' ); |
| 79 | + $ref = Utils\get_flag_value( $assoc_args, 'ref', null ); |
| 80 | + $dev = (bool) Utils\get_flag_value( $assoc_args, 'dev', true ); |
| 81 | + $repos = $args ? array_values( array_unique( array_map( [ GitHub::class, 'qualify_repo' ], $args ) ) ) : [ Bundle::REPO ]; |
| 82 | + |
| 83 | + $audits = []; |
| 84 | + foreach ( $repos as $repo ) { |
| 85 | + $audit = Dependency_Audit::fetch( $repo, $ref, $dev ); |
| 86 | + |
| 87 | + if ( null === $audit ) { |
| 88 | + WP_CLI::warning( "{$repo} has no composer.lock" . ( $ref ? " at {$ref}" : '' ) . ', skipping it.' ); |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + $audits[] = $audit; |
| 93 | + } |
| 94 | + |
| 95 | + if ( ! $audits ) { |
| 96 | + WP_CLI::error( 'None of the repositories has a composer.lock to audit.' ); |
| 97 | + } |
| 98 | + |
| 99 | + if ( 'json' === $format ) { |
| 100 | + WP_CLI::line( (string) json_encode( $audits, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) ); |
| 101 | + } else { |
| 102 | + $this->render_table( $audits ); |
| 103 | + } |
| 104 | + |
| 105 | + $vulnerable = []; |
| 106 | + foreach ( $audits as $audit ) { |
| 107 | + if ( Dependency_Audit::VULNERABLE === $audit['status'] ) { |
| 108 | + $vulnerable[] = "{$audit['repo']} at {$audit['ref']}"; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if ( $vulnerable ) { |
| 113 | + WP_CLI::error( 'Locked packages are affected by security advisories in ' . implode( ', ', $vulnerable ) . '.' ); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private function render_table( array $audits ) { |
| 118 | + $rows = []; |
| 119 | + foreach ( $audits as $audit ) { |
| 120 | + foreach ( $audit['advisories'] as $advisory ) { |
| 121 | + $rows[] = $this->row( $audit['repo'], $advisory, $advisory['advisory'], $advisory['cve'], $advisory['title'] ); |
| 122 | + } |
| 123 | + |
| 124 | + foreach ( $audit['ignored'] as $advisory ) { |
| 125 | + $rows[] = $this->row( |
| 126 | + $audit['repo'], |
| 127 | + $advisory, |
| 128 | + $advisory['advisory'], |
| 129 | + $advisory['cve'], |
| 130 | + 'Ignored' . ( $advisory['ignore_reason'] ? " ({$advisory['ignore_reason']})" : '' ) . ": {$advisory['title']}" |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + foreach ( $audit['abandoned'] as $abandoned ) { |
| 135 | + $rows[] = $this->row( |
| 136 | + $audit['repo'], |
| 137 | + $abandoned, |
| 138 | + 'abandoned', |
| 139 | + null, |
| 140 | + $abandoned['replacement'] |
| 141 | + ? "Abandoned, use {$abandoned['replacement']} instead." |
| 142 | + : 'Abandoned, no replacement suggested.' |
| 143 | + ); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + if ( $rows ) { |
| 148 | + Utils\format_items( 'table', $rows, [ 'repo', 'package', 'version', 'dev', 'advisory', 'cve', 'title' ] ); |
| 149 | + } |
| 150 | + |
| 151 | + foreach ( $audits as $audit ) { |
| 152 | + if ( Dependency_Audit::OK === $audit['status'] ) { |
| 153 | + WP_CLI::success( "No known vulnerabilities or abandoned packages in {$audit['repo']} at {$audit['ref']}." ); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private function row( $repo, array $package, $advisory, $cve, $title ) { |
| 159 | + // A package the lock does not list may or may not ship. |
| 160 | + $dev = ''; |
| 161 | + if ( null !== $package['dev'] ) { |
| 162 | + $dev = $package['dev'] ? 'yes' : 'no'; |
| 163 | + } |
| 164 | + |
| 165 | + return [ |
| 166 | + 'repo' => $repo, |
| 167 | + 'package' => $package['package'], |
| 168 | + 'version' => $package['version'], |
| 169 | + 'dev' => $dev, |
| 170 | + 'advisory' => $advisory, |
| 171 | + 'cve' => $cve, |
| 172 | + 'title' => $title, |
| 173 | + ]; |
| 174 | + } |
| 175 | +} |
0 commit comments