Skip to content

Bug: headerPos set incorrectly due to varying byte length #781

@titanism

Description

@titanism

Per https://github.com/nodemailer/wildduck/blob/master/lib/message-splitter.js the chunk might look like:

Message-ID: <ui0f6sedxw@blanditiis.io>
To: test@foo.com
List-Unsubscribe: foo@foo.com
From: Test <prani@blanditiis.io>
Subject: testing this
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

H

But the raw email sent across actually was:

Message-ID: <ui0f6sedxw@blanditiis.io>
To: test@foo.com
List-Unsubscribe: foo@foo.com
From: Test <prani@blanditiis.io>
Subject: testing this
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hi,

Note that the i, gets chopped off, and the resulting body and headers joined is:

Message-ID: <ui0f6sedxw@blanditiis.io>
To: test@foo.com
List-Unsubscribe: foo@foo.com
From: Test <prani@blanditiis.io>
Subject: testing this
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

i,

e.g. the H gets chopped off (first letter after header line break)

The trailing line break is the culprit. Curious if you think we should dummy-proof this @andris9 (e.g. for developers in case they accidentally send email with a trailing line break)?

I think it'd be nice to dummy-proof against this, perhaps using something similar to this code:

// <https://github.com/sindresorhus/strip-final-newline/blob/a1bfe78e3a3de2f73ed3a7600932d7cc952732b4/index.js#L18-26>
const LF = '\n';
const LF_BINARY = LF.codePointAt(0);
const CR = '\r';
const CR_BINARY = CR.codePointAt(0);

const stripFinalNewlineBinary = (input) =>
  input.at(-1) === LF_BINARY
    ? input.subarray(0, input.at(-2) === CR_BINARY ? -2 : -1)
    : input;

Then calling stripFinalNewlineBinary perhaps or just looking for it if headers were found. A rewrite might be needed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions