You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@spamassassin.apache.org by bu...@spamassassin.apache.org on 2026/09/25 02:23:55 UTC

[Bug 8429] New: Text-based handlers see raw bytes even when normalize_charset is on

https://bz.apache.org/SpamAssassin/show_bug.cgi?id=8429

            Bug ID: 8429
           Summary: Text-based handlers see raw bytes even when
                    normalize_charset is on
           Product: Spamassassin
           Version: 4.0.3
          Hardware: PC
                OS: Mac OS X
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Libraries
          Assignee: dev@spamassassin.apache.org
          Reporter: kent@mxguardian.net
  Target Milestone: Undefined

A UTF-16 .js file reached the JavaScript handler NUL-interleaved, because the
JavaScript handler called decode() instead of decode_and_normalize(), as the
HTML handler already did. The same applies to the other text-based handlers
(SVG & ICS).

_normalize() had to be expanded to support an undefined charset, because
handlers can return parts with no charset (e.g. a .js file inside a RAR
archive). decode_and_normalize() now passes an undefined charset through
instead of substituting us-ascii (the us-ascii default is kept for
normalize_charset 0).

Changes:

- Handlers that process text (JavaScript, SVG, ICS) now call
decode_and_normalize() and encode the result back to UTF-8 bytes for rules,
URIs and child parts. Binary handlers (PDF, Image, Archive) still call
decode(). SVG sets utf8_mode when given bytes, as the HTML handler does, so
entities decode to UTF-8.

- _normalize() accepts an undefined charset. For undeclared or declared-UTF-16
data it calls detect_utf16() once. Undeclared data that isn't UTF-16 goes to
the existing fallbacks.

- detect_utf16() now decides both whether data is UTF-16 and its byte order,
from the first 1024 bytes: a BOM, or NULs in at least a quarter of the code
units at one byte position and few at the other. Otherwise it returns undef.
Previously it assumed its input was UTF-16 and reported UTF-16BE even for plain
ASCII.

- A part declared as UTF-16 (charset=UTF-16, UTF-16LE or UTF-16BE) with no BOM
or NUL pattern is decoded using the declared byte order, or as big-endian for
plain UTF-16 (RFC 2781). Previously a declared LE/BE was ignored.

- Mail::SpamAssassin::HTML now passes <script> text and non-base64 data: URI
content to child parts as UTF-8 bytes, not Perl characters.

Behavior change:

- Parts with no declared charset that are UTF-16 (BOM or NUL pattern) are now
decoded. Undeclared UTF-16 with almost no ASCII and no BOM (e.g. CJK) is still
not detected.

- A declared UTF-16 part with no BOM or NUL pattern is decoded using the
declared byte order, or as big-endian. Previously the byte order was guessed
heuristically, or the part fell through to the fallbacks.

- With normalize_charset 0, nothing changes: handlers still see the part's
original bytes.

Tests: t/node_utf16.t (detection, stray NULs, the 1024-byte window, declared
UTF-16 charsets), t/handler_javascript_utf16.t, t/handler_decode_normalize.t.
The patch includes their fixtures: t/data/nice/handler_archive_utf16_bom,
handler_archive_utf16_nobom, handler_javascript_utf16_attach,
handler_javascript_html_utf8, handler_svg_utf8, handler_svg_cp1251,
handler_svg_entity and handler_ics_cp1251.

-- 
You are receiving this mail because:
You are the assignee for the bug.

[Bug 8429] Text-based handlers see raw bytes even when normalize_charset is on

Posted by bu...@spamassassin.apache.org.
https://bz.apache.org/SpamAssassin/show_bug.cgi?id=8429

Giovanni Bechis <gi...@paclan.it> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |giovanni@paclan.it

--- Comment #3 from Giovanni Bechis <gi...@paclan.it> ---
+1 on latest patch

-- 
You are receiving this mail because:
You are the assignee for the bug.

[Bug 8429] Text-based handlers see raw bytes even when normalize_charset is on

Posted by bu...@spamassassin.apache.org.
https://bz.apache.org/SpamAssassin/show_bug.cgi?id=8429

Giovanni Bechis <gi...@paclan.it> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |vincent-spamass@vinc17.net

--- Comment #7 from Giovanni Bechis <gi...@paclan.it> ---
*** Bug 8430 has been marked as a duplicate of this bug. ***

-- 
You are receiving this mail because:
You are the assignee for the bug.

[Bug 8429] Text-based handlers see raw bytes even when normalize_charset is on

Posted by bu...@spamassassin.apache.org.
https://bz.apache.org/SpamAssassin/show_bug.cgi?id=8429

Bill Cole <bi...@apache.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |billcole@apache.org

--- Comment #4 from Bill Cole <bi...@apache.org> ---
+1 on latest patch (code+test+MANIFEST)

-- 
You are receiving this mail because:
You are the assignee for the bug.

[Bug 8429] Text-based handlers see raw bytes even when normalize_charset is on

Posted by bu...@spamassassin.apache.org.
https://bz.apache.org/SpamAssassin/show_bug.cgi?id=8429

Kent Oyer <ke...@mxguardian.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kent@mxguardian.net

--- Comment #1 from Kent Oyer <ke...@mxguardian.net> ---
Created attachment 6098
  --> https://bz.apache.org/SpamAssassin/attachment.cgi?id=6098&action=edit
proposed patch

I realize this is a big change so if we need to postpone it until after 4.0.3
we can but it does address a bug in 3 handlers that prevents them from seeing
non-UTF-8 text.

+1 for me

-- 
You are receiving this mail because:
You are the assignee for the bug.

[Bug 8429] Text-based handlers see raw bytes even when normalize_charset is on

Posted by bu...@spamassassin.apache.org.
https://bz.apache.org/SpamAssassin/show_bug.cgi?id=8429

Giovanni Bechis <gi...@paclan.it> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|Undefined                   |4.0.3

-- 
You are receiving this mail because:
You are the assignee for the bug.

[Bug 8429] Text-based handlers see raw bytes even when normalize_charset is on

Posted by bu...@spamassassin.apache.org.
https://bz.apache.org/SpamAssassin/show_bug.cgi?id=8429

Giovanni Bechis <gi...@paclan.it> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|NEW                         |RESOLVED

--- Comment #8 from Giovanni Bechis <gi...@paclan.it> ---
fixed in r1938519

-- 
You are receiving this mail because:
You are the assignee for the bug.

[Bug 8429] Text-based handlers see raw bytes even when normalize_charset is on

Posted by bu...@spamassassin.apache.org.
https://bz.apache.org/SpamAssassin/show_bug.cgi?id=8429

--- Comment #6 from Kent Oyer <ke...@mxguardian.net> ---
Sending        MANIFEST
Sending        lib/Mail/SpamAssassin/HTML.pm
Sending        lib/Mail/SpamAssassin/Handler/ICS.pm
Sending        lib/Mail/SpamAssassin/Handler/JavaScript.pm
Sending        lib/Mail/SpamAssassin/Handler/SVG.pm
Sending        lib/Mail/SpamAssassin/Message/Node.pm
Adding         t/data/nice/handler_archive_utf16_bom
Adding         t/data/nice/handler_archive_utf16_nobom
Adding         t/data/nice/handler_ics_cp1251
Adding         t/data/nice/handler_javascript_html_utf8
Adding         t/data/nice/handler_javascript_utf16_attach
Adding         t/data/nice/handler_svg_cp1251
Adding         t/data/nice/handler_svg_entity
Adding         t/data/nice/handler_svg_utf8
Adding         t/handler_decode_normalize.t
Adding         t/handler_javascript_utf16.t
Sending        t/node_utf16.t
Transmitting file data .................done
Committing transaction...
Committed revision 1938519.

-- 
You are receiving this mail because:
You are the assignee for the bug.

[Bug 8429] Text-based handlers see raw bytes even when normalize_charset is on

Posted by bu...@spamassassin.apache.org.
https://bz.apache.org/SpamAssassin/show_bug.cgi?id=8429

Kent Oyer <ke...@mxguardian.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Attachment #6099|0                           |1
        is obsolete|                            |

--- Comment #5 from Kent Oyer <ke...@mxguardian.net> ---
Created attachment 6100
  --> https://bz.apache.org/SpamAssassin/attachment.cgi?id=6100&action=edit
Patch with lenient transcoding

Thanks for the upvotes. There is one minor change I need to make. Malicious
JavaScript is frequently obfuscated by splitting the code into chunks and
concatenating them back together. With UTF-16 source code, sometimes the chunks
are split in the middle of a surrogate pair. That leaves a lone surrogate on
both ends. If we transcode with FB_CROAK then it fails and falls back to
Windows-1251 and the whole thing is corrupted. 

The solution is to do lenient transcoding in cases where we're fairly certain
the data is UTF-16 (BOM or NUL byte pattern). That replaces the lone surrogates
with the Unicode replacement character (U+FFFD) but keeps most of the code
intact. 

If we're not sure about the charset, then still use FB_CROAK. 

It's a small change, plus another test case. I'll go ahead and commit, but if
it's problem let me know.

--- a/lib/Mail/SpamAssassin/Message/Node.pm
+++ b/lib/Mail/SpamAssassin/Message/Node.pm
@@ -650,13 +650,20 @@ sub _normalize {
     # Failing that, a UTF-16 label is trusted: its byte order if it names one,
     # else big-endian (RFC 2781).  Undeclared text that is not UTF-16 is left
     # to the detection fallbacks below.
+    #
+    # With a BOM or the NUL pattern the data is known to be UTF-16, so decode
+    # leniently: a lone surrogate (legal in a JavaScript string, e.g. a pair
+    # split across two string literals) becomes U+FFFD instead of failing the
+    # whole part.  A label alone is weaker evidence, so that decode stays
strict.
     my $decoder = detect_utf16( $_[0] );
+    my $check = Encode::FB_DEFAULT;
     if (!defined $decoder && $charset_declared ne '') {
       $decoder = Encode::find_encoding(
                    $charset_declared =~ /LE\z/i ? 'UTF-16LE' : 'UTF-16BE');
+      $check = Encode::FB_CROAK;
     }
     if (defined $decoder) {
-      if (eval { $rv = $decoder->decode($_[0], Encode::FB_CROAK |
Encode::LEAVE_SRC); defined $rv }) {
+      if (eval { $rv = $decoder->decode($_[0], $check | Encode::LEAVE_SRC);
defined $rv }) {
         dbg("message: decoded as charset %s, declared %s",
           $decoder->name, $charset_declared);
         utf8::encode($rv) if !$return_decoded;

-- 
You are receiving this mail because:
You are the assignee for the bug.

[Bug 8429] Text-based handlers see raw bytes even when normalize_charset is on

Posted by bu...@spamassassin.apache.org.
https://bz.apache.org/SpamAssassin/show_bug.cgi?id=8429

Kent Oyer <ke...@mxguardian.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Attachment #6098|0                           |1
        is obsolete|                            |

--- Comment #2 from Kent Oyer <ke...@mxguardian.net> ---
Created attachment 6099
  --> https://bz.apache.org/SpamAssassin/attachment.cgi?id=6099&action=edit
Patch with MANIFEST changes

Added new files to MANIFEST

-- 
You are receiving this mail because:
You are the assignee for the bug.