blib/lib/Mogura/Scanner/Config.pm

filter: Statementstmt 97.5% 118 / 121 Branchbran 81.6% 49 / 60 Conditioncond 59.7% 58 / 97 MC/DCmcdc 35.5% 16 / 45 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 2 totaltotal 75.5% 259 / 343 CC 69 SCAR 27.9
CC
69
Cov
81%
_validate
26.7
trusted_path
26.1
_server_url
18.4
CRAP 16.4
« lib/Mogura/Server/API.pm ↑ Summary lib/Mogura/Scanner/Disk.pm »
1 package Mogura::Scanner::Config;
2 6 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 6 ▶use B qw(svref_2object SVp_IOK);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Encode qw(decode FB_CROAK);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Fcntl qw(O_RDONLY O_NOFOLLOW O_NONBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 our @EXPORT_OK = qw(trusted_path);
14 6 has [qw(server token_file)];
15 6 has state => '/var/lib/mogura-scanner';
16 6 has runtime => '/run/mogura-scanner';
17 6 has syft => '/usr/local/bin/syft';
18 6 has grype => '/usr/local/bin/grype';
19 6 has command_timeout => 3600;
20 6 has upload_timeout => 300;
21 2 ▶has exclude_paths => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
22 6 has jobs => 2;
23 6 has inventory_interval => 10;
24 6 has spool_limit => 8 * 1024**3;
25  
26 33 ▶sub from_options {
Subroutine from_options: called CC=6 SCAR=17.9
Pod undocumented
27 33 my ($class, $options) = @_;
28 33 my %cli = %$options;
29 33 my $file = delete $cli{config};
30 33 ▶ my $settings = defined($file) ? _read_yaml($file) : {};
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 30 3 33
31 6 ▶ $cli{exclude_paths} = delete $cli{'exclude-path'} if exists $cli{'exclude-path'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $cli{"exclude-path"} 1 5 6
32 6 %cli = map { (tr/-/_/r => $cli{$_}) } keys %cli;
33 6 _validate(\%cli, 'command line');
34 6 my $config = $class->new(%$settings, %cli);
35  
36 6 for my $key (qw(server token_file)) {
37 11 ▶ die(($file // 'command line') . ": missing $key\n") unless defined $config->$key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config->$key 1 10 11
Condition50%1/2
$file // "command line"
l!l
10
MC/DC0%0/1
$file // "command line"
$file
Truth table50%1/2
A
A= $file
Aresult
11
00
38 }
39 5 return $config;
40 }
41  
42 3 ▶sub _open_config {
Subroutine _open_config: called CC=8 SCAR=23.7
43 3 my ($file) = @_;
44 3 ▶ die "$file: config requires an absolute path without dot components\n" unless _absolute($file);
Branch: 1 branch
BranchTrueFalseTotal
unless _absolute($file) 1 2 3
45 2 ▶ eval { trusted_path(path($file)->dirname, 1); 1 } or die "$file: $@";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { trusted_path(path($file)->dirname, 1); 1 } } or die "${file}: $@" 1 1 2
46 1 ▶ sysopen my $fh, $file, O_RDONLY | O_NOFOLLOW | O_NONBLOCK or die "$file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $file, 133120 or die "${file}: $!\n" 1 0 1
47 1 my @st = stat $fh;
48 1 ▶ die "$file: config must be a regular file owned by root without group or other writes\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @st and -f $fh and $st[4] == 0 and not $st[2] & 18 1 0 1
Condition33%3/9
@st and -f $fh and $st[4] == 0 and not $st[2] & 18
!ll&&!rl&&r
100
@st and -f $fh and $st[4] == 0
!ll&&!rl&&r
010
@st and -f $fh
!ll&&!rl&&r
001
MC/DC0%0/4
@st and -f $fh and $st[4] == 0 and not $st[2] & 18
@st -f $fh $st[4] == 0 not $st[2] & 18
Truth table20%1/5
A and B and C and not(D)
A= @stB= -f $fhC= $st[4] == 0D= not $st[2] & 18
ABCDresult
0XXX0
10XX0
110X0
11100
11111
49 unless @st && -f $fh && $st[4] == 0 && !($st[2] & 0022);
50 0 return $fh;
51 }
52  
53 30 ▶sub _read_yaml {
Subroutine _read_yaml: called CC=8 SCAR=20.9
54 30 my ($file) = @_;
55 30 my $fh = _open_config($file);
56 27 my $size = read $fh, my $bytes, 65537;
57 27 ▶ die "$file: cannot read config: $!\n" unless defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 0 27 27
58 27 ▶ die "$file: config exceeds 64 KiB\n" if $size > 65536;
Branch: 1 branch
BranchTrueFalseTotal
if $size > 65536 0 27 27
59 27 my $yaml = YAML::PP->new(schema => ['Core'], boolean => 'JSON::PP');
60 27 my @docs = eval { $yaml->load_string(decode('UTF-8', $bytes, FB_CROAK)) };
61 27 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 3 24 27
62  
63 # Parser diagnostics can quote input, including an accidentally pasted credential.
64 3 my ($reason) = "$error" =~ /(Duplicate key '[^'\n]+'|Found cyclic ref)/;
65 3 my ($line) = "$error" =~ /Line\s*:\s*(\d+)/;
66 3 ▶ $reason //= defined($line) ? "invalid YAML on line $line" : 'invalid YAML';
Branch: 1 branch
BranchTrueFalseTotal
defined $line ? : 0 1 1
Condition100%2/2
$reason //= defined $line ? "invalid YAML on line $line" : "invalid YAML"
l!l
21
MC/DC100%1/1
$reason //= defined $line ? "invalid YAML on line $line" : "invalid YAML"
$reason
Truth table100%2/2
A
A= $reason
Aresult
11
00
67 3 die "$file: $reason\n";
68 }
69 24 ▶ die "$file: expected a single YAML mapping\n" unless @docs == 1 && ref($docs[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless @docs == 1 and ref $docs[0] eq "HASH" 2 22 24
Condition100%3/3
@docs == 1 and ref $docs[0] eq "HASH"
!ll&&!rl&&r
1122
MC/DC100%2/2
@docs == 1 and ref $docs[0] eq "HASH"
@docs == 1 ref $docs[0] eq "HASH"
Truth table100%3/3
A and B
A= @docs == 1B= ref $docs[0] eq "HASH"
ABresult
0X0
100
111
70 22 _validate($docs[0], $file, 1);
71 3 return $docs[0];
72 }
73  
74 29 ▶sub _absolute {
Subroutine _absolute: called CC=5 SCAR=16.6
75 29 my ($path) = @_;
76 return
77 29 ▶ defined($path)
Condition75%9/12
defined $path && !ref($path) && $path =~ m[\A/]u && index($path, "\000") == -1 && !($path =~ m[(?:\A|/)\.\.?(?:/|\z)]u)
!ll&&!rl&&r
6320
defined $path and not ref $path and $path =~ m[\A/]u and index($path, "\0") == -1
!ll&&!rl&&r
5123
defined $path and not ref $path and $path =~ m[\A/]u
!ll&&!rl&&r
0524
defined $path and not ref $path
!ll&&!rl&&r
0029
MC/DC60%3/5
defined $path && !ref($path) && $path =~ m[\A/]u && index($path, "\000") == -1 && !($path =~ m[(?:\A|/)\.\.?(?:/|\z)]u)
defined $path not ref $path $path =~ m[\A/]u index($path, "\0") == -1 !($path =~ m[(?:\A|/)\.\.?(?:/|\z)]u)
Truth table66%4/6
A and not(B) and C and D && not(E)
A= defined $pathB= not ref $pathC= $path =~ m[\A/]uD= index($path, "\0") == -1E= !($path =~ m[(?:\A|/)\.\.?(?:/|\z)]u)
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
78 && !ref($path)
79 && $path =~ m{\A/}
80 && index($path, "\0") < 0
81 && $path !~ m{(?:\A|/)\.\.?(?:/|\z)};
82 }
83  
84 28 ▶sub _validate {
Subroutine _validate: called CC=26 SCAR=32.8
85 28 my ($settings, $source, $yaml) = @_;
86 28 for my $key (sort keys %$settings) {
87 45 my $value = $settings->{$key};
88 45 my $error = "$source: $key";
89 45 ▶ if ($key eq 'server') {
Branch: 4 branches
BranchTrueFalseTotal
if ($key eq 'server') { } 6 39 45
elsif ($key =~ /\A(?:token_file|state|runtime|syft|grype)\z/u) { } 11 28 39
elsif ($key eq 'exclude_paths') { } 12 16 28
elsif ($key =~ /\A(?:jobs|spool_limit|upload_timeout|command_timeout)\z/u) { } 15 1 16
90 6 ▶ die "$error requires an HTTPS URL (HTTP only on numeric loopback)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and _server_url($value) 1 5 6
Condition50%3/6
defined $value and not ref $value and _server_url($value)
!ll&&!rl&&r
015
defined $value and not ref $value
!ll&&!rl&&r
006
MC/DC33%1/3
defined $value and not ref $value and _server_url($value)
defined $value not ref $value _server_url($value)
Truth table50%2/4
A and not(B) and C
A= defined $valueB= not ref $valueC= _server_url($value)
ABCresult
0XX0
10X0
1100
1111
91 unless defined($value) && !ref($value) && _server_url($value);
92 5 $settings->{$key} =~ s{/$}{};
93 }
94 elsif ($key =~ /\A(?:token_file|state|runtime|syft|grype)\z/) {
95 11 ▶ die "$error requires an absolute path without dot components\n"
Branch: 1 branch
BranchTrueFalseTotal
unless _absolute($value) 2 9 11
96 unless _absolute($value);
97 }
98 elsif ($key eq 'exclude_paths') {
99 12 ▶ die "$error requires an array of absolute literal paths\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 1 11 12
100 unless ref($value) eq 'ARRAY';
101 11 my (@paths, %seen);
102 11 for my $path (@$value) {
103 15 ▶ die "$error requires an absolute path without dot components\n"
Branch: 1 branch
BranchTrueFalseTotal
unless _absolute($path) 6 9 15
104 unless _absolute($path);
105 9 $path =~ s{/+}{/}g;
106 9 $path =~ s{/$}{};
107 9 ▶ die "$error cannot exclude the root\n" unless length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless length $path 2 7 9
108 7 ▶ push @paths, $path unless $seen{$path}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$path}++ 6 1 7
109 }
110 3 $settings->{$key} = \@paths;
111 }
112 elsif ($key =~ /\A(?:jobs|spool_limit|upload_timeout|command_timeout)\z/) {
113 15 ▶ die "$error requires a positive integer\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and !$yaml || svref_2object(\$value)->FLAGS & 4096 and $value =~ /\A[1-9][0-9]*\z/u 4 11 15
Condition91%11/12
defined $value and not ref $value and !$yaml || svref_2object(\$value)->FLAGS & 4096 and $value =~ /\A[1-9][0-9]*\z/u
!ll&&!rl&&r
3111
defined $value and not ref $value and !$yaml || svref_2object(\$value)->FLAGS & 4096
!ll&&!rl&&r
1212
defined $value and not ref $value
!ll&&!rl&&r
0114
not $yaml or svref_2object(\$value)->FLAGS & 4096
l!l&&r!l&&!r
392
MC/DC80%4/5
defined $value and not ref $value and !$yaml || svref_2object(\$value)->FLAGS & 4096 and $value =~ /\A[1-9][0-9]*\z/u
defined $value not ref $value not $yaml svref_2object(\$value)->FLAGS & 4096 $value =~ /\A[1-9][0-9]*\z/u
Truth table71%5/7
A and not(B) and not(C) or D and E
A= defined $valueB= not ref $valueC= not $yamlD= svref_2object(\$value)->FLAGS & 4096E= $value =~ /\A[1-9][0-9]*\z/u
ABCDEresult
0XXXX0
10XXX0
1100X0
111X00
110100
111X11
110111
114 unless defined($value)
115 && !ref($value)
116 && (!$yaml || svref_2object(\$value)->FLAGS & SVp_IOK)
117 && $value =~ /\A[1-9][0-9]*\z/;
118 11 ▶ die "$error must be in 1..64\n" if $key eq 'jobs' && $value > 64;
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "jobs" and $value > 64 1 10 11
Condition100%3/3
$key eq "jobs" and $value > 64
!ll&&!rl&&r
551
MC/DC100%2/2
$key eq "jobs" and $value > 64
$key eq "jobs" $value > 64
Truth table100%3/3
A and B
A= $key eq "jobs"B= $value > 64
ABresult
0X0
100
111
119 10 ▶ die "$error must be in 1048576..9999999999999 bytes\n"
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "spool_limit" and length $value > 13 || $value < 1048576 1 9 10
Condition50%3/6
$key eq "spool_limit" and length $value > 13 || $value < 1048576
!ll&&!rl&&r
901
length $value > 13 or $value < 1048576
l!l&&r!l&&!r
010
MC/DC33%1/3
$key eq "spool_limit" and length $value > 13 || $value < 1048576
$key eq "spool_limit" length $value > 13 $value < 1048576
Truth table50%2/4
A and B or C
A= $key eq "spool_limit"B= length $value > 13C= $value < 1048576
ABCresult
0XX0
1000
11X1
1011
120 if $key eq 'spool_limit' && (length($value) > 13 || $value < 1048576);
121 9 my $integer = 0 + $value;
122 9 ▶ die "$error exceeds the integer range\n"
Branch: 1 branch
BranchTrueFalseTotal
unless svref_2object(\$integer)->FLAGS & 4096 0 9 9
123 unless svref_2object(\$integer)->FLAGS & SVp_IOK;
124 9 $settings->{$key} = $integer;
125 }
126 else {
127 1 die "$error is an unknown setting\n";
128 }
129 }
130 }
131  
132 6 ▶sub _server_url {
Subroutine _server_url: called CC=11 SCAR=29.1
133 6 my ($value) = @_;
134 6 my $url = Mojo::URL->new($value);
135 6 ▶ my $host = $url->host // '';
Condition50%1/2
$url->host // ""
l!l
60
MC/DC0%0/1
$url->host // ""
$url->host
Truth table50%1/2
A
A= $url->host
Aresult
11
00
136 6 ▶ my $scheme = $url->scheme // '';
Condition50%1/2
$url->scheme // ""
l!l
60
MC/DC0%0/1
$url->scheme // ""
$url->scheme
Truth table50%1/2
A
A= $url->scheme
Aresult
11
00
137 return
138 6 ▶ length($host)
Condition54%13/24
length $host && !defined($url->userinfo) && !defined($url->fragment) && !$url->query->to_string && $url->path->to_string =~ m[\A/?\z]u && ($scheme eq 'https' || $scheme eq 'http' && ($host eq '127.0.0.1' || $host eq '[::1]'))
!ll&&!rl&&r
015
length $host and not defined $url->userinfo and not defined $url->fragment and not $url->query->to_string and $url->path->to_string =~ m[\A/?\z]u
!ll&&!rl&&r
006
length $host and not defined $url->userinfo and not defined $url->fragment and not $url->query->to_string
!ll&&!rl&&r
006
length $host and not defined $url->userinfo and not defined $url->fragment
!ll&&!rl&&r
006
length $host and not defined $url->userinfo
!ll&&!rl&&r
006
$scheme eq "https" or $scheme eq "http" and $host eq "127.0.0.1" || $host eq "[::1]"
l!l&&r!l&&!r
321
$scheme eq "http" and $host eq "127.0.0.1" || $host eq "[::1]"
!ll&&!rl&&r
012
$host eq "127.0.0.1" or $host eq "[::1]"
l!l&&r!l&&!r
201
MC/DC22%2/9
length $host && !defined($url->userinfo) && !defined($url->fragment) && !$url->query->to_string && $url->path->to_string =~ m[\A/?\z]u && ($scheme eq 'https' || $scheme eq 'http' && ($host eq '127.0.0.1' || $host eq '[::1]'))
length $host not defined $url->userinfo not defined $url->fragment not $url->query->to_string $url->path->to_string =~ m[\A/?\z]u $scheme eq "https" $scheme eq "http" $host eq "127.0.0.1" $host eq "[::1]"
Truth table30%3/10
A and not(B) and not(C) and not(D) and E && F or G and H or I
A= length $hostB= not defined $url->userinfoC= not defined $url->fragmentD= not $url->query->to_stringE= $url->path->to_string =~ m[\A/?\z]uF= $scheme eq "https"G= $scheme eq "http"H= $host eq "127.0.0.1"I= $host eq "[::1]"
ABCDEFGHIresult
0XXXXXXXX0
10XXXXXXX0
110XXXXXX0
1110XXXXX0
11110XXXX0
1111100XX0
1111101000
111111XXX1
11111011X1
1111101011
139 && !defined($url->userinfo)
140 && !defined($url->fragment)
141 && !$url->query->to_string
142 && $url->path->to_string =~ m{\A/?\z}
143 && ($scheme eq 'https'
144 || ($scheme eq 'http' && ($host eq '127.0.0.1' || $host eq '[::1]')));
145 }
146  
147 1 ▶sub trusted_path {
Subroutine trusted_path: called CC=11 SCAR=32.6
148 1 my ($file, $no_links) = @_;
149 1 ▶ die "Expected absolute path: $file"
Branch: 1 branch
BranchTrueFalseTotal
unless "$file" =~ m[\A/]u and not "$file" =~ m[(?:\A|/)\.\.(?:/|\z)]u 0 1 1
Condition33%1/3
"$file" =~ m[\A/]u and not "$file" =~ m[(?:\A|/)\.\.(?:/|\z)]u
!ll&&!rl&&r
001
MC/DC0%0/2
"$file" =~ m[\A/]u and not "$file" =~ m[(?:\A|/)\.\.(?:/|\z)]u
"$file" =~ m[\A/]u not "$file" =~ m[(?:\A|/)\.\.(?:/|\z)]u
Truth table33%1/3
A and not(B)
A= "$file" =~ m[\A/]uB= not "$file" =~ m[(?:\A|/)\.\.(?:/|\z)]u
ABresult
0X0
100
111
150 unless "$file" =~ m{\A/} && "$file" !~ m{(?:\A|/)\.\.(?:/|\z)};
151 1 ▶ for my $candidate ("$file", abs_path($file) // die "Missing path: $file") {
Condition50%1/2
abs_path($file) // die("Missing path: $file")
l!l
10
MC/DC0%0/1
abs_path($file) // die("Missing path: $file")
abs_path($file)
Truth table50%1/2
A
A= abs_path($file)
Aresult
11
00
152 1 my $p = path($candidate);
153 1 while (1) {
154 1 ▶ die "Symlink in config path: $p" if $no_links && -l $p;
Branch: 1 branch
BranchTrueFalseTotal
if $no_links and -l $p 0 1 1
Condition33%1/3
$no_links and -l $p
!ll&&!rl&&r
010
MC/DC0%0/2
$no_links and -l $p
$no_links -l $p
Truth table33%1/3
A and B
A= $no_linksB= -l $p
ABresult
0X0
100
111
155 1 my @st = stat($p);
156 1 ▶ die "Root must own $p and deny group/other writes"
Branch: 1 branch
BranchTrueFalseTotal
unless @st and $st[4] == 0 and not $st[2] & 18 1 0 1
Condition33%2/6
@st and $st[4] == 0 and not $st[2] & 18
!ll&&!rl&&r
100
@st and $st[4] == 0
!ll&&!rl&&r
010
MC/DC0%0/3
@st and $st[4] == 0 and not $st[2] & 18
@st $st[4] == 0 not $st[2] & 18
Truth table25%1/4
A and B and not(C)
A= @stB= $st[4] == 0C= not $st[2] & 18
ABCresult
0XX0
10X0
1100
1111
157 unless @st && $st[4] == 0 && !($st[2] & 0022);
158 0 ▶ last if "$p" eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if "$p" eq "/" 0 0 0
159 0 $p = $p->dirname;
160 }
161 }
162 }
163  
164 142 ▶sub mounts {
Subroutine mounts: called CC=1 SCAR=0.0
Pod undocumented
165 142 my ($self) = @_;
166 142 return $self->runtime . '/mounts';
167 }
168  
169 6 1;
« lib/Mogura/Server/API.pm ↑ Summary lib/Mogura/Scanner/Disk.pm »