From 5d71ae8691efe6f74f1721ef7c3ce0d75b6a68c3 Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Wed, 22 Apr 2020 19:09:50 +0200 Subject: [PATCH] Fix normalizing \n after \r\n Broken in c4a7455. --- src/util.js | 7 +++++-- test/general/openpgp.js | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/util.js b/src/util.js index 9be630d9..a531db82 100644 --- a/src/util.js +++ b/src/util.js @@ -713,8 +713,11 @@ export default { const indices = []; for (let i = 0; ; i = index) { index = bytes.indexOf(LF, i) + 1; - if (index && bytes[index - 2] !== CR) indices.push(index); - else break; + if (index) { + if (bytes[index - 2] !== CR) indices.push(index); + } else { + break; + } } if (!indices.length) { return bytes; diff --git a/test/general/openpgp.js b/test/general/openpgp.js index 89b036f1..6b8eab1d 100644 --- a/test/general/openpgp.js +++ b/test/general/openpgp.js @@ -2487,6 +2487,20 @@ amnR6g== expect(data).to.equal('Hello World!'); }); + it('should normalize newlines in encrypted text message', async function() { + const message = openpgp.message.fromText('"BEGIN:VCALENDAR\nVERSION:2.0\nBEGIN:VEVENT\r\nUID:123\r\nDTSTART:20191211T121212Z\r\nDTEND:20191212T121212Z\r\nEND:VEVENT\nEND:VCALENDAR"'); + const encrypted = await openpgp.encrypt({ + passwords: 'test', + message + }); + const decrypted = await openpgp.decrypt({ + passwords: 'test', + message: await openpgp.message.readArmored(encrypted.data), + format: 'binary' + }); + expect(openpgp.util.decode_utf8(decrypted.data)).to.equal('"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nUID:123\r\nDTSTART:20191211T121212Z\r\nDTEND:20191212T121212Z\r\nEND:VEVENT\r\nEND:VCALENDAR"'); + }); + }); describe('Errors', function() {