lib/Mogura/Server.pm

filter: Statementstmt 92.6% 278 / 300 Branchbran 76.2% 90 / 118 Conditioncond 71.8% 74 / 103 MC/DCmcdc 50.0% 27 / 54 Subroutinesub 92.0% 46 / 50 Podpod 50.0% 1 / 2 totaltotal 82.3% 516 / 627 CC 105 SCAR 21.4
CC
105
Cov
85%
_result_page
18.0
_start_scan
15.5
_result_build_status
12.1
CRAP 8.5
« lib/Mogura/Scanner/Scan.pm ↑ Summary lib/Mogura/Server/Tags.pm »
1 package Mogura::Server;
2 4 ▶use Mojo::Base 'Mojolicious';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Mojo::IOLoop::Subprocess;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Mogura::Protocol qw(random_id valid_id);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Mogura::Server::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Mogura::Server::I18N;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use Mogura::Server::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Mogura::Server::Results;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 0 ▶has store => sub { Mogura::Server::Store->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
17 4 my $assets = path(abs_path(__FILE__))->sibling('Server');
18 16 ▶sub assets { $assets }
Subroutine assets: called CC=1 SCAR=0.0
Pod undocumented
19  
20 6 ▶sub startup {
Subroutine startup: called CC=1 SCAR=0.1
Pod documented
21 6 my ($self) = @_;
22  
23 6 $self->renderer->paths([Mogura::Server::assets()->child('templates')->to_string]);
24 6 $self->static->paths([Mogura::Server::assets()->child('public')->to_string]);
25 6 $self->store->initialize;
26 6 $self->mode('production');
27 6 $self->secrets(
28 [
29 scalar $self->store->db->selectrow_array(
30 "SELECT value FROM settings WHERE name='session'")
31 ]
32 );
33 6 $self->sessions->cookie_name('mogura_session');
34 6 $self->sessions->samesite('Strict');
35 6 $self->max_request_size(262144);
36 6 Mogura::Server::API->register($self);
37 6 Mogura::Server::I18N->register($self);
38 6 $self->_register_display_helpers;
39 6 ▶ $self->hook(before_dispatch => sub { $self->_protect_request(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
40  
41 6 my $routes = $self->routes;
42 6 ▶ $routes->get('/')->to(cb => sub { $self->_show_index(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
43 $routes->get('/inventory')->to(
44 9 ▶ cb => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
45 9 my ($controller) = @_;
46 9 $controller->res->headers->cache_control('no-store');
47 9 $self->_inventory_page($controller);
48 9 $controller->render(template => 'inventory');
49 }
50 6 );
51 6 ▶ $routes->get('/history')->to(cb => sub { $self->_show_history(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
52 6 ▶ $routes->post('/scans')->to(cb => sub { $self->_start_scan(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
53 $routes->post('/scans/:id/cancel')->to(
54 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
55 0 my ($c) = @_;
56 0 ▶ return $c->render(status => 403, text => $c->l('invalid_csrf'))
Branch: 1 branch
BranchTrueFalseTotal
if $c->validation->csrf_protect->has_error("csrf_token") 0 0 0
57 if $c->validation->csrf_protect->has_error('csrf_token');
58 0 my $id = $c->stash('id');
59 0 ▶ return $c->reply->not_found unless $self->store->cancel($id);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->store->cancel($id) 0 0 0
60 0 $c->res->code(303);
61 0 return $c->redirect_to("/scans/$id");
62 }
63 6 );
64 6 ▶ $routes->get('/scans/:id')->to(cb => sub { $self->_show_scan(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
65 6 ▶ $routes->post('/scans/:id/tags')->to(cb => sub { $self->_update_tags(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
66 6 ▶ $routes->post('/scans/:id/note')->to(cb => sub { $self->_update_note(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
67 6 ▶ $routes->get('/scans/:id/download/:kind')->to(cb => sub { $self->_download_artifact(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
68 6 ▶ $routes->get('/scans/:id/results/:kind')->to(cb => sub { $self->_result_page(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
69 6 ▶ $routes->post('/scans/:id/results/:kind/rebuild')->to(cb => sub { $self->_rebuild_result(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
70 $routes->get('/scans/:id/results/:kind/build')
71 6 ▶ ->to(cb => sub { $self->_result_build_status(@_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
72 }
73  
74 6 ▶sub _register_display_helpers {
Subroutine _register_display_helpers: called CC=1 SCAR=0.1
75 6 my ($self) = @_;
76  
77 $self->helper(
78 209 ▶ display => sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
79 209 my ($controller, $value) = @_;
80 209 ▶ return $controller->l('unknown')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and length $value 155 54 209
Condition66%4/6
defined $value and not ref $value and length $value
!ll&&!rl&&r
155054
defined $value and not ref $value
!ll&&!rl&&r
155054
MC/DC33%1/3
defined $value and not ref $value and length $value
defined $value not ref $value length $value
Truth table50%2/4
A and not(B) and C
A= defined $valueB= not ref $valueC= length $value
ABCresult
0XX0
10X0
1100
1111
81 unless defined $value && !ref($value) && length($value);
82 54 return $value;
83 }
84 6 );
85  
86 $self->helper(
87 77 ▶ label => sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
88 77 my ($controller, $value) = @_;
89 77 ▶ return $controller->l('unknown') unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 0 77 77
Condition33%1/3
defined $value and length $value
!ll&&!rl&&r
0077
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table33%1/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
90 77 return $controller->l($value);
91 }
92 6 );
93 $self->helper(
94 42 ▶ exclusion_reasons => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
95 42 my ($controller, $reasons) = @_;
96 42 return $self->_exclusion_reasons($controller, $reasons);
97 }
98 6 );
99 }
100  
101 42 ▶sub _exclusion_reasons {
Subroutine _exclusion_reasons: called CC=7 SCAR=19.5
102 42 my ($self, $controller, $reasons) = @_;
103  
104 # Several partitions can report the same disk-wide exclusion. Present each
105 # reason once while preserving the original diagnostic data in inventory.
106 42 my @labels = (
107 [
108 qr/\A(?:Mounted filesystem|Scanner storage:|Active swap|Referenced)/,
109 $controller->l('in_use')
110 ],
111 [qr/\ANo hardware serial/, $controller->l('no_identity')],
112 [
113 qr/\A(?:Ambiguous filesystem|Partition table present)/,
114 $controller->l('ambiguous_layout')
115 ],
116 [qr/\AOnly ext4/, $controller->l('unsupported_filesystem')],
117 [qr/\ABIOS boot partition:/, $controller->l('bios_boot')],
118 [qr/\ACannot resolve scanner storage:/, $controller->l('storage_unresolved')],
119 );
120 42 my %seen;
121 my @translated;
122 42 for my $reason (@$reasons) {
123 28 my $label = $reason;
124 28 for my $translation (@labels) {
125 150 ▶ next unless $reason =~ $translation->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $reason =~ /$translation->[0]/u 140 10 150
126 10 $label = $translation->[1];
127 10 last;
128 }
129  
130 # Keep the device and detected layout visible instead of collapsing
131 # distinct unsupported partitions into a single generic label.
132 28 ▶ if ($label =~ /\AUnsupported partition table/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($label =~ /\AUnsupported partition table/u) { } 2 26 28
elsif ($label =~ /\AUnsupported or unknown partition/u) { } 16 10 26
133 2 $label =~ s/\AUnsupported partition table/$controller->l('unsupported_table')/e;
134 }
135 elsif ($label =~ /\AUnsupported or unknown partition/) {
136 16 $label =~
137 16 s/\AUnsupported or unknown partition/$controller->l('unsupported_partition')/e;
138 }
139 else {
140 0 $label =~ s/\AUnsupported/$controller->l('unsupported_layout')/e;
141 }
142 28 ▶ push @translated, $label unless $seen{$label}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$label}++ 28 0 28
143 }
144 42 return \@translated;
145 }
146  
147 136 ▶sub _protect_request {
Subroutine _protect_request: called CC=1 SCAR=0.0
148 136 my ($self, $controller) = @_;
149  
150 136 my $headers = $controller->res->headers;
151 136 $headers->header(
152 'Content-Security-Policy' => join(
153 '; ',
154 "default-src 'none'",
155 "style-src 'self'",
156  
157 "script-src 'self'",
158 "worker-src 'self'",
159 "connect-src 'self'",
160 "form-action 'self'",
161 "frame-ancestors 'none'",
162 "base-uri 'none'",
163 )
164 );
165 136 $headers->header('X-Content-Type-Options' => 'nosniff');
166 136 $headers->header('Referrer-Policy' => 'no-referrer');
167 136 $headers->cache_control('no-store');
168 }
169  
170 17 ▶sub _inventory_page {
Subroutine _inventory_page: called CC=2 SCAR=7.0
171 17 my ($self, $controller) = @_;
172 17 my $page = $self->_page_number($controller->param('page'));
173 17 my $items = $self->store->inventories($page);
174 17 my $more = @$items > 4;
175 17 ▶ pop @$items if $more;
Branch: 1 branch
BranchTrueFalseTotal
if $more 0 17 17
176 17 $controller->stash(inventory => $items, page => $page, more => $more);
177 }
178  
179 8 ▶sub _show_index {
Subroutine _show_index: called CC=1 SCAR=0.0
180 8 my ($self, $controller) = @_;
181  
182 8 $self->_inventory_page($controller);
183 8 $controller->stash(scan_id => random_id());
184 8 $controller->render(template => 'index');
185 }
186  
187 5 ▶sub _show_history {
Subroutine _show_history: called CC=3 SCAR=11.0
188 5 my ($self, $controller) = @_;
189  
190 5 ▶ my $tag = substr($controller->param('tag') // '', 0, 120);
Condition100%2/2
$controller->param("tag") // ""
l!l
41
MC/DC100%1/1
$controller->param("tag") // ""
$controller->param("tag")
Truth table100%2/2
A
A= $controller->param("tag")
Aresult
11
00
191 5 my $page = $self->_page_number($controller->param('page'));
192 5 my $history = $self->store->history($tag, $page);
193 5 my $has_more = @$history > 50;
194 5 ▶ pop @$history if $has_more;
Branch: 1 branch
BranchTrueFalseTotal
if $has_more 0 5 5
195  
196 5 $controller->stash(
197 history => $history,
198 tag => $tag,
199 page => $page,
200 more => $has_more,
201 );
202 5 $controller->render(template => 'history');
203 }
204  
205 17 ▶sub _start_scan {
Subroutine _start_scan: called CC=13 SCAR=27.4
206 17 my ($self, $controller) = @_;
207  
208 17 ▶ return $controller->render(text => $controller->l('invalid_csrf'), status => 403)
Branch: 1 branch
BranchTrueFalseTotal
if $controller->validation->csrf_protect->has_error("csrf_token") 7 10 17
209 if $controller->validation->csrf_protect->has_error('csrf_token');
210 10 my @tags = @{$controller->every_param('tags')};
211 10 ▶ my $draft_tag = $controller->param('new_tag') // '';
Condition100%2/2
$controller->param("new_tag") // ""
l!l
28
MC/DC100%1/1
$controller->param("new_tag") // ""
$controller->param("new_tag")
Truth table100%2/2
A
A= $controller->param("new_tag")
Aresult
11
00
212 10 ▶ push @tags, $draft_tag if $draft_tag =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $draft_tag =~ /\S/u 2 8 10
213 my $request = {
214 id => $controller->param('scan_id'),
215 10 ▶ tags => eval { Mogura::Server::Tags->validate(\@tags) },
Condition100%2/2
$controller->param("note") // ""
l!l
28
MC/DC100%1/1
$controller->param("note") // ""
$controller->param("note")
Truth table100%2/2
A
A= $controller->param("note")
Aresult
11
00
216 note => $controller->param('note') // '',
217 token => $controller->param('target'),
218 };
219 return $controller->render(text => $controller->l('invalid_tags'), status => 400)
220 10 ▶ unless defined $request->{tags};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $request->{"tags"} 0 10 10
221 10 ▶ my ($scanner, $token) = split /:/, $request->{token} // '', 2;
Condition50%1/2
$request->{"token"} // ""
l!l
100
MC/DC0%0/1
$request->{"token"} // ""
$request->{"token"}
Truth table50%1/2
A
A= $request->{"token"}
Aresult
11
00
222 return $controller->render(text => $controller->l('invalid_input'), status => 400)
223 unless valid_id($request->{id})
224 && ($token // '') =~ /\A[a-f0-9]{64}\z/
225 && length($request->{note}) <= 2000
226 10 ▶ && index($request->{note}, "\0") < 0;
Branch: 1 branch
BranchTrueFalseTotal
unless valid_id($request->{"id"}) and ($token // "") =~ /\A[a-f0-9]{64}\z/u and length $request->{"note"} <= 2000 and index($request->{"note"}, "\0") == -1 0 10 10
Condition36%4/11
valid_id($request->{"id"}) and ($token // "") =~ /\A[a-f0-9]{64}\z/u and length $request->{"note"} <= 2000 and index($request->{"note"}, "\0") == -1
!ll&&!rl&&r
0010
valid_id($request->{"id"}) and ($token // "") =~ /\A[a-f0-9]{64}\z/u and length $request->{"note"} <= 2000
!ll&&!rl&&r
0010
valid_id($request->{"id"}) and ($token // "") =~ /\A[a-f0-9]{64}\z/u
!ll&&!rl&&r
0010
$token // ""
l!l
100
MC/DC0%0/4
valid_id($request->{"id"}) and ($token // "") =~ /\A[a-f0-9]{64}\z/u and length $request->{"note"} <= 2000 and index($request->{"note"}, "\0") == -1
valid_id($request->{"id"}) $token length $request->{"note"} <= 2000 index($request->{"note"}, "\0") == -1
Truth table20%1/5
A and B and C and D
A= valid_id($request->{"id"})B= $tokenC= length $request->{"note"} <= 2000D= index($request->{"note"}, "\0") == -1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
227 my $status =
228 10 $self->store->submit($request->{id}, $scanner, $token, $request->{tags}, $request->{note});
229 10 ▶ return $controller->render(text => $controller->l('target_unavailable'), status => 409)
Branch: 1 branch
BranchTrueFalseTotal
if $status == 409 5 5 10
230 if $status == 409;
231 5 return $self->_submission_response($controller, $request->{id}, $status);
232 }
233  
234 5 ▶sub _submission_response {
Subroutine _submission_response: called CC=4 SCAR=13.9
235 5 my ($self, $controller, $id, $status) = @_;
236 5 ▶ if (($controller->req->headers->accept // '') =~ /application\/json/) {
Branch: 1 branch
BranchTrueFalseTotal
if (($controller->req->headers->accept // "") =~ m[application/json]u) 2 3 5
Condition100%2/2
$controller->req->headers->accept // ""
l!l
23
MC/DC100%1/1
$controller->req->headers->accept // ""
$controller->req->headers->accept
Truth table100%2/2
A
A= $controller->req->headers->accept
Aresult
11
00
237 2 return $controller->render(
238 status => $status,
239 json => {
240 id => $id,
241 url => "/scans/$id",
242 next_id => random_id(),
243 }
244 );
245 }
246 3 ▶ $controller->res->code($status == 200 ? 302 : 303);
Branch: 1 branch
BranchTrueFalseTotal
$status == 200 ? : 1 2 3
247 3 return $controller->redirect_to("/scans/$id");
248 }
249  
250 23 ▶sub _show_scan {
Subroutine _show_scan: called CC=11 SCAR=24.1
251 23 my ($self, $controller) = @_;
252  
253 23 my $scan = $self->store->get($controller->param('id'));
254 23 ▶ return $controller->reply->not_found unless $scan;
Branch: 1 branch
BranchTrueFalseTotal
unless $scan 0 23 23
255  
256 my ($seen) = $self->store->db->selectrow_array(
257 'SELECT seen FROM scanners WHERE id=? AND digest IS NOT NULL',
258 23 undef, $scan->{scanner_id});
259 23 ▶ $scan->{online} = ($seen // 0) >= time - 30;
Condition100%2/2
$seen // 0
l!l
221
MC/DC100%1/1
$seen // 0
$seen
Truth table100%2/2
A
A= $seen
Aresult
11
00
260 23 ▶ my $host = ($scan->{target}{kind} // '') eq 'host';
Condition50%1/2
$scan->{"target"}{"kind"} // ""
l!l
023
MC/DC0%0/1
$scan->{"target"}{"kind"} // ""
$scan->{"target"}{"kind"}
Truth table50%1/2
A
A= $scan->{"target"}{"kind"}
Aresult
11
00
261 23 ▶ $scan->{removal} = $host ? 'system_volume' : 'do_not_disconnect';
Branch: 1 branch
BranchTrueFalseTotal
$host ? : 0 23 23
262 $scan->{removal} = 'can_disconnect'
263 23 ▶ if !$host && $scan->{online} && $self->store->can_disconnect($scan);
Branch: 1 branch
BranchTrueFalseTotal
if not $host and $scan->{"online"} and $self->store->can_disconnect($scan) 3 20 23
Condition83%5/6
not $host and $scan->{"online"} and $self->store->can_disconnect($scan)
!ll&&!rl&&r
2183
not $host and $scan->{"online"}
!ll&&!rl&&r
0221
MC/DC66%2/3
not $host and $scan->{"online"} and $self->store->can_disconnect($scan)
not $host $scan->{"online"} $self->store->can_disconnect($scan)
Truth table75%3/4
not(A) and B and C
A= not $hostB= $scan->{"online"}C= $self->store->can_disconnect($scan)
ABCresult
0XX0
10X0
1100
1111
264 23 my $artifacts = $self->_artifacts($scan->{id});
265 23 my (%versions, %result_states);
266 23 my $results = Mogura::Server::Results->new(state => $self->store->state);
267  
268 23 for my $kind (qw(syft grype)) {
269 46 ▶ next unless $artifacts->{$kind};
Branch: 1 branch
BranchTrueFalseTotal
unless $artifacts->{$kind} 30 16 46
270 16 my $revision = $results->revision($artifacts->{$kind});
271 16 my $file = $results->available($scan->{id}, $kind, $revision);
272 16 ▶ $versions{$kind} = $results->version($file, $revision) if $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file 5 11 16
273 16 $result_states{$kind} = $results->build_status($results->location($scan->{id}, $kind));
274 }
275 $controller->stash(
276 23 scan => $scan,
277 artifacts => $artifacts,
278 versions => \%versions,
279 result_states => \%result_states,
280 );
281 23 $controller->render(template => 'scan');
282 }
283  
284 14 ▶sub _result_page {
Subroutine _result_page: called CC=17 SCAR=28.9
285 14 my ($self, $controller) = @_;
286 14 ▶ my ($id, $kind) = map { $controller->param($_) // '' } qw(id kind);
Condition50%1/2
$controller->param($_) // ""
l!l
280
MC/DC0%0/1
$controller->param($_) // ""
$controller->param($_)
Truth table50%1/2
A
A= $controller->param($_)
Aresult
11
00
287 14 ▶ return $controller->reply->not_found unless $kind eq 'syft' || $kind eq 'grype';
Branch: 1 branch
BranchTrueFalseTotal
unless $kind eq "syft" or $kind eq "grype" 1 13 14
Condition100%3/3
$kind eq "syft" or $kind eq "grype"
l!l&&r!l&&!r
2111
MC/DC100%2/2
$kind eq "syft" or $kind eq "grype"
$kind eq "syft" $kind eq "grype"
Truth table100%3/3
A or B
A= $kind eq "syft"B= $kind eq "grype"
ABresult
1X1
011
000
288 13 my $source = $self->store->artifact($id, $kind);
289 13 ▶ return $controller->reply->not_found unless $source;
Branch: 1 branch
BranchTrueFalseTotal
unless $source 1 12 13
290 12 my $results = Mogura::Server::Results->new(state => $self->store->state);
291 12 my $revision = $results->revision($source);
292 12 my $file = $results->available($id, $kind, $revision);
293 12 ▶ return $controller->render(
Branch: 1 branch
BranchTrueFalseTotal
unless $file 3 9 12
294 status => 409,
295 json => {error => $controller->l('create_list_first')}
296 ) unless $file;
297 9 my $version = $results->version($file, $revision);
298 9 ▶ return $controller->render(status => 409, json => {error => $controller->l('results_changed')})
Branch: 1 branch
BranchTrueFalseTotal
unless ($controller->param("version") // "") eq $version 2 7 9
Condition50%1/2
$controller->param("version") // ""
l!l
90
MC/DC0%0/1
$controller->param("version") // ""
$controller->param("version")
Truth table50%1/2
A
A= $controller->param("version")
Aresult
11
00
299 unless ($controller->param('version') // '') eq $version;
300 7 ▶ my $request = {
Condition100%6/6
$controller->param("query") // ""
l!l
16
$controller->param("start") // 0
l!l
16
$controller->param("descending") // ""
l!l
16
MC/DC100%1/1
$controller->param("query") // ""
$controller->param("query")
MC/DC100%1/1
$controller->param("start") // 0
$controller->param("start")
MC/DC100%1/1
$controller->param("descending") // ""
$controller->param("descending")
Truth table100%2/2
A
A= $controller->param("query")
Aresult
11
00
Truth table100%2/2
A
A= $controller->param("start")
Aresult
11
00
Truth table100%2/2
A
A= $controller->param("descending")
Aresult
11
00
301 version => $version,
302 query => $controller->param('query') // '',
303 start => $controller->param('start') // '0',
304 column => $controller->param('column'),
305 descending => ($controller->param('descending') // '') eq '1',
306 };
307 return $controller->render(status => 400, json => {error => $controller->l('invalid_search')})
308 if length($request->{query}) > 500
309 || $request->{start} !~ /\A[0-9]{1,9}\z/
310 7 ▶ || (defined($request->{column}) && $request->{column} !~ /\A[0-4]\z/);
Branch: 1 branch
BranchTrueFalseTotal
if length $request->{"query"} > 500 or not $request->{"start"} =~ /\A[0-9]{1,9}\z/u or defined $request->{"column"} and not $request->{"column"} =~ /\A[0-4]\z/u 2 5 7
Condition88%8/9
length $request->{"query"} > 500 or not $request->{"start"} =~ /\A[0-9]{1,9}\z/u or defined $request->{"column"} and not $request->{"column"} =~ /\A[0-4]\z/u
l!l&&r!l&&!r
115
length $request->{"query"} > 500 or not $request->{"start"} =~ /\A[0-9]{1,9}\z/u
l!l&&r!l&&!r
016
defined $request->{"column"} and not $request->{"column"} =~ /\A[0-4]\z/u
!ll&&!rl&&r
411
MC/DC75%3/4
length $request->{"query"} > 500 or not $request->{"start"} =~ /\A[0-9]{1,9}\z/u or defined $request->{"column"} and not $request->{"column"} =~ /\A[0-4]\z/u
length $request->{"query"} > 500 not $request->{"start"} =~ /\A[0-9]{1,9}\z/u defined $request->{"column"} not $request->{"column"} =~ /\A[0-4]\z/u
Truth table80%4/5
A or not(B) or C and not(D)
A= length $request->{"query"} > 500B= not $request->{"start"} =~ /\A[0-9]{1,9}\z/uC= defined $request->{"column"}D= not $request->{"column"} =~ /\A[0-4]\z/u
ABCDresult
1XXX1
01XX1
00111
000X0
00100
311  
312 5 my $key = "$id-$kind-$revision";
313  
314 # Searches and sorting run outside the HTTP event loop, including on old
315 # scans. The browser never falls back to decoding the original artifact.
316 5 ▶ if (($self->{result_queries} // 0) >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{"result_queries"} // 0) >= 2) 0 5 5
Condition100%2/2
$self->{"result_queries"} // 0
l!l
41
MC/DC100%1/1
$self->{"result_queries"} // 0
$self->{"result_queries"}
Truth table100%2/2
A
A= $self->{"result_queries"}
Aresult
11
00
317 0 $controller->res->headers->header('Retry-After' => 1);
318 0 return $controller->render(status => 429, json => {pending => 1});
319 }
320 5 $self->{result_queries}++;
321 5 $controller->render_later;
322 Mojo::IOLoop::Subprocess->new->run(
323 0 ▶ sub { $results->page($file, $revision, $request) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
324 5 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.9
325 5 my ($process, $error, $page) = @_;
326 5 $self->{result_queries}--;
327 5 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 0 5 5
328 0 $self->log->error("Result query $key: $error");
329 0 $controller->render(
330 status => 500,
331 json => {error => $controller->l('result_load_failed')}
332 );
333 }
334 else {
335 5 $controller->render(json => $page);
336 }
337 }
338 5 );
339 }
340  
341 7 ▶sub _rebuild_result {
Subroutine _rebuild_result: called CC=10 SCAR=24.1
342 7 my ($self, $controller) = @_;
343 7 ▶ return $controller->render(status => 403, json => {error => $controller->l('invalid_csrf')})
Branch: 1 branch
BranchTrueFalseTotal
if $controller->validation->csrf_protect->has_error("csrf_token") 1 6 7
344 if $controller->validation->csrf_protect->has_error('csrf_token');
345 6 ▶ my ($id, $kind) = map { $controller->param($_) // '' } qw(id kind);
Condition50%1/2
$controller->param($_) // ""
l!l
120
MC/DC0%0/1
$controller->param($_) // ""
$controller->param($_)
Truth table50%1/2
A
A= $controller->param($_)
Aresult
11
00
346 6 ▶ return $controller->reply->not_found unless $kind eq 'syft' || $kind eq 'grype';
Branch: 1 branch
BranchTrueFalseTotal
unless $kind eq "syft" or $kind eq "grype" 0 6 6
Condition33%1/3
$kind eq "syft" or $kind eq "grype"
l!l&&r!l&&!r
060
MC/DC0%0/2
$kind eq "syft" or $kind eq "grype"
$kind eq "syft" $kind eq "grype"
Truth table33%1/3
A or B
A= $kind eq "syft"B= $kind eq "grype"
ABresult
1X1
011
000
347 6 my $scan = $self->store->get($id);
348 6 ▶ return $controller->reply->not_found unless $scan;
Branch: 1 branch
BranchTrueFalseTotal
unless $scan 0 6 6
349 return $controller->render(status => 409, json => {error => $controller->l('wait_scan')})
350 6 ▶ if $scan->{status} eq 'running' || $scan->{status} eq 'queued';
Branch: 1 branch
BranchTrueFalseTotal
if $scan->{"status"} eq "running" or $scan->{"status"} eq "queued" 1 5 6
Condition66%2/3
$scan->{"status"} eq "running" or $scan->{"status"} eq "queued"
l!l&&r!l&&!r
105
MC/DC50%1/2
$scan->{"status"} eq "running" or $scan->{"status"} eq "queued"
$scan->{"status"} eq "running" $scan->{"status"} eq "queued"
Truth table66%2/3
A or B
A= $scan->{"status"} eq "running"B= $scan->{"status"} eq "queued"
ABresult
1X1
011
000
351 5 my $source = $self->store->artifact($id, $kind);
352 5 ▶ return $controller->reply->not_found unless $source;
Branch: 1 branch
BranchTrueFalseTotal
unless $source 0 5 5
353 5 my $results = Mogura::Server::Results->new(state => $self->store->state);
354 5 my $file = $results->location($id, $kind);
355 5 my $lock = $results->build_lock($file);
356 5 ▶ return $controller->render(status => 409, json => {error => $controller->l('wait_build')})
Branch: 1 branch
BranchTrueFalseTotal
unless $lock 1 4 5
357 unless $lock;
358 4 my $revision = $results->revision($source);
359 4 my $status = {state => 'running', token => random_id()};
360 4 $results->save_build_status($file, $status);
361 4 my $process = Mojo::IOLoop::Subprocess->new;
362 4 my $key = "$id-$kind";
363 4 $self->{result_jobs}{$key} = $process;
364 $process->run(
365 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
366 0 my $ok = eval {
367 0 $results->build($source, $file, $kind, $revision, force => 1, lock => $lock);
368 };
369 0 my $error = $@;
370 0 ▶ $results->save_build_status($file, {%$status, state => $ok ? 'ready' : 'failed'});
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 0 0 0
371 0 return $error;
372 },
373 4 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
374 4 my ($process, $error, $build_error) = @_;
375 4 ▶ if ($error || $build_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error or $build_error) 2 2 4
Condition100%3/3
$error or $build_error
l!l&&r!l&&!r
112
MC/DC100%2/2
$error or $build_error
$error $build_error
Truth table100%3/3
A or B
A= $errorB= $build_error
ABresult
1X1
011
000
376 2 ▶ $self->log->error("Result rebuild $key: " . ($error || $build_error));
Condition66%2/3
$error || $build_error
l!l&&r!l&&!r
110
MC/DC0%0/2
$error || $build_error
$error $build_error
Truth table66%2/3
A || B
A= $errorB= $build_error
ABresult
1X1
011
000
377  
378 # Status writes can fail for the same reason as the build
379 # (for example a full disk). Always release the build lock.
380 2 eval { $results->save_build_status($file, {%$status, state => 'failed'}); };
381 2 ▶ $self->log->error("Result rebuild status $key: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 1 2
382 }
383 4 close $lock;
384 4 delete $self->{result_jobs}{$key};
385 }
386 4 );
387 4 return $controller->render(
388 status => 202,
389 json => {
390 status_url => "/scans/$id/results/$kind/build?token=$status->{token}",
391 }
392 );
393 }
394  
395 4 ▶sub _result_build_status {
Subroutine _result_build_status: called CC=10 SCAR=25.0
396 4 my ($self, $controller) = @_;
397 4 ▶ my ($id, $kind) = map { $controller->param($_) // '' } qw(id kind);
Condition50%1/2
$controller->param($_) // ""
l!l
80
MC/DC0%0/1
$controller->param($_) // ""
$controller->param($_)
Truth table50%1/2
A
A= $controller->param($_)
Aresult
11
00
398 4 ▶ return $controller->reply->not_found unless $kind eq 'syft' || $kind eq 'grype';
Branch: 1 branch
BranchTrueFalseTotal
unless $kind eq "syft" or $kind eq "grype" 0 4 4
Condition33%1/3
$kind eq "syft" or $kind eq "grype"
l!l&&r!l&&!r
040
MC/DC0%0/2
$kind eq "syft" or $kind eq "grype"
$kind eq "syft" $kind eq "grype"
Truth table33%1/3
A or B
A= $kind eq "syft"B= $kind eq "grype"
ABresult
1X1
011
000
399 4 ▶ return $controller->reply->not_found unless $self->store->artifact($id, $kind);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->store->artifact($id, $kind) 0 4 4
400 4 my $results = Mogura::Server::Results->new(state => $self->store->state);
401 4 my $status = $results->build_status($results->location($id, $kind));
402 return $controller->render(status => 409, json => {error => $controller->l('refresh_status')})
403 4 ▶ unless ($controller->param('token') // '') eq ($status->{token} // '');
Branch: 1 branch
BranchTrueFalseTotal
unless ($controller->param("token") // "") eq ($status->{"token"} // "") 0 4 4
Condition50%2/4
$controller->param("token") // ""
l!l
40
$status->{"token"} // ""
l!l
40
MC/DC0%0/1
$controller->param("token") // ""
$controller->param("token")
MC/DC0%0/1
$status->{"token"} // ""
$status->{"token"}
Truth table50%1/2
A
A= $controller->param("token")
Aresult
11
00
Truth table50%1/2
A
A= $status->{"token"}
Aresult
11
00
404 4 my $response = {state => $status->{state}};
405  
406 4 ▶ if ($status->{state} eq 'ready') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->{"state"} eq "ready") 2 2 4
407 2 my $source = $self->store->artifact($id, $kind);
408 2 my $revision = $results->revision($source);
409 2 my $file = $results->available($id, $kind, $revision);
410 2 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 2 0 2
411 $response->{url} =
412 2 "/scans/$id/results/$kind?version=" . $results->version($file, $revision);
413 }
414 else {
415 0 $response->{state} = 'failed';
416 }
417 }
418 4 return $controller->render(json => $response);
419 }
420  
421 23 ▶sub _artifacts {
Subroutine _artifacts: called CC=1 SCAR=0.0
422 23 my ($self, $id) = @_;
423 23 return {map { $_ => scalar($self->store->artifact($id, $_)) } qw(syft cdx grype log)};
424 }
425  
426 5 ▶sub _update_tags {
Subroutine _update_tags: called CC=11 SCAR=24.6
427 5 my ($self, $controller) = @_;
428 5 ▶ return $controller->render(text => $controller->l('invalid_csrf'), status => 403)
Branch: 1 branch
BranchTrueFalseTotal
if $controller->validation->csrf_protect->has_error("csrf_token") 1 4 5
429 if $controller->validation->csrf_protect->has_error('csrf_token');
430 4 my $scan = $self->store->get($controller->param('id'));
431 4 ▶ return $controller->reply->not_found unless $scan;
Branch: 1 branch
BranchTrueFalseTotal
unless $scan 0 4 4
432 4 ▶ my $operation = $controller->param('operation') // '';
Condition100%2/2
$controller->param("operation") // ""
l!l
31
MC/DC100%1/1
$controller->param("operation") // ""
$controller->param("operation")
Truth table100%2/2
A
A= $controller->param("operation")
Aresult
11
00
433 4 ▶ return $controller->render(text => $controller->l('invalid_tag_operation'), status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless $operation eq "add" or $operation eq "remove" 1 3 4
Condition100%3/3
$operation eq "add" or $operation eq "remove"
l!l&&r!l&&!r
211
MC/DC100%2/2
$operation eq "add" or $operation eq "remove"
$operation eq "add" $operation eq "remove"
Truth table100%3/3
A or B
A= $operation eq "add"B= $operation eq "remove"
ABresult
1X1
011
000
434 unless $operation eq 'add' || $operation eq 'remove';
435 3 my $values = eval { Mogura::Server::Tags->validate([$controller->param('tag')]); };
436 3 ▶ return $controller->render(text => $controller->l('tag_limit'), status => 400)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $values 1 2 3
437 unless defined $values;
438 2 my $tags = eval { $self->store->change_tags($scan->{id}, $operation, $values); };
439  
440 2 ▶ unless (defined $tags) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tags) 0 2 2
441 0 my $error = $@;
442 0 ▶ die $error unless $error =~ /\A(?:Invalid tag|Tag too long|Remove exactly)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $error =~ /\A(?:Invalid tag|Tag too long|Remove exactly)/u 0 0 0
443 0 return $controller->render(text => $controller->l('tag_limit'), status => 400);
444 }
445 2 ▶ return $controller->render(json => {tags => $tags})
Branch: 1 branch
BranchTrueFalseTotal
if ($controller->req->headers->accept // "") eq "application/json" 1 1 2
Condition100%2/2
$controller->req->headers->accept // ""
l!l
11
MC/DC100%1/1
$controller->req->headers->accept // ""
$controller->req->headers->accept
Truth table100%2/2
A
A= $controller->req->headers->accept
Aresult
11
00
446 if ($controller->req->headers->accept // '') eq 'application/json';
447 1 $controller->res->code(303);
448 1 $controller->redirect_to('/scans/' . $scan->{id});
449 }
450  
451 7 ▶sub _update_note {
Subroutine _update_note: called CC=8 SCAR=20.8
452 7 my ($self, $controller) = @_;
453 7 ▶ return $controller->render(text => $controller->l('invalid_csrf'), status => 403)
Branch: 1 branch
BranchTrueFalseTotal
if $controller->validation->csrf_protect->has_error("csrf_token") 1 6 7
454 if $controller->validation->csrf_protect->has_error('csrf_token');
455 6 my $scan = $self->store->get($controller->param('id'));
456 6 ▶ return $controller->reply->not_found unless $scan;
Branch: 1 branch
BranchTrueFalseTotal
unless $scan 1 5 6
457 5 ▶ my $note = $controller->param('note') // '';
Condition50%1/2
$controller->param("note") // ""
l!l
50
MC/DC0%0/1
$controller->param("note") // ""
$controller->param("note")
Truth table50%1/2
A
A= $controller->param("note")
Aresult
11
00
458 5 ▶ return $controller->render(text => $controller->l('note_limit'), status => 400)
Branch: 1 branch
BranchTrueFalseTotal
if length $note > 2000 or index($note, "\0") != -1 3 2 5
Condition66%2/3
length $note > 2000 or index($note, "\0") != -1
l!l&&r!l&&!r
302
MC/DC50%1/2
length $note > 2000 or index($note, "\0") != -1
length $note > 2000 index($note, "\0") != -1
Truth table66%2/3
A or B
A= length $note > 2000B= index($note, "\0") != -1
ABresult
1X1
011
000
459 if length($note) > 2000 || index($note, "\0") >= 0;
460 2 $self->store->db->do('UPDATE jobs SET note=? WHERE id=?', undef, $note, $scan->{id});
461 2 ▶ return $controller->render(json => {note => $note})
Branch: 1 branch
BranchTrueFalseTotal
if ($controller->req->headers->accept // "") eq "application/json" 1 1 2
Condition100%2/2
$controller->req->headers->accept // ""
l!l
11
MC/DC100%1/1
$controller->req->headers->accept // ""
$controller->req->headers->accept
Truth table100%2/2
A
A= $controller->req->headers->accept
Aresult
11
00
462 if ($controller->req->headers->accept // '') eq 'application/json';
463 1 $controller->res->code(303);
464 1 $controller->redirect_to('/scans/' . $scan->{id});
465 }
466  
467 22 ▶sub _page_number {
Subroutine _page_number: called CC=3 SCAR=11.0
468 22 my ($self, $value) = @_;
469  
470 22 ▶ return 1 unless ($value // '') =~ /\A[1-9]\d{0,5}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless ($value // "") =~ /\A[1-9]\d{0,5}\z/u 20 2 22
Condition100%2/2
$value // ""
l!l
220
MC/DC100%1/1
$value // ""
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
471 2 return $value;
472 }
473  
474 7 ▶sub _download_artifact {
Subroutine _download_artifact: called CC=5 SCAR=16.1
475 7 my ($self, $controller) = @_;
476  
477 # Store resolves a Scan ID plus an allowlisted kind; no request value is
478 # ever interpreted as a filesystem path or a download filename.
479 7 my $kind = $controller->param('kind');
480 7 ▶ if ($kind eq 'metadata') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "metadata") 4 3 7
481 4 ▶ my $scan = $self->store->get($controller->param('id'))
Branch: 1 branch
BranchTrueFalseTotal
my $scan = $self->store->get($controller->param("id")) or return $controller->reply->not_found 3 1 4
482 or return $controller->reply->not_found;
483 3 $controller->res->headers->content_disposition('attachment; filename="metadata.json"');
484 3 return $controller->render(json => $scan);
485 }
486 3 my $file = $self->store->artifact($controller->param('id'), $kind);
487 3 ▶ return $controller->reply->not_found unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 1 2 3
488  
489 2 my $headers = $controller->res->headers;
490 2 $headers->content_disposition('attachment; filename="' . $file->basename . '"');
491 2 ▶ $headers->content_type($kind eq 'log' ? 'text/plain; charset=UTF-8' : 'application/json');
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'log' ? : 0 2 2
492 2 $controller->reply->file("$file");
493 }
494  
495 4 1;
« lib/Mogura/Scanner/Scan.pm ↑ Summary lib/Mogura/Server/Tags.pm »