-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dependencies): add Dependencies module for dependency alert proc…
…essing
- Loading branch information
1 parent
afdd477
commit 962b4f1
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package SecurityGate::Engine::Dependencies; | ||
|
||
use strict; | ||
use warnings; | ||
use Mojo::UserAgent; | ||
use Mojo::JSON; | ||
use Exporter 'import'; | ||
|
||
our @EXPORT_OK = qw(@SEVERITIES); | ||
our @SEVERITIES = ("critical", "high", "medium", "low"); | ||
|
||
sub new { | ||
my ($class, $token, $repository, $severity_limits) = @_; | ||
|
||
my %severity_counts = map { $_ => 0 } @SEVERITIES; | ||
|
||
my $endpoint = "https://api.github.com/repos/$repository/dependabot/alerts"; | ||
my $userAgent = Mojo::UserAgent -> new(); | ||
my $request = $userAgent -> get($endpoint, {Authorization => "Bearer $token"}) -> result(); | ||
|
||
if ($request -> code() == 200) { | ||
my $data = $request -> json(); | ||
|
||
foreach my $alert (@$data) { | ||
if ($alert -> {state} eq "open") { | ||
my $severity = $alert -> {security_vulnerability} -> {severity}; | ||
$severity_counts{$severity}++; | ||
} | ||
} | ||
|
||
print "[!] Total of security alerts:\n\n"; | ||
|
||
foreach my $severity (@SEVERITIES) { | ||
print "[-] $severity: $severity_counts{$severity}\n"; | ||
} | ||
|
||
print "\n"; | ||
|
||
my $threshold_exceeded = 0; | ||
foreach my $severity (@SEVERITIES) { | ||
if ($severity_counts{$severity} > $severity_limits -> {$severity}) { | ||
print "[+] More than $severity_limits -> {$severity} $severity security alerts found.\n"; | ||
$threshold_exceeded = 1; | ||
} | ||
} | ||
|
||
return $threshold_exceeded; | ||
} | ||
|
||
else { | ||
print "Error: Unable to fetch alerts. HTTP status code: " . $request -> code() . "\n"; | ||
return 1; | ||
} | ||
} | ||
|
||
1; |