Skip to content

Commit 463be56

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

4 files changed

Lines changed: 345 additions & 7 deletions

File tree

‎.maintenance/src/Dependencies_Command.php‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ public function audit( $args, $assoc_args ) {
131131
*
132132
* Without --packages, exits with an error code when advisories are left
133133
* that the update cannot fix, as fixing them needs a change to
134-
* composer.json.
134+
* composer.json. For each of them, it says which version fixes it, and
135+
* what `composer why-not` says holds that version back, both here and in
136+
* the pull request.
135137
*
136138
* Needs Composer 2.7 or later. Works from the files on GitHub, so no clone
137139
* of the repository is needed.
@@ -269,7 +271,22 @@ static function ( $change ) {
269271

270272
private function report_remaining( array $plan ) {
271273
foreach ( $plan['remaining'] as $advisory ) {
272-
WP_CLI::warning( "{$advisory['package']} {$advisory['version']} is still affected by {$advisory['advisory']}: {$advisory['title']}" );
274+
$message = "{$advisory['package']} {$advisory['version']} is still affected by {$advisory['advisory']}: {$advisory['title']}";
275+
276+
if ( ! empty( $advisory['fixed_in'] ) ) {
277+
$message .= "\nIt is fixed in {$advisory['fixed_in']}";
278+
279+
if ( ! empty( $advisory['held_back_by'] ) ) {
280+
$message .= ', which is held back by:';
281+
foreach ( $advisory['held_back_by'] as $reason ) {
282+
$message .= "\n {$reason}";
283+
}
284+
} else {
285+
$message .= '.';
286+
}
287+
}
288+
289+
WP_CLI::warning( $message );
273290
}
274291
}
275292

‎.maintenance/src/Dependency_Update.php‎

Lines changed: 224 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ final class Dependency_Update {
3434
*/
3535
private static $runner;
3636

37+
/**
38+
* Runs `composer why-not`, or null for the real one. Tests swap it out.
39+
*
40+
* @var callable|null
41+
*/
42+
private static $why_not_runner;
43+
3744
/**
3845
* Replaces the `composer update` run, for tests.
3946
*
@@ -46,6 +53,19 @@ public static function set_runner( $runner ) {
4653
self::$runner = $runner;
4754
}
4855

56+
/**
57+
* Replaces the `composer why-not` run, for tests.
58+
*
59+
* @param callable|null $runner Receives the contents of composer.json and
60+
* composer.lock, a package, a version and the
61+
* branch, and returns what holds that version
62+
* back, as for get_held_back_by(). Null
63+
* restores the real run.
64+
*/
65+
public static function set_why_not_runner( $runner ) {
66+
self::$why_not_runner = $runner;
67+
}
68+
4969
/**
5070
* Works out what an update of the default branch of a repository changes.
5171
*
@@ -59,7 +79,10 @@ public static function set_runner( $runner ) {
5979
* `branch` is the default branch and `head` the branch for the
6080
* pull request. `unmatched` lists the given packages that match no
6181
* locked package. `composer_lock` holds the new lock, or null when
62-
* the update changes no locked version.
82+
* the update changes no locked version. Each of the `remaining`
83+
* advisories also has a `fixed_in` version, the first one that
84+
* fixes it, and `held_back_by`, what `composer why-not` says keeps
85+
* that version out, see explain().
6386
*/
6487
public static function prepare( $repo, array $packages = [] ) {
6588
$files = Dependency_Audit::fetch_files( $repo );
@@ -68,6 +91,165 @@ public static function prepare( $repo, array $packages = [] ) {
6891
return null;
6992
}
7093

94+
$plan = self::plan( $repo, $packages, $files );
95+
96+
$plan['remaining'] = self::explain(
97+
$plan['remaining'],
98+
$files['composer_json'],
99+
null === $plan['composer_lock'] ? $files['composer_lock'] : $plan['composer_lock'],
100+
$files['ref']
101+
);
102+
103+
return $plan;
104+
}
105+
106+
/**
107+
* Adds to each advisory the first version that fixes it, and what holds
108+
* that version back.
109+
*
110+
* @param array $advisories Advisories, see Dependency_Audit.
111+
* @param string $composer_json Contents of composer.json.
112+
* @param string $composer_lock Contents of the composer.lock they apply to.
113+
* @param string $branch Branch the files are from.
114+
*
115+
* @return array The advisories, each with `fixed_in`, null when no fixed
116+
* version is known, and `held_back_by`, the reasons
117+
* `composer why-not` gives, an empty list when it finds none,
118+
* or null when it was not asked.
119+
*/
120+
public static function explain( array $advisories, $composer_json, $composer_lock, $branch ) {
121+
$runner = self::$why_not_runner ? self::$why_not_runner : [ __CLASS__, 'get_held_back_by' ];
122+
$reasons = [];
123+
124+
foreach ( $advisories as $index => $advisory ) {
125+
$fixed_in = self::get_first_fixed_version( isset( $advisory['affected_versions'] ) ? $advisory['affected_versions'] : null, $advisory['version'] );
126+
$held_back_by = null;
127+
128+
if ( null !== $fixed_in ) {
129+
$key = "{$advisory['package']} {$fixed_in}";
130+
131+
if ( ! array_key_exists( $key, $reasons ) ) {
132+
$reasons[ $key ] = call_user_func( $runner, $composer_json, $composer_lock, $advisory['package'], $fixed_in, $branch );
133+
}
134+
135+
$held_back_by = $reasons[ $key ];
136+
}
137+
138+
$advisories[ $index ]['fixed_in'] = $fixed_in;
139+
$advisories[ $index ]['held_back_by'] = $held_back_by;
140+
}
141+
142+
return $advisories;
143+
}
144+
145+
/**
146+
* Finds the lowest version above a locked one that none of the affected
147+
* ranges of an advisory covers. Advisories often list one range per
148+
* minor version, as in `>=5.3.0,<5.4.0|>=5.4.0,<5.4.56`, so the upper
149+
* bound of one range can be the start of the next.
150+
*
151+
* @param string|null $affected Affected versions, as Composer reports them.
152+
* @param string|null $version Locked version.
153+
*
154+
* @return string|null The version, or null when none is known to be fixed
155+
* or the ranges cannot be read.
156+
*/
157+
public static function get_first_fixed_version( $affected, $version ) {
158+
if ( ! is_string( $affected ) || ! is_string( $version ) || ! preg_match( '/^v?(\d[\w.\-]*)$/i', $version, $match ) ) {
159+
return null;
160+
}
161+
162+
$ranges = [];
163+
foreach ( explode( '|', $affected ) as $constraint ) {
164+
$range = [
165+
'lower' => null,
166+
'upper' => null,
167+
];
168+
169+
foreach ( preg_split( '/[\s,]+/', trim( $constraint ), -1, PREG_SPLIT_NO_EMPTY ) as $part ) {
170+
if ( ! preg_match( '/^(>=|>|<=|<|==|=)?v?(\d[\w.\-]*)$/i', $part, $bound ) ) {
171+
return null;
172+
}
173+
174+
if ( '' === $bound[1] || '=' === $bound[1][0] ) {
175+
$range['lower'] = [ $bound[2], true ];
176+
$range['upper'] = [ $bound[2], true ];
177+
} elseif ( '>' === $bound[1][0] ) {
178+
$range['lower'] = [ $bound[2], '>=' === $bound[1] ];
179+
} else {
180+
$range['upper'] = [ $bound[2], '<=' === $bound[1] ];
181+
}
182+
}
183+
184+
$ranges[] = $range;
185+
}
186+
187+
$candidate = $match[1];
188+
$passes = count( $ranges ) + 1;
189+
190+
// Each pass but the last moves past a range, so this many will do.
191+
for ( $pass = 0; $pass < $passes; $pass++ ) {
192+
$containing = null;
193+
foreach ( $ranges as $range ) {
194+
if ( self::in_range( $candidate, $range ) ) {
195+
$containing = $range;
196+
break;
197+
}
198+
}
199+
200+
if ( null === $containing ) {
201+
return $candidate === $match[1] ? null : $candidate;
202+
}
203+
204+
if ( null === $containing['upper'] || $containing['upper'][1] ) {
205+
return null;
206+
}
207+
208+
$candidate = $containing['upper'][0];
209+
}
210+
211+
return null;
212+
}
213+
214+
/**
215+
* Asks `composer why-not` what keeps a version of a package from being
216+
* locked.
217+
*
218+
* @param string $composer_json Contents of composer.json.
219+
* @param string $composer_lock Contents of composer.lock.
220+
* @param string $package Package name.
221+
* @param string $version Version that should be locked.
222+
* @param string $branch Branch the files are from.
223+
*
224+
* @return string[]|null The reasons, one per line, such as
225+
* `symfony/yaml v6.4.50 requires php (>=8.1 but 7.2.24 is installed)`;
226+
* none when it finds nothing, and null when it fails.
227+
*/
228+
public static function get_held_back_by( $composer_json, $composer_lock, $package, $version, $branch ) {
229+
$result = Composer::run(
230+
[ 'why-not', $package, $version, '--locked' ],
231+
[
232+
'composer.json' => $composer_json,
233+
'composer.lock' => $composer_lock,
234+
],
235+
[],
236+
[ 'COMPOSER_ROOT_VERSION' => "dev-{$branch}" ]
237+
);
238+
239+
// It exits with 1 when it finds something that holds the version back.
240+
if ( 0 === $result['return_code'] ) {
241+
return [];
242+
}
243+
244+
if ( 1 !== $result['return_code'] ) {
245+
WP_CLI::debug( "`composer why-not {$package} {$version}` failed (exit code {$result['return_code']}).", 'composer' );
246+
return null;
247+
}
248+
249+
return array_values( array_filter( array_map( 'trim', explode( "\n", $result['stdout'] ) ), 'strlen' ) );
250+
}
251+
252+
private static function plan( $repo, array $packages, array $files ) {
71253
$before = self::audit( $repo, $files, $files['composer_lock'], $files['lock'] );
72254

73255
$plan = [
@@ -307,6 +489,10 @@ public static function describe_pull_request( array $plan ) {
307489
$lines[] = '';
308490
foreach ( $plan['remaining'] as $advisory ) {
309491
$lines[] = self::describe_advisory( $advisory );
492+
493+
foreach ( self::describe_held_back_by( $advisory ) as $line ) {
494+
$lines[] = '' === $line ? '' : " {$line}";
495+
}
310496
}
311497
}
312498

@@ -487,6 +673,43 @@ private static function describe_version( array $package, $show_reference ) {
487673
return sprintf( '%s (%s)', $package['version'], substr( $package['reference'], 0, 7 ) );
488674
}
489675

676+
/**
677+
* Says what keeps the fix of an advisory out of the lock, as Markdown.
678+
*
679+
* @param array $advisory Advisory, see explain().
680+
*
681+
* @return string[] Lines.
682+
*/
683+
private static function describe_held_back_by( array $advisory ) {
684+
if ( empty( $advisory['fixed_in'] ) ) {
685+
return [];
686+
}
687+
688+
$fixed_in = $advisory['fixed_in'];
689+
690+
if ( null === $advisory['held_back_by'] ) {
691+
return [ "Fixed in {$fixed_in}." ];
692+
}
693+
694+
if ( ! $advisory['held_back_by'] ) {
695+
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." ];
696+
}
697+
698+
return array_merge(
699+
[ "Fixed in {$fixed_in}, which `composer why-not` says is held back by:", '', '```' ],
700+
$advisory['held_back_by'],
701+
[ '```' ]
702+
);
703+
}
704+
705+
private static function in_range( $version, array $range ) {
706+
if ( null !== $range['lower'] && ! version_compare( $version, $range['lower'][0], $range['lower'][1] ? '>=' : '>' ) ) {
707+
return false;
708+
}
709+
710+
return null === $range['upper'] || version_compare( $version, $range['upper'][0], $range['upper'][1] ? '<=' : '<' );
711+
}
712+
490713
private static function describe_advisory( array $advisory ) {
491714
$id = $advisory['link'] ? "[{$advisory['advisory']}]({$advisory['link']})" : $advisory['advisory'];
492715

‎tests/DependenciesUpdateCommandTest.php‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ class DependenciesUpdateCommandTest extends GitHub_TestCase {
3535
*/
3636
private $plugin_api = '2.9.0';
3737

38+
/**
39+
* Calls to the fake `composer why-not`: composer.lock, package, version.
40+
*
41+
* @var array<int, array{0: string, 1: string, 2: string}>
42+
*/
43+
private $why_nots = [];
44+
3845
/**
3946
* What the command printed to STDOUT.
4047
*
@@ -54,10 +61,11 @@ static function ( $composer_json, $composer_lock ) {
5461
'advisories' => $affected ? [
5562
'symfony/yaml' => [
5663
[
57-
'advisoryId' => 'PKSA-nktx-f1n1-b4yd',
58-
'cve' => 'CVE-2026-123792',
59-
'title' => 'Exponential and Quadratic Parse Times in the YAML Parser',
60-
'link' => 'https://github.com/symfony/symfony/security/advisories/GHSA-hwfg-qwvh-2wfx',
64+
'advisoryId' => 'PKSA-nktx-f1n1-b4yd',
65+
'cve' => 'CVE-2026-123792',
66+
'title' => 'Exponential and Quadratic Parse Times in the YAML Parser',
67+
'link' => 'https://github.com/symfony/symfony/security/advisories/GHSA-hwfg-qwvh-2wfx',
68+
'affectedVersions' => '>=5.0.0,<5.4.0|>=5.4.0,<5.4.56|>=6.0.0,<6.4.50',
6169
],
6270
],
6371
] : [],
@@ -73,11 +81,20 @@ function ( $composer_json, $composer_lock, array $packages, $branch ) {
7381
return $this->unfixable ? $composer_lock : $this->lock_file( 'v5.4.56', $this->plugin_api );
7482
}
7583
);
84+
85+
Dependency_Update::set_why_not_runner(
86+
function ( $composer_json, $composer_lock, $package, $version ) {
87+
$this->why_nots[] = [ $composer_lock, $package, $version ];
88+
89+
return [ "wp-cli/wp-cli-bundle dev-main requires {$package} (5.4.54)" ];
90+
}
91+
);
7692
}
7793

7894
protected function tear_down(): void {
7995
Dependency_Audit::set_runner( null );
8096
Dependency_Update::set_runner( null );
97+
Dependency_Update::set_why_not_runner( null );
8198

8299
parent::tear_down();
83100
}
@@ -352,7 +369,19 @@ public function test_advisories_the_update_cannot_fix_fail_the_run(): void {
352369

353370
$this->assertStringContainsString( 'Updating symfony/yaml changes no locked version of wp-cli/wp-cli-bundle.', $this->logger->stderr );
354371
$this->assertStringContainsString( 'symfony/yaml v5.4.54 is still affected by PKSA-nktx-f1n1-b4yd', $this->logger->stderr );
372+
$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 );
355373
$this->assertStringContainsString( 'Fixing them needs a change to composer.json.', $this->logger->stderr );
374+
$this->assertCount( 1, $this->why_nots );
375+
$this->assertSame( $this->lock( 'v5.4.54' ), json_decode( $this->why_nots[0][0], true ) );
376+
$this->assertSame( [ 'symfony/yaml', '5.4.56' ], array_slice( $this->why_nots[0], 1 ) );
377+
}
378+
379+
public function test_fixed_advisories_are_not_explained(): void {
380+
$this->bundle();
381+
382+
$this->assertSame( 0, $this->update( [], [ 'dry-run' => true ] ) );
383+
384+
$this->assertSame( [], $this->why_nots );
356385
}
357386

358387
public function test_given_packages_are_updated_instead_and_may_leave_advisories(): void {

0 commit comments

Comments
 (0)