[Rt-commit] rt branch 4.4/message-attachment-original-content created. rt-4.4.6-36-gaf39698745

BPS Git Server git at git.bestpractical.com
Fri Sep 2 20:33:56 UTC 2022


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt".

The branch, 4.4/message-attachment-original-content has been created
        at  af39698745921ae47954580011dcd0b4996aba17 (commit)

- Log -----------------------------------------------------------------
commit af39698745921ae47954580011dcd0b4996aba17
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Sat Sep 3 02:57:59 2022 +0800

    Test textual and UTF-8 encoded "message/..." attachments
    
    It covers both cases where $TreatAttachedEmailAsFiles is true or false.

diff --git a/t/mail/rfc822-attachment.t b/t/mail/rfc822-attachment.t
index f498ec55ae..f66584f737 100644
--- a/t/mail/rfc822-attachment.t
+++ b/t/mail/rfc822-attachment.t
@@ -19,21 +19,24 @@ diag "simple rfc822 attachment";
         From    => 'foo at localhost',
         To      => 'bar at localhost',
         Subject => 'rfc822',
-        Data    => ['rfc822 attachment'],
+        Data    => ['rfc822 attachment 测试'],
+        Charset => 'UTF-8',
         'X-Brokenness' => 'high',
     );
 
     $top->attach(
         Data => $rfc822->stringify,
         Type => 'message/rfc822',
+        Charset => 'UTF-8',
     );
 
-    my $parsed = content_as_mime($top);
-
-    for my $mime ($top, $parsed) {
+    for my $mime ( $top, contents_as_mime($top) ) {
         diag "testing mail";
         is $mime->parts, 2, 'two mime parts';
 
+        eval { $mime->as_string };
+        ok( !$@, 'stringifying mime does not die' );
+
         like $mime->head->get('Subject'), qr/this is top/, 'top subject';
         like $mime->head->get('From'), qr/root\@localhost/, 'top From';
         like $mime->parts(0)->bodyhandle->as_string, qr/top mail/, 'content of top';
@@ -42,7 +45,7 @@ diag "simple rfc822 attachment";
         my $body   = $attach->bodyhandle->as_string;
 
         like $attach->head->mime_type, qr/message\/rfc822/, 'attach of type message/rfc822';
-        like $body, qr/rfc822 attachment/, 'attach content';
+        like $body, qr/rfc822 attachment 测试/, 'attach content';
 
         headers_like(
             $attach,
@@ -67,25 +70,28 @@ diag "multipart rfc822 attachment";
         From    => 'foo at localhost',
         To      => 'bar at localhost',
         Subject => 'rfc822',
-        Data    => ['rfc822 attachment'],
+        Data    => ['rfc822 attachment 测试附件'],
+        Charset => 'UTF-8',
         'X-Brokenness' => 'high',
     );
 
     $rfc822->attach(
-        Data => '<b>attachment of rfc822 attachment</b>',
+        Data => ['<b>attachment of rfc822 attachment 测试</b>'],
         Type => 'text/html',
+        Charset  => 'UTF-8',
     );
 
     $top->attach(
         Data => $rfc822->stringify,
         Type => 'message/rfc822',
+        Charset => 'UTF-8',
     );
-    
-    my $parsed = content_as_mime($top);
 
-    for my $mime ($top, $parsed) {
+    for my $mime ( $top, contents_as_mime($top) ) {
         diag "testing mail";
         is $mime->parts, 2, 'two mime parts';
+        eval { $mime->as_string };
+        ok( !$@, 'stringifying mime does not die' );
 
         like $mime->head->get('Subject'), qr/this is top/, 'top subject';
         like $mime->head->get('From'), qr/root\@localhost/, 'top From';
@@ -95,8 +101,9 @@ diag "multipart rfc822 attachment";
         my $body   = $attach->bodyhandle->as_string;
 
         like $attach->head->mime_type, qr/message\/rfc822/, 'attach of type message/rfc822';
-        like $body, qr/rfc822 attachment/, 'attach content';
-        like $body, qr/attachment of rfc822 attachment/, 'attach content';
+        like $body, qr/rfc822 attachment 测试附件/, 'attach content';
+
+        like $body, qr/attachment of rfc822 attachment 测试/, 'attach content';
 
         headers_like(
             $attach,
@@ -119,6 +126,15 @@ sub content_as_mime {
     return RT::Test->last_ticket->Transactions->First->Attachments->First->ContentAsMIME(Children => 1);
 }
 
+sub contents_as_mime {
+    my $entity = shift;
+    RT->Config->Set( 'TreatAttachedEmailAsFiles', 1 );
+    my @contents = content_as_mime($entity);
+    RT->Config->Set( 'TreatAttachedEmailAsFiles', 0 );
+    push @contents, content_as_mime($entity);
+    return @contents;
+}
+
 sub headers_like {
     my $attach = shift;
     my %header = (@_);

commit b122dc266a479197d96a8b3b34adfd60232e0777
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Sat Sep 3 02:55:40 2022 +0800

    Encode content for textual "message/..." attachments
    
    If $TreatAttachedEmailAsFiles is true, message-like attachments wouldn't
    be split into parts in RT database, in which case "OriginalContent"
    could return decoded strings(with utf-8 flag on), which "ContentAsMIME"
    doesn't like(it feeds OriginalContent to MIME::Body::Scalar) and could
    cause the following warning(when there are some non-ascii chars):
    
        Strings with code points over 0xFF may not be mapped into in-memory file handles
    
    Even worse, it dies if you stringify the returned MIME::Entity object:
    
        open body: Invalid argument at .../MIME/Entity.pm line 1897.
    
    This commit fixes the issue by encoding content accordingly.

diff --git a/lib/RT/Attachment.pm b/lib/RT/Attachment.pm
index e51a9b7073..858c42907f 100644
--- a/lib/RT/Attachment.pm
+++ b/lib/RT/Attachment.pm
@@ -376,8 +376,14 @@ sub OriginalContent {
     if ($self->IsMessageContentType) {
         # There shouldn't be more than one "subpart" to a message/* attachment
         my $child = $self->Children->First;
-        return $self->Content unless $child and $child->id;
-        return $child->ContentAsMIME(Children => 1)->as_string;
+        if ( $child and $child->id ) {
+            return $child->ContentAsMIME( Children => 1 )->as_string;
+        }
+        else {
+            # No children could happen if $TreatAttachedEmailAsFiles is true.
+            # Can't indiscriminately return $self->Content as it might be decoded(for textual messages).
+            # Leave it to the follwing code, which covers this case.
+        }
     }
 
     return $self->Content unless RT::I18N::IsTextualContentType($self->ContentType);

-----------------------------------------------------------------------


hooks/post-receive
-- 
rt


More information about the rt-commit mailing list