Skip to content

Commit 7b42786

Browse files
schlesseraclaude
andcommitted
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 <noreply@anthropic.com>
1 parent d778382 commit 7b42786

11 files changed

Lines changed: 1091 additions & 10 deletions

File tree

‎.maintenance/bootstrap.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
WP_CLI::add_command( 'maintenance', 'WP_CLI\Maintenance\Maintenance_Namespace' );
88

99
WP_CLI::add_command( 'maintenance contrib-list', 'WP_CLI\Maintenance\Contrib_List_Command' );
10+
WP_CLI::add_command( 'maintenance dependencies', 'WP_CLI\Maintenance\Dependencies_Command' );
1011
WP_CLI::add_command( 'maintenance doctor', 'WP_CLI\Maintenance\Doctor_Command' );
1112
WP_CLI::add_command( 'maintenance milestones-after', 'WP_CLI\Maintenance\Milestones_After_Command' );
1213
WP_CLI::add_command( 'maintenance milestones-since', 'WP_CLI\Maintenance\Milestones_Since_Command' );

‎.maintenance/src/Bundle.php‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php namespace WP_CLI\Maintenance;
22

33
use WP_CLI;
4-
use WP_CLI\Utils;
54

65
/**
76
* What a wp-cli/wp-cli-bundle release ships, read from its composer.lock.
@@ -191,18 +190,17 @@ public static function find_previous_release( array $milestones, $release = null
191190
* and that is not fatal.
192191
*/
193192
public static function get_lock( $ref, $missing_is_fatal = true ) {
194-
$url = sprintf( 'https://raw.githubusercontent.com/%s/%s/composer.lock', self::REPO, $ref );
195-
$response = Utils\http_request( 'GET', $url );
193+
$lock = GitHub::get_raw_file( self::REPO, $ref, 'composer.lock' );
196194

197-
if ( 404 === (int) $response->status_code && ! $missing_is_fatal ) {
198-
return false;
199-
}
195+
if ( null === $lock ) {
196+
if ( ! $missing_is_fatal ) {
197+
return false;
198+
}
200199

201-
if ( 200 !== (int) $response->status_code ) {
202-
WP_CLI::error( sprintf( 'Could not fetch %s (HTTP code %d)', $url, $response->status_code ) );
200+
WP_CLI::error( sprintf( 'Could not fetch https://raw.githubusercontent.com/%s/%s/composer.lock (HTTP code 404)', self::REPO, $ref ) );
203201
}
204202

205-
return json_decode( $response->body, true );
203+
return json_decode( $lock, true );
206204
}
207205

208206
/**
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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

Comments
 (0)