1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jorijn\LaravelSecurityChecker\Notifications; |
4
|
|
|
|
5
|
|
|
use Illuminate\Bus\Queueable; |
6
|
|
|
use Illuminate\Queue\SerializesModels; |
7
|
|
|
use Illuminate\Notifications\Notification; |
8
|
|
|
use Illuminate\Notifications\Messages\SlackMessage; |
9
|
|
|
|
10
|
|
|
class SecuritySlackNotification extends Notification |
11
|
|
|
{ |
12
|
|
|
use SerializesModels, Queueable; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $vulnerabilities; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $composerLockPath; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create a new notification instance. |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function __construct($vulnerabilities, $composerLockPath) |
32
|
|
|
{ |
33
|
|
|
$this->vulnerabilities = $vulnerabilities; |
34
|
|
|
$this->composerLockPath = $composerLockPath; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the notification's delivery channels. |
39
|
|
|
* |
40
|
|
|
* @param mixed $notifiable |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public function via($notifiable) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
return ['slack']; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the slack representation of the notification. |
50
|
|
|
* |
51
|
|
|
* @param mixed $notifiable |
52
|
|
|
* @return \Illuminate\Notifications\Messages\SlackMessage |
53
|
|
|
*/ |
54
|
|
|
public function toSlack($notifiable) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$vulnerabilities = $this->vulnerabilities; |
|
|
|
|
57
|
|
|
|
58
|
|
|
return (new SlackMessage) |
59
|
|
|
->from(config('app.url')) |
60
|
|
|
->content("*Security Check Report:* `{$this->composerLockPath}`") |
61
|
|
|
->attachment(function ($attachment) { |
62
|
|
|
$attachment->content($this->textFormatter()) |
63
|
|
|
->markdown(['pretext']); |
64
|
|
|
}); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the array representation of the notification. |
69
|
|
|
* |
70
|
|
|
* @param mixed $notifiable |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function toArray($notifiable) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
return $this->vulnerabilities; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function textFormatter() |
79
|
|
|
{ |
80
|
|
|
$count = count($this->vulnerabilities); |
81
|
|
|
|
82
|
|
|
$txt = sprintf("%d %s known vulnerabilities\n", $count, 1 === $count ? 'package has' : 'packages have'); |
83
|
|
|
|
84
|
|
|
if (0 !== $count) { |
85
|
|
|
foreach ($this->vulnerabilities as $dependency => $issues) { |
86
|
|
|
$dependencyFullName = $dependency . ' (' . $issues['version'] . ')'; |
87
|
|
|
$txt .= "\n"; |
88
|
|
|
$txt .= "*{$dependencyFullName}*" . "\n" . str_repeat('-', strlen($dependencyFullName)) . "\n"; |
89
|
|
|
|
90
|
|
|
foreach ($issues['advisories'] as $issue => $details) { |
91
|
|
|
$txt .= ' * '; |
92
|
|
|
if ($details['cve']) { |
93
|
|
|
$txt .= "{$details['cve']} "; |
94
|
|
|
} |
95
|
|
|
$txt .= "{$details['title']} "; |
96
|
|
|
|
97
|
|
|
if ('' !== $details['link']) { |
98
|
|
|
$txt .= "{$details['link']}"; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$txt .= "\n"; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
return $txt; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.