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() {