Add error handling for invalid PGP key user id

This commit is contained in:
Tankred Hase 2015-06-12 16:50:03 +02:00
parent 39d19df187
commit f32863dc54
1 changed files with 11 additions and 2 deletions

View File

@ -106,7 +106,7 @@ PGP.prototype.getKeyId = function(keyArmored) {
* Read all relevant params of an armored key. * Read all relevant params of an armored key.
*/ */
PGP.prototype.getKeyParams = function(keyArmored) { PGP.prototype.getKeyParams = function(keyArmored) {
var key, packet, userIds; var key, packet, userIds, emailAddress;
// process armored key input // process armored key input
if (keyArmored) { if (keyArmored) {
@ -122,15 +122,24 @@ PGP.prototype.getKeyParams = function(keyArmored) {
// read user names and email addresses // read user names and email addresses
userIds = []; userIds = [];
key.getUserIds().forEach(function(userId) { key.getUserIds().forEach(function(userId) {
if (!userId || userId.indexOf('<') < 0 || userId.indexOf('>') < 0) {
return;
}
userIds.push({ userIds.push({
name: userId.split('<')[0].trim(), name: userId.split('<')[0].trim(),
emailAddress: userId.split('<')[1].split('>')[0].trim() emailAddress: userId.split('<')[1].split('>')[0].trim()
}); });
}); });
// check user ID
emailAddress = userIds[0] && userIds[0].emailAddress;
if (!emailAddress) {
throw new Error('Cannot parse PGP key user ID!');
}
return { return {
_id: packet.getKeyId().toHex().toUpperCase(), _id: packet.getKeyId().toHex().toUpperCase(),
userId: userIds[0].emailAddress, // the primary (first) email address of the key userId: emailAddress, // the primary (first) email address of the key
userIds: userIds, // a dictonary of all the key's name/address pairs userIds: userIds, // a dictonary of all the key's name/address pairs
fingerprint: packet.getFingerprint().toUpperCase(), fingerprint: packet.getFingerprint().toUpperCase(),
algorithm: packet.algorithm, algorithm: packet.algorithm,