<?xml version="1.0" encoding="US-ASCII"?>
<!DOCTYPE rfc SYSTEM "rfc2629.dtd">

<rfc number="7103" ipr="trust200902" category="info" consensus="yes" submissionType="IETF">
<!-- used v2 -->

<?rfc toc="yes" ?>
<?rfc symrefs="yes" ?>
<?rfc sortrefs="yes"?>
<?rfc rfcedstyle="yes"?>
<?rfc compact="yes"?>
<?rfc subcompact="no"?>

<front>
	<title abbrev="Safe Mail Handling">
		Advice for Safe Handling of Malformed Messages
	</title>

	<author initials="M. S." surname="Kucherawy"
	        fullname="Murray S. Kucherawy">

		<address>
			<email>superuser@gmail.com</email>
		</address>
	</author>

	<author initials="G. S." surname="Shapiro"
	        fullname="Gregory N. Shapiro">

		<address>
			<email>gshapiro@proofpoint.com</email>
		</address>
	</author>

	<author initials="N." surname="Freed"
	        fullname="Ned Freed">

		<address>
			<email>ned.freed@mrochek.com</email>
		</address>
	</author>

	<date month="January" year="2014"/>

	<area>Applications</area>
	<workgroup>APPSAWG</workgroup>

	<keyword>MTA</keyword>
	<keyword>SMTP</keyword>

	<abstract>
		<t> Although Internet message formats have been precisely defined
		    since the 1970s, authoring and handling software often
		    shows only mild conformance to the specifications.  The
		    malformed messages that result are non-standard.
		    Nonetheless, decades of experience have shown that
		    using some tolerance in the handling of the malformations that
		    result is often an acceptable approach and is better
		    than rejecting the messages outright as nonconformant.
		    This document includes a collection of the best advice
		    available regarding a variety of common malformed mail
		    situations; it is to be used as implementation guidance.  </t>

	</abstract>
</front>

<middle>
	<section anchor="intro" title="Introduction">
		<section anchor="for" title="The Purpose of This Work">
			<t> The history of email standards, going back to
			    <xref target="RFC733"/> and beyond, contains a
			    fairly rigid evolution of specifications.
			    However, implementations within that culture have
			    also long had an undercurrent known formally as "the
			    robustness principle", also known informally
			    as "Postel's Law": "Be liberal in what you accept,
			    and conservative in what you send"
			    <xref target="RFC1122"/>.</t>

			<t>
Jon Postel's directive is often interpreted to mean that any deviance
from a specification is acceptable.  However, we believe it was
intended only to account for legitimate variations in interpretation
within specifications, as well as basic transit errors, like bit
errors.

			    Taken to
			    its unintended extreme, excessive tolerance would
			    imply that there are no limits to the liberties
			    that a sender might take, while presuming a burden
			    on a receiver to guess "correctly" at the meaning
			    of any such variation.  These matters are further
			    compounded by receiver software -- the
			    end users' mail readers -- which are also
			    sometimes flawed, leaving senders to craft
			    messages (sometimes bending the rules) to overcome
			    those flaws. </t>

			<t> In general, this served the email ecosystem well
			    by allowing a few errors in implementations
			    without obstructing participation in the game.
			    The proverbial bar was set low.  However, as we
			    have evolved into the current era, some of these
			    lenient stances have begun to expose opportunities
			    that can be exploited by malefactors.  Various
			    email-based applications rely on the strong
			    application of these standards for simple security
			    checks, while the very basic building blocks of
			    that infrastructure, intending to be robust, fail
			    utterly to assert those standards. </t>

  			<t> The distributed and non-interactive nature of
			    email has often prompted adjustments to receiving
			    software, to handle these variations, rather than
			    trying to gain better conformance by senders,
			    since the receiving operator is primarily driven
			    by complaints from recipient users and has no
			    authority over the sending side of the system.
			    Processing with such flexibility comes at some
			    cost, since mail software is faced with decisions
			    about whether to permit non-conforming messages to
			    continue toward their destinations unaltered,
			    adjust them to conform (possibly at the cost of
			    losing some of the original message), or
			    reject them outright.</t>

			<t> This document includes a collection of the best
			    advice available regarding a variety of common
			    malformed mail situations; it is to be used as
			    implementation guidance.  These malformations are
			    typically based around loose interpretations or
			    implementations of specifications such as the
			    Internet Message Format <xref target="MAIL"/> and
			    Multipurpose Internet Mail Extensions
			    <xref target="MIME"/>. </t>
		</section>

		<section anchor="not_for" title="Not the Purpose of This Work">
			<t> It is important to understand that this work is
			    not an effort to endorse or standardize certain
			    common malformations.  The code and culture that
			    introduces such messages into the mail stream
			    needs to be repaired, as the security penalty now
			    being paid for this lax processing arguably 
			    outweighs the reduction in support costs to end
			    users who are not expected to understand the
			    standards.  However, the reality is that this
			    will not be fixed quickly. </t>

			<t> Given this, it is beneficial to provide
			    implementers with guidance about the safest or
			    most effective way to handle malformed messages
			    when they arrive, taking into consideration the
			    trade-offs of the choices available especially
			    with respect to how various actors in the email
			    ecosystem respond to such messages in terms of
			    handling, parsing, or rendering to end users. </t>
		</section>

		<section anchor="gencons" title="General Considerations">
			<t> Many deviations from message format standards
			    are considered by some receivers to be
			    strong indications that the message is undesirable,
			    such as spam or something containing malware.
			    These receivers quickly decide that the best
			    handling choice is simply to reject or discard the
			    message.  This means malformations caused by
			    innocent misunderstandings or ignorance of proper
			    syntax can cause messages with no ill intent also
			    to fail to be delivered. </t>

			<t> Senders that want to ensure message delivery are
			    best advised to adhere strictly to the relevant
			    standards (including, but not limited to,
			    <xref target="MAIL"/>, <xref target="MIME"/>,
			    and <xref target="DKIM"/>), as well as observe
			    other industry best practices such as may be
			    published from time to time by either the IETF or
			    independently. </t>

			<t> Receivers that haven't the luxury of strict
			    enforcement of the standards on inbound messages
			    are usually best served by observing the following
			    guidelines for handling of malformed messages:

			    <list style="numbers">
				<t> Whenever possible, mitigation of syntactic
				    malformations should be guided by an
				    assessment of the most likely semantic
				    intent.  For example, it is reasonable to
				    conclude that multiple sets of angle
				    brackets around an address are simply
				    superfluous and can be dropped. </t>

				<t> When the intent is unclear, or
				    when it is clear but also impractical to
				    change the content to reflect that intent,
				    mitigation should be limited to cases
				    where not taking any corrective action
				    would clearly lead to a worse outcome. </t>

				<t> Security issues, when present, need to be
				    addressed and may force mitigation
				    strategies that are otherwise
				    suboptimal. </t>
			    </list> </t>
		</section>
	</section>

	<section anchor="doc" title="Document Conventions">
		<section anchor="examples" title="Examples">
			<t> Examples of message content include a number
			    within braces at the end of each line.  These
			    are line numbers for use in subsequent discussion,
			    and they are not actually part of the message content
			    presented in the example. </t>

			<t> Blank lines are not numbered in the examples. </t>
		</section>
	</section>

	<section anchor="background" title="Background">
		<t> The reader would benefit from reading
		    <xref target="EMAIL-ARCH"/> for some general background
		    about the overall email architecture.  Of particular
		    interest is the Internet Message Format, detailed in
		    <xref target="MAIL"/>.  Throughout this document, the
		    use of the term "message" should be assumed to mean
		    a block of text conforming to the Internet Message
		    Format. </t>
	</section>

	<section anchor="internal" title="Invariant Content">
		<t> An agent handling a message could use several distinct
		    representations of the message.  One is an internal
		    representation, such as separate blocks of storage for
		    the header and body, some header or body alterations, or
		    tables indexed by header name, set up to make particular
		    kinds of processing easier.  The other is the representation
		    passed along to the next agent in the handling chain.
		    This might be identical to the message input to the module,
		    or it might have some changes such as added or reordered
		    header fields or body elisions to remove malicious
		    content. </t>

		<t> Message handling is usually most effective when each in
		    a sequence of handling modules receives the same content
		    for analysis.  A module that "fixes" or otherwise alters
		    the content passed to later modules can prevent the later
		    modules from identifying malicious or other content that
		    exposes the end user to harm.  It is important that all
		    processing modules can make consistent assertions about
		    the content.  Modules that operate sequentially sometimes
		    add private header fields to relay information downstream
		    for later filters to use (and possibly remove), or they
		    may have out-of-band ways of doing so.  However, even
		    the presence of private header fields can impact a
		    downstream handling agent unaware of its local semantics,
		    so an out-of-band method is always preferable. </t>

		<t> The above is less of a concern when multiple analysis
		    modules are operated in parallel, independent of one
		    another. </t>

		<t> Often, abuse reporting systems can act effectively only
		    when a complaint or report contains the original message
		    exactly as it was generated.
   Messages that have been altered by handling modules might
   render a complaint not actionable as the system receiving the report
   may be unable to identify the original message as one of its own.
</t>

		<t> Some message changes alter syntax without changing
		    semantics.  For example, <xref target="malformations"/>
		    describes a situation where an agent removes additional
		    header whitespace.  This is a syntax change without a
		    change in semantics, though some systems (such as DKIM) are
		    sensitive to such changes.  Message system developers
		    need to be aware of the downstream impact of making either
		    kind of change. </t>

		<t> Where a change to content between modules is unavoidable,
		    it is a good idea to add standard trace data to indicate
		    a "visible" handoff between modules has occurred.  The
		    only advisable way to do this is to prepend Received
		    fields with the appropriate information, as described in
		    Section 3.6.7 of <xref target="MAIL"/>. </t>

		<t> There will always be local handling exceptions, but these
		    guidelines should be useful for developing integrated
		    message processing environments. </t>

		<t> In most cases, this document only discusses techniques used
		    on internal representations.  It is occasionally necessary
		    to make changes between the input and output versions;
		    such cases will be called out explicitly. </t>
	</section>

	<section anchor="msa" title="Mail Submission Agents">
		<t> Within the email context, the single most influential
		    component that can reduce the presence of malformed
		    items in the email system is the Mail Handling Service
		    (MHS; see <xref target="EMAIL-ARCH"/>), which includes
		    the Mail Submission Agent (MSA).  This is the
		    component that is essentially the interface between end
		    users that create content and the mail stream. </t>

		<t> MHSs need to become more strict about enforcement
		    of all relevant email standards, especially
		    <xref target="MAIL"/> and the <xref target="MIME"/>
		    family of documents. </t>

		<t> More strict conformance by relaying Mail Transfer Agents
		    (MTAs) will also be helpful.  Although preventing the
		    dissemination of malformed messages is desirable, the
		    rejection of such mail already in transit also has a
		    support cost -- namely, the creation of a
		    <xref target="DSN"/> that many end users might not
		    understand. </t>
	</section>

	<section anchor="lines" title="Line Termination">
		<t> For interoperable Internet Mail messages, the only
		    valid line separation sequence during a typical SMTP
		    session is ASCII 0x0D ("carriage return", or CR) followed by
		    ASCII 0x0A ("line feed", or LF), commonly referred to as
		    "CRLF".  This is not the case for binary mode SMTP
		    (see <xref target="BINARYSMTP"/>). </t>

		<t> Common UNIX user tools, however, typically only use LF
		    for internal line termination.  This means that a protocol
		    engine that converts between UNIX and Internet message
		    formats has to convert between these two end-of-line
		    representations before transmitting a message or after
		    receiving it. </t>

		<t> Non-compliant implementations can create messages
		    with a mix of line terminations, such as LF
		    everywhere except CRLF only at the end of the message.
		    According to <xref target="SMTP"/> and
		    <xref target="MAIL"/>, this means the entire
		    message actually exists on a single line. </t>

		<t> Within modern Internet Mail, it is highly unlikely that
		    an isolated CR or LF is valid in common ASCII text.
		    Furthermore, when content actually does need to contain
		    such an unusual character sequence, <xref target="MIME"/>
		    provides mechanisms for encoding that content in an
		    SMTP-safe manner. </t>

		<t> Thus, it will typically be safe and helpful to treat an
		    isolated CR or LF as equivalent to a CRLF when parsing
		    a message. </t>

		<t> Note that this advice pertains only to the raw SMTP data
		    and not to decoded MIME entities.  As noted above,
		    when MIME encoding mechanisms are used, the unusual
		    character sequences are not visible in the raw SMTP
		    stream. </t>
	</section>

	<section anchor="header" title="Header Anomalies">
		<t> This section covers common syntactic and semantic
		    anomalies found in a message header and presents
		    suggested methods of mitigation. </t>

		<section anchor="obsolete"
		         title="Converting Obsolete and Invalid Syntaxes">
			<t> A message using an obsolete header syntax (see
			    Section 4 of <xref target="MAIL"/>) might
			    confound an agent that is attempting to be robust
			    in its handling of syntax variations.  A bad
			    actor could exploit such a weakness in order to
			    get abusive or malicious content through a filter.
			    This section presents some examples of such
			    variations.  Messages including these variations ought
			    to be
			    rejected; where this is not possible, recommended
			    internal interpretations are provided. </t>

			<section anchor="hostaddr" title="Host-Address Syntax">
				<t> The following obsolete syntax attempts
				    to specify source routing:

				    <figure><artwork>
    To: &lt;@example.net:fran@example.com&gt;
				    </artwork></figure> </t>

				<t> This means "send to fran@example.com via
				    the mail service at example.net".  It
				    can safely be interpreted as:

				    <figure><artwork>
    To: &lt;fran@example.com&gt;
				    </artwork></figure> </t>
			</section>

			<section anchor="angle"
			         title="Excessive Angle Brackets">
				<t> The following overuse of angle brackets:

				    <figure><artwork>
    To: &lt;&lt;&lt;user2@example.org&gt;&gt;&gt;
				    </artwork></figure> </t>

				<t> can safely be interpreted as:

				    <figure><artwork>
    To: &lt;user2@example.org&gt;
				    </artwork></figure> </t>
			</section>

			<section anchor="angle2"
			         title="Unbalanced Angle Brackets">
				<t> The following use of unbalanced angle
				    brackets:

				    <figure><artwork>
    To: &lt;another@example.net
				    </artwork></figure> </t>

				<t> can usually be treated as:

				    <figure><artwork>
    To: &lt;another@example.net&gt;
				    </artwork></figure> </t>

				<t> The following:

				    <figure><artwork>
    To: second@example.org&gt;
				    </artwork></figure> </t>

				<t> can usually be treated as:

				    <figure><artwork>
    To: second@example.org
				    </artwork></figure> </t>
			</section>

			<section anchor="parens"
			         title="Unbalanced Parentheses">
				<t> The following use of unbalanced
				    parentheses:

				    <figure><artwork>
    To: (Testing &lt;fran@example.com&gt;
				    </artwork></figure> </t>

				<t> can safely be interpreted as:

				    <figure><artwork>
    To: (Testing) &lt;fran@example.com&gt;
				    </artwork></figure> </t>

				<t> Likewise, this case:

				    <figure><artwork>
    To: Testing) &lt;sam@example.com&gt;
				    </artwork></figure> </t>

				<t> can safely be interpreted as:

				    <figure><artwork>
    To: "Testing)" &lt;sam@example.com&gt;
				    </artwork></figure> </t>

				<t> In both cases, it is obvious where the
				    active email address in the string can be
				    found.  The former case retains the active
				    email address in the string by completing
				    what appears to be intended as a comment;
				    the intent in the latter case is less
				    obvious, so the leading string is
				    interpreted as a display name. </t>
			</section>

			<section anchor="commas"
			         title="Commas in Address Lists">
				<t> This use of an errant comma:

				    <figure><artwork>
    To: &lt;third@example.net, fourth@example.net&gt;
				    </artwork></figure> </t>

				<t> can usually be interpreted as ending
				    an address, so the above is usually
				    best interpreted as: 

				    <figure><artwork>
    To: third@example.net, fourth@example.net
				    </artwork></figure> </t>
			</section>

			<section anchor="quotes" title="Unbalanced Quotes">
				<t> The following use of unbalanced
				    quotation marks:

				    <figure><artwork>
    To: "Joe &lt;joe@example.com&gt;
				    </artwork></figure> </t>

<!-- " this comment is just to close the quotation marks so that the
sytax highlighting is right. Please disregard. -->

				<t> leaves software with no unambiguous
				    interpretation.  One possible
				    interpretation is:
				    <figure><artwork>
    To: "Joe &lt;joe@example.com&gt;"@example.net
				    </artwork></figure> </t>

				<t> where "example.net" is the domain name
				    or host name of the handling agent
				    making the interpretation.  However, the
				    more obvious and likely best interpretation
				    is simply: 

				    <figure><artwork>
    To: "Joe" &lt;joe@example.com&gt;
				    </artwork></figure> </t>
			</section>

			<section anchor="localpart" title="Naked Local-Parts">
				<t> <xref target="MAIL"/> defines a local-part
				    as the user portion of an email address,
				    and the display-name as the "user-friendly"
				    label that accompanies the address
				    specification. </t>

				<t> Some broken submission agents might
				    introduce messages with only a local-part
				    or only a display-name and no properly
				    formed address.  For example:

				    <figure><artwork>
    To: Joe
				    </artwork></figure> </t>

				<t> A submission agent ought to reject this or,
				    at a minimum, append "@" followed by its
				    own host name or some other valid
				    name likely to enable a reply to be
				    delivered to the correct mailbox.  Where
				    this is not done, an agent receiving 
				    such a message will probably be successful
				    by synthesizing a valid header field
				    for evaluation using the techniques
				    described in <xref target="missing"/>. </t>
			</section>
		</section>

		<section anchor="non-headers" title="Non-Header Lines">
			<t> Some messages contain
			    a line of text in the header that is not a valid
			    message header field of any kind.  For example:

			    <figure><artwork>
    From: user@example.com {1}
    To: userpal@example.net {2}
    Subject: This is your reminder {3}
    about the football game tonight {4}
    Date: Wed, 20 Oct 2010 20:53:35 -0400 {5}

    Don't forget to meet us for the tailgate party! {7}
			    </artwork></figure> </t>

			<t> The cause of this is typically a bug in a message
			    generator of some kind.  Line {4} was intended to
			    be a continuation of line {3}; it should have been
			    indented by whitespace as set out in Section 2.2.3
			    of <xref target="MAIL"/>.  </t>

			<t> This anomaly has varying impacts on processing
			    software, depending on the implementation:

			    <list style="numbers">
				<t> Some agents choose to separate the header
				    of the message from the body only at the
				    first empty line (that is, a CRLF
				    immediately followed by another CRLF). </t>

				<t> Some agents assume this anomaly should
				    be interpreted to mean the body starts
				    at line {4}, as the end of the header
				    is assumed by encountering something that
				    is not a valid header field or folded
				    portion thereof. </t>

				<t> Some agents assume this should be
				    interpreted as an intended header folding
				    as described above and thus simply
				    append a single space character (ASCII
				    0x20) and the content of line {4} to
				    that of line {3}.</t>

				<t> Some agents reject this outright as line
				    {4} is neither a valid header field nor
				    a folded continuation of a header
				    field prior to an empty line. </t>
			    </list></t>


			<t> This can be exploited if it is known that one
			    message handling agent will take one action,
			    while the next agent in the handling chain will
			    take another.  Consider, for example, a message
			    filter that searches message headers for properties
			    indicative of abusive or malicious content that is
			    attached to a Mail Transfer Agent (MTA)
			    implementing option 2 above. 

                            An attacker could
			    craft a message that includes this malformation at
			    a position above the property of interest, knowing
			    the MTA will not consider that content part of
			    the header.  Consequently, the MTA will not feed it
			    to the filter; thus, it avoids detection.  Meanwhile,
			    the Mail User Agent (MUA), which presents the
			    content to an end user, implements option 1 or 3,
			    which has some undesirable effect. </t>

			<t> It should be noted that a few implementations
			    choose option 4 above since any reputable message
			    generation program will get header folding right,
			    and thus anything so blatant as this malformation
			    is likely an error caused by a malefactor. </t>

			<t> The preferred implementation if option 4 above is
			    not employed is to apply the following heuristic
			    when this malformation is detected:

			    <list style="numbers">
				<t> Search forward for an empty line.  If one
				    is found, then apply option 3 above to the
				    anomalous line, and continue. </t>

				<t> Search forward for another line that
				    appears to be a new header field
				    (a name followed by a colon).  If one is
				    found, then apply option 3 above to the
				    anomalous line, and continue. </t>
			    </list> </t>
		</section>

		<section anchor="spacing" title="Unusual Spacing">
			<t> The following message is valid per
			    <xref target="MAIL"/>:

			    <figure><artwork>
    From: user@example.com {1}
    To: userpal@example.net {2}
    Subject: This is your reminder {3}
     {4}
     about the football game tonight {5}
    Date: Wed, 20 Oct 2010 20:53:35 -0400 {6}

    Don't forget to meet us for the tailgate party! {8}
			    </artwork></figure> </t>

			<t> Line {4} contains a single whitespace.  The
			    intended result is that lines {3}, {4}, and {5}
			    comprise a single continued header field.  However,
			    some agents are aggressive at stripping trailing
			    whitespace, which will cause line {4} to be
			    treated as an empty line, and thus the separator
			    line between header and body.  This can affect
			    header-specific processing algorithms as described
			    in the previous section. </t>

			<t> This example was legal in earlier versions of the
			    Internet message format standard but was rendered
			    obsolete as of <xref target="RFC2822"/> as line {4}
			    could be interpreted as the separator between the
			    header and body. </t>


			<t> The best handling of this example is for a
			    message parsing engine to behave as if line {4}
			    were not present in the message and for a message
			    creation engine to emit the message with line {4}
			    removed. </t>
		</section>

		<section anchor="malformations" title="Header Malformations">
			<t> Among the many possible malformations, a
			    common one is insertion of whitespace at
			    unusual locations, such as:

			    <figure><artwork>
    From: user@example.com {1}
    To: userpal@example.net {2}
    Subject: This is your reminder {3}
    MIME-Version : 1.0 {4}
    Content-Type: text/plain {5}
    Date: Wed, 20 Oct 2010 20:53:35 -0400 {6}

    Don't forget to meet us for the tailgate party! {8}
			    </artwork></figure> </t>

			<t> Note the addition of whitespace in line {4}
			    after the header field name but before the colon
			    that separates the name from the value. </t>

			<t> The obsolete grammar of Section 4 of
			    <xref target="MAIL"/>
			    permits that extra whitespace, so it cannot be
			    considered invalid.  However, a consensus of
			    implementations prefers to remove that whitespace.
			    There is no perceived change to the semantics of
			    the header field being altered as the whitespace
			    is itself semantically meaningless.  Therefore,
			    it is best to remove all whitespace after the
			    field name but before the colon and to emit the
			    field in this modified form. </t>
		</section>

		<section anchor="counts" title="Header Field Counts">
			<t> Section 3.6 of <xref target="MAIL"/> prescribes
			    specific header field counts for a valid
			    message.  Few agents actually enforce these
			    in the sense that a message whose header contents
			    exceed one or more limits set there are generally
			    allowed to pass; they typically add any required
			    fields that are missing, however.  </t>

			<t> Also, few agents that use messages as input,
			    including MUAs that actually
			    display messages to users, verify that the input
			    is valid before proceeding.  Some popular open-source
			     filtering programs and some popular Mailing
			    List Management (MLM) packages select either the
			    first or last instance of a particular field name,
			    such as From, to decide who sent a message.  Absent
			    strict enforcement of <xref target="MAIL"/>, an
			    attacker can craft a message with multiple 
			    instances of the same fields
			    if that attacker knows the filter will make a
			    decision based on one, but the user will be shown
			    the others. </t>

			<t> This situation is exacerbated when message validity
			    is assessed, such as through enhanced
			    authentication methods like DomainKeys Identified
			    Mail <xref target="DKIM"/>.  Such methods might
			    cover one instance of a constrained field but not
			    another, taking the wrong one as "good" or "safe". 
			    An MUA, for example, could show the first of two
			    From fields to an end user as "good" or "safe",
			    while an authentication method actually only
			    verified the second. </t>

			<t> In attempting to counter this exposure, one of
			    the following strategies can be used:

			    <list style="numbers">
				<t> reject outright or refuse to process
				    further any input message that does not
				    conform to Section 3.6 of
				    <xref target="MAIL"/>; </t>

				<t> remove or, in the case of an MUA, refuse
				    to render any instances of a header field
				    whose presence exceeds a limit prescribed
				    in Section 3.6 of
				    <xref target="MAIL"/> when generating its
				    output; </t>

				<t> where a field can contain multiple distinct
				    values (such as From) or is free-form
				    text (such as Subject), combine them into
				    a semantically identical, single header
				    field of the same name (see 
				    <xref target="repeated"/>); </t>

				<t> alter the name of any header field 
				    whose presence exceeds a limit prescribed
				    in Section 3.6 of
				    <xref target="MAIL"/> when generating its
				    output so that later agents can produce a
				    consistent result.  Any alteration likely
				    to cause the field to be ignored by
				    downstream agents is acceptable.  A common
				    approach is to prefix the field names
				    with a string such as "BAD-". </t>
			    </list> </t>


			<t> When selecting a mitigation action (or some other action) from the above list,
			    an operator must consider its needs and the
			    nature of its user base. </t>

			<section anchor="repeated"
			         title="Repeated Header Fields">
				<t> There are some occasions where repeated
				    fields are encountered where only one is
				    expected.  Two examples are presented.
				    First:

					<figure><artwork>
    From: reminders@example.com {1}
    To: jqpublic@example.com {2}
    Subject: Automatic Meeting Reminder {3}
    Subject: 4pm Today -- Staff Meeting {4}
    Date: Wed, 20 Oct 2010 08:00:00 -0700 {5}

    Reminder of the staff meeting today in the small {6}
    auditorium.  Come early! {7}
					</artwork></figure></t>

				<t> The message above has two Subject fields,
				    which is in violation of Section 3.6 of
				    <xref target="MAIL"/>.  A safe
				    interpretation of this would be to treat
				    it as though the two Subject field values
				    were concatenated, so long as they are
				    not identical, such as:

					<figure><artwork>
    From: reminders@example.com {1}
    To: jqpublic@example.com {2}
    Subject: Automatic Meeting Reminder {3}
      4pm Today -- Staff Meeting {4}
    Date: Wed, 20 Oct 2010 08:00:00 -0700 {5}

    Reminder of the staff meeting today in the small {6}
    auditorium.  Come early! {7}
					</artwork></figure></t>

				<t> Second:
					<figure><artwork>
    From: president@example.com {1}
    From: vice-president@example.com {2}
    To: jqpublic@example.com {3}
    Subject: A note from the E-Team {4}
    Date: Wed, 20 Oct 2010 08:00:00 -0700 {5}

    This memo is to remind you of the corporate dress {6}
    code.  Attached you will find an updated copy of {7}
    the policy. {8}
    ...
					</artwork></figure></t>

				<t> As with the first example, there is a
				    violation in terms of the number of
				    instances of the From field.  A likely
				    safe interpretation would be to combine
				    these into a comma-separated address
				    list in a single From field:
					<figure><artwork>
    From: president@example.com, {1}
          vice-president@example.com {2}
    To: jqpublic@example.com {3}
    Subject: A note from the E-Team {4}
    Date: Wed, 20 Oct 2010 08:00:00 -0700 {5}

    This memo is to remind you of the corporate dress {6}
    code.  Attached you will find an updated copy of {7}
    the policy. {8}
    ...
					</artwork></figure></t>
			</section>

			<section anchor="missing"
				title="Missing Header Fields">
			<t> Similar to the previous section, there are
			    messages seen in the wild that lack certain
			    required header fields.  In particular,
			    <xref target="MAIL"/> requires that a From and
			    Date field be present in all messages. </t>

			<t> When presented with a message lacking these fields,
			    the MTA might perform one of the following:

			    <list style="numbers">
				<t> Make no changes. </t>

				<t> Add an instance of the missing field(s)
				    using synthesized content based on
				    data provided in other parts of the
				    protocol. </t>
			    </list> </t>

			<t> Option 2 is recommended for handling this
			    case.  Handling agents should add these for
			    internal handling if they are missing, but should
			    not add them to the external
			    representation. 

			    The reason for this advice is that there
			    are some filter modules that would consider the
			    absence of such fields to be a condition warranting
			    special treatment (for example, rejection), and
			    thus the effectiveness of such modules would be
			    stymied by an upstream filter adding them in a
			    way visible to other components. </t>

			<t> The synthesized fields should contain a best
			    guess as to what should have been there; for From,
			    the SMTP MAIL command's address can be used
			    (if not null) or a placeholder address followed
			    by an address literal (for example,
			    unknown@[192.0.2.1]); for Date, a date extracted
			    from a Received field is a reasonable choice. </t>

			<t> One other important case to consider is a missing
			    Message-ID field.  
An MTA that encounters a message missing this field should synthesize
a valid one and add it to the external representation, since many
deployed tools commonly use the content of that field as a unique
message reference, so its absence inhibits correlation of message
processing.

 Section 3.6.4
			    of <xref target="MAIL"/> describes advisable
			    practice for synthesizing the content of this field
			    when it is absent, and establishes a requirement
			    that it be globally unique. </t>
			</section>

			<section anchor="rp" title="Return-Path">
			<t> While legitimate messages can contain more than
			    one Return-Path header field, such usage is often
			    an error rather that a valid message containing
			    multiple header field blocks as described in
			    Sections 3.6 of <xref target="MAIL"/>.
			    Accordingly, when a message containing multiple
			    Return-Path header fields is encountered, all but
			    the topmost one is to be disregarded, as it is
			    most likely to have been added nearest to the
			    mailbox that received that message.  </t>
			</section>
		</section>

		<section anchor="charset"
		         title="Missing or Incorrect Charset Information">
			<t> MIME provides the means to include textual
			    material employing character sets ("charsets")
			    other than US-ASCII.  Such material is required to
			    have an identified charset.  Charset identification
			    is done using a "charset" parameter in the
			    Content-Type header field, a charset label
			    within the MIME entity itself, or the charset
			    can be implicitly specified by the Content-Type
			    (see <xref target="CHARSET"/>). </t>
		
			<t> Unfortunately, it is fairly common for required
			    character set information to be missing or
			    incorrect in textual MIME entities.  As such,
			    processing agents should perform basic sanity
			    checks, such as:

			    <list style="symbols">
				<t> US-ASCII contains bytes between 1 and 127
				    inclusive only (colloquially, "7-bit"
				    data), so material including bytes outside
				    of that range ("8-bit" data) is
				    necessarily not US-ASCII.  (See Section
				    2.1 of <xref target="MAIL"/>.)</t>

				<t> <xref target="UTF-8"/> has a very specific
				    syntactic structure that other 8-bit
				    charsets are unlikely to follow. </t>
				<t> Null bytes (ASCII 0x00) are not allowed
				    in either 7-bit or 8-bit data. </t>
				<t> Not all 7-bit material is US-ASCII.  The
				    presence of the various escape sequences
				    used for character switching can be used
				    as an indication of the various charsets
				    based on ISO/IEC 2022 <xref target="ISO-2022"/>, such as those
				    defined in
				    <xref target="ISO-2022-CN"/>, 
				    <xref target="ISO-2022-JP"/>, and
				    <xref target="ISO-2022-KR"/>. </t>
			    </list> </t>

			<t> When a character set error is detected, processing
			    agents should:

			    <list style="numbers">
				<t> apply heuristics to determine the most
				    likely character set and, if successful,
				    proceed using that information; or </t>

				<t> refuse to process the malformed MIME
				    entity. </t>
			    </list> </t>

			<t> A null byte inside a textual MIME entity can
			    cause typical string processing functions to
			    misidentify the end of a string, which can be
			    exploited to hide malicious content from
			    analysis processes.  Accordingly, null bytes
			    require additional special handling. </t>

			<t> A few null bytes in isolation is likely to be the
			    result of poor message construction practices.
			    Such nulls should be silently dropped. </t>

			<t> Large numbers of null bytes are usually the result
			    of binary material that is improperly encoded,
			    improperly labeled, or both.  Such material is
			    likely to be damaged beyond the hope of recovery,
			    so the best course of action is to refuse to
			    process it. </t>

			<t> Finally, the presence of null bytes may be used
			    as indication of possible malicious intent. </t>
		</section>

		<section anchor="eightbit" title="Eight-Bit Data">
			<t> Standards-compliant email messages do not contain
			    any non-ASCII data without indicating that such
			    content is present by means of published SMTP
			    extensions.  Absent that, MIME encodings are
			    typically used to convert non-ASCII data to
			    ASCII in a way that can be reversed by other
			    handling agents or end users. </t>

			<t> The best way to handle non-compliant 8-bit material
			    depends on its location. </t>

			<t> Non-compliant 8-bit material in MIME entity content
			    should simply be processed as if the necessary SMTP
			    extensions had been used to transfer the message.
			    Note that improperly labeled 8-bit material in
			    textual MIME entities may require treatment as
			    described in <xref target="charset"/>. </t>

			<t> Non-compliant 8-bit material in message or MIME
			    entity header fields can be handled as follows:

			    <list style="numbers">
				<t> Occurrences in unstructured text fields,
				    comments, and phrases can be converted
				    into encoded-words (see
				    <xref target="MIME3"/> if a likely
				    character set can be determined).
				    Alternatively, 8-bit characters can be
				    removed or replaced with some other
				    character. </t>

				<t> Occurrences in header fields whose syntax
				    is unknown may be handled by dropping the
				    field entirely or by removing/replacing
				    the 8-bit character as described above. </t>

				<t> Occurrences in addresses are especially
				    problematic.  Agents supporting
				    <xref target="EAI"/> may, if the 8-bit
				    material conforms to 8-bit syntax, elect
				    to treat the message as an EAI message and
				    process it accordingly.
				    Otherwise, in most cases, it is
				    best to exclude the address
				    from any sort of processing -- which may
				    mean dropping it entirely -- since any
				    attempt to fix it definitively
				    is unlikely to be successful. </t>
				</list> </t>
		</section>
	</section>

	<section anchor="mime" title="MIME Anomalies">
		<t> The five-part set of MIME specifications includes a
		    mechanism of message extensions for providing text in
		    character sets other than ASCII, non-text attachments to
		    messages, multipart message bodies, and similar
		    facilities. </t>

		<t> Some anomalies with MIME-compliant generation are also
		    common.  This section discusses some of those and
		    presents preferred methods of mitigation. </t>

		<section anchor="no_version" title="Missing MIME-Version Field">
			<t> Any message that uses <xref target="MIME"/>
			    constructs is required to have a MIME-Version
			    header field.  Without it, the Content-Type
			    and associated fields have no semantic
			    meaning. </t>

			<t> It is often observed that a message has complete
			    MIME structure, yet lacks this header field.
			    It is prudent to disregard this absence and
			    conduct analysis of the message as if it were
			    present, especially by agents attempting to
			    identify malicious material. </t>
			    
			<t> Further, the absence of MIME-Version might be
			    an indication of malicious intent, and extra
			    scrutiny of the message may be warranted.  Such
			    omissions are not expected from compliant
			    message generators. </t>
		</section>

		<section anchor="bad_encoding"
		         title="Faulty Encodings">
			<t> There have been a few different specifications of
			    base64 in the past.  The implementation defined
			    in <xref target="MIME"/> instructs decoders
			    to discard characters that are not part of the
			    base64 alphabet.  Other implementations consider
			    an encoded body containing such characters to be
			    completely invalid.  Very early specifications of
			    base64 (see <xref target="PEM89"/>, for example,
which was later obsoleted by <xref target="PEM93"/>)
			    allowed email-style comments within base64-encoded
			    data. </t>

			<t> The attack vector here involves constructing
			    a base64 body whose meaning varies given different
			    possible decodings.  If a security analysis
			    module wishes to be thorough, it should consider
			    scanning the possible outputs of the known
			    decoding dialects in an attempt to anticipate how
			    the MUA will interpret the data. </t>
		</section>
	</section>

	<section anchor="body" title="Body Anomalies">
		<section anchor="longlines" title="Oversized Lines">
			<t> A message containing a line of content that
			    exceeds 998 characters plus the line terminator
			    (1000 total) violates Section 2.1.1 of
			    <xref target="MAIL"/>.  Some handling
			    agents may not look at content in a single line
			    past the first 998 bytes, providing bad actors
			    an opportunity to hide malicious content. </t>

			<t> There is no specified way to handle such messages,
			    other than to observe that they are non-compliant
			    and reject them or rewrite the oversized line
			    such that the message is compliant. </t>

			<t> To ensure long lines do not prevent analysis of
			    potentially malicious data, handling agents are
			    strongly encouraged to take one of the following
			    actions:

			    <list style="numbers">
				<t>
       Break such lines into multiple lines at a position that does
       not change the semantics of the text being thus altered.  For
       example, break an oversized line at a position such that a <xref target="URI"/>
       does not span two lines (which could inhibit the proper
       identification of the URI).
</t>


				<t> Rewrite the MIME part (or the entire
				    message if not MIME) that contains the
				    excessively long line using a content
				    encoding that breaks the line in the
				    transmission but would still result in
				    the line being intact on decoding for
				    presentation to the user.  Both of the
				    encodings declared in <xref target="MIME"/>
				    can accomplish this. </t>
			    </list> </t>
		</section>
	</section>

	<section anchor="security" title="Security Considerations">
		<t> The discussions of the anomalies above and their
		    prescribed solutions are themselves security
		    considerations.  The practices enumerated in this document
		    are generally perceived as attempts to resolve security
		    considerations that already exist rather than introducing
		    new ones.  However, some of the attacks described here
		    may not have appeared in previous email
		    specifications. </t>
	</section>


</middle>

<back>
	<references title="Normative References">
		<reference anchor="EMAIL-ARCH">
			<front>
				<title> Internet Mail Architecture </title>
				<author initials="D." surname="Crocker"
				        fullname="D. Crocker">
					<organization>
						Brandenburg InternetWorking
					</organization>
				</author>
				<date month="July" year="2009" />
			</front>
			<seriesInfo name="RFC" value="5598" />
		</reference>

		<reference anchor="MAIL">
			<front>
				<title> Internet Message Format </title>
				<author initials="P." surname="Resnick"
					fullname="P. Resnick (editor)">
					<organization>
						Qualcomm, Inc.
					</organization>
				</author>
				<date month="October" year="2008" />
			</front>
			<seriesInfo name="RFC" value="5322" />
		</reference>

		<reference anchor="MIME">
			<front>
				<title> Multipurpose Internet Mail
				        Extensions (MIME) Part One: Format of
				        Internet Message Bodies </title>
				<author initials="N." surname="Freed"
					fullname="N. Freed">
					<organization>
						Innosoft
					</organization>
				</author>
				<author initials="N." surname="Borenstein"
					fullname="N. Borenstein">
					<organization>
						First Virtual
					</organization>
				</author>
				<date month="November" year="1996" />
			</front>
			<seriesInfo name="RFC" value="2045" />
		</reference>
	</references>

	<references title="Informative References">
		<reference anchor="BINARYSMTP">
			<front>
				<title> SMTP Service Extensions for Transmission
					of Large and Binary MIME
					Messages </title>
				<author initials="G." surname="Vaudreuil"
					fullname="G. Vaudreuil">
					<organization>
						Lucent Technologies
					</organization>
				</author>
				<date month="December" year="2000" />
			</front>
			<seriesInfo name="RFC" value="3030" />
		</reference>

		<reference anchor="CHARSET">
			<front>
				<title> Update to MIME regarding "charset"
				        Parameter Handling in Textual Media
				        Types </title>
				<author initials="A." surname="Melnikov"
					fullname="A. Melnikov">
					<organization>
						Isode Limited
					</organization>
				</author>
				<author initials="J." surname="Reschke"
					fullname="J. Reschke">
					<organization>
						greenbytes
					</organization>
				</author>
				<date month="July" year="2012" />
			</front>
			<seriesInfo name="RFC" value="6657" />
		</reference>

		<reference anchor="DKIM">
			<front>
				<title>
					DomainKeys Identified Mail (DKIM)
					Signatures
				</title>

				<author initials="D." surname="Crocker"
					fullname="D. Crocker" role="editor">
					<organization>
						Brandenburg InternetWorking
					</organization>
				</author>

				<author initials="T." surname="Hansen"
					fullname="T. Hansen" role="editor">
					<organization>
						AT&amp;T Laboratories
					</organization>
				</author>

				<author initials="M." surname="Kucherawy"
					fullname="M. Kucherawy" role="editor">
					<organization>
						Cloudmark
					</organization>
				</author>

				<date year="2011" month="September"/>
			</front>

			<seriesInfo name="RFC" value="6376"/>
		</reference>

		<reference anchor="DSN">
			<front>
				<title> An Extensible Message Format for
				        Delivery Status Notifications </title>
				<author initials="K." surname="Moore"
					fullname="K. Moore">
					<organization>
						University of Tennessee
					</organization>
				</author>
				<author initials="G." surname="Vaudreuil"
					fullname="G. Vaudreuil">
					<organization>
						Lucent Technologies
					</organization>
				</author>
				<date month="January" year="2003" />
			</front>
			<seriesInfo name="RFC" value="3464" />
		</reference>

		<reference anchor="EAI">
			<front>
				<title> Internationalized Email
				        Headers </title>
				<author initials="A." surname="Yang"
					fullname="A. Yang">
					<organization>
						TWNIC
					</organization>
				</author>
				<author initials="S." surname="Steele"
					fullname="S. Steele">
					<organization>
						Microsoft
					</organization>
				</author>
				<author initials="N." surname="Freed"
					fullname="N. Freed">
					<organization>
						Oracle
					</organization>
				</author>
				<date month="February" year="2012" />
			</front>
			<seriesInfo name="RFC" value="6532" />
		</reference>

		<reference anchor="ISO-2022" target="http://www.iso.org/iso/catalogue_detail.htm?csnumber=22747">
			<front>
				<title>Information technology -- Character code structure and extension techniques</title>
				<author>
					<organization>
						ISO/IEC
					</organization>
				</author>
				<date year="1994" />
			</front>
			<seriesInfo name="ISO/IEC" value="2022" />
		</reference>


		<reference anchor="ISO-2022-CN">
			<front>
				<title> Chinese Character Encoding for Internet
				        Messages </title>
				<author initials="HF." surname="Zhu"
					fullname="HF. Zhu">
					<organization>
						Tsinghua U
					</organization>
				</author>
				<author initials="DY." surname="Hu"
					fullname="DY. Hu">
					<organization>
						Tsinghua U
					</organization>
				</author>
				<author initials="ZG." surname="Wang"
					fullname="ZG. Wang">
					<organization>
						CITS
					</organization>
				</author>
				<author initials="TC." surname="Kao"
					fullname="TC. Kao">
					<organization>
						III
					</organization>
				</author>
				<author initials="WCH." surname="Chang"
					fullname="WCH. Chang">
					<organization>
						III
					</organization>
				</author>
				<author initials="M." surname="Crispin"
					fullname="M. Crispin">
					<organization>
						U Washington
					</organization>
				</author>
				<date month="March" year="1996" />
			</front>
			<seriesInfo name="RFC" value="1922" />
		</reference>

		<reference anchor="ISO-2022-JP">
			<front>
				<title> Japanese Character Encoding for
				        Internet Messages </title>
				<author initials="J." surname="Murai"
					fullname="J. Murai">
					<organization>
						Kaio University
					</organization>
				</author>
				<author initials="M." surname="Crispin"
					fullname="M. Crispin">
					<organization>
						Panda Programming
					</organization>
				</author>
				<author initials="E." surname="van der Poel"
					fullname="E. van der Poel">
					<organization/>
				</author>
				<date month="June" year="1993" />
			</front>
			<seriesInfo name="RFC" value="1468" />
		</reference>

		<reference anchor="ISO-2022-KR">
			<front>
				<title> Korean Character Encoding for Internet
				        Messages </title>
				<author initials="U." surname="Choi"
					fullname="U. Choi">
					<organization>
						KAIST
					</organization>
				</author>
				<author initials="K." surname="Chon"
					fullname="K. Chon">
					<organization>
						KAIST
					</organization>
				</author>
				<author initials="H." surname="Park"
					fullname="H. Park">
					<organization>
						Solvit Chosun Media
					</organization>
				</author>
				<date month="December" year="1993" />
			</front>
			<seriesInfo name="RFC" value="1557" />
		</reference>

		<reference anchor="MIME3">
			<front>
				<title> MIME (Multipurpose Internet Mail
				        Extensions) Part Three: Message Header
				        Extensions for Non-ASCII Text </title>
				<author initials="K." surname="Moore"
					fullname="K. Moore">
					<organization>
						University of Tennessee
					</organization>
				</author>
				<date month="November" year="1996" />
			</front>
			<seriesInfo name="RFC" value="2047" />
		</reference>

		<reference anchor="PEM89">
			<front>
				<title> Privacy Enhancement for Internet
				       Electronic Mail: Part I -- Message
				       Encipherment and Authentication
				       Procedures </title>
				<author initials="J." surname="Linn"
				        fullname="J. Linn">
					<organization>
						DEC
					</organization>
				</author>
				<date month="August" year="1989" />
			</front>
			<seriesInfo name="RFC" value="1113" />
		</reference>

<reference anchor='PEM93'>

<front>
<title abbrev='Privacy Enhancement for Electronic Mail'>Privacy Enhancement for Internet Electronic Mail: Part I: Message Encryption and Authentication Procedures</title>
<author initials='J.' surname='Linn' fullname='John Linn'>
</author>
<date year='1993' month='February' /></front>

<seriesInfo name='RFC' value='1421' />
<format type='TXT' octets='103894' target='http://www.rfc-editor.org/rfc/rfc1421.txt' />
</reference>


		<reference anchor="RFC733">
			<front>
				<title> Standard for the Format of Internet
				        Text Messages  </title>
				<author initials="D. H." surname="Crocker"
				        fullname="D. H. Crocker">
					<organization>
						The Rand Corporation
					</organization>
				</author>
				<author initials="J. J." surname="Vittal"
				        fullname="J. J. Vittal">
					<organization>
						Bolt Beranek and Newman, Inc.
					</organization>
				</author>
				<author initials="K. T." surname="Pogran"
				        fullname="K. T. Pogran">
					<organization>
						Massachusets Institute
						of Technology
					</organization>
				</author>
				<author initials="D. A." surname="Henderson, Jr."
				        fullname="D. A. Henderson, Jr.">
					<organization>
						Bolt Beranek and Newman, Inc.
					</organization>
				</author>
				<date month="November" year="1977" />
			</front>
			<seriesInfo name="RFC" value="733" />
		</reference>

		<reference anchor="RFC1122">
			<front>
				<title> Requirements for Internet Hosts --
				        Communication Layers  </title>
				<author initials="R." surname="Braden"
				        fullname="R. Braden" role="editor">
					<organization/>
				</author>
				<date month="October" year="1989" />
			</front>
			<seriesInfo name="RFC" value="1122" />
		</reference>

		<reference anchor="RFC2822">
			<front>
				<title> Internet Message Format </title>
				<author initials="P." surname="Resnick"
				        fullname="P. Resnick" role="editor">
					<organization>
						QUALCOMM Incorporated
					</organization>
				</author>
				<date month="April" year="2001" />
			</front>
			<seriesInfo name="RFC" value="2822" />
		</reference>

		<reference anchor="SMTP">
			<front>
				<title>Simple Mail Transfer Protocol</title>

				<author
					fullname="J. Klensin"
					initials="J."
					surname="Klensin">
					<organization></organization>
				</author>

				<date month="October" year="2008"></date>
			</front>

			<seriesInfo name="RFC" value="5321"></seriesInfo>
		</reference>

		<reference anchor="URI">
			<front>
				<title> Uniform Resource Identifier (URI):
					Generic Syntax </title>
				<author initials="T." surname="Berners-Lee"
					fullname="T. Berners-Lee">
					<organization>
						W3C/MIT
					</organization>
				</author>
				<author initials="R." surname="Fielding"
					fullname="R. Fielding">
					<organization>
						Day Software
					</organization>
				</author>
				<author initials="L." surname="Masinter"
					fullname="L. Masinter">
					<organization>
						Adobe Systems
					</organization>
				</author>
				<date month="January" year="2005"/>
			</front>
			<seriesInfo name="RFC" value="3986" />
		</reference>

		<reference anchor="UTF-8">
			<front>
				<title> UTF-8, a transformation format of
					ISO 10646 </title>
				<author initials="F." surname="Yergeau"
					fullname="F. Yergeau">
					<organization>
						Alis Technologies
					</organization>
				</author>
				<date year="2003" />
			</front>
			<seriesInfo name="RFC" value="3629" />
		</reference>
	</references>

<!-- [rfced]  You wrote:
   I can't seem to figure out how to do this with xml2rfc,
   but the ISO-2022 reference above should contain the
   following URI:
   http://www.iso.org/iso/catalogue_detail.htm?csnumber=22747 

(Using xml2rfc, it's the target attribute of the reference.)

FYI, rather than adding this URL for the references to RFCs 1922,
1468, and 1557, we added a reference for ISO-2022, which uses this
URL. Please review.
-->


	<section anchor="thanks" title="Acknowledgements">
   		<t> The authors wish to acknowledge the following for their
		    review and constructive criticism of this proposal:
		    Dave Cridland,
		    Dave Crocker,
		    Jim Galvin,
		    Tony Hansen,
		    John Levine,
		    Franck Martin,
		    Alexey Melnikov, and
		    Timo Sirainen.
                    </t>
	</section>
</back>

</rfc>
