|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * Copyright (c) 2017-2018 Tobias Reich |
| 6 | + * Copyright (c) 2018-2026 LycheeOrg. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace App\Actions\Diagnostics\Pipes\Checks; |
| 10 | + |
| 11 | +use App\Assets\Features; |
| 12 | +use App\Contracts\DiagnosticPipe; |
| 13 | +use App\DTO\DiagnosticData; |
| 14 | +use App\Models\User; |
| 15 | +use App\Services\SecurityAdvisoriesService; |
| 16 | +use Illuminate\Support\Facades\Auth; |
| 17 | + |
| 18 | +/** |
| 19 | + * Diagnostic pipe that reports known security vulnerabilities affecting the |
| 20 | + * currently installed Lychee version. |
| 21 | + * |
| 22 | + * Only runs when: |
| 23 | + * - the `vulnerability-check` feature flag is enabled, and |
| 24 | + * - the currently authenticated user is an administrator. |
| 25 | + * |
| 26 | + * Vulnerability data is never disclosed to non-admin users. |
| 27 | + */ |
| 28 | +class SecurityAdvisoriesCheck implements DiagnosticPipe |
| 29 | +{ |
| 30 | + public function __construct( |
| 31 | + private SecurityAdvisoriesService $service, |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * {@inheritDoc} |
| 37 | + */ |
| 38 | + public function handle(array &$data, \Closure $next): array |
| 39 | + { |
| 40 | + if (Features::inactive('vulnerability-check')) { |
| 41 | + return $next($data); |
| 42 | + } |
| 43 | + |
| 44 | + /** @var User|null */ |
| 45 | + $user = Auth::user(); |
| 46 | + |
| 47 | + if ($user?->may_administrate !== true) { |
| 48 | + return $next($data); |
| 49 | + } |
| 50 | + |
| 51 | + $advisories = $this->service->getMatchingAdvisories(); |
| 52 | + |
| 53 | + foreach ($advisories as $advisory) { |
| 54 | + $identifier = $advisory->cve_id ?? $advisory->ghsa_id; |
| 55 | + $score = $advisory->cvss_score !== null |
| 56 | + ? number_format($advisory->cvss_score, 1) |
| 57 | + : '(no CVSS score)'; |
| 58 | + |
| 59 | + $data[] = DiagnosticData::error( |
| 60 | + 'Security vulnerability: ' . $identifier . ' (CVSS ' . $score . ')', |
| 61 | + self::class, |
| 62 | + [$advisory->summary], |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + return $next($data); |
| 67 | + } |
| 68 | +} |
0 commit comments