Add configurable public key server usage

Persist per-server enablement, search enabled servers, and require an explicit publication target. Update documentation and tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-10-10 20:28:24 +03:00
co-authored by Copilot
parent 04cd4117fe
commit f3e68b10bd
11 changed files with 196 additions and 42 deletions
+42 -1
View File
@@ -1,7 +1,7 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const openpgp = require('openpgp');
const { createKeyServerService, normalizeCustomServer } = require('../desktop/pgp-keyservers.cjs');
const { createKeyServerService, listServers, normalizeCustomServer } = require('../desktop/pgp-keyservers.cjs');
const { createKeyStore } = require('../desktop/pgp-keystore.cjs');
const email = 'ada@example.org';
@@ -45,6 +45,20 @@ test('validates custom HTTPS key server configuration', () => {
assert.throws(() => normalizeCustomServer({ name: 'Unknown protocol', url: 'https://keys.example.org', protocol: 'unknown' }), /protocol/);
});
test('all built-in and custom key servers start disabled', () => {
const servers = listServers([{
id: 'custom',
name: 'Internal server',
url: 'https://keys.example.org',
protocol: 'hkp'
}]);
assert.equal(servers.length, 3);
assert.ok(servers.every(server => server.enabled === false));
const enabled = listServers([], ['openpgp-vks']);
assert.equal(enabled.find(server => server.id === 'openpgp-vks').enabled, true);
assert.equal(enabled.find(server => server.id === 'mailvelope-hkp').enabled, false);
});
test('VKS lookup returns only valid keys matching the requested address', async () => {
const key = await makeKey();
let requestedUrl;
@@ -85,6 +99,33 @@ test('HKP lookup filters unrelated keys and reports an empty result', async () =
assert.deepEqual(await service.lookup(server, 'other@example.org'), []);
});
test('enabled-server lookup checks every enabled server, merges duplicate keys and reports failures', async () => {
const key = await makeKey();
const requested = [];
const service = createKeyServerService({
async fetchImpl(url) {
const target = new URL(url);
requested.push(target.hostname);
if (target.hostname === 'broken.example.org') throw new Error('server unavailable');
return new Response(key.armoredKey, { status: 200 });
}
});
const results = await service.lookupEnabled([
{ id: 'one', name: 'First', url: 'https://first.example.org', protocol: 'hkp', enabled: true },
{ id: 'two', name: 'Second', url: 'https://second.example.org', protocol: 'vks', enabled: true },
{ id: 'bad', name: 'Offline', url: 'https://broken.example.org', protocol: 'hkp', enabled: true },
{ id: 'off', name: 'Disabled', url: 'https://disabled.example.org', protocol: 'hkp', enabled: false }
], email);
assert.deepEqual(requested.sort(), ['broken.example.org', 'first.example.org', 'second.example.org']);
assert.equal(results.keys.length, 1);
assert.deepEqual(results.keys[0].sources, ['First', 'Second']);
assert.deepEqual(results.errors.map(error => error.name), ['Offline']);
assert.deepEqual(results.searchedServers.map(server => server.name), ['First', 'Second', 'Offline']);
await assert.rejects(() => service.lookupEnabled([
{ id: 'off', name: 'Disabled', url: 'https://disabled.example.org', protocol: 'hkp', enabled: false }
], email), /Enable at least one key server/);
});
test('VKS publication uploads only the public key and requests email verification', async () => {
const key = await makeKey();
const calls = [];
+5 -2
View File
@@ -166,12 +166,15 @@ 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, customKeyServers: [] });
assert.deepEqual(await alice.keyStore.getSettings(), { cacheMinutes: 10, customKeyServers: [], enabledKeyServers: [] });
const customServer = { id: 'custom', name: 'Example', url: 'https://keys.example.org/', protocol: 'hkp' };
await alice.keyStore.setSettings({ cacheMinutes: 0, customKeyServers: [customServer] });
await alice.keyStore.setSettings({ cacheMinutes: 0, customKeyServers: [customServer], enabledKeyServers: ['custom', 'openpgp-vks'] });
assert.equal((await alice.keyStore.getSettings()).cacheMinutes, 0);
await alice.keyStore.setSettings({ cacheMinutes: 5 });
assert.deepEqual((await alice.keyStore.getSettings()).customKeyServers, [customServer]);
assert.deepEqual((await alice.keyStore.getSettings()).enabledKeyServers, ['custom', 'openpgp-vks']);
await alice.keyStore.setSettings({ cacheMinutes: 5, enabledKeyServers: [] });
assert.deepEqual((await alice.keyStore.getSettings()).enabledKeyServers, []);
await alice.keyStore.removeContact(contact.fingerprint);
assert.equal((await alice.keyStore.listContacts()).length, 0);