Fix removing whitespace from the last line of cleartext signed messages

Also, move normalizing line endings and removing whitespace to util functions
This commit is contained in:
Daniel Huigens 2018-04-19 13:34:28 +02:00
parent 4e204d7331
commit ebeedd3443
4 changed files with 28 additions and 5 deletions

View File

@ -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 <CR><LF>
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);
};
/**

View File

@ -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;

View File

@ -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));
}

View File

@ -574,5 +574,26 @@ export default {
return false;
}
return /</.test(data) && />$/.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, "");
}
};