@@ -268,6 +268,40 @@ def self.[](config)
268
268
false , :when_capabilities_cached , true
269
269
]
270
270
271
+ # The maximum allowed server response size. When +nil+, there is no limit
272
+ # on response size.
273
+ #
274
+ # The default value (512 MiB, since +v0.5.7+) is <em>very high</em> and
275
+ # unlikely to be reached. A _much_ lower value should be used with
276
+ # untrusted servers (for example, when connecting to a user-provided
277
+ # hostname). When using a lower limit, message bodies should be fetched
278
+ # in chunks rather than all at once.
279
+ #
280
+ # <em>Please Note:</em> this only limits the size per response. It does
281
+ # not prevent a flood of individual responses and it does not limit how
282
+ # many unhandled responses may be stored on the responses hash. See
283
+ # Net::IMAP@Unbounded+memory+use.
284
+ #
285
+ # Socket reads are limited to the maximum remaining bytes for the current
286
+ # response: max_response_size minus the bytes that have already been read.
287
+ # When the limit is reached, or reading a +literal+ _would_ go over the
288
+ # limit, ResponseTooLargeError is raised and the connection is closed.
289
+ #
290
+ # Note that changes will not take effect immediately, because the receiver
291
+ # thread may already be waiting for the next response using the previous
292
+ # value. Net::IMAP#noop can force a response and enforce the new setting
293
+ # immediately.
294
+ #
295
+ # ==== Versioned Defaults
296
+ #
297
+ # Net::IMAP#max_response_size <em>was added in +v0.2.5+ and +v0.3.9+ as an
298
+ # attr_accessor, and in +v0.4.20+ and +v0.5.7+ as a delegator to this
299
+ # config attribute.</em>
300
+ #
301
+ # * original: +nil+ <em>(no limit)</em>
302
+ # * +0.5+: 512 MiB
303
+ attr_accessor :max_response_size , type : Integer?
304
+
271
305
# Controls the behavior of Net::IMAP#responses when called without any
272
306
# arguments (+type+ or +block+).
273
307
#
@@ -446,6 +480,7 @@ def defaults_hash
446
480
idle_response_timeout : 5 ,
447
481
sasl_ir : true ,
448
482
enforce_logindisabled : true ,
483
+ max_response_size : 512 << 20 , # 512 MiB
449
484
responses_without_block : :warn ,
450
485
parser_use_deprecated_uidplus_data : :up_to_max_size ,
451
486
parser_max_deprecated_uidplus_data_size : 100 ,
@@ -459,6 +494,7 @@ def defaults_hash
459
494
sasl_ir : false ,
460
495
responses_without_block : :silence_deprecation_warning ,
461
496
enforce_logindisabled : false ,
497
+ max_response_size : nil ,
462
498
parser_use_deprecated_uidplus_data : true ,
463
499
parser_max_deprecated_uidplus_data_size : 10_000 ,
464
500
) . freeze
@@ -474,6 +510,7 @@ def defaults_hash
474
510
475
511
version_defaults [ 0.5 r] = Config [ 0.4 r] . dup . update (
476
512
enforce_logindisabled : true ,
513
+ max_response_size : 512 << 20 , # 512 MiB
477
514
responses_without_block : :warn ,
478
515
parser_use_deprecated_uidplus_data : :up_to_max_size ,
479
516
parser_max_deprecated_uidplus_data_size : 100 ,
0 commit comments