Let the code prove tracker connections instead of the user

Both sides derive an HMAC key from the code, next to the swarm ID, and sign
their role and both DTLS fingerprints as they see them. An interposer or a
peer that found the swarm without the code can't, so its connection ends and
the next peer gets its turn, as does one that proves nothing within 10 s.
Tracker transfers need no comparing and no clicks anymore, the manual
exchange keeps its fingerprint check. The swarm ID changes once more, old and
new clients don't find each other.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SchluffeandClaude Opus 5.5 committed 2026-10-25 13:05:58 +01:00
1 parent 707248f36b
commit c6145b8919
4 files changed
+193 -89

No files matched your search

+91 -53
View File
@@ -1,4 +1,4 @@
import { createHash, pbkdf2Sync, randomBytes } from 'node:crypto';
import { createHash, hkdfSync, pbkdf2Sync, randomBytes } from 'node:crypto';
import { createSocket, type Socket } from 'node:dgram';
import { createReadStream } from 'node:fs';
import { open as openFile, readFile, writeFile } from 'node:fs/promises';
@@ -28,9 +28,10 @@ const SIZE = 5_000_000;
const LARGE_SIZE = 2.5 * 2 ** 30;
// Big enough that the transfer still runs while a test cancels or disconnects.
const RUNNING_SIZE = 100_000_000;
// WINDOW, CHUNK_SIZE and ICE_TIMEOUT in src/core.ts.
// WINDOW, CHUNK_SIZE, ICE_TIMEOUT and PROOF_TIMEOUT in src/core.ts.
const WINDOW = 8 * 2 ** 20;
const ICE_TIMEOUT = 5000;
const PROOF_TIMEOUT = 10_000;
// Enough for a transfer whose receiver holds back its reads to pause at the window.
const HELD_SIZE = 3 * WINDOW;
// What the fake STUN server reports as public address, reserved for documentation.
@@ -301,10 +302,12 @@ async function withLogs(sender: Page, receiver: Page, fn: () => Promise<void>) {
}
}
// Connects and verifies that both sides show the same security check, swapped.
// Connects and verifies that both sides show the same security check, swapped. Over a tracker the
// code proves the connection, nobody sees one.
async function connect(mode: string, sender: Page, receiver: Page, source: string) {
await sender.setInputFiles('input[type=file]', source);
await MODES[mode]!(sender, receiver);
if (mode === 'tracker') return proven(sender, receiver);
const check = (page: Page, side: string) => page.locator(`#check-${side}`).textContent();
await expect(sender.locator('#check-local')).toHaveText(/^([0-9A-F]{2} ){7}[0-9A-F]{2}$/, {
@@ -324,14 +327,24 @@ const downloadStarts = (receiver: Page) =>
.waitForEvent('download', { timeout: test.info().timeout })
.then(async (download) => ({ download, logAtStart: await log(receiver) }));
// Connects, confirms the security check on both sides and starts the download.
// Both sides passed the security check of tracker signaling, without showing one.
async function proven(sender: Page, receiver: Page) {
for (const page of [sender, receiver]) {
await expect(page.locator('#log')).toContainText('security check passed', { timeout: 45_000 });
await expect(page.locator('#check-local')).toHaveCount(0);
}
}
// Connects, confirms the security check on both sides unless the code proved it, and starts the download.
async function start(mode: string, sender: Page, receiver: Page, source: string) {
await connect(mode, sender, receiver, source);
const started = downloadStarts(receiver);
await sender.click('#confirm');
await expect(sender.locator('#log')).toContainText("waiting for the receiver's security check");
await expect(receiver.locator('#log')).not.toContainText('receiving');
await receiver.click('#confirm');
if (mode !== 'tracker') {
await sender.click('#confirm');
await expect(sender.locator('#log')).toContainText("waiting for the receiver's security check");
await expect(receiver.locator('#log')).not.toContainText('receiving');
await receiver.click('#confirm');
}
// Offered, but nothing is sent before the user asks for it.
await expect(receiver.locator('#incoming')).toContainText(source.split('/').pop()!);
await expect(sender.locator('#log')).toContainText('waiting for the receiver to download');
@@ -697,10 +710,8 @@ for (const name of Object.keys(BROWSERS)) {
await receiver.fill('#receive-code', code);
await receiver.click('#receive');
await expect(receiver.locator('#check-local')).toBeVisible({ timeout: 45_000 });
await proven(sender, receiver);
const started = downloadStarts(receiver);
await sender.click('#confirm');
await receiver.click('#confirm');
await receiver.click('#download');
const { download } = await started;
expect(await hash(await download.path())).toBe(await hash(source));
@@ -717,7 +728,7 @@ for (const name of Object.keys(BROWSERS)) {
await withLogs(sender, receiver, async () => {
await sender.setInputFiles('input[type=file]', source);
await MODES.tracker!(sender, receiver);
await expect(receiver.locator('#check-local')).toBeVisible({ timeout: 45_000 });
await proven(sender, receiver);
await Promise.all([sender, receiver].map((page) => page.clock.fastForward('06:00')));
for (const page of [sender, receiver]) {
await expect.poll(() => sockets.get(page)!.filter((ws) => !ws.isClosed()).length).toBe(0);
@@ -725,8 +736,6 @@ for (const name of Object.keys(BROWSERS)) {
await expect(sender.locator('label[for=send-code]')).toHaveText('Code, no longer valid:');
const started = downloadStarts(receiver);
await sender.click('#confirm');
await receiver.click('#confirm');
await receiver.click('#download');
const { download } = await started;
expect(await hash(await download.path())).toBe(await hash(source));
@@ -881,26 +890,33 @@ for (const name of Object.keys(BROWSERS)) {
});
});
// A peer that guessed the code, or got the same one by chance, takes the sender's turn first.
test(`live check: ${name} sender shares on after a mismatch`, async () => {
// A peer that found the swarm without the code takes the sender's turn first: here one that
// announces another code on the sender's info_hash. Neither side can prove anything to the other.
test(`live check: ${name} sender shares on after a peer without the code`, async () => {
const [sender, intruder, receiver] = await Promise.all([open(name), open(name), open(name)]);
const source = await payload(randomBytes(SIZE));
await withLogs(sender, receiver, async () => {
await connect('tracker', sender, intruder, source);
await receiver.fill('#receive-code', (await sender.locator('#send-code').textContent())!);
await receiver.click('#receive');
await expect(receiver.locator('#log')).toContainText('looking on');
await sender.click('#reject');
await sender.setInputFiles('input[type=file]', source);
await sender.click('#share');
await expect(sender.locator('#log')).toContainText(`waiting on ${TRACKER}`);
const code = (await sender.locator('#send-code').textContent())!;
await intrude(intruder, swarm(code));
await intruder.fill('#receive-code', 'notthecode');
await intruder.click('#receive');
await expect(sender.locator('#log')).toContainText('waiting for the next receiver', { timeout: 45_000 });
// Whoever verifies first closes the connection, the other side may only see the peer leave.
await expect
.poll(async () => `${await log(sender)}${await log(intruder)}`)
.toContain('security check failed, the peer does not know the code');
for (const page of [sender, intruder]) await expect(page.locator('#check-local')).toHaveCount(0);
await intruder.context().close();
await expect(sender.locator('#log')).toContainText('waiting for the next receiver');
expect(await log(sender)).not.toContain('offered');
await expect(receiver.locator('#check-local')).toBeVisible({ timeout: 45_000 });
await expect(sender.locator('#check-local')).toHaveText((await receiver.locator('#check-remote').textContent())!);
await expect(sender.locator('#check-remote')).toHaveText((await receiver.locator('#check-local').textContent())!);
await receiver.fill('#receive-code', code);
await receiver.click('#receive');
await proven(sender, receiver);
const started = downloadStarts(receiver);
await sender.click('#confirm');
await receiver.click('#confirm');
await receiver.click('#download');
const { download } = await started;
expect(await hash(await download.path())).toBe(await hash(source));
@@ -908,34 +924,22 @@ for (const name of Object.keys(BROWSERS)) {
});
});
test(`live check: ${name} both sides go on after the receiver rejected`, async () => {
const [sender, receiver] = await Promise.all([open(name), open(name)]);
test(`live check: ${name} sender gives up on a peer that proves nothing`, async () => {
const [sender, receiver] = await Promise.all([open(name), open(name, { init: withholdProof })]);
await sender.clock.install();
const source = await payload(randomBytes(SIZE));
await withLogs(sender, receiver, async () => {
await connect('tracker', sender, receiver, source);
const first = await receiver.locator('#check-local').textContent();
// Retrying still has to work for a side that already confirmed.
await sender.click('#confirm');
await receiver.click('#reject');
await expect(receiver.locator('#log')).toContainText('looking again');
// The sender notices the receiver left, Chromium only once the connection counts as failed.
await expect(sender.locator('#log')).toContainText('waiting for the next receiver', { timeout: 30_000 });
await expect(receiver.locator('#check-local')).toHaveText(
new RegExp(`^(?!${first}$)([0-9A-F]{2} ){7}[0-9A-F]{2}$`),
{
timeout: 45_000,
},
);
await expect(sender.locator('#check-remote')).toHaveText((await receiver.locator('#check-local').textContent())!);
const started = downloadStarts(receiver);
await sender.click('#confirm');
await receiver.click('#confirm');
await receiver.click('#download');
const { download } = await started;
expect(await hash(await download.path())).toBe(await hash(source));
await expect(sender.locator('#log')).toContainText('delivered');
await sender.setInputFiles('input[type=file]', source);
await MODES.tracker!(sender, receiver);
// The sender's proof still reaches the receiver.
await expect(receiver.locator('#log')).toContainText('security check passed', { timeout: 45_000 });
await expect(sender.locator('#log')).toContainText('waiting for the security check');
await sender.clock.fastForward(PROOF_TIMEOUT);
await expect(sender.locator('#log')).toContainText('the peer proved no code');
await expect(sender.locator('#log')).toContainText('waiting for the next receiver');
expect(await log(sender)).not.toContain('offered');
await expect(sender.locator('#send-error')).toHaveCount(0);
});
});
@@ -957,6 +961,40 @@ for (const name of Object.keys(BROWSERS)) {
});
}
// Makes the page a tracker intruder: it announces its offers on the info_hash hash instead of its own,
// on TRACKER only and just the first offer. A later one could still take the sender's turn after the
// test closed the page.
async function intrude(page: Page, hash: string) {
let offered = false;
await page.routeWebSocket(/^wss:/, (ws) => {
if (!ws.url().startsWith(TRACKER)) return ws.close();
const server = ws.connectToServer();
ws.onMessage((data) => {
const msg = JSON.parse(String(data));
if (msg.offers && offered) return;
offered ||= !!msg.offers;
server.send(JSON.stringify({ ...msg, info_hash: hash }));
});
});
}
// Init script: the page never sends a proof of the code, everything else as usual. It runs in the
// hidden frames too, only the app's own window counts.
function withholdProof() {
if (window !== window.top) return;
const send = RTCDataChannel.prototype.send as (this: RTCDataChannel, data: unknown) => void;
RTCDataChannel.prototype.send = function (this: RTCDataChannel, data: unknown) {
if (typeof data === 'string' && data.includes('"proof"')) return;
send.call(this, data);
};
}
// The code's info_hash, derived like secrets in src/core.ts.
const swarm = (code: string) =>
Buffer.from(hkdfSync('sha256', pbkdf2Sync(code, 'hellcon', 600_000, 32, 'sha256'), '', 'swarm', 20)).toString(
'latin1',
);
// What a broken or hostile receiver could announce for the code: an offer no browser can parse.
async function announceGarbage(code: string) {
const ws = new WebSocket(TRACKER);
@@ -968,7 +1006,7 @@ async function announceGarbage(code: string) {
ws.send(
JSON.stringify({
action: 'announce',
info_hash: binary(pbkdf2Sync(code, 'hellcon', 600_000, 20, 'sha256')),
info_hash: swarm(code),
peer_id: binary(randomBytes(20)),
numwant: 1,
uploaded: 0,