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

filter: Statementstmt 25.2% 30 / 119 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 60.0% 9 / 15 Podpod 75.0% 3 / 4 totaltotal 21.1% 42 / 199 CC 30 SCAR 44.3
CC
30
Cov
18%
_build_attachment_tree
132.0
attachmentpresent_archive_count
110.0
attachmentpresent_file_count
110.0
CRAP 83.6
↑ 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: 218 $
16 # $Author: merijn $
17 # $Date: 2017-03-01 11:11:37 +0100 (Wed, 01 Mar 2017) $
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 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 1 ▶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.05';
80  
81 1 our $LOG_FACILITY = 'AttachmentPresent';
82  
83 # Fields:
84 # mailsa - Mail::SpamAssassin instance
85 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
86 0 my ($class, $mailsa) = @_;
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 0 ▶sub l {
Subroutine l: not called CC=1 SCAR=6.9
Pod undocumented
101 0 Mail::SpamAssassin::Logger::dbg("$LOG_FACILITY: " . join('', @_));
102 }
103  
104 0 ▶sub _zip_error_handler(){
Subroutine _zip_error_handler: not called CC=1 SCAR=6.9
105 0 l("_zip_error_handler: ".$_[0]);
106 }
107  
108 0 ▶sub _build_attachment_tree {
Subroutine _build_attachment_tree: not called CC=11 SCAR=48.8
109 0 my ($self,$pms) = @_;
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 }else{
146 # fallback check in Content-Disposition header
147 # some (spam) has Content-Disposition with filename but no filename in Content-Type,
148 # but mail clients will still display the attachment.
149 0 ▶ my $cdp = $part->get_header('content-disposition') || '';
Condition0%0/2
$part->get_header('content-disposition') || ''
l!l
00
MC/DC0%0/1
$part->get_header('content-disposition') || ''
$part->get_header('content-disposition')
Truth table0%0/2
A
A= $part->get_header('content-disposition')
Aresult
11
00
150 # Content-* headers and thus the filename in the Content-Type header is not decoded :(
151 0 $cdp=Encode::decode('MIME-Header', $cdp);
152 # $cdp might contain wide characters now
153 0 ▶ if($cdp =~ m/name\s*=\s*"?([^";]*)"?/is){
Branch: 1 branch
BranchTrueFalseTotal
if ($cdp =~ /name\s*=\s*"?([^";]*)"?/is) 0 0 0
154 0 $attachment_filename=$1;
155 # lets be sure and remove any whitespace from the end
156 0 $attachment_filename =~ s/\s+$//;
157 0 l('_build_attachment_tree->part: part has name (in Content-Disposition) '.$attachment_filename);
158 0 push(@{$pms->{'attachment_data'}->{'files'}},$attachment_filename);
159 }
160 }
161
162 # now process attachments
163
164 # Zip
165 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
166 # seems to be a zip attachment
167 0 l('_build_attachment_tree->part found Zip archive: '.$ctt);
168 # how much we grab? for now only 500kb, bigger files will just not
169 # be properly parsed as zip files
170 0 my $num_of_bytes=512000;
171
172 0 my $zip_binary_head=$part->decode($num_of_bytes);
173 # use Archive::Zip
174 0 my $SH = IO::String->new($zip_binary_head);
175  
176 0 Archive::Zip::setErrorHandler( \&_zip_error_handler );
177 0 my $zip = Archive::Zip->new();
178 0 ▶ if($zip->readFromFileHandle( $SH ) != AZ_OK){
Branch: 1 branch
BranchTrueFalseTotal
if ($zip->readFromFileHandle($SH) != 0) 0 0 0
179 0 l("_build_attachment_tree: cannot read zipfile $attachment_filename");
180 # as we cannot read it its not a zip (or too big/corrupted)
181 # so skip processing.
182 0 next;
183 }
184
185 # ok seems to be a zip
186 0 push(@{$pms->{'attachment_data'}->{'archives'}},$attachment_filename);
187
188 # list all files in the zip file and add them as a file
189 0 my @members = $zip->members();
190 0 foreach my $member (@members){
191 0 push(@{$pms->{'attachment_data'}->{'files'}},$member->fileName());
192 }
193 }
194  
195 }
196  
197 }
198  
199 =head1 FUNCTIONS
200  
201 =over
202  
203 =item $int = attachmentpresent_archive_count([$ext[, $more_than]])
204  
205 Returns the amount of recognised archive files inside
206 this message. Currently only Zip files are recognised.
207 If the file could not be parsed because it was too big
208 or corrupted, its not counted.
209  
210 Optionally you can filter on extension, where $ext should
211 be set to the extension to filter on. Eg. $ext='zip'
212  
213 =cut
214  
215 0 ▶sub attachmentpresent_archive_count {
Subroutine attachmentpresent_archive_count: not called CC=10 SCAR=47.0
Pod documented
216 0 my $self = shift;
217 0 my $pms = shift;
218 0 my $rendered = shift;# body tests: fully rendered message as array reference
219 0 my $extension = shift;
220 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
221  
222 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
223
224 # make sure we have attachment data read in.
225 0 ▶ if (!exists $pms->{'attachment_data'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $pms->{"attachment_data"}) 0 0 0
226 0 $self->_build_attachment_tree($pms);
227 }
228  
229 0 my $count=0;
230 0 ▶ if($extension){
Branch: 1 branch
BranchTrueFalseTotal
if ($extension) { } 0 0 0
231 0 foreach my $archive (@{$pms->{'attachment_data'}->{'archives'}}){
232 0 ▶ if($archive =~ m/\.$extension$/i){
Branch: 1 branch
BranchTrueFalseTotal
if ($archive =~ /\.$extension$/i) 0 0 0
233 0 $count++;
234 }
235 }
236 }else{
237 0 $count=scalar (@{$pms->{'attachment_data'}->{'archives'}});
238 }
239
240 0 l('attachmentpresent_archive_count actual count: '.$count);
241 0 ▶ l('attachmentpresent_archive_count: '.(($count > $larger_than) ? 1 : 0));
Branch: 1 branch
BranchTrueFalseTotal
$count > $larger_than ? : 0 0 0
242 0 ▶ return (($count > $larger_than) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$count > $larger_than ? : 0 0 0
243 }
244  
245 =item $int = attachmentpresent_file_count([$ext[, $more_than]])
246  
247 Returns the amount of files inside this message. It also
248 counts files inside recognised archive files. Currently only
249 Zip files are recognised.
250  
251 Optionally you can filter on extension, where $ext should
252 be set to the extension to filter on. Eg. $ext='js'
253  
254 Optionally you can specify the minimum amount of files
255 (of the given type) which will be required to trigger the rule.
256 By default $more_than is 0, so at least one file of the
257 given type is needed, but you can set it to 4 if you want the
258 rule to trigger when at least 5 files are present.
259  
260 =cut
261  
262 0 ▶sub attachmentpresent_file_count {
Subroutine attachmentpresent_file_count: not called CC=10 SCAR=47.0
Pod documented
263 0 my $self = shift;
264 0 my $pms = shift;
265 0 my $rendered = shift;# body tests: fully rendered message as array reference
266 0 my $extension = shift;
267 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
268  
269 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
270
271 # make sure we have attachment data read in.
272 0 ▶ if (!exists $pms->{'attachment_data'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $pms->{"attachment_data"}) 0 0 0
273 0 $self->_build_attachment_tree($pms);
274 }
275  
276 0 my $count=0;
277 0 ▶ if($extension){
Branch: 1 branch
BranchTrueFalseTotal
if ($extension) { } 0 0 0
278 0 foreach my $file (@{$pms->{'attachment_data'}->{'files'}}){
279 0 ▶ if($file =~ m/\.$extension$/i){
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.$extension$/i) 0 0 0
280 0 $count++;
281 0 l('attachmentpresent_file_count found matching file: '.$file);
282 }
283 }
284 }else{
285 0 $count=scalar (@{$pms->{'attachment_data'}->{'files'}});
286 }
287
288 0 l('attachmentpresent_file_count actual count: '.$count);
289 0 ▶ l('attachmentpresent_file_count: '.(($count > $larger_than) ? 1 : 0));
Branch: 1 branch
BranchTrueFalseTotal
$count > $larger_than ? : 0 0 0
290 0 ▶ return (($count > $larger_than) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$count > $larger_than ? : 0 0 0
291 }
292  
293 =back
294 =cut
295  
296 1 1;
↑ Summary