Plugin/ClamAV.pm

filter: Statementstmt 98.2% 112 / 114 Branchbran 71.2% 47 / 66 Conditioncond 55.5% 15 / 27 MC/DCmcdc 25.0% 4 / 16 Subroutinesub 100% 19 / 19 Podpod 100% 1 / 1 totaltotal 81.4% 198 / 243 CC 49 SCAR 21.0
CC
49
Cov
84%
__ANON__
14.9
_drive
13.5
register
8.3
CRAP 8.1
↑ Summary ClamAV.pm »
1 package Punk::Plugin::ClamAV;
2  
3 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use parent 'Punk::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use ClamAV::Clamd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Hyperman;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Hyperman::Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = '0.02';
13  
14 3 our %WORDS;
15  
16 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 %WORDS = map { $_ => 1 } qw(reject allow);
18 }
19  
20 11 ▶sub register {
Subroutine register: called CC=8 SCAR=21.1
Pod documented
21 11 my ($self, $app, $opts) = @_;
22 11 ▶ $opts ||= {};
Condition50%1/2
$opts ||= {}
l!l
110
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
23  
24 11 ▶ my %client = map { exists $opts->{$_} ? ($_ => $opts->{$_}) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{$_} ? : 11 88 99
25 qw(socket host port connect_timeout reply_timeout reply_max
26 chunk max_size frame);
27  
28 Carp::croak("Punk::Plugin::ClamAV: one of 'socket' or 'host' is required")
29 11 ▶ unless defined $client{socket} || defined $client{host};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $client{"socket"} or defined $client{"host"} 1 10 11
Condition66%2/3
defined $client{"socket"} or defined $client{"host"}
l!l&&r!l&&!r
1001
MC/DC50%1/2
defined $client{"socket"} or defined $client{"host"}
defined $client{"socket"} defined $client{"host"}
Truth table66%2/3
A or B
A= defined $client{"socket"}B= defined $client{"host"}
ABresult
1X1
011
000
30  
31 10 my $clamd = ClamAV::Clamd->new(%client);
32  
33 my %policy = (
34 infected => _policy($opts->{on_infected}, 'reject', 'on_infected'),
35 unscannable => _policy($opts->{on_unscannable}, 'reject', 'on_unscannable'),
36 10 error => _policy($opts->{on_error}, 'reject', 'on_error'),
37 );
38 9 ▶ my $status = $opts->{status} || 422;
Condition50%1/2
$opts->{'status'} || 422
l!l
09
MC/DC0%0/1
$opts->{'status'} || 422
$opts->{'status'}
Truth table50%1/2
A
A= $opts->{'status'}
Aresult
11
00
39 9 my $loop_opt = $opts->{loop};
40 9 ▶ my $backstop = $opts->{reply_timeout} || 30;
Condition100%2/2
$opts->{'reply_timeout'} || 30
l!l
18
MC/DC100%1/1
$opts->{'reply_timeout'} || 30
$opts->{'reply_timeout'}
Truth table100%2/2
A
A= $opts->{'reply_timeout'}
Aresult
11
00
41  
42 9 ▶ $app->helper(clamd => sub { $clamd });
Subroutine __ANON__: called CC=1 SCAR=0.0
43  
44 20 ▶ $app->helper(scan_upload => sub {
Subroutine __ANON__: called CC=5 SCAR=16.5
45 20 my ($c, $up) = @_;
46 20 ▶ $up = $c->upload($up) unless ref $up;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $up 1 19 20
47 20 ▶ return undef unless $up;
Branch: 1 branch
BranchTrueFalseTotal
unless $up 1 19 20
48 19 my $loop = _loop($loop_opt);
49 19 my $path = $up->path;
50 19 ▶ return (defined $path && length $path)
Branch: 1 branch
BranchTrueFalseTotal
defined $path && length $path ? : 0 19 19
Condition33%1/3
defined $path and length $path
!ll&&!rl&&r
1900
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table33%1/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
51 ? _drive($clamd, $loop, 'path', $path, $backstop)
52 : _drive($clamd, $loop, 'bytes', $up->content, $backstop);
53 9 });
54  
55 1 ▶ $app->helper(scan_uploads => sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
56 1 my ($c) = @_;
57 1 ▶ my $ups = $c->req->uploads or return {};
Branch: 1 branch
BranchTrueFalseTotal
my $ups = $c->req->uploads or return {} 1 0 1
58 1 my %out;
59 1 for my $name (keys %$ups) {
60 2 my $v = $ups->{$name};
61 2 ▶ $out{$name} = [ map { $c->scan_upload($_) }
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 0 2 2
62 (ref $v eq 'ARRAY' ? @$v : $v) ];
63 }
64 1 return \%out;
65 9 });
66  
67 7 ▶ $app->helper(upload_ok => sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
68 7 my ($c, $up) = @_;
69 7 my $v = $c->scan_upload($up);
70 7 ▶ return 0 unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 1 6 7
71 6 ▶ return 1 if $v->is_clean;
Branch: 1 branch
BranchTrueFalseTotal
if $v->is_clean 1 5 6
72 5 ▶ my $act = $policy{ $v->state } // 'reject';
Condition50%1/2
$policy{$v->state} // "reject"
l!l
50
MC/DC0%0/1
$policy{$v->state} // "reject"
$policy{$v->state}
Truth table50%1/2
A
A= $policy{$v->state}
Aresult
11
00
73 5 ▶ return ref $act eq 'CODE' ? 0 : ($act eq 'allow' ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
ref $act eq 'CODE' ? : 0 5 5
$act eq 'allow' ? : 2 3 5
74 9 });
75  
76 9 ▶ if ($opts->{auto}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"auto"}) 1 8 9
77 3 ▶ $app->hook(before_dispatch => sub {
Subroutine __ANON__: called CC=13 SCAR=27.0
78 3 my ($c) = @_;
79  
80 3 ▶ my $ct = $c->req->header('Content-Type') // '';
Condition100%2/2
$c->req->header("Content-Type") // ""
l!l
21
MC/DC100%1/1
$c->req->header("Content-Type") // ""
$c->req->header("Content-Type")
Truth table100%2/2
A
A= $c->req->header("Content-Type")
Aresult
11
00
81 3 ▶ return unless $ct =~ m{^\s*multipart/form-data}i;
Branch: 1 branch
BranchTrueFalseTotal
unless $ct =~ m[^\s*multipart/form-data]i 1 2 3
82  
83 2 ▶ my $ups = $c->req->uploads or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ups = $c->req->uploads or return 2 0 2
84  
85 2 for my $name (sort keys %$ups) {
86 2 my $v = $ups->{$name};
87 2 ▶ for my $up (ref $v eq 'ARRAY' ? @$v : $v) {
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 0 2 2
88 2 my $verdict = $c->scan_upload($up);
89  
90 2 ▶ next if !defined $verdict || $verdict->is_clean;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $verdict or $verdict->is_clean 1 1 2
Condition66%2/3
not defined $verdict or $verdict->is_clean
l!l&&r!l&&!r
011
MC/DC50%1/2
not defined $verdict or $verdict->is_clean
not defined $verdict $verdict->is_clean
Truth table66%2/3
not(A) or B
A= not defined $verdictB= $verdict->is_clean
ABresult
1X1
011
000
91  
92 1 ▶ my $act = $policy{ $verdict->state } // 'reject';
Condition50%1/2
$policy{$verdict->state} // "reject"
l!l
10
MC/DC0%0/1
$policy{$verdict->state} // "reject"
$policy{$verdict->state}
Truth table50%1/2
A
A= $policy{$verdict->state}
Aresult
11
00
93 1 ▶ next if !ref($act) && $act eq 'allow';
Branch: 1 branch
BranchTrueFalseTotal
if not ref $act and $act eq "allow" 0 1 1
Condition33%1/3
not ref $act and $act eq "allow"
!ll&&!rl&&r
010
MC/DC0%0/2
not ref $act and $act eq "allow"
not ref $act $act eq "allow"
Truth table33%1/3
not(A) and B
A= not ref $actB= $act eq "allow"
ABresult
0X0
100
111
94  
95 1 ▶ return $act->($c, $verdict, $up) if ref $act eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $act eq "CODE" 0 1 1
96 1 return $c->text(_why($verdict), $status);
97 }
98 }
99 1 return;
100 1 });
101 }
102  
103 9 return;
104 }
105  
106 19 ▶sub _loop {
Subroutine _loop: called CC=2 SCAR=6.9
107 19 my ($explicit) = @_;
108 19 ▶ return $explicit if $explicit;
Branch: 1 branch
BranchTrueFalseTotal
if $explicit 4 15 19
109 15 my $l = eval { Hyperman->loop };
110 15 return $l;
111 }
112  
113 19 ▶sub _drive {
Subroutine _drive: called CC=12 SCAR=26.0
114 19 my ($clamd, $loop, $kind, $what, $backstop) = @_;
115  
116 19 ▶ unless ($loop) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loop) 15 4 19
117 15 ▶ return $kind eq 'path' ? $clamd->scan_path($what)
Branch: 2 branches
BranchTrueFalseTotal
$kind eq 'path' ? : 0 15 15
$kind eq 'fd' ? : 0 15 15
118 : $kind eq 'fd' ? $clamd->scan_fd($what)
119 : $clamd->scan($what);
120 }
121  
122 4 my $scan = $clamd->start_scan($what, $kind);
123  
124 4 ▶ until ($scan->step) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($scan->step) 4 4 8
125 4 my $fd = $scan->fd;
126 4 my $want = $scan->want;
127 4 ▶ last unless defined $fd && defined $want;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fd and defined $want 0 4 4
Condition33%1/3
defined $fd and defined $want
!ll&&!rl&&r
004
MC/DC0%0/2
defined $fd and defined $want
defined $fd defined $want
Truth table33%1/3
A and B
A= defined $fdB= defined $want
ABresult
0X0
100
111
128  
129 4 ▶ open my $fh, '+<&', $fd or last;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "+<&", $fd or last 4 0 4
130  
131 4 ▶ my $io = $want eq 'read' ? $loop->readable_f($fh)
Branch: 1 branch
BranchTrueFalseTotal
$want eq 'read' ? : 4 0 4
132 : $loop->writable_f($fh);
133 4 my $to = $loop->timer_f($backstop);
134  
135 4 my $ok = eval { Hyperman::Future->wait_any($io, $to)->get; 1 };
136  
137 4 ▶ for my $f ($io, $to) { eval { $f->cancel unless $f->is_ready } }
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 8 8
138 4 close $fh;
139 4 ▶ last unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 4 4
140 }
141 4 return $scan->verdict;
142 }
143  
144 30 ▶sub _policy {
Subroutine _policy: called CC=4 SCAR=13.9
145 30 my ($given, $default, $what) = @_;
146 30 ▶ return $default unless defined $given;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $given 28 2 30
147 2 ▶ return $given if ref $given eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $given eq "CODE" 0 2 2
148 Carp::croak("Punk::Plugin::ClamAV: $what must be 'reject', 'allow' "
149 . "or a coderef, not '$given'")
150 2 ▶ unless $WORDS{$given};
Branch: 1 branch
BranchTrueFalseTotal
unless $Punk::Plugin::ClamAV::WORDS{$given} 1 1 2
151 1 return $given;
152 }
153  
154 1 ▶sub _why {
Subroutine _why: called CC=3 SCAR=16.5
155 1 my ($v) = @_;
156 1 ▶ return 'upload rejected: infected' if $v->is_infected;
Branch: 1 branch
BranchTrueFalseTotal
if $v->is_infected 1 0 1
157 0 ▶ return 'upload rejected: could not be scanned' if $v->is_unscannable;
Branch: 1 branch
BranchTrueFalseTotal
if $v->is_unscannable 0 0 0
158 0 return 'upload rejected: virus scanner unavailable';
159 }
160  
161 3 1;
162  
163 __END__
↑ Summary ClamAV.pm »