From ebeedd3443ceda323d0fbe7a62c2c7c41d2c0809 Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Thu, 19 Apr 2018 13:34:28 +0200 Subject: [PATCH] Fix removing whitespace from the last line of cleartext signed messages Also, move normalizing line endings and removing whitespace to util functions --- src/cleartext.js | 6 ++++-- src/packet/literal.js | 4 ++-- src/packet/signature.js | 2 +- src/util.js | 21 +++++++++++++++++++++ 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/cleartext.js b/src/cleartext.js index 60352fa4..8c423b96 100644 --- a/src/cleartext.js +++ b/src/cleartext.js @@ -19,6 +19,7 @@ * @requires config * @requires encoding/armor * @requires enums + * @requires util * @requires packet * @requires signature * @module cleartext @@ -27,6 +28,7 @@ import config from './config'; import armor from './encoding/armor'; import enums from './enums'; +import util from './util'; import packet from './packet'; import { Signature } from './signature'; import { createVerificationObjects, createSignaturePackets } from './message'; @@ -43,7 +45,7 @@ export function CleartextMessage(text, signature) { return new CleartextMessage(text, signature); } // normalize EOL to canonical form - this.text = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/[ \t]+\n/g, "\n").replace(/\n/g, "\r\n"); + this.text = util.canonicalizeEOL(util.removeTrailingSpaces(text)); if (signature && !(signature instanceof Signature)) { throw new Error('Invalid signature input'); } @@ -122,7 +124,7 @@ CleartextMessage.prototype.verifyDetached = function(signature, keys, date=new D */ CleartextMessage.prototype.getText = function() { // normalize end of line to \n - return this.text.replace(/\r\n/g, "\n"); + return util.nativeEOL(this.text); }; /** diff --git a/src/packet/literal.js b/src/packet/literal.js index 6a0cad1b..87c5cd1e 100644 --- a/src/packet/literal.js +++ b/src/packet/literal.js @@ -66,7 +66,7 @@ Literal.prototype.getText = function() { // decode UTF8 const text = util.decode_utf8(util.Uint8Array_to_str(this.data)); // normalize EOL to \n - this.text = text.replace(/\r\n/g, '\n'); + this.text = util.nativeEOL(text); return this.text; }; @@ -92,7 +92,7 @@ Literal.prototype.getBytes = function() { } // normalize EOL to \r\n - const text = this.text.replace(/\r\n/g, '\n').replace(/\r/g, '\n').replace(/\n/g, '\r\n'); + const text = util.canonicalizeEOL(this.text); // encode UTF8 this.data = util.str_to_Uint8Array(util.encode_utf8(text)); return this.data; diff --git a/src/packet/signature.js b/src/packet/signature.js index 936a1520..943d0925 100644 --- a/src/packet/signature.js +++ b/src/packet/signature.js @@ -556,7 +556,7 @@ Signature.prototype.toSign = function (type, data) { case t.text: { let text = data.getText(); // normalize EOL to \r\n - text = text.replace(/\r\n/g, '\n').replace(/\r/g, '\n').replace(/\n/g, '\r\n'); + text = util.canonicalizeEOL(text); // encode UTF8 return util.str_to_Uint8Array(util.encode_utf8(text)); } diff --git a/src/util.js b/src/util.js index 719d2bef..375dc887 100644 --- a/src/util.js +++ b/src/util.js @@ -574,5 +574,26 @@ export default { return false; } return /$/.test(data); + }, + + /** + * Normalize line endings to \r\n + */ + canonicalizeEOL: function(text) { + return text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/\n/g, "\r\n"); + }, + + /** + * Convert line endings from canonicalized \r\n to native \n + */ + nativeEOL: function(text) { + return text.replace(/\r\n/g, "\n"); + }, + + /** + * Remove trailing spaces and tabs from each line + */ + removeTrailingSpaces: function(text) { + return text.replace(/[ \t]+$/mg, ""); } };