blib/lib/Mail/SpamAssassin/Plugin/AttachmentPresent.pm

filter: Statementstmt 27.0% 30 / 111 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 60.0% 9 / 15 Podpod 75.0% 3 / 4 totaltotal 22.5% 42 / 186 SCAR 61.0
CC
28
Cov
19%
attachmentpresent_archive_count
110.0
attachmentpresent_file_count
110.0
_build_attachment_tree
90.0
CRAP 446.7
↑ Summary
1 # <@LICENSE>
2 # Copyright 2016 Web2All B.V.
3 #
4 # This Plugin is free software; you can redistribute
5 # it and/or modify it under the same terms as Perl 5.18.1.
6 #
7 # you may not use this file except in compliance with the License.
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # </@LICENSE>
13  
14 # Version control:
15 # $Revision: 169 $
16 # $Author: merijn $
17 # $Date: 2016-09-09 14:36:13 +0200 (Fri, 09 Sep 2016) $
18  
19 =head1 NAME
20  
21 Mail::SpamAssassin::Plugin::AttachmentPresent - SpamAssassin plugin to score mail based on attachments
22 including attachments inside archives.
23  
24 =head1 SYNOPSIS
25  
26 loadplugin Mail::SpamAssassin::Plugin::AttachmentPresent
27 body RULENAME eval:attachmentpresent_archive_count()
28 body RULENAME eval:attachmentpresent_file_count()
29  
30 =head1 DESCRIPTION
31  
32 Get information about attached files, including inside archives.
33 Only supports Zip right now.
34  
35 =head1 CONFIGURATION
36  
37 None
38  
39 =head1 INSTALL
40  
41 =over
42  
43 =item Install the required Perl modules:
44  
45 Encode::MIME::Header;
46 Archive::Zip
47 IO::String
48 Mail::SpamAssassin::Plugin::AttachmentPresent
49  
50 Should already be installed by spamassassin
51  
52 =item Configure spamassassin
53  
54 Typically in local.cf, include lines:
55 loadplugin Mail::SpamAssassin::Plugin::AttachmentPresent
56  
57 body HAS_JS_FILES eval:attachmentpresent_file_count('js')
58 describe HAS_JS_FILES The e-mail has attached javascript files (or inside archives)
59 score HAS_JS_FILES 0.001
60  
61 =back
62  
63 =cut
64  
65 package Mail::SpamAssassin::Plugin::AttachmentPresent;
66  
67 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 2 ▶use Mail::SpamAssassin::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 ▶use Mail::SpamAssassin::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 1 ▶use Encode::MIME::Header;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 1 ▶use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
Subroutine BEGIN: called CC=1 SCAR=0.0
76 1 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 1 ▶use base qw(Mail::SpamAssassin::Plugin);
Subroutine BEGIN: called CC=1 SCAR=0.0
79 1 our $VERSION = '1.04';
80  
81 1 our $LOG_FACILITY = 'AttachmentPresent';
82  
83 # Fields:
84 # mailsa - Mail::SpamAssassin instance
85 sub new {
86 0 ▶ my ($class, $mailsa) = @_;
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
87 0 ▶ $class = ref($class) || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
88 0 my $self = $class->SUPER::new($mailsa);
89 0 bless($self, $class);
90
91 0 Mail::SpamAssassin::Logger::add_facilities($LOG_FACILITY);
92
93 0 $self->register_eval_rule('attachmentpresent_archive_count');
94 0 $self->register_eval_rule('attachmentpresent_file_count');
95
96 0 return $self;
97 }
98  
99  
100 sub l {
101 0 ▶ Mail::SpamAssassin::Logger::dbg("$LOG_FACILITY: " . join('', @_));
Subroutine l: not called CC=1 SCAR=6.9
Pod undocumented
102 }
103  
104 sub _zip_error_handler(){
105 0 ▶ l("_zip_error_handler: ".$_[0]);
Subroutine _zip_error_handler: not called CC=1 SCAR=6.9
106 }
107  
108 sub _build_attachment_tree {
109 0 ▶ my ($self,$pms) = @_;
Subroutine _build_attachment_tree: not called CC=9 SCAR=45.0
110
111 # init storage
112 0 $pms->{'attachment_data'}={
113 'files' => [],
114 'archives' => []
115 };
116 0 l('_build_attachment_tree');
117 # $pms->{msg} Mail::SpamAssassin::Message
118 0 foreach my $part ($pms->{msg}->find_parts(qr/.*/, 1)) {
119 # $part Mail::SpamAssassin::Message::Node
120 # now we get all parts which are leaves (so text parts and attachments, not multiparts)
121 0 l('_build_attachment_tree->part');
122 # we ignore all parts which are part of the text body
123
124 # For zipfiles, find out whats in them
125 # Content-Type: application/zip;
126 # Content-Transfer-Encoding: base64
127 0 ▶ my $ctt = $part->get_header('content-type') || '';
Condition0%0/2
$part->get_header('content-type') || ''
l!l
00
MC/DC0%0/1
$part->get_header('content-type') || ''
$part->get_header('content-type')
Truth table0%0/2
A
A= $part->get_header('content-type')
Aresult
11
00
128 # Mail::SpamAssassin::Message::Node has _decode_header() method, but it doesnt decode
129 # Content-* headers and thus the filename in the Content-Type header is not decoded :(
130 0 $ctt=Encode::decode('MIME-Header', $ctt);
131 # $ctt might contain wide characters now
132  
133 0 ▶ my $cte = lc($part->get_header('content-transfer-encoding') || '');
Condition0%0/2
$part->get_header('content-transfer-encoding') || ''
l!l
00
MC/DC0%0/1
$part->get_header('content-transfer-encoding') || ''
$part->get_header('content-transfer-encoding')
Truth table0%0/2
A
A= $part->get_header('content-transfer-encoding')
Aresult
11
00
134
135 0 l('_build_attachment_tree->part: content-type: '.$ctt);
136
137 # consider the attachment a file if it has a name
138 0 my $attachment_filename='';
139 0 ▶ if($ctt =~ m/name\s*=\s*"?([^";]*)"?/is){
Branch: 1 branch
BranchTrueFalseTotal
if ($ctt =~ /name\s*=\s*"?([^";]*)"?/is) 0 0 0
140 0 $attachment_filename=$1;
141 # lets be sure and remove any whitespace from the end
142 0 $attachment_filename =~ s/\s+$//;
143 0 l('_build_attachment_tree->part: part has name '.$attachment_filename);
144 0 push(@{$pms->{'attachment_data'}->{'files'}},$attachment_filename);
145 }
146
147 # now process attachments
148
149 # Zip
150 0 ▶ if ($ctt =~ /zip/i && $cte =~ /^base64$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($ctt =~ /zip/i and $cte =~ /^base64$/) 0 0 0
Condition0%0/3
$ctt =~ /zip/i and $cte =~ /^base64$/
!ll&&!rl&&r
000
MC/DC0%0/2
$ctt =~ /zip/i and $cte =~ /^base64$/
$ctt =~ /zip/i $cte =~ /^base64$/
Truth table0%0/3
A and B
A= $ctt =~ /zip/iB= $cte =~ /^base64$/
ABresult
0X0
100
111
151 # seems to be a zip attachment
152 0 l('_build_attachment_tree->part found Zip archive: '.$ctt);
153 # how much we grab? for now only 500kb, bigger files will just not
154 # be properly parsed as zip files
155 0 my $num_of_bytes=512000;
156
157 0 my $zip_binary_head=$part->decode($num_of_bytes);
158 # use Archive::Zip
159 0 my $SH = IO::String->new($zip_binary_head);
160  
161 0 Archive::Zip::setErrorHandler( \&_zip_error_handler );
162 0 my $zip = Archive::Zip->new();
163 0 ▶ if($zip->readFromFileHandle( $SH ) != AZ_OK){
Branch: 1 branch
BranchTrueFalseTotal
if ($zip->readFromFileHandle($SH) != 0) 0 0 0
164 0 l("_build_attachment_tree: cannot read zipfile $attachment_filename");
165 # as we cannot read it its not a zip (or too big/corrupted)
166 # so skip processing.
167 0 next;
168 }
169
170 # ok seems to be a zip
171 0 push(@{$pms->{'attachment_data'}->{'archives'}},$attachment_filename);
172
173 # list all files in the zip file and add them as a file
174 0 my @members = $zip->members();
175 0 foreach my $member (@members){
176 0 push(@{$pms->{'attachment_data'}->{'files'}},$member->fileName());
177 }
178 }
179  
180 }
181  
182 }
183  
184 =head1 FUNCTIONS
185  
186 =over
187  
188 =item $int = attachmentpresent_archive_count([$ext[, $more_than]])
189  
190 Returns the amount of recognised archive files inside
191 this message. Currently only Zip files are recognised.
192 If the file could not be parsed because it was too big
193 or corrupted, its not counted.
194  
195 Optionally you can filter on extension, where $ext should
196 be set to the extension to filter on. Eg. $ext='zip'
197  
198 =cut
199  
200 sub attachmentpresent_archive_count {
201 0 ▶ my $self = shift;
Subroutine attachmentpresent_archive_count: not called CC=10 SCAR=47.0
Pod documented
202 0 my $pms = shift;
203 0 my $rendered = shift;# body tests: fully rendered message as array reference
204 0 my $extension = shift;
205 0 ▶ my $larger_than = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
206  
207 0 ▶ l('attachmentpresent_archive_count ('.($extension ? $extension : 'all').')'.($larger_than ? ' more than '.$larger_than : ''));
Branch: 2 branches
BranchTrueFalseTotal
$extension ? : 0 0 0
$larger_than ? : 0 0 0
208
209 # make sure we have attachment data read in.
210 0 ▶ if (!exists $pms->{'attachment_data'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $pms->{"attachment_data"}) 0 0 0
211 0 $self->_build_attachment_tree($pms);
212 }
213  
214 0 my $count=0;
215 0 ▶ if($extension){
Branch: 1 branch
BranchTrueFalseTotal
if ($extension) { } 0 0 0
216 0 foreach my $archive (@{$pms->{'attachment_data'}->{'archives'}}){
217 0 ▶ if($archive =~ m/\.$extension$/i){
Branch: 1 branch
BranchTrueFalseTotal
if ($archive =~ /\.$extension$/i) 0 0 0
218 0 $count++;
219 }
220 }
221 }else{
222 0 $count=scalar (@{$pms->{'attachment_data'}->{'archives'}});
223 }
224
225 0 l('attachmentpresent_archive_count actual count: '.$count);
226 0 ▶ l('attachmentpresent_archive_count: '.(($count > $larger_than) ? 1 : 0));
Branch: 1 branch
BranchTrueFalseTotal
$count > $larger_than ? : 0 0 0
227 0 ▶ return (($count > $larger_than) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$count > $larger_than ? : 0 0 0
228 }
229  
230 =item $int = attachmentpresent_file_count([$ext[, $more_than]])
231  
232 Returns the amount of files inside this message. It also
233 counts files inside recognised archive files. Currently only
234 Zip files are recognised.
235  
236 Optionally you can filter on extension, where $ext should
237 be set to the extension to filter on. Eg. $ext='js'
238  
239 Optionally you can specify the minimum amount of files
240 (of the given type) which will be required to trigger the rule.
241 By default $more_than is 0, so at least one file of the
242 given type is needed, but you can set it to 4 if you want the
243 rule to trigger when at least 5 files are present.
244  
245 =cut
246  
247 sub attachmentpresent_file_count {
248 0 ▶ my $self = shift;
Subroutine attachmentpresent_file_count: not called CC=10 SCAR=47.0
Pod documented
249 0 my $pms = shift;
250 0 my $rendered = shift;# body tests: fully rendered message as array reference
251 0 my $extension = shift;
252 0 ▶ my $larger_than = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
253  
254 0 ▶ l('attachmentpresent_file_count ('.($extension ? $extension : 'all').')'.($larger_than ? ' more than '.$larger_than : ''));
Branch: 2 branches
BranchTrueFalseTotal
$extension ? : 0 0 0
$larger_than ? : 0 0 0
255
256 # make sure we have attachment data read in.
257 0 ▶ if (!exists $pms->{'attachment_data'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $pms->{"attachment_data"}) 0 0 0
258 0 $self->_build_attachment_tree($pms);
259 }
260  
261 0 my $count=0;
262 0 ▶ if($extension){
Branch: 1 branch
BranchTrueFalseTotal
if ($extension) { } 0 0 0
263 0 foreach my $file (@{$pms->{'attachment_data'}->{'files'}}){
264 0 ▶ if($file =~ m/\.$extension$/i){
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.$extension$/i) 0 0 0
265 0 $count++;
266 0 l('attachmentpresent_file_count found matching file: '.$file);
267 }
268 }
269 }else{
270 0 $count=scalar (@{$pms->{'attachment_data'}->{'files'}});
271 }
272
273 0 l('attachmentpresent_file_count actual count: '.$count);
274 0 ▶ l('attachmentpresent_file_count: '.(($count > $larger_than) ? 1 : 0));
Branch: 1 branch
BranchTrueFalseTotal
$count > $larger_than ? : 0 0 0
275 0 ▶ return (($count > $larger_than) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$count > $larger_than ? : 0 0 0
276 }
277  
278 =back
279 =cut
280  
281 1 1;
↑ Summary