A security review of the transport and verification layers. Every item is a fix
to how untrusted peer input is handled; no features changed.
- SAS verification could be bypassed. `verification_both_confirmed` is an
unauthenticated frame on a channel that is not yet trusted, but it was taken as
proof that both sides had compared their codes — so a peer who completed the
signalling exchange could send it right after the data channel opened and drive
the other side to a "verified" session while the user never looked at the code.
It is now only an acknowledgement: refused unless this side already confirmed
locally, and _setVerifiedStatus() independently rejects any SAS-based
transition without a local confirmation. Holding ECDH-derived keys was never
proof of identity — a MITM has those too.
- Unauthenticated frames could be injected into the chat. A bare
{type:"message"} frame, a raw non-JSON frame and a binary frame were each
decoded and rendered, bypassing decryption, the HMAC check and the verification
gate; the injected text was indistinguishable from a genuine message. Chat
content now reaches the UI only through the authenticated enhanced_message
path.
- A peer could supply the verification code. `sas_code` announcements were
adopted verbatim when no local SAS had been derived yet. They may now only
corroborate the locally derived code.
- Anti-replay never ran. The sequence-number and AAD validators were defined on
SecureKeyStorage instead of the connection manager, so every call site failed
with a TypeError and the sliding replay window was dead code. Moved onto the
manager, wired into the live chat path, and a missing or non-numeric sequence
number now fails closed instead of sailing through the range checks.
- File transfers are gated on verification in both directions. Control frames are
written straight to the data channel by the transfer system; sending was
already gated, receiving now is too.
- Tighter CSP: connect-src and img-src no longer allow arbitrary https: hosts
(nothing in the app talks to a third party), plus base-uri 'none'.
- The SAS is no longer written to logs, and is compared in constant time on every
path. Fixed SecureMasterKeyManager.isUnlocked() testing a field renamed long
ago, so it never actually gated anything.
- Fixed the header showing "Secure undefined%": getRealSecurityLevel() became
reachable for the first time by the move above and returned only per-feature
booleans, while the header renders `level` and `score` directly. It now runs
the same verified scoring as every other consumer.
Adds regression tests for the verification gate, inbound frame authentication and
the security-level shape.
111 lines
5.1 KiB
JavaScript
111 lines
5.1 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
|
|
// No DOM needed: we mock the incoming-chat sanitizer so DOMPurify/window are
|
|
// not required, and exercise the transport/meta plumbing directly.
|
|
globalThis.window = globalThis.window || {};
|
|
|
|
const { EnhancedSecureWebRTCManager } = await import('../src/network/EnhancedSecureWebRTCManager.js');
|
|
const P = EnhancedSecureWebRTCManager.prototype;
|
|
const T = EnhancedSecureWebRTCManager.MESSAGE_TYPES;
|
|
|
|
// ── _sanitizeMessageMeta: whitelist + bounds ────────────────────────────────
|
|
{
|
|
const ok = P._sanitizeMessageMeta.call({}, { mid: 'm_1-a', once: true, onceTtl: 15, ttl: 300, code: true });
|
|
assert.deepEqual(ok, { mid: 'm_1-a', code: true, once: true, onceTtl: 15, ttl: 300 });
|
|
|
|
// onceTtl is clamped to [1, 3600]; out-of-range is dropped.
|
|
assert.equal(P._sanitizeMessageMeta.call({}, { once: true, onceTtl: 99999 }).onceTtl, undefined);
|
|
assert.equal(P._sanitizeMessageMeta.call({}, { once: true, onceTtl: 30 }).onceTtl, 30);
|
|
|
|
// Junk and out-of-range values are stripped; with no valid keys -> null.
|
|
assert.equal(P._sanitizeMessageMeta.call({}, { foo: 1 }), null);
|
|
assert.equal(P._sanitizeMessageMeta.call({}, null), null);
|
|
assert.equal(P._sanitizeMessageMeta.call({}, { ttl: 999999 }), null); // above 24h cap
|
|
assert.equal(P._sanitizeMessageMeta.call({}, { ttl: 1 }), null); // below 5s floor
|
|
assert.equal(P._sanitizeMessageMeta.call({}, { once: 'yes' }), null); // must be exactly true
|
|
assert.deepEqual(P._sanitizeMessageMeta.call({}, { mid: 'bad id!@#' }), { mid: 'badid' }); // sanitized
|
|
}
|
|
|
|
// ── deliverMessageToUI forwards sanitized meta to onMessage ──────────────────
|
|
{
|
|
const calls = [];
|
|
const manager = {
|
|
_debugMode: false,
|
|
_secureLog() {},
|
|
_sanitizeIncomingChatMessage: (m) => m, // bypass DOMPurify in test
|
|
_sanitizeMessageMeta: P._sanitizeMessageMeta,
|
|
onMessage: (message, type, meta) => calls.push({ message, type, meta })
|
|
};
|
|
P.deliverMessageToUI.call(manager, 'hello', 'received', { once: true, ttl: 30, mid: 'm1', junk: 9 });
|
|
assert.equal(calls.length, 1);
|
|
assert.equal(calls[0].message, 'hello');
|
|
assert.equal(calls[0].type, 'received');
|
|
assert.deepEqual(calls[0].meta, { mid: 'm1', once: true, ttl: 30 });
|
|
|
|
// No meta -> onMessage gets undefined (backward compatible).
|
|
calls.length = 0;
|
|
P.deliverMessageToUI.call(manager, 'plain', 'received');
|
|
assert.equal(calls[0].meta, undefined);
|
|
}
|
|
|
|
// ── processMessage routes message_delete to onMessageDelete ──────────────────
|
|
{
|
|
const deleted = [];
|
|
const manager = {
|
|
_secureLog() {},
|
|
onMessageDelete: (id) => deleted.push(id)
|
|
};
|
|
await P.processMessage.call(
|
|
manager,
|
|
JSON.stringify({ type: T.MESSAGE_DELETE, data: { messageId: 'm_42' } })
|
|
);
|
|
assert.deepEqual(deleted, ['m_42']);
|
|
}
|
|
|
|
// ── live enhanced-message path delivers metadata to the UI ───────────────────
|
|
// This is the path real chat uses (dataChannel.onmessage -> _processEnhancedMessageWithoutMutex).
|
|
{
|
|
const envelope = JSON.stringify({ type: 'message', data: 'hi there', meta: { mid: 'm7', once: true, ttl: 30 } });
|
|
// decryptMessage always returns the authenticated sequence number alongside
|
|
// the plaintext; the receive path uses it for anti-replay validation.
|
|
globalThis.window.EnhancedSecureCryptoUtils = {
|
|
decryptMessage: async () => ({ message: envelope, messageId: 'msg_7', sequenceNumber: 0 })
|
|
};
|
|
const calls = [];
|
|
const manager = {
|
|
encryptionKey: {}, macKey: {}, metadataKey: {},
|
|
_secureLog() {},
|
|
_checkInboundRateLimit: () => true,
|
|
_sanitizeIncomingChatMessage: (m) => m,
|
|
_sanitizeMessageMeta: P._sanitizeMessageMeta,
|
|
_validateIncomingSequenceNumber: P._validateIncomingSequenceNumber,
|
|
replayProtectionEnabled: true,
|
|
expectedSequenceNumber: 0,
|
|
replayWindow: new Set(),
|
|
replayWindowSize: 64,
|
|
maxSequenceGap: 100,
|
|
onMessage: (message, type, meta) => calls.push({ message, type, meta }),
|
|
deliverMessageToUI: P.deliverMessageToUI
|
|
};
|
|
await P._processEnhancedMessageWithoutMutex.call(manager, { type: 'enhanced_message', data: 'enc' });
|
|
assert.equal(calls.length, 1);
|
|
assert.equal(calls[0].message, 'hi there');
|
|
assert.deepEqual(calls[0].meta, { mid: 'm7', once: true, ttl: 30 });
|
|
}
|
|
|
|
// ── sendMessageDelete emits a well-formed control message ────────────────────
|
|
{
|
|
const sent = [];
|
|
const manager = { sendSystemMessage: (m) => { sent.push(m); return true; } };
|
|
const result = P.sendMessageDelete.call(manager, 'm_99');
|
|
assert.equal(result, true);
|
|
assert.deepEqual(sent, [{ type: T.MESSAGE_DELETE, messageId: 'm_99' }]);
|
|
|
|
// Invalid ids are rejected without emitting anything.
|
|
sent.length = 0;
|
|
assert.equal(P.sendMessageDelete.call(manager, ''), false);
|
|
assert.equal(sent.length, 0);
|
|
}
|
|
|
|
console.log('Secure chat features tests passed');
|