Files
HellCon/tests/webrtc.test.js
T
Schluffe 752d0cf3af fix: harden WebRTC transfers for v0.0.1
Require mutual fingerprint confirmation before sharing metadata or files.
Validate incoming messages, bound buffering, and confirm receipt before
reporting success. Add direct-to-disk saving and explicit TURN opt-in.

Fix reset and retry handling, clean up cancelled connections, and preserve
completed Firefox downloads when the sender disconnects.

Update dependencies, clarify security assumptions, and add protocol and
cross-browser regression tests. Set version 0.0.1 and document the release
in CHANGELOG.md, linked from the README.

BREAKING CHANGE: both peers must use signaling and transfer protocol v2;
older connection codes are no longer accepted.
2026-10-09 12:11:14 +02:00

61 lines
3.6 KiB
JavaScript

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.js';
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`;
test('TURN is absent by default and dormant credentials cannot enable it', () => {
assert.deepEqual(iceConfiguration(), { iceServers: [] });
const settings = { stunUrl: 'stun:example.com:3478', turnUrl: 'turn:relay.example', username: 'user', credential: 'secret' };
assert.equal(iceConfiguration(settings).iceServers.length, 1);
assert.equal(iceConfiguration({ ...settings, allowTurn: true }).iceServers.length, 2);
assert.throws(() => iceConfiguration({ stunUrl: 'turn:relay.example' }));
assert.throws(() => iceConfiguration({ allowTurn: true }));
});
test('remote relay candidates require local explicit opt-in', () => {
const relayed = sdp + 'a=candidate:1 1 udp 10 192.0.2.1 5000 typ relay raddr 0.0.0.0 rport 0\r\n';
assert.throws(() => decodeSignal(encodeSignal(relayed)), /explicitly enable TURN/);
assert.equal(decodeSignal(encodeSignal(relayed), { allowTurn: true }), relayed);
});
test('fingerprints show all 32 bytes and reject ambiguous descriptions', () => {
const expected = fingerprint.toUpperCase().replaceAll(':', ' ');
assert.equal(validateDescription(sdp), expected);
assert.deepEqual(getFingerprints({ localDescription: { sdp }, remoteDescription: { sdp } }), { local: expected, remote: expected });
assert.throws(() => validateDescription(sdp + `a=fingerprint:sha-256 ${fingerprint}\r\n`));
assert.throws(() => validateDescription(sdp.replace(fingerprint, 'AA:BB:CC:DD:EE')));
assert.throws(() => validateDescription(sdp + 'm=audio 9 UDP/TLS/RTP/SAVPF 111\r\n'));
});
test('signals round-trip but reject legacy, malformed and oversized codes', () => {
assert.equal(decodeSignal(encodeSignal(sdp)), sdp);
assert.throws(() => decodeSignal(btoa(sdp)));
assert.throws(() => decodeSignal('bad!'));
assert.throws(() => decodeSignal('a'.repeat(MAX_SIGNAL_LENGTH + 1)));
assert.throws(() => decodeSignal(btoa(JSON.stringify({ version: 1, sdp }))));
});
class Peer extends EventTarget { iceGatheringState = 'gathering'; connectionState = 'new'; }
test('ICE gathering waits for completion, rejects timeout, and supports cancellation', async () => {
const pc = new Peer();
const completed = waitForIceComplete(pc, { timeout: 100 });
pc.iceGatheringState = 'complete'; pc.dispatchEvent(new Event('icegatheringstatechange')); await completed;
await assert.rejects(waitForIceComplete(new Peer(), { timeout: 5 }), /timed out/);
const controller = new AbortController();
const cancelled = waitForIceComplete(new Peer(), { signal: controller.signal }); controller.abort();
await assert.rejects(cancelled, /cancelled/);
});
test('buffer wait resolves when already drained and rejects close or cancellation', async () => {
const channel = new EventTarget(); channel.readyState = 'open'; channel.bufferedAmount = 0;
await waitForBufferDrain(channel);
channel.bufferedAmount = MAX_BUFFERED + 1;
const wait = waitForBufferDrain(channel); channel.readyState = 'closed'; channel.dispatchEvent(new Event('close'));
await assert.rejects(wait, /interrupted/);
channel.readyState = 'open'; const controller = new AbortController();
const cancelled = waitForBufferDrain(channel, { signal: controller.signal }); controller.abort();
await assert.rejects(cancelled, /interrupted/);
});