about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorEric Wong <e@yhbt.net>2020-05-10 06:21:04 +0000
committerEric Wong <e@yhbt.net>2020-05-10 06:56:42 +0000
commit1dde99e0badebfaf509cee4f15bbfa284996c8fc (patch)
tree34a8fb6e89397530abbabe1223f6990844af029a /lib
parent8c3016372fb40ad3f19bc69c90e49cbd5a5f081f (diff)
downloadpublic-inbox-1dde99e0badebfaf509cee4f15bbfa284996c8fc.tar.gz
While our header processing is more efficient than
Email::*::Header, capping the maximum size for a `m//g' match
still limits memory growth on a header we care for.

Use the same limit as postfix (header_size_limit=102400), since
messages fetched via git/HTTP/NNTP/etc can bypass MTA limits.
Diffstat (limited to 'lib')
-rw-r--r--lib/PublicInbox/Eml.pm23
1 files changed, 22 insertions, 1 deletions
diff --git a/lib/PublicInbox/Eml.pm b/lib/PublicInbox/Eml.pm
index f022516c..2ccbb659 100644
--- a/lib/PublicInbox/Eml.pm
+++ b/lib/PublicInbox/Eml.pm
@@ -41,6 +41,7 @@ $PublicInbox::EmlContentFoo::STRICT_PARAMS = 0;
 our $MAXPARTS = 1000; # same as SpamAssassin
 our $MAXDEPTH = 20; # seems enough, Perl sucks, here
 our $MAXBOUNDLEN = 2048; # same as postfix
+our $header_size_limit = 102400; # same as postfix
 
 my %MIME_ENC = (qp => \&enc_qp, base64 => \&encode_base64);
 my %MIME_DEC = (qp => \&dec_qp, base64 => \&decode_base64);
@@ -68,6 +69,22 @@ sub re_memo ($) {
                                         /ismx
 }
 
+sub hdr_truncate ($) {
+        my $len = length($_[0]);
+        substr($_[0], $header_size_limit, $len) = '';
+        my $end = rindex($_[0], "\n");
+        if ($end >= 0) {
+                ++$end;
+                substr($_[0], $end, $len) = '';
+                warn "header of $len bytes truncated to $end bytes\n";
+        } else {
+                $_[0] = '';
+                warn <<EOF
+header of $len bytes without `\\n' within $header_size_limit ignored
+EOF
+        }
+}
+
 # compatible with our uses of Email::MIME
 sub new {
         my $ref = ref($_[1]) ? $_[1] : \(my $cpy = $_[1]);
@@ -81,14 +98,18 @@ sub new {
                 # likely on *nix
                 my $hdr = substr($$ref, 0, $pos + 2, ''); # sv_chop on $$ref
                 chop($hdr); # lower SvCUR
+                hdr_truncate($hdr) if length($hdr) > $header_size_limit;
                 bless { hdr => \$hdr, crlf => "\n", bdy => $ref }, __PACKAGE__;
         } elsif ($$ref =~ /\r?\n(\r?\n)/s) {
                 my $hdr = substr($$ref, 0, $+[0], ''); # sv_chop on $$ref
                 substr($hdr, -(length($1))) = ''; # lower SvCUR
+                hdr_truncate($hdr) if length($hdr) > $header_size_limit;
                 bless { hdr => \$hdr, crlf => $1, bdy => $ref }, __PACKAGE__;
         } elsif ($$ref =~ /^[a-z0-9-]+[ \t]*:/ims && $$ref =~ /(\r?\n)\z/s) {
                 # body is optional :P
-                bless { hdr => \($$ref), crlf => $1 }, __PACKAGE__;
+                my $hdr = substr($$ref, 0, $header_size_limit + 1);
+                hdr_truncate($hdr) if length($hdr) > $header_size_limit;
+                bless { hdr => \$hdr, crlf => $1 }, __PACKAGE__;
         } else { # nothing useful
                 my $hdr = $$ref = '';
                 bless { hdr => \$hdr, crlf => "\n" }, __PACKAGE__;