Add OpenPGP public key server support
- Add VKS and HTTPS HKP lookup and publication with bounded, validated requests. - Add desktop key-server settings, lookup/import flow, and per-publication native confirmation. - Keep discovered keys untrusted and update architecture, encryption, and user documentation. - Add mocked server protocol and key-store tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const openpgp = require('openpgp');
|
||||
const { createKeyServerService, normalizeCustomServer } = require('../desktop/pgp-keyservers.cjs');
|
||||
const { createKeyStore } = require('../desktop/pgp-keystore.cjs');
|
||||
|
||||
const email = 'ada@example.org';
|
||||
|
||||
function memoryStore() {
|
||||
let data = null;
|
||||
return {
|
||||
async load() { return data && JSON.parse(JSON.stringify(data)); },
|
||||
async save(value) { data = JSON.parse(JSON.stringify(value)); }
|
||||
};
|
||||
}
|
||||
|
||||
async function makeKey() {
|
||||
const generated = await openpgp.generateKey({
|
||||
type: 'ecc',
|
||||
curve: 'curve25519Legacy',
|
||||
userIDs: [{ name: 'Ada Lovelace', email }],
|
||||
passphrase: 'correct horse battery',
|
||||
format: 'armored'
|
||||
});
|
||||
const key = await openpgp.readKey({ armoredKey: generated.publicKey });
|
||||
return { fingerprint: key.getFingerprint().toUpperCase(), armoredKey: key.armor() };
|
||||
}
|
||||
|
||||
test('validates custom HTTPS key server configuration', () => {
|
||||
assert.deepEqual(normalizeCustomServer({
|
||||
id: 'custom',
|
||||
name: 'Internal server',
|
||||
url: 'https://keys.example.org/openpgp',
|
||||
protocol: 'hkp'
|
||||
}), {
|
||||
id: 'custom',
|
||||
name: 'Internal server',
|
||||
url: 'https://keys.example.org/openpgp/',
|
||||
protocol: 'hkp',
|
||||
builtin: false
|
||||
});
|
||||
assert.throws(() => normalizeCustomServer({ name: 'Plain HTTP', url: 'http://keys.example.org', protocol: 'hkp' }), /HTTPS/);
|
||||
assert.throws(() => normalizeCustomServer({ name: 'Credential URL', url: 'https://user:pass@keys.example.org', protocol: 'vks' }), /credentials/);
|
||||
assert.throws(() => normalizeCustomServer({ name: 'Query URL', url: 'https://keys.example.org/?token=x', protocol: 'vks' }), /query/);
|
||||
assert.throws(() => normalizeCustomServer({ name: 'Unknown protocol', url: 'https://keys.example.org', protocol: 'unknown' }), /protocol/);
|
||||
});
|
||||
|
||||
test('VKS lookup returns only valid keys matching the requested address', async () => {
|
||||
const key = await makeKey();
|
||||
let requestedUrl;
|
||||
const service = createKeyServerService({
|
||||
async fetchImpl(url, options) {
|
||||
requestedUrl = new URL(url);
|
||||
assert.equal(options.redirect, 'error');
|
||||
return new Response(key.armoredKey, { status: 200, headers: { 'content-type': 'application/pgp-keys' } });
|
||||
}
|
||||
});
|
||||
const found = await service.lookup({
|
||||
id: 'openpgp-vks',
|
||||
name: 'keys.openpgp.org',
|
||||
url: 'https://keys.openpgp.org',
|
||||
protocol: 'vks'
|
||||
}, email);
|
||||
assert.equal(requestedUrl.pathname, '/vks/v1/by-email/ada%40example.org');
|
||||
assert.equal(found.length, 1);
|
||||
assert.equal(found[0].fingerprint, key.fingerprint);
|
||||
assert.deepEqual(found[0].emails, [email]);
|
||||
});
|
||||
|
||||
test('HKP lookup filters unrelated keys and reports an empty result', async () => {
|
||||
const key = await makeKey();
|
||||
let requestedUrl;
|
||||
const service = createKeyServerService({
|
||||
async fetchImpl(url) {
|
||||
requestedUrl = new URL(url);
|
||||
return new Response(`not a key\n${key.armoredKey}`, { status: 200 });
|
||||
}
|
||||
});
|
||||
const server = { id: 'custom', name: 'Example', url: 'https://keys.example.org/base/', protocol: 'hkp' };
|
||||
const found = await service.lookup(server, email);
|
||||
assert.equal(requestedUrl.pathname, '/base/pks/lookup');
|
||||
assert.equal(requestedUrl.searchParams.get('op'), 'get');
|
||||
assert.equal(requestedUrl.searchParams.get('search'), email);
|
||||
assert.equal(found.length, 1);
|
||||
assert.deepEqual(await service.lookup(server, 'other@example.org'), []);
|
||||
});
|
||||
|
||||
test('VKS publication uploads only the public key and requests email verification', async () => {
|
||||
const key = await makeKey();
|
||||
const calls = [];
|
||||
const service = createKeyServerService({
|
||||
async fetchImpl(url, options) {
|
||||
calls.push({ url: new URL(url), options });
|
||||
if (calls.length === 1) {
|
||||
return Response.json({
|
||||
key_fpr: key.fingerprint,
|
||||
token: 'temporary-upload-token',
|
||||
status: { [email]: 'unpublished' }
|
||||
});
|
||||
}
|
||||
return Response.json({ key_fpr: key.fingerprint, token: 'temporary-upload-token', status: { [email]: 'pending' } });
|
||||
}
|
||||
});
|
||||
const result = await service.publish({
|
||||
id: 'openpgp-vks',
|
||||
name: 'keys.openpgp.org',
|
||||
url: 'https://keys.openpgp.org',
|
||||
protocol: 'vks'
|
||||
}, email, key.armoredKey, key.fingerprint);
|
||||
assert.equal(calls[0].url.pathname, '/vks/v1/upload');
|
||||
assert.equal(calls[1].url.pathname, '/vks/v1/request-verify');
|
||||
assert.equal(result.verification, 'pending');
|
||||
const upload = JSON.parse(calls[0].options.body);
|
||||
assert.equal(upload.keytext, key.armoredKey);
|
||||
assert.equal(upload.keytext.includes('PRIVATE KEY'), false);
|
||||
assert.deepEqual(JSON.parse(calls[1].options.body).addresses, [email]);
|
||||
});
|
||||
|
||||
test('HKP publication posts armored public material only', async () => {
|
||||
const key = await makeKey();
|
||||
let requestInfo;
|
||||
const service = createKeyServerService({
|
||||
async fetchImpl(url, options) {
|
||||
requestInfo = { url: new URL(url), options };
|
||||
return new Response('Upload successful. Check your inbox to verify your email address.', { status: 200 });
|
||||
}
|
||||
});
|
||||
const result = await service.publish({
|
||||
id: 'mailvelope-hkp',
|
||||
name: 'keys.mailvelope.com',
|
||||
url: 'https://keys.mailvelope.com',
|
||||
protocol: 'hkp'
|
||||
}, email, key.armoredKey, key.fingerprint);
|
||||
assert.equal(requestInfo.url.pathname, '/pks/add');
|
||||
assert.equal(requestInfo.options.headers['content-type'], 'application/x-www-form-urlencoded');
|
||||
assert.equal(new URLSearchParams(requestInfo.options.body).get('keytext'), key.armoredKey);
|
||||
assert.equal(requestInfo.options.body.includes('PRIVATE KEY'), false);
|
||||
assert.equal(result.verification, 'check-email');
|
||||
});
|
||||
|
||||
test('key-server contact imports stay untrusted and retain keyserver provenance', async () => {
|
||||
const key = await makeKey();
|
||||
const keyStore = createKeyStore(memoryStore());
|
||||
const added = await keyStore.addContact({ armoredKey: key.armoredKey, source: 'keyserver' });
|
||||
assert.equal(added.contact.trust, 'unknown');
|
||||
assert.equal(added.contact.source, 'keyserver');
|
||||
});
|
||||
|
||||
test('lookup rejects oversized responses and HTTP failures', async () => {
|
||||
const server = { id: 'openpgp-vks', name: 'keys.openpgp.org', url: 'https://keys.openpgp.org', protocol: 'vks' };
|
||||
const tooLarge = createKeyServerService({
|
||||
async fetchImpl() { return new Response('x'.repeat(600001), { status: 200 }); }
|
||||
});
|
||||
await assert.rejects(() => tooLarge.lookup(server, email), /too large/);
|
||||
const failure = createKeyServerService({
|
||||
async fetchImpl() { return new Response('', { status: 503 }); }
|
||||
});
|
||||
await assert.rejects(() => failure.lookup(server, email), /HTTP 503/);
|
||||
const invalid = createKeyServerService({
|
||||
async fetchImpl() { return new Response('<html>unexpected response</html>', { status: 200 }); }
|
||||
});
|
||||
await assert.rejects(() => invalid.lookup(server, email), /did not contain an OpenPGP public key/);
|
||||
});
|
||||
|
||||
test('VKS verification failure reports that the key upload already succeeded', async () => {
|
||||
const key = await makeKey();
|
||||
let calls = 0;
|
||||
const service = createKeyServerService({
|
||||
async fetchImpl() {
|
||||
calls += 1;
|
||||
if (calls === 1) {
|
||||
return Response.json({
|
||||
key_fpr: key.fingerprint,
|
||||
token: 'temporary-upload-token',
|
||||
status: { [email]: 'unpublished' }
|
||||
});
|
||||
}
|
||||
throw new Error('network unavailable');
|
||||
}
|
||||
});
|
||||
const result = await service.publish({
|
||||
id: 'openpgp-vks',
|
||||
name: 'keys.openpgp.org',
|
||||
url: 'https://keys.openpgp.org',
|
||||
protocol: 'vks'
|
||||
}, email, key.armoredKey, key.fingerprint);
|
||||
assert.equal(result.uploaded, true);
|
||||
assert.equal(result.verification, 'request-failed');
|
||||
assert.match(result.warning, /key was uploaded/);
|
||||
});
|
||||
|
||||
test('publication rejects a fingerprint that does not match the public key', async () => {
|
||||
const key = await makeKey();
|
||||
const service = createKeyServerService({
|
||||
async fetchImpl() { throw new Error('unexpected network request'); }
|
||||
});
|
||||
await assert.rejects(() => service.publish({
|
||||
id: 'mailvelope-hkp',
|
||||
name: 'keys.mailvelope.com',
|
||||
url: 'https://keys.mailvelope.com',
|
||||
protocol: 'hkp'
|
||||
}, email, key.armoredKey, '0'.repeat(40)), /does not match/);
|
||||
});
|
||||
@@ -166,9 +166,12 @@ test('keyring: contacts, trust levels, expired passphrase cache and settings', a
|
||||
await assert.rejects(async () => alice.keyStore.addContact({ armoredKey: await bob.keyStore.exportPublic(bob.key.fingerprint) }), /already/);
|
||||
await assert.rejects(async () => alice.keyStore.addContact({ armoredKey: await bob.keyStore.exportPrivate(bob.key.fingerprint, bob.passphrase) }), /private key/);
|
||||
await assert.rejects(() => alice.keyStore.addContact({ armoredKey: 'junk' }), /valid OpenPGP/);
|
||||
assert.deepEqual(await alice.keyStore.getSettings(), { cacheMinutes: 10 });
|
||||
await alice.keyStore.setSettings({ cacheMinutes: 0 });
|
||||
assert.deepEqual(await alice.keyStore.getSettings(), { cacheMinutes: 10, customKeyServers: [] });
|
||||
const customServer = { id: 'custom', name: 'Example', url: 'https://keys.example.org/', protocol: 'hkp' };
|
||||
await alice.keyStore.setSettings({ cacheMinutes: 0, customKeyServers: [customServer] });
|
||||
assert.equal((await alice.keyStore.getSettings()).cacheMinutes, 0);
|
||||
await alice.keyStore.setSettings({ cacheMinutes: 5 });
|
||||
assert.deepEqual((await alice.keyStore.getSettings()).customKeyServers, [customServer]);
|
||||
await alice.keyStore.removeContact(contact.fingerprint);
|
||||
assert.equal((await alice.keyStore.listContacts()).length, 0);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user