|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\AssetMapper\Command; |
| 13 | + |
| 14 | +use Symfony\Component\AssetMapper\ImportMap\ImportMapAuditor; |
| 15 | +use Symfony\Component\AssetMapper\ImportMap\ImportMapPackageAuditVulnerability; |
| 16 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 17 | +use Symfony\Component\Console\Command\Command; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Input\InputOption; |
| 20 | +use Symfony\Component\Console\Output\OutputInterface; |
| 21 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 22 | + |
| 23 | +#[AsCommand(name: 'importmap:audit', description: 'Checks for security vulnerability advisories for dependencies.')] |
| 24 | +class ImportMapAuditCommand extends Command |
| 25 | +{ |
| 26 | + private const SEVERITY_COLORS = [ |
| 27 | + 'critical' => 'red', |
| 28 | + 'high' => 'red', |
| 29 | + 'medium' => 'yellow', |
| 30 | + 'low' => 'default', |
| 31 | + 'unknown' => 'default', |
| 32 | + ]; |
| 33 | + |
| 34 | + private SymfonyStyle $io; |
| 35 | + |
| 36 | + public function __construct( |
| 37 | + private readonly ImportMapAuditor $importMapAuditor, |
| 38 | + ) { |
| 39 | + parent::__construct(); |
| 40 | + } |
| 41 | + |
| 42 | + protected function configure(): void |
| 43 | + { |
| 44 | + $this->addOption( |
| 45 | + name: 'format', |
| 46 | + mode: InputOption::VALUE_REQUIRED, |
| 47 | + description: sprintf('The output format ("%s")', implode(', ', $this->getAvailableFormatOptions())), |
| 48 | + default: 'txt', |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + protected function initialize(InputInterface $input, OutputInterface $output): void |
| 53 | + { |
| 54 | + $this->io = new SymfonyStyle($input, $output); |
| 55 | + } |
| 56 | + |
| 57 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 58 | + { |
| 59 | + $format = $input->getOption('format'); |
| 60 | + |
| 61 | + $audit = $this->importMapAuditor->audit(); |
| 62 | + |
| 63 | + return match ($format) { |
| 64 | + 'txt' => $this->displayTxt($audit), |
| 65 | + 'json' => $this->displayJson($audit), |
| 66 | + default => throw new \InvalidArgumentException(sprintf('Supported formats are "%s".', implode('", "', $this->getAvailableFormatOptions()))), |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + private function displayTxt(array $audit): int |
| 71 | + { |
| 72 | + $rows = []; |
| 73 | + |
| 74 | + $packagesWithoutVersion = []; |
| 75 | + $vulnerabilitiesCount = array_map(fn() => 0, self::SEVERITY_COLORS); |
| 76 | + foreach ($audit as $packageAudit) { |
| 77 | + if (!$packageAudit->version) { |
| 78 | + $packagesWithoutVersion[] = $packageAudit->package; |
| 79 | + } |
| 80 | + foreach($packageAudit->vulnerabilities as $vulnerability) { |
| 81 | + $rows[] = [ |
| 82 | + sprintf('<fg=%s>%s</>', self::SEVERITY_COLORS[$vulnerability->severity] ?? 'default', ucfirst($vulnerability->severity)), |
| 83 | + $vulnerability->summary, |
| 84 | + $packageAudit->package, |
| 85 | + $packageAudit->version ?? 'n/a', |
| 86 | + $vulnerability->firstPatchedVersion ?? 'n/a', |
| 87 | + $vulnerability->url, |
| 88 | + ]; |
| 89 | + ++$vulnerabilitiesCount[$vulnerability->severity]; |
| 90 | + } |
| 91 | + } |
| 92 | + $packagesCount = count($audit); |
| 93 | + $packagesWithoutVersionCount = count($packagesWithoutVersion); |
| 94 | + |
| 95 | + if ([] === $rows && 0 === $packagesWithoutVersionCount) { |
| 96 | + $this->io->info('No vulnerabilities found.'); |
| 97 | + |
| 98 | + return self::SUCCESS; |
| 99 | + } |
| 100 | + |
| 101 | + if ([] !== $rows) { |
| 102 | + $table = $this->io->createTable(); |
| 103 | + $table->setHeaders([ |
| 104 | + 'Severity', |
| 105 | + 'Title', |
| 106 | + 'Package', |
| 107 | + 'Version', |
| 108 | + 'Patched in', |
| 109 | + 'More info', |
| 110 | + ]); |
| 111 | + $table->addRows($rows); |
| 112 | + $table->render(); |
| 113 | + $this->io->newLine(); |
| 114 | + } |
| 115 | + |
| 116 | + $this->io->text(sprintf('%d package%s found: %d audited / %d skipped', |
| 117 | + $packagesCount, |
| 118 | + 1 === $packagesCount ? '' : 's', |
| 119 | + $packagesCount - $packagesWithoutVersionCount, |
| 120 | + $packagesWithoutVersionCount, |
| 121 | + )); |
| 122 | + |
| 123 | + if (0 < $packagesWithoutVersionCount) { |
| 124 | + $this->io->warning(sprintf('Unable to retrieve versions for package%s: %s', |
| 125 | + 1 === $packagesWithoutVersionCount ? '' : 's', |
| 126 | + implode(', ', $packagesWithoutVersion) |
| 127 | + )); |
| 128 | + } |
| 129 | + |
| 130 | + if ([] !== $rows) { |
| 131 | + $vulnerabilityCount = 0; |
| 132 | + $vulnerabilitySummary = []; |
| 133 | + foreach ($vulnerabilitiesCount as $severity => $count) { |
| 134 | + if (0 === $count) { |
| 135 | + continue; |
| 136 | + } |
| 137 | + $vulnerabilitySummary[] = sprintf( '%d %s', $count, ucfirst($severity)); |
| 138 | + $vulnerabilityCount += $count; |
| 139 | + } |
| 140 | + $this->io->text(sprintf('%d vulnerabilit%s found: %s', |
| 141 | + $vulnerabilityCount, |
| 142 | + 1 === $vulnerabilityCount ? 'y' : 'ies', |
| 143 | + implode(' / ', $vulnerabilitySummary), |
| 144 | + )); |
| 145 | + } |
| 146 | + |
| 147 | + return self::FAILURE; |
| 148 | + } |
| 149 | + |
| 150 | + private function displayJson(array $audit): int |
| 151 | + { |
| 152 | + $vulnerabilitiesCount = array_map(fn() => 0, self::SEVERITY_COLORS); |
| 153 | + |
| 154 | + $json = [ |
| 155 | + 'packages' => [], |
| 156 | + 'summary' => $vulnerabilitiesCount, |
| 157 | + ]; |
| 158 | + |
| 159 | + foreach ($audit as $packageAudit) { |
| 160 | + $json['packages'][] = [ |
| 161 | + 'package' => $packageAudit->package, |
| 162 | + 'version' => $packageAudit->version, |
| 163 | + 'vulnerabilities' => array_map(fn (ImportMapPackageAuditVulnerability $v) => [ |
| 164 | + 'ghsa_id' => $v->ghsaId, |
| 165 | + 'cve_id' => $v->cveId, |
| 166 | + 'url' => $v->url, |
| 167 | + 'summary' => $v->summary, |
| 168 | + 'severity' => $v->severity, |
| 169 | + 'vulnerable_version_range' => $v->vulnerableVersionRange, |
| 170 | + 'first_patched_version' => $v->firstPatchedVersion, |
| 171 | + ], $packageAudit->vulnerabilities), |
| 172 | + ]; |
| 173 | + foreach ($packageAudit->vulnerabilities as $vulnerability) { |
| 174 | + ++$json['summary'][$vulnerability->severity]; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + $this->io->write(json_encode($json)); |
| 179 | + |
| 180 | + return 0 < array_sum($json['summary']) ? self::FAILURE : self::SUCCESS; |
| 181 | + } |
| 182 | + |
| 183 | + private function getAvailableFormatOptions(): array |
| 184 | + { |
| 185 | + return ['txt', 'json']; |
| 186 | + } |
| 187 | +} |
0 commit comments