Fix user ID filtering. Do not filter out user attribute packets.

This commit is contained in:
Thomas Oberndörfer 2019-03-15 16:55:53 +01:00
parent f88f4d5d2e
commit 9e7149108a
10 changed files with 99 additions and 10 deletions

View File

@ -156,7 +156,7 @@ class PGP {
async filterKeyByUserIds(userIds, armored) {
const emails = userIds.map(({email}) => email);
const {keys: [key]} = await openpgp.key.readArmored(armored);
key.users = key.users.filter(({userId: {email}}) => emails.includes(util.normalizeEmail(email)));
key.users = key.users.filter(({userId}) => !userId || emails.includes(util.normalizeEmail(userId.email)));
return key.armor();
}
@ -189,7 +189,7 @@ class PGP {
*/
async removeUserId(email, publicKeyArmored) {
const {keys: [key]} = await openpgp.key.readArmored(publicKeyArmored);
key.users = key.users.filter(({userId}) => util.normalizeEmail(userId.email) !== email);
key.users = key.users.filter(({userId}) => !userId || util.normalizeEmail(userId.email) !== email);
return key.armor();
}
}

40
test/fixtures/key5.asc vendored Normal file
View File

@ -0,0 +1,40 @@
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v2.0.22 (GNU/Linux)
mI0EUmEvTgEEANyWtQQMOybQ9JltDqmaX0WnNPJeLILIM36sw6zL0nfTQ5zXSS3+
fIF6P29lJFxpblWk02PSID5zX/DYU9/zjM2xPO8Oa4xo0cVTOTLj++Ri5mtr//f5
GLsIXxFrBJhD/ghFsL3Op0GXOeLJ9A5bsOn8th7x6JucNKuaRB6bQbSPABEBAAG0
JFRlc3QgTWNUZXN0aW5ndG9uIDx0ZXN0QGV4YW1wbGUuY29tPoi5BBMBAgAjBQJS
YS9OAhsvBwsJCAcDAgEGFQgCCQoLBBYCAwECHgECF4AACgkQSmNhOk1uQJQwDAP6
AgrTyqkRlJVqz2pb46TfbDM2TDF7o9CBnBzIGoxBhlRwpqALz7z2kxBDmwpQa+ki
Bq3jZN/UosY9y8bhwMAlnrDY9jP1gdCo+H0sD48CdXybblNwaYpwqC8VSpDdTndf
9j2wE/weihGp/DAdy/2kyBCaiOY1sjhUfJ1GogF49rDR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Y50mvTETzOKemCt1GYI0GhOY2uZOVRtQLrkX0CB9r5hEQalkrnjNKlbghj
LfOYu1uARF16cZUWuI0EUmEvTgEEAOkfz7QRWiWk+I6tdMqgEpOLKsFTLHOh3Inz
OZUnccxMRT++J2lDDMhLChz+d0MUxdBq6rrGoEIP2bYE9AjdR1DNedsuwAjnadYI
io6TMzk0ApagqHJcr1jhQfi/0sBhCCX+y0ghK8KAbiYnyXPMQFa9F19CbYaFvrj/
dXk0N16bABEBAAGJAT0EGAECAAkFAlJhL04CGy4AqAkQSmNhOk1uQJSdIAQZAQIA
BgUCUmEvTgAKCRDghPdEbCAsl7qiBADZpokQgEhe2Cuz7xZIniTcM3itFdxdpRl/
rrumN0P2cXbcHOMUfpnvwkgZrFEcl0ztvTloTxi7Mzx/c0iVPQXQ4ur9Mjaa5hT1
/9TYNAG5/7ApMHrb48QtWCL0yxcLVC/+7+jUtm2abFMUU4PfnEqzFlkjY4mPalCm
o5tbbszw2VwFBADDZgDd8Vzfyo8r49jitnJNF1u+PLJf7XN6oijzCftAJDBez44Z
ofZ8ahPfkAhJe6opxaqgS47s4FIQVOEJcF9RgwLTU6uooSzA+b9XfNmQu7TWrXZQ
zBlpyHbxDAr9hmXLiKg0Pa11rOPXu7atTZ3C2Ic97WIyoaBUyhCKt8tz6Q==
=MVfN
-----END PGP PUBLIC KEY BLOCK-----

View File

@ -26,7 +26,7 @@ describe('Koa App (HTTP Server) Integration Tests', function() {
before(async () => {
sandbox.stub(log);
publicKeyArmored = fs.readFileSync(`${__dirname}/../key1.asc`, 'utf8');
publicKeyArmored = fs.readFileSync(`${__dirname}/../fixtures/key1.asc`, 'utf8');
mongo = new Mongo();
await mongo.init(config.mongo);

View File

@ -16,7 +16,7 @@ describe('Email Integration Tests', function() {
const recipient = {name: 'Test User', email: 'safewithme.testuser@gmail.com'};
before(() => {
publicKeyArmored = require('fs').readFileSync(`${__dirname}/../key1.asc`, 'utf8');
publicKeyArmored = require('fs').readFileSync(`${__dirname}/../fixtures/key1.asc`, 'utf8');
origin = {
protocol: 'http',
host: `localhost:${config.server.port}`

View File

@ -27,8 +27,8 @@ describe('Public Key Integration Tests', function() {
const origin = {host: 'localhost', protocol: 'http'};
before(async () => {
publicKeyArmored = require('fs').readFileSync(`${__dirname}/../key3.asc`, 'utf8');
publicKeyArmored2 = require('fs').readFileSync(`${__dirname}/../key4.asc`, 'utf8');
publicKeyArmored = require('fs').readFileSync(`${__dirname}/../fixtures/key3.asc`, 'utf8');
publicKeyArmored2 = require('fs').readFileSync(`${__dirname}/../fixtures/key4.asc`, 'utf8');
sinon.stub(log, 'info');
mongo = new Mongo();
await mongo.init(config.mongo);

View File

@ -11,13 +11,17 @@ describe('PGP Unit Tests', () => {
let key1Armored;
let key2Armored;
let key3Armored;
let key5Armored;
before(() => {
key1Armored = fs.readFileSync(`${__dirname}/../fixtures/key1.asc`, 'utf8');
key2Armored = fs.readFileSync(`${__dirname}/../fixtures/key2.asc`, 'utf8');
key3Armored = fs.readFileSync(`${__dirname}/../fixtures/key3.asc`, 'utf8');
key5Armored = fs.readFileSync(`${__dirname}/../fixtures/key5.asc`, 'utf8');
});
beforeEach(() => {
sandbox.stub(log);
key1Armored = fs.readFileSync(`${__dirname}/../key1.asc`, 'utf8');
key2Armored = fs.readFileSync(`${__dirname}/../key2.asc`, 'utf8');
key3Armored = fs.readFileSync(`${__dirname}/../key3.asc`, 'utf8');
pgp = new PGP();
});
@ -195,4 +199,49 @@ describe('PGP Unit Tests', () => {
expect(parsed.length).to.equal(0);
});
});
describe('filterKeyByUserIds', () => {
it('should filter user IDs', async () => {
const email = 'test1@example.com';
const {keys: [key]} = await openpgp.key.readArmored(key3Armored);
expect(key.users.length).to.equal(4);
const filtered = await pgp.filterKeyByUserIds([{email}], key3Armored);
const {keys: [filteredKey]} = await openpgp.key.readArmored(filtered);
expect(filteredKey.users.length).to.equal(1);
expect(filteredKey.users[0].userId.email).to.equal(email);
});
it('should not filter user attributes', async () => {
const email = 'test@example.com';
const {keys: [key]} = await openpgp.key.readArmored(key5Armored);
expect(key.users.length).to.equal(2);
const filtered = await pgp.filterKeyByUserIds([{email}], key5Armored);
const {keys: [filteredKey]} = await openpgp.key.readArmored(filtered);
expect(filteredKey.users.length).to.equal(2);
expect(filteredKey.users[0].userId).to.exist;
expect(filteredKey.users[1].userAttribute).to.exist;
});
});
describe('removeUserId', () => {
it('should remove user IDs', async () => {
const email = 'test1@example.com';
const {keys: [key]} = await openpgp.key.readArmored(key3Armored);
expect(key.users.length).to.equal(4);
const reduced = await pgp.removeUserId(email, key3Armored);
const {keys: [reducedKey]} = await openpgp.key.readArmored(reduced);
expect(reducedKey.users.length).to.equal(3);
expect(reducedKey.users.includes(({userId}) => userId.email === email)).to.be.false;
});
it('should not remove user attributes', async () => {
const email = 'test@example.com';
const {keys: [key]} = await openpgp.key.readArmored(key5Armored);
expect(key.users.length).to.equal(2);
const reduced = await pgp.removeUserId(email, key5Armored);
const {keys: [reducedKey]} = await openpgp.key.readArmored(reduced);
expect(reducedKey.users.length).to.equal(1);
expect(reducedKey.users[0].userAttribute).to.exist;
});
});
});