| 1 | package Mogura::Server::Results; | ||||||||||||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use feature 'fc'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use DBI; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Digest::SHA; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Fcntl qw(:flock); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Mojo::File qw(path); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Mojo::JSON qw(encode_json decode_json); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Mogura::ResultStream; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Mogura::Protocol qw(random_id); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||
| 13 | 5 | has 'state'; | |||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||
| 15 | 40 | ▶ | sub revision { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
revision: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 16 | 40 | my ($class, $file) = @_; | |||||||||||||||||||||||||||||||||||||
| 17 | 40 | my @stat = stat $file; | |||||||||||||||||||||||||||||||||||||
| 18 | 40 | ▶ | die 'Result unavailable' unless @stat; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||
| 20 | # A projection change requires a fresh index even for the same source file. | ||||||||||||||||||||||||||||||||||||||
| 21 | 40 | return join '-', 'v2', @stat[1, 7, 9]; | |||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||
| 24 | 57 | ▶ | sub location { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
location: called CC=3 SCAR=12.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 25 | 57 | my ($self, $id, $kind) = @_; | |||||||||||||||||||||||||||||||||||||
| 26 | 57 | ▶ | die 'Invalid result identity' unless $id =~ /\A[0-9a-f]{32}\z/ && $kind =~ /\A(?:syft|grype)\z/; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$id =~ /\A[0-9a-f]{32}\z/u and $kind =~ /\A(?:syft|grype)\z/u
MC/DC0%0/2
$id =~ /\A[0-9a-f]{32}\z/u and $kind =~ /\A(?:syft|grype)\z/u
$id =~ /\A[0-9a-f]{32}\z/u
$kind =~ /\A(?:syft|grype)\z/u
Truth table33%1/3
A and B
A= $id =~ /\A[0-9a-f]{32}\z/uB= $kind =~ /\A(?:syft|grype)\z/u
| |||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||
| 28 | 57 | return path($self->state, 'indexes', "$id-$kind.sqlite"); | |||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||
| 31 | 53 | ▶ | sub connect { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
connect: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 32 | 53 | my ($self, $file, $readonly) = @_; | |||||||||||||||||||||||||||||||||||||
| 33 | 53 | ▶ | return DBI->connect( | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 34 | "dbi:SQLite:dbname=$file", | ||||||||||||||||||||||||||||||||||||||
| 35 | '', '', | ||||||||||||||||||||||||||||||||||||||
| 36 | { | ||||||||||||||||||||||||||||||||||||||
| 37 | RaiseError => 1, | ||||||||||||||||||||||||||||||||||||||
| 38 | PrintError => 0, | ||||||||||||||||||||||||||||||||||||||
| 39 | AutoCommit => 1, | ||||||||||||||||||||||||||||||||||||||
| 40 | sqlite_unicode => 1, | ||||||||||||||||||||||||||||||||||||||
| 41 | sqlite_open_flags => $readonly ? 1 : 6, | ||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||
| 46 | 30 | ▶ | sub available { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
available: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 47 | 30 | my ($self, $id, $kind, $revision) = @_; | |||||||||||||||||||||||||||||||||||||
| 48 | 30 | my $file = $self->location($id, $kind); | |||||||||||||||||||||||||||||||||||||
| 49 | 30 | ▶ | return $self->ready($file, $revision) ? $file : undef; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||
| 52 | 16 | ▶ | sub version { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
version: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 53 | 16 | my ($self, $file, $revision) = @_; | |||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||
| 55 | # A rebuild of unchanged JSON must invalidate cached API responses too. | ||||||||||||||||||||||||||||||||||||||
| 56 | return | ||||||||||||||||||||||||||||||||||||||
| 57 | 16 | scalar $self->connect($file, 1) | |||||||||||||||||||||||||||||||||||||
| 58 | ->selectrow_array('SELECT generation FROM metadata WHERE revision=?', undef, $revision); | ||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||
| 61 | 36 | ▶ | sub ready { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
ready: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 62 | 36 | my ($self, $file, $revision) = @_; | |||||||||||||||||||||||||||||||||||||
| 63 | 36 | ▶ | return 0 unless -f $file; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 64 | 21 | ▶ | return eval { | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
eval {
do {
my $db = $self->connect($file, 1);
my($saved) = $db->selectrow_array('SELECT revision FROM metadata');
defined $saved and $saved eq $revision
}
} || 0
MC/DC100%1/1
eval {
do {
my $db = $self->connect($file, 1);
my($saved) = $db->selectrow_array('SELECT revision FROM metadata');
defined $saved and $saved eq $revision
}
} || 0
eval {
do {
my $db = $self->connect($file, 1);
my($saved) = $db->selectrow_array('SELECT revision FROM metadata');
defined $saved and $saved eq $revision
}
}
Truth table100%2/2
A
A= eval {
do {
my $db = $self->connect($file, 1);
my($saved) = $db->selectrow_array('SELECT revision FROM metadata');
defined $saved and $saved eq $revision
}
}
| |||||||||||||||||||||||||||||||||||||||
| 65 | 21 | my $db = $self->connect($file, 1); | |||||||||||||||||||||||||||||||||||||
| 66 | 21 | my ($saved) = $db->selectrow_array('SELECT revision FROM metadata'); | |||||||||||||||||||||||||||||||||||||
| 67 | 21 | ▶ | defined($saved) && $saved eq $revision; | ||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $saved && $saved eq $revision
MC/DC50%1/2
defined $saved && $saved eq $revision
defined $saved
$saved eq $revision
Truth table66%2/3
A && B
A= defined $savedB= $saved eq $revision
| |||||||||||||||||||||||||||||||||||||||
| 68 | } || 0; | ||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||
| 71 | 1514 | ▶ | sub text { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
text: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 72 | 1514 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||
| 73 | 1514 | ▶ | return defined($value) && !ref($value) ? "$value" : ''; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $value and not ref $value
MC/DC50%1/2
defined $value and not ref $value
defined $value
not ref $value
Truth table66%2/3
A and not(B)
A= defined $valueB= not ref $value
| |||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||
| 76 | 252 | ▶ | sub row { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
row: called CC=10 SCAR=23.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 77 | 252 | my ($self, $kind, $value) = @_; | |||||||||||||||||||||||||||||||||||||
| 78 | 252 | ▶ | die 'Invalid result row' unless ref($value) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 79 | 252 | ▶ | if ($kind eq 'syft') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 80 | 1 | my @paths; | |||||||||||||||||||||||||||||||||||||
| 81 | 1 | ▶ | for my $location (@{$value->{locations} // []}) { | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$value->{"locations"} // []
MC/DC0%0/1
$value->{"locations"} // []
$value->{"locations"}
Truth table50%1/2
A
A= $value->{"locations"}
| |||||||||||||||||||||||||||||||||||||||
| 82 | 2 | my $path = text($location->{path}); | |||||||||||||||||||||||||||||||||||||
| 83 | 2 | ▶ | $path = text($location->{accessPath}) unless length $path; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 84 | 2 | ▶ | push @paths, $path if length $path; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||
| 86 | return [ | ||||||||||||||||||||||||||||||||||||||
| 87 | 5 | map { text($_) } ( | |||||||||||||||||||||||||||||||||||||
| 88 | $value->{name}, $value->{version}, $value->{type}, | ||||||||||||||||||||||||||||||||||||||
| 89 | 1 | $value->{foundBy}, join(', ', @paths) | |||||||||||||||||||||||||||||||||||||
| 90 | ) | ||||||||||||||||||||||||||||||||||||||
| 91 | ]; | ||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||
| 93 | 251 | ▶ | my $vulnerability = $value->{vulnerability} // {}; | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$value->{"vulnerability"} // {}
MC/DC100%1/1
$value->{"vulnerability"} // {}
$value->{"vulnerability"}
Truth table100%2/2
A
A= $value->{"vulnerability"}
| |||||||||||||||||||||||||||||||||||||||
| 94 | 251 | ▶ | my $artifact = $value->{artifact} // {}; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$value->{"artifact"} // {}
MC/DC0%0/1
$value->{"artifact"} // {}
$value->{"artifact"}
Truth table50%1/2
A
A= $value->{"artifact"}
| |||||||||||||||||||||||||||||||||||||||
| 95 | return [ | ||||||||||||||||||||||||||||||||||||||
| 96 | 1255 | map { text($_) } ( | |||||||||||||||||||||||||||||||||||||
| 97 | $vulnerability->{id}, | ||||||||||||||||||||||||||||||||||||||
| 98 | $artifact->{name}, | ||||||||||||||||||||||||||||||||||||||
| 99 | $artifact->{version}, | ||||||||||||||||||||||||||||||||||||||
| 100 | $vulnerability->{severity}, | ||||||||||||||||||||||||||||||||||||||
| 101 | 251 | ▶ | join(', ', grep { length } map { text($_) } @{$vulnerability->{fix}{versions} // []}) | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$vulnerability->{"fix"}{"versions"} // []
MC/DC100%1/1
$vulnerability->{"fix"}{"versions"} // []
$vulnerability->{"fix"}{"versions"}
Truth table100%2/2
A
A= $vulnerability->{"fix"}{"versions"}
| |||||||||||||||||||||||||||||||||||||||
| 102 | ) | ||||||||||||||||||||||||||||||||||||||
| 103 | ]; | ||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||
| 106 | 1004 | ▶ | sub sort_key { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
sort_key: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 107 | 1004 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||
| 108 | 1004 | $value = fc($value); | |||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||
| 110 | # Numeric segments sort by digit count, then value, without numeric overflow. | ||||||||||||||||||||||||||||||||||||||
| 111 | 1004 | $value =~ | |||||||||||||||||||||||||||||||||||||
| 112 | 1250 | s/([0-9]+)/my $n = $1; $n =~ s!^0+(?=\d)!!; sprintf("\x01%010d%s\x01", length($n), $n)/ge; | |||||||||||||||||||||||||||||||||||||
| 113 | 1004 | return $value; | |||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||
| 116 | 17 | ▶ | sub build_lock { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
build_lock: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 117 | 17 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||||||||
| 118 | 17 | $file->dirname->make_path({mode => 0750}); | |||||||||||||||||||||||||||||||||||||
| 119 | 17 | ▶ | open my $lock, '>>', $file->dirname->child('build.lock') or die "Result lock: $!"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 120 | 17 | ▶ | return unless flock($lock, LOCK_EX | LOCK_NB); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 121 | 16 | return $lock; | |||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||
| 124 | 5 | ▶ | sub save_build_status { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
save_build_status: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 125 | 5 | my ($self, $file, $status) = @_; | |||||||||||||||||||||||||||||||||||||
| 126 | 5 | my $temporary = path("$file.build.json.tmp"); | |||||||||||||||||||||||||||||||||||||
| 127 | 5 | $temporary->spurt(encode_json($status)); | |||||||||||||||||||||||||||||||||||||
| 128 | 5 | ▶ | chmod 0640, $temporary or die "Build status permissions: $!"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 129 | 5 | ▶ | rename $temporary, "$file.build.json" or die "Publish build status: $!"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||
| 132 | 20 | ▶ | sub build_status { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
build_status: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 133 | 20 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||||||||
| 134 | 20 | my $status = eval { decode_json(path("$file.build.json")->slurp) }; | |||||||||||||||||||||||||||||||||||||
| 135 | 20 | ▶ | return {state => 'idle'} unless ref($status) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 136 | 9 | ▶ | if ($status->{state} eq 'running') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||
| 138 | # A stopped Web process must not leave the button waiting forever. | ||||||||||||||||||||||||||||||||||||||
| 139 | 6 | my $lock = $self->build_lock($file); | |||||||||||||||||||||||||||||||||||||
| 140 | 6 | ▶ | return {%$status, state => 'failed'} if $lock; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||
| 142 | 3 | return $status; | |||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||
| 145 | 4 | ▶ | sub build { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
build: called CC=16 SCAR=28.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 146 | 4 | my ($self, $source, $file, $kind, $revision, %options) = @_; | |||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||
| 148 | 4 | ▶ | my $lock = $options{lock} || $self->build_lock($file); | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$options{'lock'} || $self->build_lock($file)
MC/DC0%0/2
$options{'lock'} || $self->build_lock($file)
$options{'lock'}
$self->build_lock($file)
Truth table33%1/3
A || B
A= $options{'lock'}B= $self->build_lock($file)
| |||||||||||||||||||||||||||||||||||||||
| 149 | 4 | ▶ | return 0 unless $lock; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 150 | 4 | ▶ | return 1 if !$options{force} && $self->ready($file, $revision); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $options{"force"} and $self->ready($file, $revision)
MC/DC0%0/2
not $options{"force"} and $self->ready($file, $revision)
not $options{"force"}
$self->ready($file, $revision)
Truth table66%2/3
not(A) and B
A= not $options{"force"}B= $self->ready($file, $revision)
| |||||||||||||||||||||||||||||||||||||||
| 151 | 4 | my $temporary = path("$file.part"); | |||||||||||||||||||||||||||||||||||||
| 152 | 4 | ▶ | unlink $temporary if -f $temporary; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 153 | 4 | ▶ | unlink "$temporary-journal" if -f "$temporary-journal"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 154 | 4 | my $db; | |||||||||||||||||||||||||||||||||||||
| 155 | 4 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||
| 156 | 4 | $db = $self->connect($temporary, 0); | |||||||||||||||||||||||||||||||||||||
| 157 | 4 | $db->do('PRAGMA cache_size=-8192'); | |||||||||||||||||||||||||||||||||||||
| 158 | 4 | $db->do('PRAGMA temp_store=FILE'); | |||||||||||||||||||||||||||||||||||||
| 159 | 4 | $db->do( | |||||||||||||||||||||||||||||||||||||
| 160 | 'CREATE TABLE metadata (generation TEXT NOT NULL, revision TEXT NOT NULL, total INTEGER NOT NULL, source_sha256 TEXT NOT NULL, rows_sha256 TEXT NOT NULL)' | ||||||||||||||||||||||||||||||||||||||
| 161 | ); | ||||||||||||||||||||||||||||||||||||||
| 162 | $db->do( | ||||||||||||||||||||||||||||||||||||||
| 163 | 'CREATE TABLE rows (id INTEGER PRIMARY KEY, ' | ||||||||||||||||||||||||||||||||||||||
| 164 | . join( | ||||||||||||||||||||||||||||||||||||||
| 165 | 4 | ', ', map { "c$_ TEXT NOT NULL, f$_ TEXT NOT NULL, s$_ TEXT NOT NULL" } 0 .. 4 | |||||||||||||||||||||||||||||||||||||
| 166 | ) | ||||||||||||||||||||||||||||||||||||||
| 167 | . ')' | ||||||||||||||||||||||||||||||||||||||
| 168 | ); | ||||||||||||||||||||||||||||||||||||||
| 169 | 4 | $db->begin_work; | |||||||||||||||||||||||||||||||||||||
| 170 | 4 | my $insert = $db->prepare('INSERT INTO rows VALUES (' . join(',', ('?') x 16) . ')'); | |||||||||||||||||||||||||||||||||||||
| 171 | 4 | my $total = 0; | |||||||||||||||||||||||||||||||||||||
| 172 | 4 | my $source_digest = Digest::SHA->new(256); | |||||||||||||||||||||||||||||||||||||
| 173 | 4 | my $rows_digest = Digest::SHA->new(256); | |||||||||||||||||||||||||||||||||||||
| 174 | 4 | my %severity = | |||||||||||||||||||||||||||||||||||||
| 175 | (Critical => 0, High => 1, Medium => 2, Low => 3, Negligible => 4, Unknown => 5); | ||||||||||||||||||||||||||||||||||||||
| 176 | my $envelope = Mogura::ResultStream->read( | ||||||||||||||||||||||||||||||||||||||
| 177 | $source, $kind, | ||||||||||||||||||||||||||||||||||||||
| 178 | 251 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||
| 179 | 251 | my $row = $self->row($kind, $_[0]); | |||||||||||||||||||||||||||||||||||||
| 180 | 251 | my @columns; | |||||||||||||||||||||||||||||||||||||
| 181 | 251 | for my $column (0 .. 4) { | |||||||||||||||||||||||||||||||||||||
| 182 | 1255 | my $value = $row->[$column]; | |||||||||||||||||||||||||||||||||||||
| 183 | my $sort = $kind eq 'grype' | ||||||||||||||||||||||||||||||||||||||
| 184 | 1255 | ▶ | && $column == 3 ? ($severity{$value} // 6) : sort_key($value); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition80%4/5
$kind eq "grype" and $column == 3
$severity{$value} // 6
MC/DC50%1/2
$kind eq "grype" and $column == 3
$kind eq "grype"
$column == 3
MC/DC100%1/1
$severity{$value} // 6
$severity{$value}
Truth table66%2/3
A and B
A= $kind eq "grype"B= $column == 3
Truth table100%2/2
A
A= $severity{$value}
| |||||||||||||||||||||||||||||||||||||||
| 185 | 1255 | push @columns, $value, fc($value), "$sort"; | |||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||
| 187 | 251 | $rows_digest->add(encode_json([$total, @columns]) . "\n"); | |||||||||||||||||||||||||||||||||||||
| 188 | 251 | $insert->execute($total++, @columns); | |||||||||||||||||||||||||||||||||||||
| 189 | }, | ||||||||||||||||||||||||||||||||||||||
| 190 | 4 | $source_digest, | |||||||||||||||||||||||||||||||||||||
| 191 | ); | ||||||||||||||||||||||||||||||||||||||
| 192 | 3 | ▶ | my $array = $kind eq 'syft' ? 'artifacts' : 'matches'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 193 | 3 | ▶ | die 'Invalid result array' unless ref($envelope->{$array}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 194 | 3 | for my $column (0 .. 4) { | |||||||||||||||||||||||||||||||||||||
| 195 | 15 | $db->do("CREATE INDEX sort_$column ON rows(s$column, id)"); | |||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||
| 197 | 3 | ▶ | die 'Result changed during indexing' unless __PACKAGE__->revision($source) eq $revision; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 198 | 3 | my $rows_hash = $rows_digest->hexdigest; | |||||||||||||||||||||||||||||||||||||
| 199 | 3 | $db->do('INSERT INTO metadata VALUES (?, ?, ?, ?, ?)', | |||||||||||||||||||||||||||||||||||||
| 200 | undef, random_id(), $revision, $total, $source_digest->hexdigest, $rows_hash); | ||||||||||||||||||||||||||||||||||||||
| 201 | 3 | $db->commit; | |||||||||||||||||||||||||||||||||||||
| 202 | 3 | $db->disconnect; | |||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||
| 204 | # Read back committed data through a new connection before publication. | ||||||||||||||||||||||||||||||||||||||
| 205 | 3 | $db = $self->connect($temporary, 1); | |||||||||||||||||||||||||||||||||||||
| 206 | 3 | $self->verify($db, $total, $rows_hash); | |||||||||||||||||||||||||||||||||||||
| 207 | 3 | $db->disconnect; | |||||||||||||||||||||||||||||||||||||
| 208 | 3 | undef $db; | |||||||||||||||||||||||||||||||||||||
| 209 | 3 | ▶ | chmod 0640, $temporary or die "Result permissions: $!"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 210 | 3 | ▶ | rename $temporary, $file or die "Publish result index: $!"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 211 | 3 | 1; | |||||||||||||||||||||||||||||||||||||
| 212 | }; | ||||||||||||||||||||||||||||||||||||||
| 213 | 4 | ▶ | unless ($ok) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 214 | 1 | my $error = $@; | |||||||||||||||||||||||||||||||||||||
| 215 | 1 | ▶ | if ($db) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 216 | 1 | ▶ | eval { $db->rollback unless $db->{AutoCommit} }; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 217 | 1 | $db->disconnect; | |||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||
| 219 | 1 | unlink $temporary; | |||||||||||||||||||||||||||||||||||||
| 220 | 1 | die $error; | |||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||
| 222 | 3 | return 1; | |||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||
| 225 | 6 | ▶ | sub verify { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
verify: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 226 | 6 | my ($self, $db, $expected_total, $expected_hash) = @_; | |||||||||||||||||||||||||||||||||||||
| 227 | 6 | my $checks = $db->selectcol_arrayref('PRAGMA integrity_check'); | |||||||||||||||||||||||||||||||||||||
| 228 | 6 | ▶ | die 'Result database integrity check failed' unless @$checks == 1 && $checks->[0] eq 'ok'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@$checks == 1 and $checks->[0] eq "ok"
MC/DC0%0/2
@$checks == 1 and $checks->[0] eq "ok"
@$checks == 1
$checks->[0] eq "ok"
Truth table33%1/3
A and B
A= @$checks == 1B= $checks->[0] eq "ok"
| |||||||||||||||||||||||||||||||||||||||
| 229 | 6 | my $rows = $db->prepare('SELECT * FROM rows ORDER BY id'); | |||||||||||||||||||||||||||||||||||||
| 230 | 6 | $rows->execute; | |||||||||||||||||||||||||||||||||||||
| 231 | 6 | my $digest = Digest::SHA->new(256); | |||||||||||||||||||||||||||||||||||||
| 232 | 6 | my $count = 0; | |||||||||||||||||||||||||||||||||||||
| 233 | 6 | ▶ | while (my $row = $rows->fetchrow_arrayref) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 234 | 624 | $digest->add(encode_json([0 + $row->[0], map { "$_" } @$row[1 .. $#$row]]) . "\n"); | |||||||||||||||||||||||||||||||||||||
| 235 | 624 | $count++; | |||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||
| 237 | 6 | ▶ | die 'Result row count mismatch' unless $count == $expected_total; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 238 | 5 | ▶ | die 'Result row content mismatch' unless $digest->hexdigest eq $expected_hash; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 239 | 4 | return 1; | |||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||
| 242 | 8 | ▶ | sub page { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
page: called CC=14 SCAR=26.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 243 | 8 | my ($self, $file, $revision, $request) = @_; | |||||||||||||||||||||||||||||||||||||
| 244 | 8 | my $db = $self->connect($file, 1); | |||||||||||||||||||||||||||||||||||||
| 245 | 8 | my $metadata = $db->selectrow_hashref('SELECT * FROM metadata'); | |||||||||||||||||||||||||||||||||||||
| 246 | 8 | ▶ | die 'Result version changed' unless $metadata->{revision} eq $revision; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 247 | 8 | ▶ | my $query = fc($request->{query} // ''); | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$request->{"query"} // ""
MC/DC100%1/1
$request->{"query"} // ""
$request->{"query"}
Truth table100%2/2
A
A= $request->{"query"}
| |||||||||||||||||||||||||||||||||||||||
| 248 | 8 | $query =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||||||||||||
| 249 | 8 | ▶ | my $where = length($query) ? ' WHERE ' . join(' OR ', map { "instr(f$_, ?) > 0" } 0 .. 4) : ''; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 250 | 8 | ▶ | my @bind = length($query) ? (($query) x 5) : (); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 251 | 8 | my $total = 0 + $metadata->{total}; | |||||||||||||||||||||||||||||||||||||
| 252 | 8 | ▶ | my $count = | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 253 | length($query) | ||||||||||||||||||||||||||||||||||||||
| 254 | ? $db->selectrow_array("SELECT count(*) FROM rows$where", undef, @bind) | ||||||||||||||||||||||||||||||||||||||
| 255 | : $total; | ||||||||||||||||||||||||||||||||||||||
| 256 | 8 | ▶ | my $start = 0 + ($request->{start} // 0); | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$request->{"start"} // 0
MC/DC100%1/1
$request->{"start"} // 0
$request->{"start"}
Truth table100%2/2
A
A= $request->{"start"}
| |||||||||||||||||||||||||||||||||||||||
| 257 | 8 | ▶ | $start = $count ? $count - 1 : 0 if $start >= $count; | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 258 | 8 | my $column = $request->{column}; | |||||||||||||||||||||||||||||||||||||
| 259 | my $order = | ||||||||||||||||||||||||||||||||||||||
| 260 | defined($column) | ||||||||||||||||||||||||||||||||||||||
| 261 | 8 | ▶ | ? 's' . $column . ($request->{descending} ? ' DESC' : ' ASC') . ', id' | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 262 | : 'id'; | ||||||||||||||||||||||||||||||||||||||
| 263 | my $rows = $db->selectall_arrayref( | ||||||||||||||||||||||||||||||||||||||
| 264 | "SELECT " | ||||||||||||||||||||||||||||||||||||||
| 265 | 8 | . join(',', map { "substr(c$_,1,1024)" } 0 .. 4) | |||||||||||||||||||||||||||||||||||||
| 266 | . " FROM rows$where ORDER BY $order LIMIT 60 OFFSET ?", | ||||||||||||||||||||||||||||||||||||||
| 267 | undef, @bind, $start | ||||||||||||||||||||||||||||||||||||||
| 268 | ); | ||||||||||||||||||||||||||||||||||||||
| 269 | die 'Result generation changed' | ||||||||||||||||||||||||||||||||||||||
| 270 | if defined($request->{version}) | ||||||||||||||||||||||||||||||||||||||
| 271 | 8 | ▶ | && $request->{version} ne $metadata->{generation}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $request->{"version"} and $request->{"version"} ne $metadata->{"generation"}
MC/DC0%0/2
defined $request->{"version"} and $request->{"version"} ne $metadata->{"generation"}
defined $request->{"version"}
$request->{"version"} ne $metadata->{"generation"}
Truth table33%1/3
A and B
A= defined $request->{"version"}B= $request->{"version"} ne $metadata->{"generation"}
| |||||||||||||||||||||||||||||||||||||||
| 272 | return { | ||||||||||||||||||||||||||||||||||||||
| 273 | 8 | ▶ | revision => $request->{version} // $revision, | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$request->{"version"} // $revision
MC/DC0%0/2
$request->{"version"} // $revision
$request->{"version"}
$revision
Truth table33%1/3
A // B
A= $request->{"version"}B= $revision
| |||||||||||||||||||||||||||||||||||||||
| 274 | total => $total, | ||||||||||||||||||||||||||||||||||||||
| 275 | count => 0 + $count, | ||||||||||||||||||||||||||||||||||||||
| 276 | start => $start, | ||||||||||||||||||||||||||||||||||||||
| 277 | rows => $rows | ||||||||||||||||||||||||||||||||||||||
| 278 | }; | ||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||
| 281 | 5 | 1; | |||||||||||||||||||||||||||||||||||||