WIP IV improve UX [snapshot task 2]
This commit is contained in:
1 parent
28c90d1d71
commit
58c3e2af86
16 files changed
+779
-36
No files matched your search
@@ -0,0 +1,49 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createIdentity, hello, signIdentity, verifyIdentity, exportBackup, importBackup, parseHello, validateContacts, type IdentityContext } from '../src/lib/identity.ts';
|
||||
const fingerprint = (byte: string) => Array(32).fill(byte).join(' ');
|
||||
async function setup() {
|
||||
const first = await createIdentity(), second = await createIdentity();
|
||||
const context: IdentityContext = { offer: hello(first), answer: hello(second), offerFingerprint: fingerprint('AA'), answerFingerprint: fingerprint('BB') };
|
||||
return { first, second, context };
|
||||
}
|
||||
test('device proof binds both keys, fresh nonces, roles, and both DTLS fingerprints', async () => {
|
||||
const { first, second, context } = await setup();
|
||||
const proof = await signIdentity(first, context, true);
|
||||
await verifyIdentity(proof, context, true);
|
||||
await verifyIdentity(await signIdentity(second, context, false), context, false);
|
||||
await assert.rejects(verifyIdentity(proof, context, false));
|
||||
for (const changed of [
|
||||
{ ...context, offer: hello(first) }, { ...context, answer: hello(second) },
|
||||
{ ...context, offer: hello(second) }, { ...context, answer: hello(first) },
|
||||
{ ...context, offerFingerprint: fingerprint('CC') }, { ...context, answerFingerprint: fingerprint('CC') },
|
||||
]) await assert.rejects(verifyIdentity(proof, changed, true));
|
||||
const imposter = await createIdentity();
|
||||
await assert.rejects(signIdentity(imposter, context, true));
|
||||
await assert.rejects(verifyIdentity('00'.repeat(64), context, true));
|
||||
});
|
||||
test('identity input is bounded and contacts cannot supply duplicate identities', async () => {
|
||||
const identity = await createIdentity();
|
||||
assert.throws(() => parseHello({ key: identity.publicKey, nonce: 'bad' }));
|
||||
assert.throws(() => parseHello({ key: 'ff'.repeat(65), nonce: '00'.repeat(32) }));
|
||||
assert.throws(() => validateContacts([{ key: identity.publicKey, name: ' ' }]));
|
||||
assert.throws(() => validateContacts(Array(101).fill({ key: identity.publicKey, name: 'friend' })));
|
||||
assert.throws(() => validateContacts(Array(2).fill({ key: identity.publicKey, name: 'friend' })));
|
||||
});
|
||||
test('encrypted backup restores signing identity and contacts; wrong password and tampering fail', async () => {
|
||||
const { first, second, context } = await setup();
|
||||
const state = { identity: first, contacts: [{ key: second.publicKey, name: 'Other device' }] };
|
||||
const password = 'a long private backup passphrase';
|
||||
const backup = await exportBackup(state, password);
|
||||
assert.doesNotMatch(backup, /Other device|privateKey/);
|
||||
const restored = await importBackup(backup, password);
|
||||
assert.deepEqual(restored.contacts, state.contacts);
|
||||
assert.equal(restored.identity!.publicKey, first.publicKey);
|
||||
await verifyIdentity(await signIdentity(restored.identity!, context, true), context, true);
|
||||
await assert.rejects(importBackup(backup, 'the incorrect password'));
|
||||
const damaged = JSON.parse(backup); damaged.data = (damaged.data.startsWith('00') ? 'ff' : '00') + damaged.data.slice(2);
|
||||
await assert.rejects(importBackup(JSON.stringify(damaged), password));
|
||||
damaged.iterations = 1; await assert.rejects(importBackup(JSON.stringify(damaged), password));
|
||||
await assert.rejects(importBackup('x'.repeat(100001), password));
|
||||
await assert.rejects(exportBackup(state, 'short'));
|
||||
});
|
||||
@@ -108,6 +108,7 @@ test('reused file IDs and stale receipts fail closed', async t => {
|
||||
await until(() => first.state.results.length === 1);
|
||||
const target = stale === 'start' ? second : first;
|
||||
target.channel.onmessage?.(new MessageEvent('message', { data: JSON.stringify({ v: 3, type: stale, id: 1, data: JSON.stringify({ v: 2, type: 'complete', bytes: 0 }) }) }));
|
||||
await until(() => target.state.closed);
|
||||
assert.equal(target.state.closed, true); assert.equal(target.state.errors.length, 1);
|
||||
}
|
||||
});
|
||||
@@ -116,5 +117,30 @@ test('unsolicited offers before verification and excess binary payload fail clos
|
||||
const { first, second } = pair(t);
|
||||
first.channel.onmessage?.(new MessageEvent('message', { data: JSON.stringify({ v: 3, type: 'start', id: 2 }) }));
|
||||
second.channel.onmessage?.(new MessageEvent('message', { data: new ArrayBuffer(100000) }));
|
||||
await until(() => first.state.closed && second.state.closed);
|
||||
assert.equal(first.state.closed, true); assert.equal(second.state.closed, true);
|
||||
});
|
||||
|
||||
test('identity proofs gate metadata even when a remembered contact requests automatic verification', async t => {
|
||||
const { first, second } = pair(t);
|
||||
let release!: () => void;
|
||||
const pending = new Promise<void>(resolve => { release = resolve; });
|
||||
first.session.authenticate({ sign: async () => 'sender-proof', verify: async proof => { assert.equal(proof, 'receiver-proof'); await pending; } });
|
||||
second.session.authenticate({ sign: async () => 'receiver-proof', verify: async proof => { assert.equal(proof, 'sender-proof'); await pending; } });
|
||||
first.session.add([file('private-name')]); first.session.verify(); second.session.verify();
|
||||
await tick();
|
||||
assert.equal(first.state.ready, false); assert.equal(second.state.ready, false);
|
||||
assert.doesNotMatch(first.channel.frames.join(''), /private-name/);
|
||||
release(); await until(() => second.state.receive === 'choose-storage');
|
||||
second.session.accept(); await until(() => first.state.results.length === 1);
|
||||
assert.deepEqual(first.state.errors, []); assert.deepEqual(second.state.errors, []);
|
||||
});
|
||||
|
||||
test('invalid or missing identity proof closes the session without releasing file metadata', async t => {
|
||||
const { first, second } = pair(t);
|
||||
first.session.authenticate({ sign: async () => 'proof', verify: async () => { throw new Error('Device proof failed'); } });
|
||||
first.session.add([file('private-name')]); first.session.verify(); second.session.verify();
|
||||
await until(() => first.state.closed);
|
||||
assert.match(first.state.errors.join(''), /Device proof failed/);
|
||||
assert.doesNotMatch(first.channel.frames.join(''), /private-name/);
|
||||
});
|
||||
+15
-1
@@ -1,6 +1,6 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { iceConfiguration, validateDescription, getFingerprints, encodeSignal, decodeSignal, waitForIceComplete, waitForBufferDrain, MAX_SIGNAL_LENGTH, MAX_BUFFERED } from '../src/lib/webrtc.ts';
|
||||
import { iceConfiguration, validateDescription, getFingerprints, signalIdentity, encodeSignal, decodeSignal, waitForIceComplete, waitForBufferDrain, MAX_SIGNAL_LENGTH, MAX_BUFFERED } from '../src/lib/webrtc.ts';
|
||||
const fingerprint = Array.from({ length: 32 }, (_, i) => i.toString(16).padStart(2, '0')).join(':');
|
||||
const sdp = `v=0\r\nm=application 9 UDP/DTLS/SCTP webrtc-datachannel\r\na=fingerprint:sha-256 ${fingerprint}\r\n`;
|
||||
|
||||
@@ -62,3 +62,17 @@ test('buffer wait resolves when already drained and rejects close or cancellatio
|
||||
const cancelled = waitForBufferDrain(channel, { signal: controller.signal }); controller.abort();
|
||||
await assert.rejects(cancelled, /interrupted/);
|
||||
});
|
||||
|
||||
|
||||
test('identity signaling is versioned, bounded, and optional for temporary sessions', () => {
|
||||
const identity = { key: '04' + 'ab'.repeat(64), nonce: 'cd'.repeat(32) };
|
||||
const code = encodeSignal(sdp, identity);
|
||||
assert.equal(JSON.parse(atob(code)).version, 4);
|
||||
assert.equal(decodeSignal(code), sdp);
|
||||
assert.deepEqual(signalIdentity(code), identity);
|
||||
assert.equal(signalIdentity(encodeSignal(sdp)), undefined);
|
||||
assert.equal(JSON.parse(atob(encodeSignal(sdp))).version, 3);
|
||||
assert.throws(() => decodeSignal(btoa(JSON.stringify({ version: 4, sdp }))));
|
||||
assert.throws(() => decodeSignal(btoa(JSON.stringify({ version: 4, sdp, identity: { ...identity, nonce: 'bad' } }))));
|
||||
assert.throws(() => decodeSignal(btoa(JSON.stringify({ version: 5, sdp, identity }))));
|
||||
});
|
||||
Reference in new issue
Block a user