Files
client/app/main/privacy-prefs.js
T

67 lines
3.8 KiB
JavaScript

'use strict';
/**
* Privacy preferences that follow the installed TS6 client's workflowSettings (client_ui/main.js, class Xe/Ge):
* - trustedExternalHosts: hosts the user chose to "Always trust" in the external-link warning.
* - allowWhitelistMalicious (default off): lets the warning offer "Always trust" for a host the phishing list
* flags. Off, a flagged host can still be opened after the warning but never becomes trusted.
* - confirmationSettings: one boolean per remembered confirmation (true = ask). A confirmation that offers
* "Don't ask again" stores false; Settings > Privacy > Reset Confirmations sets every key back to true.
*
* Myteamspeak chat settings (enableServerShare, enableTypingNotifications) and analytics are not stored:
* TeamsPeach has no chat-service presence or typing channel and sends no diagnostics.
*/
// Official class Ge defaults: every confirmation is shown until the user opts out.
const CONFIRMATION_KEYS = Object.freeze(['delete_ban', 'delete_temp_pw', 'delete_privilege_key', 'clear_file_transfer_history']);
const CONFIRMATION_DEFAULTS = Object.freeze(Object.fromEntries(CONFIRMATION_KEYS.map((key) => [key, true])));
const MAX_TRUSTED_HOSTS = 500;
const HOST = /^[a-z0-9.-]+$/;
const isPlainObject = (v) => v !== null && typeof v === 'object' && !Array.isArray(v) && Object.getPrototypeOf(v) === Object.prototype;
function validTrustedHosts(v) {
return Array.isArray(v) && v.length <= MAX_TRUSTED_HOSTS
&& v.every((h) => typeof h === 'string' && h.length >= 1 && h.length <= 253 && HOST.test(h));
}
/** Exactly the known keys, each a boolean: the renderer always writes the complete object. */
function validConfirmationSettings(v) {
if (!isPlainObject(v)) return false;
const keys = Object.keys(v);
return keys.length === CONFIRMATION_KEYS.length && CONFIRMATION_KEYS.every((key) => typeof v[key] === 'boolean');
}
const PRIVACY_SCHEMA = {
trustedExternalHosts: { default: [], check: validTrustedHosts },
allowWhitelistMalicious: { default: false, check: (v) => typeof v === 'boolean' },
confirmationSettings: { default: { ...CONFIRMATION_DEFAULTS }, check: validConfirmationSettings },
};
/**
* Keeps the remembered choices of a stored confirmationSettings object that does not match the current key set
* (written by another version): known boolean keys are kept, missing keys default to "ask", unknown keys drop.
* Mirrors the official load merge `{...defaults, ...stored}`. `values` already holds validated keys/defaults.
*/
function migratePrivacyPrefs(raw, values) {
const stored = raw?.confirmationSettings;
if (!isPlainObject(stored) || validConfirmationSettings(stored)) return;
values.confirmationSettings = Object.fromEntries(CONFIRMATION_KEYS.map((key) => [key, typeof stored[key] === 'boolean' ? stored[key] : true]));
}
/**
* Main-side guard for a prefs update: a host the phishing list flags may only become newly trusted while
* allowWhitelistMalicious is (or becomes, in the same update) enabled. Hosts already trusted stay as they are,
* like the official client. Returns an error message or null.
*/
function trustedHostsUpdateError(current, partial, isMalicious) {
if (!partial || !Object.hasOwn(partial, 'trustedExternalHosts') || !Array.isArray(partial.trustedExternalHosts)) return null;
const allow = Object.hasOwn(partial, 'allowWhitelistMalicious') ? partial.allowWhitelistMalicious === true : current.allowWhitelistMalicious === true;
if (allow) return null;
const before = new Set(current.trustedExternalHosts ?? []);
const flagged = partial.trustedExternalHosts.find((host) => !before.has(host) && isMalicious(host));
return flagged ? `prefs: ${flagged} is flagged as malicious; enable "Allow trusting sites flagged as malicious" first` : null;
}
module.exports = { PRIVACY_SCHEMA, CONFIRMATION_KEYS, CONFIRMATION_DEFAULTS, migratePrivacyPrefs, trustedHostsUpdateError };