Files
client/test/privacy-renderer.test.mjs
T

107 lines
6.4 KiB
JavaScript

// Renderer privacy rules with the fake bridge: link classification, trust guard, remembered confirmations.
// No Electron, dialog, network or server.
import { test, beforeEach } from 'node:test';
import assert from 'node:assert/strict';
import './fixtures/renderer-env.mjs';
const { S } = await import('../app/renderer/js/state.js');
const links = await import('../app/renderer/js/external-links.js');
const confirmations = await import('../app/renderer/js/confirmations.js');
const writes = [];
const flagged = new Set(['evil.example']);
beforeEach(() => {
writes.length = 0;
S.prefs = { trustedExternalHosts: [], allowWhitelistMalicious: false, confirmationSettings: { delete_ban: true, delete_temp_pw: true, delete_privilege_key: true, clear_file_transfer_history: true } };
globalThis.teamspeach.setPrefs = async (patch) => { writes.push(patch); return { ok: true, prefs: { ...S.prefs, ...patch } }; };
globalThis.teamspeach.privacyLinkThreat = async (host) => ({ ok: true, malicious: flagged.has(host), loaded: true });
});
test('classification follows the official guard order; the phishing list only applies to untrusted hosts', () => {
const isMalicious = (host) => flagged.has(host);
assert.equal(links.checkLink('https://www.teamspeak.com/x', [], isMalicious), 'pass');
assert.equal(links.checkLink('https://twitch.tv/teamspeak_tv', [], isMalicious), 'pass');
assert.equal(links.checkLink('https://twitch.tv/teamspeak_tv.evil', [], isMalicious), 'warn');
assert.equal(links.checkLink('mailto:a@b.example', [], isMalicious), 'pass');
assert.equal(links.checkLink('https://evil.example/login', [], isMalicious), 'warn-malicious');
assert.equal(links.checkLink('https://sub.evil.example/', [], isMalicious), 'warn');
assert.equal(links.checkLink('https://evil.example/', ['evil.example'], isMalicious), 'pass');
assert.equal(links.checkLink('https://www.site.example/', ['site.example'], isMalicious), 'pass');
assert.equal(links.checkLink('https://site.example/', ['www.site.example'], isMalicious), 'pass');
assert.equal(links.checkLink('https://user:pw@site.example/', ['site.example'], isMalicious), 'warn');
assert.equal(links.checkLink('javascript:alert(1)', [], isMalicious), 'warn');
});
test('main lookup failures never mark a host malicious', async () => {
assert.equal(await links.isFlaggedHost('evil.example'), true);
assert.equal(await links.isFlaggedHost('good.example'), false);
globalThis.teamspeach.privacyLinkThreat = async () => { throw new Error('ipc gone'); };
assert.equal(await links.isFlaggedHost('evil.example'), false);
});
test('a flagged host is trusted only while "Allow trusting sites flagged as malicious" is on', async () => {
await assert.rejects(links.addTrustedHost('evil.example', { malicious: true }));
assert.equal(writes.length, 0);
S.prefs = { ...S.prefs, allowWhitelistMalicious: true };
await links.addTrustedHost('Evil.Example', { malicious: true });
assert.deepEqual(writes.at(-1), { trustedExternalHosts: ['evil.example'] });
await links.addTrustedHost('evil.example');
assert.equal(writes.length, 1, 'already trusted hosts are not written again');
});
test('trusted sites management removes one host or clears all through prefs', async () => {
S.prefs = { ...S.prefs, trustedExternalHosts: ['a.example', 'b.example'] };
await links.removeTrustedHost('A.example');
assert.deepEqual(S.prefs.trustedExternalHosts, ['b.example']);
await links.clearTrustedHosts();
assert.deepEqual(S.prefs.trustedExternalHosts, []);
globalThis.teamspeach.setPrefs = async () => ({ ok: false, message: 'disk full' });
await assert.rejects(links.removeTrustedHost('b.example'), /disk full/);
});
test('confirmations: defaults ask, skip choices persist the complete object, reset restores every key', async () => {
assert.deepEqual(confirmations.confirmationSettings({}), { delete_ban: true, delete_temp_pw: true, delete_privilege_key: true, clear_file_transfer_history: true });
await confirmations.applyConfirmationSettings({ delete_ban: false });
assert.deepEqual(writes.at(-1), { confirmationSettings: { delete_ban: false, delete_temp_pw: true, delete_privilege_key: true, clear_file_transfer_history: true } });
assert.equal(confirmations.shouldConfirm('delete_ban'), false);
await confirmations.applyConfirmationSettings({ clear_file_transfer_history: false });
await confirmations.resetConfirmations();
assert.deepEqual(writes.at(-1).confirmationSettings, { delete_ban: true, delete_temp_pw: true, delete_privilege_key: true, clear_file_transfer_history: true });
assert.equal(confirmations.shouldConfirm('clear_file_transfer_history'), true);
assert.throws(() => confirmations.applyConfirmationSettings({ bogus: false }), /Invalid confirmation/);
assert.throws(() => confirmations.shouldConfirm('bogus'), /Unknown confirmation/);
});
test('a skipped confirmation runs the action at once without a popup', async () => {
S.prefs = { ...S.prefs, confirmationSettings: { ...S.prefs.confirmationSettings, delete_temp_pw: false } };
let ran = 0;
assert.equal(await confirmations.confirmAction(null, 'delete_temp_pw', 'sure?', async () => { ran += 1; }), true);
assert.equal(ran, 1);
assert.equal(writes.length, 0);
});
test('a failed reset reports the error instead of claiming success', async () => {
globalThis.teamspeach.setPrefs = async () => ({ ok: false, error: { message: 'prefs: invalid value for confirmationSettings' } });
await assert.rejects(confirmations.resetConfirmations(), /invalid value/);
});
test('concurrent skip changes merge in order and a later reset cannot be overwritten', async () => {
let release;
const gate = new Promise(resolve => { release = resolve; });
let first = true;
globalThis.teamspeach.setPrefs = async patch => {
if (first) { first = false; await gate; }
return { ok: true, prefs: { ...S.prefs, ...patch } };
};
const ban = confirmations.applyConfirmationSettings({ delete_ban: false });
const password = confirmations.applyConfirmationSettings({ delete_temp_pw: false });
release();
await Promise.all([ban, password]);
assert.equal(confirmations.shouldConfirm('delete_ban'), false);
assert.equal(confirmations.shouldConfirm('delete_temp_pw'), false);
const skip = confirmations.applyConfirmationSettings({ delete_privilege_key: false });
const reset = confirmations.resetConfirmations();
await Promise.all([skip, reset]);
for (const key of confirmations.CONFIRMATION_KEYS) assert.equal(confirmations.shouldConfirm(key), true);
});