Debian Codegraph @

cassandane/Cassandane/Cyrus/IMAPLimits.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2023 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::IMAPLimits;
41 use strict;
42 use warnings;
43 use Mail::JMAPTalk 0.13;
44 use Data::Dumper;
45
46 use lib '.';
47 use base qw(Cassandane::Cyrus::TestCase);
48 use Cassandane::Util::Log;
49
50 my $email = <<EOF;
51 Subject: foo
52 Date: bar
53 From: <foobar\@example.com>
54
55 Body
56 EOF
57
58 $email =~ s/\r?\n/\r\n/gs;
59
60 my $toobig_email = $email . "X" x 100;
61
62 # Check that we got an untagged BYE [TOOBIG] response
63 sub assert_bye_toobig
64 {
65 my ($self, $store) = @_;
66
67 $store = $self->{store} if (!defined $store);
68
69 # We want to override Mail::IMAPTalk's builtin handling of the BYE
70 # untagged response, as it will 'die' immediately without parsing
71 # the remainder of the line and especially without picking out the
72 # [TOOBIG] response code that we want to see.
73 my $got_toobig = 0;
74 my $handlers =
75 {
76 bye => sub
77 {
78 my (undef, $resp) = @_;
79 $got_toobig = 1 if (uc($resp->[0]) eq '[TOOBIG]');
80 }
81 };
82
83 $store->idle_response($handlers, 1);
84 $self->assert_num_equals(1, $got_toobig);
85 }
86
87 # Send a command and expect an untagged BYE [TOOBIG] response
88 sub assert_cmd_bye_toobig
89 {
90 my $self = shift;
91 my $cmd = shift;
92
93 my $talk = $self->{store}->get_client();
94 $talk->enable('qresync'); # IMAPTalk requires lower-case
95 $talk->select('INBOX');
96
97 $talk->_send_cmd($cmd, @_);
98 $self->assert_bye_toobig();
99 }
100
101 # Check that we got a tagged NO [TOOBIG] response
102 sub assert_no_toobig
103 {
104 my ($self, $talk) = @_;
105
106 my $got_toobig = 0;
107 my $handlers =
108 {
109 'no' => sub
110 {
111 my (undef, $resp) = @_;
112 $got_toobig = 1 if (uc($resp->[0]) eq '[TOOBIG]');
113 }
114 };
115
116 eval {
117 $talk->_parse_response($handlers);
118 };
119
120 $self->assert_num_equals(1, $got_toobig);
121 }
122
123 # Send a command and expect a tagged NO [TOOBIG] response
124 sub assert_cmd_no_toobig
125 {
126 my $self = shift;
127 my $talk = shift;
128 my $cmd = shift;
129
130 $talk->_send_cmd($cmd, @_);
131 $self->assert_no_toobig($talk);
132 }
133
134 sub new
135 {
136 my $class = shift;
137
138 my $config = Cassandane::Config->default()->clone();
139 $config->set(maxword => 25);
140 $config->set(maxquoted => 25);
141 $config->set(maxliteral => 25);
142 $config->set(literalminus => 1);
143 $config->set(maxargssize => 45);
144 $config->set(maxmessagesize => 100);
145 $config->set(event_groups => "message mailbox applepushservice");
146 $config->set(aps_topic => "mail");
147
148 return $class->SUPER::new({
149 adminstore => 1,
150 config => $config,
151 services => ['imap'],
152 }, @_);
153 }
154
155 sub set_up
156 {
157 my ($self) = @_;
158 $self->SUPER::set_up();
159 }
160
161 sub tear_down
162 {
163 my ($self) = @_;
164 $self->SUPER::tear_down();
165 }
166
167 sub test_maxword
168 {
169 my ($self) = @_;
170
171 # Oversized command name
172 $self->assert_cmd_bye_toobig("X" x 26);
173 }
174
175 sub test_maxword_astring
176 {
177 my ($self) = @_;
178
179 # Oversized mailbox name
180 $self->assert_cmd_bye_toobig('SELECT', "X" x 26);
181 }
182
183 sub test_maxquoted
184 {
185 my ($self) = @_;
186
187 # Oversized mailbox name
188 $self->assert_cmd_bye_toobig('SELECT', { Quote => "X" x 26 });
189 }
190
191 sub test_maxliteral_nosync
192 {
193 my ($self) = @_;
194
195 my $talk = $self->{store}->get_client();
196 # Do this by brute force until we have IMAPTalk v4.06+
197 $talk->_imap_socket_out($talk->{CmdId}++ . " SELECT {26+}\015\012");
198 $self->assert_bye_toobig();
199 }
200
201 sub test_maxliteral_sync
202 {
203 my ($self) = @_;
204
205 # Unlike oversized non-sync literals which fatal() in one central location,
206 # oversized sync literals fail with a NO response in multiple places,
207 # so we test as many of those places as possible.
208 # Having said that, arguments parsed in cmdloop() or in get_search_criterion()
209 # are mostly handled centrally.
210
211 # Authenticated State
212
213 # Synchronizing literals are the default in IMAPTalk v4.05 (and earlier)
214 my $talk = $self->{store}->get_client(NoLiteralPlus => 1);
215
216 $self->assert_cmd_no_toobig($talk, 'SELECT',
217 { Literal => "X" x 26 });
218
219 $self->assert_cmd_no_toobig($talk, 'ID',
220 [ { Literal => "X" x 26 } ]);
221
222 $self->assert_cmd_no_toobig($talk, 'ID',
223 [ { Quote => 'foo' }, { Literal => "X" x 26 } ] );
224
225 $self->assert_cmd_no_toobig($talk, 'LIST',
226 { Literal => "X" x 26 });
227
228 $self->assert_cmd_no_toobig($talk, 'LIST',
229 { Quote => '' }, { Literal => "X" x 26 });
230
231 $self->assert_cmd_no_toobig($talk, 'NOTIFY',
232 'SET', [ 'MAILBOXES', [ { Literal => "X" x 26 } ] ] );
233
234 $self->assert_cmd_no_toobig($talk, 'LISTRIGHTS',
235 'INBOX', { Literal => "X" x 26 });
236
237 $self->assert_cmd_no_toobig($talk, 'SETACL',
238 'INBOX', 'anyone', { Literal => "X" x 26 });
239
240 $self->assert_cmd_no_toobig($talk, 'GETMETADATA',
241 'INBOX', { Literal => "X" x 26 } );
242
243 $self->assert_cmd_no_toobig($talk, 'GETMETADATA',
244 'INBOX', [ { Literal => "X" x 26 } ] );
245
246 $self->assert_cmd_no_toobig($talk, 'SETMETADATA',
247 'INBOX', [ { Literal => "X" x 26 } ] );
248
249 $self->assert_cmd_no_toobig($talk, 'SETMETADATA',
250 'INBOX', [ '/comment', { Literal => "X" x 26 } ] );
251
252 $self->assert_cmd_no_toobig($talk, 'XAPPLEPUSHSERVICE',
253 { Literal => "X" x 26 });
254
255 $self->assert_cmd_no_toobig($talk, 'XAPPLEPUSHSERVICE',
256 'FOO', { Literal => "X" x 26 });
257
258 # Selected State
259 $talk->select('INBOX');
260
261 $self->assert_cmd_no_toobig($talk, 'FETCH',
262 '1', [ 'ANNOTATION',
263 [ { Literal => "X" x 26 } ] ] );
264
265 $self->assert_cmd_no_toobig($talk, 'FETCH',
266 '1', [ 'BODY[HEADER.FIELDS',
267 [ { Literal => "X" x 26 } ] ] );
268
269 $self->assert_cmd_no_toobig($talk, 'FETCH',
270 '1', [ 'RFC822.HEADER.LINES',
271 [ { Literal => "X" x 26 } ] ] );
272
273 $self->assert_cmd_no_toobig($talk, 'STORE',
274 '1', 'ANNOTATION', [ { Literal => "X" x 26 } ] );
275
276 $self->assert_cmd_no_toobig($talk, 'STORE',
277 '1', 'ANNOTATION',
278 [ { Quote => '/comment' },
279 [ { Literal => "X" x 26 } ] ] );
280
281 $self->assert_cmd_no_toobig($talk, 'STORE',
282 '1', 'ANNOTATION',
283 [ { Quote => '/comment' },
284 [ { Quote => 'value' },
285 { Literal => "X" x 26 } ] ] );
286
287 $self->assert_cmd_no_toobig($talk, 'SEARCH',
288 'HEADER', { Literal => "X" x 26 } );
289
290 $self->assert_cmd_no_toobig($talk, 'SEARCH',
291 'HEADER', 'SUBJECT', { Literal => "X" x 26 } );
292
293 $self->assert_cmd_no_toobig($talk, 'SEARCH',
294 'ANNOTATION', { Literal => "X" x 26 } );
295
296 $self->assert_cmd_no_toobig($talk, 'SEARCH',
297 'ANNOTATION', '/comment', { Literal => "X" x 26 } );
298
299 $self->assert_cmd_no_toobig($talk, 'SEARCH',
300 'ANNOTATION', '/comment',
301 'value', { Literal => "X" x 26 } );
302
303 $self->assert_cmd_no_toobig($talk, 'ESEARCH',
304 'IN', [ 'MAILBOXES', { Literal => "X" x 26 } ] );
305 }
306
307 sub test_maxargssize_append_flags
308 {
309 my ($self) = @_;
310
311 $self->assert_cmd_bye_toobig('APPEND', 'INBOX',
312 [ "X" x 25, "X" x 25 ], { Literal => $email } );
313 }
314
315 sub test_maxargssize_append_annot
316 {
317 my ($self) = @_;
318
319 # Use MULTIAPPEND, fail the second
320 $self->assert_cmd_bye_toobig('APPEND', 'INBOX',
321 { Literal => $email },
322 'ANNOTATION',
323 [ "X" x 25, [ 'VALUE', { Quote => "X" x 25 } ] ],
324 { Literal => $email } );
325 }
326
327 sub test_maxargssize_create
328 {
329 my ($self) = @_;
330
331 $self->assert_cmd_bye_toobig('CREATE', "X" x 25, [ "X" x 25 ] );
332 }
333
334 sub test_maxargssize_create_ext
335 {
336 my ($self) = @_;
337
338 $self->assert_cmd_bye_toobig('CREATE',
339 "X" x 5, [ "X" x 5, [ "X" x 25, "X" x 25 ] ] );
340 }
341
342 sub test_maxargssize_fetch
343 {
344 my ($self) = @_;
345
346 $self->assert_cmd_bye_toobig('FETCH', '1',
347 [ 'BODY', 'ENVELOPE', 'FLAGS',
348 'INTERNALDATE', 'RFC822.SIZE' ]);
349 }
350
351 sub test_maxargssize_fetch_annot
352 {
353 my ($self) = @_;
354
355 $self->assert_cmd_bye_toobig('FETCH', '1',
356 [ 'ANNOTATION',
357 [ [ "X" x 25, "X" x 25 ] ], "X" x 5 ] );
358 }
359
360 sub test_maxargssize_fetch_annot2
361 {
362 my ($self) = @_;
363
364 $self->assert_cmd_bye_toobig('FETCH', '1',
365 [ 'ANNOTATION',
366 [ "X" x 5, [ "X" x 25, "X" x 25 ] ] ] );
367 }
368
369 sub test_maxargssize_fetch_headers
370 {
371 my ($self) = @_;
372
373 $self->assert_cmd_bye_toobig('FETCH', '1',
374 [ 'BODY[HEADER.FIELDS', [ "X" x 25, "X" x 25 ] ] );
375 }
376
377 sub test_maxargssize_getmetadata
378 {
379 my ($self) = @_;
380
381 $self->assert_cmd_bye_toobig('GETMETADATA', 'INBOX', [ "X" x 25, "X" x 25 ] );
382 }
383
384 sub test_maxargssize_list_multi
385 {
386 my ($self) = @_;
387
388 $self->assert_cmd_bye_toobig('LIST', { Quote => '' }, [ "X" x 25, "X" x 25 ]);
389 }
390
391 sub test_maxargssize_list_select
392 {
393 my ($self) = @_;
394
395 $self->assert_cmd_bye_toobig('LIST',
396 [ 'SUBSCRIBED', 'REMOTE',
397 'RECURSIVEMATCH', 'SPECIAL-USE' ],
398 { Quote => '' }, '*');
399 }
400
401 sub test_maxargssize_list_return
402 {
403 my ($self) = @_;
404
405 $self->assert_cmd_bye_toobig('LIST',
406 { Quote => '' }, '*', 'RETURN',
407 [ 'SUBSCRIBED', 'CHILDREN',
408 'MYRIGHTS', 'SPECIAL-USE' ] );
409 }
410
411 sub test_maxargssize_notify_events
412 {
413 my ($self) = @_;
414
415 $self->assert_cmd_bye_toobig('NOTIFY', 'SET',
416 [ 'SELECTED',
417 [ 'MessageNew', 'MessageExpunge', 'FlagChange' ] ] );
418 }
419
420 sub test_maxargssize_notify_multi
421 {
422 my ($self) = @_;
423
424 $self->assert_cmd_bye_toobig('NOTIFY', 'SET',
425 [ 'PERSONAL', 'NONE' ],
426 [ 'SELECTED', 'NONE' ],
427 [ 'SUBSCRIBED', 'NONE' ] );
428 }
429
430 sub test_maxargssize_notify_subtree
431 {
432 my ($self) = @_;
433
434 $self->assert_cmd_bye_toobig('NOTIFY', 'SET',
435 [ 'SUBTREE', [ "X" x 25, "X" x 25 ] ] );
436 }
437
438 sub test_maxargssize_search
439 {
440 my ($self) = @_;
441
442 $self->assert_cmd_bye_toobig('SEARCH',
443 'TEXT', "X" x 25, 'TEXT', { Quote => "X" x 25 } );
444 }
445
446 sub test_maxargssize_multisearch
447 {
448 my ($self) = @_;
449
450 $self->assert_cmd_bye_toobig('ESEARCH',
451 'IN', [ 'MAILBOXES', [ "X" x 25, "X" x 25 ] ]);
452 }
453
454 sub test_maxargssize_select
455 {
456 my ($self) = @_;
457
458 $self->assert_cmd_bye_toobig('SELECT', 'INBOX',
459 [ 'QRESYNC', [ '1234567890', '1234567890' ],
460 'ANNOTATE' ] );
461 }
462
463 sub test_maxargssize_setmetadata
464 {
465 my ($self) = @_;
466
467 $self->assert_cmd_bye_toobig('SETMETADATA', 'INBOX',
468 [ "X" x 25, { Quote => "X" x 25 } ] );
469 }
470
471 sub test_maxargssize_setmetadata2
472 {
473 my ($self) = @_;
474
475 $self->assert_cmd_bye_toobig('SETMETADATA', 'INBOX',
476 [ '/shared', { Quote => "X" x 25 },
477 '/shared', { Quote => "X" x 25 } ] );
478 }
479
480 sub test_maxargssize_setquota
481 {
482 my ($self) = @_;
483
484 my $store = $self->{adminstore};
485 my $talk = $store->get_client();
486
487 $talk->_send_cmd('SETQUOTA', 'user.cassandane',
488 [ 'STORAGE', '1234567890',
489 'MESSAGE', '1234567890',
490 'MAILBOX', '1234567890' ] );
491 $self->assert_bye_toobig($store);
492 }
493
494 sub test_maxargssize_sort
495 {
496 my ($self) = @_;
497
498 $self->assert_cmd_bye_toobig('SORT',
499 [ 'ARRIVAL', 'CC', 'DATE',
500 'FROM', 'REVERSE', 'SIZE', 'TO' ],
501 'UTF-8', 'ALL');
502 }
503
504 sub test_maxargssize_status
505 {
506 my ($self) = @_;
507
508 $self->assert_cmd_bye_toobig('STATUS', 'INBOX',
509 [ 'MESSAGES', 'UIDNEXT',
510 'UIDVALIDITY', 'UNSEEN', 'SIZE' ] );
511 }
512
513 sub test_maxargssize_store_annot
514 {
515 my ($self) = @_;
516
517 $self->assert_cmd_bye_toobig('STORE', '1', 'ANNOTATION',
518 [ "X" x 25, [ 'VALUE', { Quote => "X" x 25 } ] ] );
519 }
520
521 sub test_maxargssize_store_annot2
522 {
523 my ($self) = @_;
524
525 $self->assert_cmd_bye_toobig('STORE', '1', 'ANNOTATION',
526 [ "X" x 5, [ 'VALUE', { Quote => "X" x 25 } ],
527 "X" x 5, [ 'VALUE', { Quote => "X" x 25 } ] ] );
528 }
529
530 sub test_append_zero
531 {
532 my ($self) = @_;
533
534 my $talk = $self->{store}->get_client();
535 $talk->_imap_cmd('APPEND', 0, '', 'INBOX', { Literal => '' } );
536 $self->assert_str_equals('no', $talk->get_last_completion_response());
537 }
538
539 sub test_maxmessagesize_sync_literal
540 {
541 my ($self) = @_;
542
543 # Synchronizing literals are the default in IMAPTalk v4.05 (and earlier)
544 my $talk = $self->{store}->get_client(NoLiteralPlus => 1);
545
546 $self->assert_cmd_no_toobig($talk, 'APPEND',
547 'INBOX', { Literal => $toobig_email } );
548 }
549
550 sub test_maxmessagesize_nosync_literal
551 {
552 my ($self) = @_;
553
554 my $talk = $self->{store}->get_client();
555 # Do this by brute force until we have IMAPTalk v4.06+
556 $talk->_imap_socket_out($talk->{CmdId}++ . " APPEND INBOX {101+}\015\012");
557 $self->assert_no_toobig($talk);
558 $self->assert_bye_toobig();
559 }
560
561 sub test_literal_minus
562 {
563 my ($self) = @_;
564
565 my $talk = $self->{store}->get_client();
566 $talk->_imap_socket_out($talk->{CmdId}++ . " APPEND INBOX {4097+}\015\012");
567 $self->assert_no_toobig($talk);
568 $self->assert_bye_toobig();
569 }
570
571 1;