Skip to content

Commit

Permalink
Introduce COMPOSER_AUDIT_ABANDONED env var
Browse files Browse the repository at this point in the history
  • Loading branch information
mxr576 committed Jan 12, 2024
1 parent 3491986 commit 918d14b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/06-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ Defaults to `report` in Composer 2.6, and defaults to `fail` from Composer 2.7 o
- `report` means abandoned packages are reported as an error but do not cause the command to exit with a non-zero code.
- `fail` means abandoned packages will cause audits to fail with a non-zero code.

Since Composer 2.6.7 the default and the configuration in composer.json can be overriden via the `COMPOSER_AUDIT_ABANDONED` environmant variable.

## use-parent-dir

When running Composer in a directory where there is no composer.json, if there
Expand Down
5 changes: 5 additions & 0 deletions src/Composer/Advisory/Auditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Composer\Package\PackageInterface;
use Composer\Repository\RepositorySet;
use Composer\Util\PackageInfo;
use Composer\Util\Platform;
use InvalidArgumentException;
use Symfony\Component\Console\Formatter\OutputFormatter;

Expand Down Expand Up @@ -58,6 +59,10 @@ class Auditor
*/
public function audit(IOInterface $io, RepositorySet $repoSet, array $packages, string $format, bool $warningOnly = true, array $ignoreList = [], string $abandoned = self::ABANDONED_FAIL): int
{
if (Platform::getEnv('COMPOSER_AUDIT_ABANDONED') !== FALSE) {
$abandoned = Platform::getEnv('COMPOSER_AUDIT_ABANDONED');
}

$allAdvisories = $repoSet->getMatchingSecurityAdvisories($packages, $format === self::FORMAT_SUMMARY);
// we need the CVE & remote IDs set to filter ignores correctly so if we have any matches using the optimized codepath above
// and ignores are set then we need to query again the full data to make sure it can be filtered
Expand Down
28 changes: 28 additions & 0 deletions tests/Composer/Test/Advisory/AuditorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Composer\Repository\RepositorySet;
use Composer\Test\TestCase;
use Composer\Advisory\Auditor;
use Composer\Util\Platform;
use InvalidArgumentException;

class AuditorTest extends TestCase
Expand Down Expand Up @@ -144,6 +145,29 @@ public static function auditProvider()
}
}',
];

yield 'abandoned packages reporting mode override via env var' => [
'data' => [
'packages' => [
$abandonedWithReplacement,
$abandonedNoReplacement,
],
'warningOnly' => false,
'abandoned' => Auditor::ABANDONED_FAIL,
'abandoned_via_env_var' => Auditor::ABANDONED_REPORT,
'format' => Auditor::FORMAT_TABLE,
],
'expected' => 0,
'output' => 'No security vulnerability advisories found.
Found 2 abandoned packages:
+-------------------+----------------------------------------------------------------------------------+
| Abandoned Package | Suggested Replacement |
+-------------------+----------------------------------------------------------------------------------+
| vendor/abandoned | foo/bar |
| vendor/abandoned2 | none |
+-------------------+----------------------------------------------------------------------------------+',
];

}

/**
Expand All @@ -156,9 +180,13 @@ public function testAudit(array $data, int $expected, string $output): void
$this->expectException(InvalidArgumentException::class);
}
$auditor = new Auditor();
if ($data['abandoned_via_env_var'] ?? NULL) {

Check failure on line 183 in tests/Composer/Test/Advisory/AuditorTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, false)

Only booleans are allowed in an if condition, mixed given.

Check failure on line 183 in tests/Composer/Test/Advisory/AuditorTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, true)

Only booleans are allowed in an if condition, mixed given.
Platform::putEnv('COMPOSER_AUDIT_ABANDONED', $data['abandoned_via_env_var']);
}
$result = $auditor->audit($io = new BufferIO(), $this->getRepoSet(), $data['packages'], $data['format'] ?? Auditor::FORMAT_PLAIN, $data['warningOnly'], [], $data['abandoned'] ?? Auditor::ABANDONED_IGNORE);
$this->assertSame($expected, $result);
$this->assertSame($output, trim(str_replace("\r", '', $io->getOutput())));
Platform::clearEnv('COMPOSER_AUDIT_ABANDONED');
}

public function ignoredIdsProvider(): \Generator {
Expand Down

0 comments on commit 918d14b

Please sign in to comment.