Debian Codegraph @

cassandane/Cassandane/Cyrus/ClamAV.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2017 FastMail Pty Ltd. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
15 # distribution.
16 #
17 # 3. The name "Fastmail Pty Ltd" must not be used to
18 # endorse or promote products derived from this software without
19 # prior written permission. For permission or any legal
20 # details, please contact
21 # FastMail Pty Ltd
22 # PO Box 234
23 # Collins St West 8007
24 # Victoria
25 # Australia
26 #
27 # 4. Redistributions of any form whatsoever must retain the following
28 # acknowledgment:
29 # "This product includes software developed by Fastmail Pty. Ltd."
30 #
31 # FASTMAIL PTY LTD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
32 # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
33 # EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE FOR ANY SPECIAL, INDIRECT
34 # OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
35 # USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
36 # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
37 # OF THIS SOFTWARE.
38 #
39
40 package Cassandane::Cyrus::ClamAV;
41 use strict;
42 use warnings;
43 use Cwd qw(abs_path);
44 use Data::Dumper;
45
46 use lib '.';
47 use base qw(Cassandane::Cyrus::TestCase);
48 use Cassandane::Util::Log;
49 use Cassandane::Util::Slurp;
50 use Cassandane::Instance;
51
52 $Data::Dumper::Sortkeys = 1;
53
54 my %eicar_attached = (
55 mime_type => "multipart/mixed",
56 mime_boundary => "boundary",
57 body => ""
58 . "--boundary\r\n"
59 . "Content-Type: text/plain\r\n"
60 . "\r\n"
61 . "body"
62 . "\r\n"
63 . "--boundary\r\n"
64 . "Content-Disposition: attachment; filename=eicar.txt;\r\n"
65 . "Content-Type: text/plain\r\n"
66 . "\r\n"
67 # This is the EICAR AV test file:
68 # http://www.eicar.org/83-0-Anti-Malware-Testfile.html
69 . 'X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*'
70 . "\r\n"
71 . "--boundary\r\n",
72 );
73
74 my %custom_header = (
75 'extra_headers' => [
76 [ 'x-delete-me' => 'please' ],
77 ],
78 );
79
80 sub new
81 {
82 my $class = shift;
83
84 my $self = $class->SUPER::new({ adminstore => 1 }, @_);
85
86 $self->needs('dependency', 'clamav');
87 return $self;
88 }
89
90 sub set_up
91 {
92 my ($self) = @_;
93 $self->SUPER::set_up();
94 }
95
96 sub tear_down
97 {
98 my ($self) = @_;
99 $self->SUPER::tear_down();
100 }
101
102 sub test_aaasetup
103 {
104 my ($self) = @_;
105
106 # does everything set up and tear down cleanly?
107 $self->assert(1);
108 }
109
110 # This test uses the AV engine, which can be very slow to initialise.
111 sub test_remove_infected_slow
112 :NoAltNamespace
113 {
114 my ($self) = @_;
115
116 # set up a shared folder that's easy to write to
117 my $admintalk = $self->{adminstore}->get_client();
118 $admintalk->create('shared.folder');
119 $admintalk->setacl('shared.folder', 'cassandane' => 'lrswipkxtecd');
120
121 $self->{store}->set_fetch_attributes(qw(uid flags));
122
123 my $talk = $self->{store}->get_client();
124 $talk->select("INBOX");
125 $self->assert_num_equals(1, $talk->uid());
126 $talk->select("shared.folder");
127 $self->assert_num_equals(1, $talk->uid());
128
129 # put some test messages in INBOX (and verify)
130 $self->{store}->set_folder("INBOX");
131 my %cass_exp;
132 $cass_exp{1} = $self->make_message("eicar attached", uid => 1, %eicar_attached);
133 $cass_exp{2} = $self->make_message("clean", uid => 2);
134 $self->check_messages(\%cass_exp, ( keyed_on => 'uid' ));
135
136 # put some test messages in shared.folder (and verify)
137 $self->{store}->set_folder("shared.folder");
138 my %shared_exp;
139 $shared_exp{1} = $self->make_message("eicar attached", uid => 1, %eicar_attached);
140 $shared_exp{2} = $self->make_message("clean", uid => 2);
141 $self->check_messages(\%shared_exp, ( keyed_on => 'uid' ));
142
143 # run cyr_virusscan
144 my $out = "$self->{instance}->{basedir}/$self->{_name}-cyr_virusscan.stdout";
145 $self->{instance}->run_command(
146 { cyrus => 1,
147 redirects => { 'stdout' => $out },
148 }, 'cyr_virusscan', '-r');
149
150 # check the output
151 # user.cassandane 1 UNREAD Eicar-Test-Signature
152 # shared.folder 1 UNREAD Eicar-Test-Signature
153 $out = slurp_file($out);
154 xlog $self, $out;
155
156 # XXX is there a better way than hard coding UID:1 ?
157 my ($v) = Cassandane::Instance->get_version();
158 if ($v >= 3) {
159 $self->assert_matches(
160 qr/user\.cassandane\s+1\s+UNREAD\s+Eicar(?:-Test){0,1}-Signature/,
161 $out);
162 $self->assert_matches(
163 qr/shared\.folder\s+1\s+UNREAD\s+Eicar(?:-Test){0,1}-Signature/,
164 $out);
165 }
166 else {
167 # pre-3.0 a different output format was used
168 $self->assert_matches(
169 qr/Working\son\sshared\.folder\.\.\.\nVirus\sdetected\sin\smessage\s1:\sEicar(?:-Test){0,1}-Signature/,
170 $out);
171 $self->assert_matches(
172 qr/Working\son\suser\.cassandane\.\.\.\nVirus\sdetected\sin\smessage\s1:\sEicar(?:-Test){0,1}-Signature/,
173 $out);
174 }
175
176 # make sure the infected ones were expunged, but the clean ones weren't
177 $self->{store}->set_folder("INBOX");
178 delete $cass_exp{1};
179 $self->check_messages(\%cass_exp, ( keyed_on => 'uid' ));
180
181 $self->{store}->set_folder("shared.folder");
182 delete $shared_exp{1};
183 $self->check_messages(\%shared_exp, ( keyed_on => 'uid' ));
184 }
185
186 # This test uses the '-s search-string' invocation, which is much faster
187 # than waiting for the AV engine to load when we just care about whether
188 # the notification gets sent
189 sub test_notify_deleted
190 {
191 my ($self) = @_;
192
193 $self->{store}->set_fetch_attributes(qw(uid flags));
194
195 # put some test messages in INBOX (and verify)
196 $self->{store}->set_folder("INBOX");
197 my %cass_exp;
198 $cass_exp{1} = $self->make_message("custom header 1", uid => 1, %custom_header);
199 $cass_exp{2} = $self->make_message("custom header 2", uid => 2, %custom_header);
200 $self->check_messages(\%cass_exp, ( keyed_on => 'uid' ));
201
202 # run cyr_virusscan
203 $self->{instance}->run_command({ cyrus => 1, },
204 'cyr_virusscan', '-r', '-n',
205 '-s', 'header "x-delete-me" "please"');
206
207 # let's see what's in there now
208 my $found_notifications = 0;
209 $self->{store}->read_begin();
210 while (my $msg = $self->{store}->read_message()) {
211 # should not be any of our test messages remaining
212 $self->assert_null($msg->get_header('x-cassandane-unique'));
213
214 # if we find something that looks like a notification, check it
215 if ($msg->get_header('message-id') =~ m{^<cmu-cyrus-\d+-\d+-\d+\@}) {
216 $found_notifications ++;
217
218 my $body = $msg->get_body();
219 # xlog $self, "body:\n>>>>>>\n$body<<<<<<";
220
221 # make sure report body includes all our infected tests
222 foreach my $exp (values %cass_exp) {
223 my $message_id = $exp->get_header('message-id');
224 $self->assert_matches(qr/Message-ID: $message_id/, $body);
225
226 my $subject = $exp->get_header('subject');
227 $self->assert_matches(qr/Subject: $subject/, $body);
228
229 my $uid = $exp->get_attribute('uid');
230 $self->assert_matches(qr/IMAP UID: $uid/, $body);
231 }
232
233 # make sure the message was removed for the reason we expect
234 $self->assert_matches(qr/Cyrus Administrator Targeted Removal/,
235 $body);
236 }
237 }
238 $self->{store}->read_end();
239
240 # finally, there should've been exactly one notification email sent
241 $self->assert_num_equals(1, $found_notifications);
242 }
243
244 # This test uses the '-s search-string' invocation, which is much faster
245 # than waiting for the AV engine to load when we just care about whether
246 # the custom notification gets sent
247 # XXX https://github.com/cyrusimap/cyrus-imapd/issues/2516 might be
248 # XXX backported to 3.0 if anyone volunteers to test it
249 sub test_custom_notify_deleted
250 :NoStartInstances
251 :min_version_3_1
252 {
253 my ($self) = @_;
254
255 # set up a custom notification template
256 $self->{instance}->{config}->set(
257 virusscan_notification_subject => 'custom ½ subject',
258 virusscan_notification_template =>
259 abs_path('data/custom-notification-template'),
260 );
261 $self->_start_instances();
262
263 $self->{store}->set_fetch_attributes(qw(uid flags));
264
265 # put some test messages in INBOX (and verify)
266 $self->{store}->set_folder("INBOX");
267 my %cass_exp;
268 $cass_exp{1} = $self->make_message("custom header 1", uid => 1, %custom_header);
269 $cass_exp{2} = $self->make_message("custom header 2", uid => 2, %custom_header);
270 $self->check_messages(\%cass_exp, ( keyed_on => 'uid' ));
271
272 # run cyr_virusscan
273 $self->{instance}->run_command({ cyrus => 1, },
274 'cyr_virusscan', '-r', '-n',
275 '-s', 'header "x-delete-me" "please"');
276
277 # let's see what's in there now
278 my $found_notifications = 0;
279 $self->{store}->read_begin();
280 while (my $msg = $self->{store}->read_message()) {
281 # should not be any of our test messages remaining
282 $self->assert_null($msg->get_header('x-cassandane-unique'));
283
284 # if we find something that looks like a notification, check it
285 if ($msg->get_header('message-id') =~ m{^<cmu-cyrus-\d+-\d+-\d+\@}) {
286 $found_notifications ++;
287
288 my $subject = $msg->get_header('subject');
289 # xlog $self, "subject: $subject";
290
291 # make sure our custom subject was used (and correctly encoded)
292 $self->assert_str_equals('=?UTF-8?Q?custom_=C2=BD_subject?=',
293 $subject);
294
295 my $body = $msg->get_body();
296 # xlog $self, "body:\n>>>>>>\n$body<<<<<<";
297
298 # make sure report body includes all our infected tests
299 foreach my $exp (values %cass_exp) {
300 my $message_id = $exp->get_header('message-id');
301 $self->assert_matches(qr/Message-ID: $message_id/, $body);
302
303 my $subject = $exp->get_header('subject');
304 $self->assert_matches(qr/Subject: $subject/, $body);
305
306 my $uid = $exp->get_attribute('uid');
307 $self->assert_matches(qr/IMAP UID: $uid/, $body);
308 }
309
310 # make sure the message was removed for the reason we expect
311 $self->assert_matches(qr/Cyrus Administrator Targeted Removal/,
312 $body);
313
314 # make sure our custom notification template was used
315 $self->assert_matches(qr/^custom notification!/, $body);
316
317 # make sure message was qp-encoded
318 $self->assert_matches(qr/with =C2=BD as much 8bit/, $body);
319 }
320 }
321 $self->{store}->read_end();
322
323 # finally, there should've been exactly one notification email sent
324 $self->assert_num_equals(1, $found_notifications);
325 }
326
327 1;