| 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 ||= {}
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 11 | ▶ | my %client = map { exists $opts->{$_} ? ($_ => $opts->{$_}) : () } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $client{"socket"} or defined $client{"host"}
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"}
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$opts->{'status'} || 422
$opts->{'status'}
Truth table50%1/2
A
A= $opts->{'status'}
| |||||||||||||||||||||||||||||
| 39 | 9 | my $loop_opt = $opts->{loop}; | |||||||||||||||||||||||||||
| 40 | 9 | ▶ | my $backstop = $opts->{reply_timeout} || 30; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{'reply_timeout'} || 30
MC/DC100%1/1
$opts->{'reply_timeout'} || 30
$opts->{'reply_timeout'}
Truth table100%2/2
A
A= $opts->{'reply_timeout'}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 47 | 20 | ▶ | return undef unless $up; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 19 | my $loop = _loop($loop_opt); | |||||||||||||||||||||||||||
| 49 | 19 | my $path = $up->path; | |||||||||||||||||||||||||||
| 50 | 19 | ▶ | return (defined $path && length $path) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $path and length $path
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 71 | 6 | ▶ | return 1 if $v->is_clean; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 5 | ▶ | my $act = $policy{ $v->state } // 'reject'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$policy{$v->state} // "reject"
MC/DC0%0/1
$policy{$v->state} // "reject"
$policy{$v->state}
Truth table50%1/2
A
A= $policy{$v->state}
| |||||||||||||||||||||||||||||
| 73 | 5 | ▶ | return ref $act eq 'CODE' ? 0 : ($act eq 'allow' ? 1 : 0); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 74 | 9 | }); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 9 | ▶ | if ($opts->{auto}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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") // ""
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")
| |||||||||||||||||||||||||||||
| 81 | 3 | ▶ | return unless $ct =~ m{^\s*multipart/form-data}i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | ▶ | my $ups = $c->req->uploads or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 88 | 2 | my $verdict = $c->scan_upload($up); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 2 | ▶ | next if !defined $verdict || $verdict->is_clean; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $verdict or $verdict->is_clean
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
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 1 | ▶ | my $act = $policy{ $verdict->state } // 'reject'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$policy{$verdict->state} // "reject"
MC/DC0%0/1
$policy{$verdict->state} // "reject"
$policy{$verdict->state}
Truth table50%1/2
A
A= $policy{$verdict->state}
| |||||||||||||||||||||||||||||
| 93 | 1 | ▶ | next if !ref($act) && $act eq 'allow'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not ref $act and $act eq "allow"
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"
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | ▶ | return $act->($c, $verdict, $up) if ref $act eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 117 | 15 | ▶ | return $kind eq 'path' ? $clamd->scan_path($what) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 125 | 4 | my $fd = $scan->fd; | |||||||||||||||||||||||||||
| 126 | 4 | my $want = $scan->want; | |||||||||||||||||||||||||||
| 127 | 4 | ▶ | last unless defined $fd && defined $want; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $fd and defined $want
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
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 4 | ▶ | open my $fh, '+<&', $fd or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 4 | ▶ | my $io = $want eq 'read' ? $loop->readable_f($fh) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 138 | 4 | close $fh; | |||||||||||||||||||||||||||
| 139 | 4 | ▶ | last unless $ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 147 | 2 | ▶ | return $given if ref $given eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | return 'upload rejected: could not be scanned' if $v->is_unscannable; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 0 | return 'upload rejected: virus scanner unavailable'; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 3 | 1; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | __END__ | ||||||||||||||||||||||||||||