245 lines
10 KiB
JavaScript
245 lines
10 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const openpgp = require('openpgp');
|
|
const { createKeyServerService, listServers, 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('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;
|
|
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('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 = [];
|
|
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/);
|
|
});
|