Warn about suspicious mail senders and links
Bump version to 0.1.21 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -6,6 +6,102 @@ let mailboxSyncQueued = false;
|
||||
let mailboxRefreshPending = false;
|
||||
let folderBeforeSearch = null;
|
||||
|
||||
const senderBrandDomains = {
|
||||
amazon: ['amazon.com', 'amazon.co.uk'],
|
||||
apple: ['apple.com', 'icloud.com'],
|
||||
google: ['google.com', 'gmail.com'],
|
||||
microsoft: ['microsoft.com', 'outlook.com', 'live.com'],
|
||||
paypal: ['paypal.com']
|
||||
};
|
||||
|
||||
function suspiciousSenderReasons(message) {
|
||||
const email = String(message.email || '').trim().toLowerCase();
|
||||
const domain = email.split('@').pop() || '';
|
||||
if (!domain) return [];
|
||||
const reasons = [];
|
||||
if (domain.includes('xn--')) reasons.push('The sender address uses an internationalized domain that may imitate another domain.');
|
||||
const name = String(message.sender || '').toLowerCase();
|
||||
for (const [brand, domains] of Object.entries(senderBrandDomains)) {
|
||||
const brandName = new RegExp(`^(?:the\\s+)?${brand}(?:\\s+(?:customer\\s+service|support|security|account|billing|team|payments?|services?))*$`, 'i');
|
||||
if (brandName.test(name) && !domains.some(allowed => domain === allowed || domain.endsWith(`.${allowed}`))) {
|
||||
reasons.push(`The display name mentions ${brand}, but the sender domain is ${domain}.`);
|
||||
}
|
||||
}
|
||||
if (/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(name) && name.split('@').pop() !== domain) {
|
||||
reasons.push('The address-like display name does not match the actual sender domain.');
|
||||
}
|
||||
return [...new Set(reasons)];
|
||||
}
|
||||
|
||||
function inspectMailLink(raw) {
|
||||
const cleaned = String(raw).replace(/[.,!?;:)\]}>"']+$/g, '');
|
||||
let parsed;
|
||||
try { parsed = new URL(cleaned); } catch { return null; }
|
||||
if (!['http:', 'https:'].includes(parsed.protocol) || parsed.username || parsed.password) return null;
|
||||
const host = parsed.hostname.toLowerCase();
|
||||
const reasons = [];
|
||||
if (parsed.protocol === 'http:') reasons.push('unencrypted HTTP connection');
|
||||
if (host.includes('xn--')) reasons.push('internationalized domain that may imitate another address');
|
||||
if (/^(?:\d{1,3}\.){3}\d{1,3}$/.test(host) || host.startsWith('[')) reasons.push('IP-address destination instead of a named website');
|
||||
if (['bit.ly', 't.co', 'tinyurl.com', 'is.gd', 'lnkd.in'].includes(host)) reasons.push('shortened link hides its final destination');
|
||||
for (const [brand, domains] of Object.entries(senderBrandDomains)) {
|
||||
const mentionsBrand = new RegExp(`(^|[.-])${brand.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')}([.-]|$)`, 'i').test(host);
|
||||
if (mentionsBrand && !domains.some(domain => host === domain || host.endsWith(`.${domain}`))) {
|
||||
reasons.push(`hostname mentions ${brand} without using its usual domain`);
|
||||
}
|
||||
}
|
||||
return { url: parsed.href, host, reasons };
|
||||
}
|
||||
|
||||
function messageLinks(message) {
|
||||
const bodyLinks = (message.body || []).join('\n').match(/https?:\/\/[^\s<>"']+/gi) || [];
|
||||
const declaredLinks = Array.isArray(message.links) ? message.links : [];
|
||||
return [...new Set([...bodyLinks, ...declaredLinks])]
|
||||
.map(inspectMailLink)
|
||||
.filter(Boolean)
|
||||
.slice(0, 30);
|
||||
}
|
||||
|
||||
const originalOpenMessage = openMessage;
|
||||
openMessage = function (id) {
|
||||
originalOpenMessage(id);
|
||||
const message = messages.find(item => item.id === id);
|
||||
if (!message) return;
|
||||
const senderReasons = suspiciousSenderReasons(message);
|
||||
const links = messageLinks(message);
|
||||
const warnings = [];
|
||||
if (senderReasons.length) {
|
||||
warnings.push(`<section class="mail-safety-warning" role="alert"><strong>Sender details may be misleading</strong><ul>${senderReasons.map(reason => `<li>${esc(reason)}</li>`).join('')}</ul><small>Sender names and addresses can be forged. Verify the sender through a trusted channel.</small></section>`);
|
||||
}
|
||||
const linkCards = links.map((link, index) => `
|
||||
<div class="mail-link-row ${link.reasons.length ? 'mail-link-risky' : ''}">
|
||||
<div><strong>${link.reasons.length ? 'Potentially unsafe link' : 'External link'}</strong><span>${esc(link.host)}</span>
|
||||
${link.reasons.length ? `<small>${esc(link.reasons.join('; '))}</small>` : ''}</div>
|
||||
<button type="button" class="mail-link-open" data-mail-link="${index}">Review & open</button>
|
||||
</div>`).join('');
|
||||
const reader = $('#reader-pane');
|
||||
const header = reader.querySelector('.reader-header');
|
||||
if (warnings.length && header) header.insertAdjacentHTML('afterend', warnings.join(''));
|
||||
const replyBar = reader.querySelector('.reply-bar');
|
||||
if (links.length && replyBar) {
|
||||
replyBar.insertAdjacentHTML('beforebegin', `<section class="mail-links"><h3>Links in this message</h3>${links.some(link => link.reasons.length) ? '<p class="mail-safety-intro">Some links show warning signs. Every link requires confirmation before it opens.</p>' : ''}${linkCards}</section>`);
|
||||
reader.querySelectorAll('[data-mail-link]').forEach(button => {
|
||||
button.onclick = async () => {
|
||||
const link = links[Number(button.dataset.mailLink)];
|
||||
if (!link) return;
|
||||
button.disabled = true;
|
||||
try {
|
||||
await window.daybreakMail.openExternal(link.url);
|
||||
} catch (error) {
|
||||
toast(error.message || 'Could not open this link');
|
||||
} finally {
|
||||
button.disabled = false;
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
function updateAccountCard(account) {
|
||||
const card = $('#account-settings');
|
||||
if (!card) return;
|
||||
|
||||
Reference in New Issue
Block a user