haraka / Haraka

A fast, highly extensible, and event driven SMTP server
https://haraka.github.io
MIT License
5.02k stars 662 forks source link

"ct_match" in add_body_filter not working #1386

Closed longlostnick closed 8 years ago

longlostnick commented 8 years ago

Haraka version

2.8.0-alpha.7

Expected behavior

By the documentation here I should be able to pass in a regex or string to match on the content type.

Observed behavior

If I pass in 'text/plain' or use a regex of something like /text\/plain/ or /^text\/plain/, my add_body_filter callback is never called.

Setting it to an empty string works fine. If I log content_type in then callback it outputs 'text/plain'.

I also noticed that if I log connection.transaction.headers it returns a bunch of auth related headers, and no other headers (am using an auth plugin). Is that because at the point of hook_data it hasn't received the headers of the content of the message yet?

Steps to reproduce

exports.hook_data = function (next, connection) {
  connection.transaction.parse_body = 1;
  connection.transaction.add_body_filter('text/plain', function (content_type, encoding, body_buffer) {
    return body_buffer;
  });
  next();
}
baudehlo commented 8 years ago

I'm thinking this is because often there is no content-type header at all. Will investigate.

baudehlo commented 8 years ago

Can you supply an example email on which things don't trigger right?

longlostnick commented 8 years ago

Sorry for the late reply. Here's an example:

Authentication-Results: ip-172-31-20-221; auth=pass (plain)
Received: from [10.1.2.119] ([99.43.92.137])
    by ip-172-31-20-221 (Haraka/2.7.3) with ESMTPSA id C4D7B63A-0219-4C72-993D-192535D36D1A.1
    envelope-from <username@me.com> (authenticated bits=0)
    (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-SHA verify=NO);
    Wed, 13 Apr 2016 02:58:10 +0000
From: Nick Larson <username@me.com>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Subject: this is a test.
Message-Id: <F8E749C0-9D1C-46AA-B5FB-386982047C5C@me.com>
Date: Tue, 12 Apr 2016 19:58:08 -0700
To: Nick <username@gmail.com>
Mime-Version: 1.0 (Mac OS X Mail 9.2 \(3112\))
X-Mailer: Apple Mail (2.3112)

nick

Oddly this one does have a Content-Type. Here's my plugin:

exports.hook_data = function (next, connection) {

  connection.transaction.parse_body = 1;
  connection.transaction.add_body_filter(/^text/, function (content_type, encoding, body_buffer) {
    var body = body_buffer.toString();
    var transformed = body.replace(/nick/ig, 'nicky l');

    return new Buffer(transformed);
  });

  next();
}

It works fine if I provide an empty string, but then I'm guessing it's called on every Content-Type. I'm on node v5.9.0

Dexus commented 8 years ago

Hey normally it should work, for me it does with:

connection.transaction.add_body_filter(/^text\/plain/i, func....