postal icon indicating copy to clipboard operation
postal copied to clipboard

Attachment with LF in body is being replaced to CR LF

Open dudilugasi opened this issue 1 year ago • 5 comments

Describe the bug

I am sending an text attachment with LF in the api but the attachment is received with CR LF

this is an issue because my client is comparing hashes and becuase of that the hashes are not the same

To Reproduce

  1. Convert a file with LF to base64
  2. Send it using the api
  3. The attachment wiill be with CR LF instead of LF

Expected behaviour

The attachment should be exactly how sent it

Environment details

  • OS: sending http with Windows , node js with axios
  • Version 3.3.4

dudilugasi avatar Oct 22 '24 15:10 dudilugasi

Interesting, I don't know much about ruby or base64 decoding specifics but if you're able to try editing here

https://github.com/postalserver/postal/blob/da90e75036c27482699921613d838f4058a100e7/app/models/outgoing_message_prototype.rb#L102C57-L102C73

You can apparently swap it to Base64.strict_decode64 but I don't know if that is any better or worse. I doubt the database is doing any line ending conversion and this seems to be one of the few places the attachment could be altered.

willpower232 avatar Oct 27 '24 15:10 willpower232

I don't know if it is related to the decode64 i've tried this code block:

require "base64"
require "json"
str1 = Base64.decode64("dGVzdAp0ZXN0CnRlc3QK")
str2 = Base64.decode64("dGVzdA0KdGVzdA0KdGVzdA")
puts str1.to_json
puts str2.to_json

and got this output:

"test\ntest\ntest\n"
"test\r\ntest\r\ntest"

so the Base64.decode64 is actually working ok in ruby

dudilugasi avatar Oct 27 '24 16:10 dudilugasi

Ah good to know. I did a little digging and there are a few bits and pieces about using \r\n when sending the message to its recipients so perhaps it is being converted on the way out in spite of being stored as you intended?

willpower232 avatar Oct 28 '24 13:10 willpower232

Thing is, when im going to the outgoing messages and click on the message i can see in the attachments tab the attachment, and when i click to download it is already changed so the base64 is stored in the db (i guess) and when its converting it to text when im downloading the attachment its adding the \r maybe

BTW when im sending directly through smtp using a node.js library called nodemailer it is sending the file without changing it

const transporter = nodemailer.createTransport({
    "host" : "***",
    "secureConnection" : true,
    "port" : 587,
    "tls" : {
      "rejectUnauthorized" : false
    },
    auth: {
      user: '***',
      pass: '****'
    }
  })

  let info = await transporter.sendMail({
    to: '[email protected]',
    from: '[email protected]',
    subject: 'Hello ✔',
    text: 'Hello world?',
    html: '<b>Hello world?</b>',
    attachments: [
      {
        filename: 'test.sh',
        content: 'ClVzaW5nIEdOVSBjb21waWxlcnM6IGdjYyBhbmQgZ2ZvcnRyYW4KCiQgZ2NjIC1jIGMtY2FsbC1mLmMKJCBnZm9ydHJhbiAtZmZyZWUtZm9ybSAtYyBmX3N1Yi5mCiQgZ2NjIC1vIGMtY2FsbC1mLWdjYyAgYy1jYWxsLWYubyBmX3N1Yi5vICAtbGdmb3J0cmFuCiQgLi9jLWNhbGwtZi1nY2MvVm9sdW1lcy9teU9yYW5nZS9Vc2Vycy9kZWxlZXV3L215U3R1ZmYvbXlDb3Vyc2VzL1JfZmxpLzA1X2RvdEZvcnRyYW4vc3dlZXAvY19jYWxsX2Yvc2NyaXB0LnNo',
        encoding: 'base64'
      }
    ]
  });

//works fine

dudilugasi avatar Oct 28 '24 13:10 dudilugasi

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Nov 29 '24 02:11 github-actions[bot]