1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jorijn\LaravelSecurityChecker\Console; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Facades\Notification; |
7
|
|
|
use Jorijn\LaravelSecurityChecker\Notifications\SecuritySlackNotification; |
8
|
|
|
use SensioLabs\Security\SecurityChecker; |
9
|
|
|
|
10
|
|
|
class SecuritySlackCommand extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $name = 'security-check:slack'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $description = 'Send vulnerabilities to a Slack channel.'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var SecurityChecker |
24
|
|
|
*/ |
25
|
|
|
protected $checker; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* SecurityCommand constructor. |
29
|
|
|
* |
30
|
|
|
* @param SecurityChecker $checker |
31
|
|
|
*/ |
32
|
|
|
public function __construct(SecurityChecker $checker) |
33
|
|
|
{ |
34
|
|
|
parent::__construct(); |
35
|
|
|
|
36
|
|
|
$this->checker = $checker; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Execute the command |
41
|
|
|
*/ |
42
|
|
|
public function handle() |
43
|
|
|
{ |
44
|
|
|
// require that the user specifies a slack channel in the .env file |
45
|
|
|
if (!config('laravel-security-checker.slack_webhook_url')) { |
46
|
|
|
throw new \Exception('No Slack Webhook has been specified.'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// get the path to composer.lock |
50
|
|
|
$composerLock = base_path('composer.lock'); |
51
|
|
|
|
52
|
|
|
// and feed it into the SecurityChecker |
53
|
|
|
$vulnerabilities = $this->checker->check($composerLock); |
54
|
|
|
|
55
|
|
|
// cancel execution here if user does not want to be notified when there are 0 vulns. |
56
|
|
|
$proceed = config('laravel-security-checker.email_even_without_vulnerabilities', false); |
57
|
|
|
if (count($vulnerabilities) === 0 && $proceed !== true) { |
58
|
|
|
return 0; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
Notification::route('slack', config('laravel-security-checker.slack_webhook_url', null)) |
62
|
|
|
->notify(new SecuritySlackNotification($vulnerabilities, realpath($composerLock))); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|