mirror of
https://github.com/HeyPuter/puter.git
synced 2026-11-25 15:17:23 +00:00
* feat(email): notification emails ask listeners before each recipient
Share notification emails (account holders and invites) and the app
feedback email to the owner now emit `email.notify.validate` with
`{ to, kind, allow }` once per recipient address, next to the existing
unsubscribe and blocked-domain checks. A listener that sets `allow` to
false skips that recipient's email; the others are sent as before. The
invite check runs before its send budget is claimed and the feedback
check before the per-app cap slot, so a skipped address uses neither.
When no listener answers or the hook fails, the email is sent.
* refactor(email): type the notify kind as a template name, drop the dead catch
`emitAndWait` already catches a listener that throws, so the try/catch
around it never ran; a failed listener leaves `allow` true and the email
is sent. `kind` is now `EmailTemplateName`, so renaming a template breaks
the type check instead of the listener.
* fix(share): ask email.notify.validate when the digest is sent
A digest entry can wait out its window or a retried send, so the
listener is asked about the recipient right before the mail goes.
Invites are still asked once more before their budget is claimed.
* fix(email): any falsy allow on email.notify.validate skips the mail
Matches how email.validate reads its answer. The helper also takes the
event client as required, since every caller has one.
* feat(email): email.notify.validate carries every recipient of one kind of mail
The payload is { kind, recipients: [{ to, allow }] }. Share invites ask
once for all their addresses before any budget is claimed; the digest
send and the feedback mail ask with one entry. kind is narrowed to the
three templates that ask.
222 lines
7.4 KiB
TypeScript
222 lines
7.4 KiB
TypeScript
/*
|
|
* Copyright (C) 2024-present Puter Technologies Inc.
|
|
*
|
|
* This file is part of Puter.
|
|
*
|
|
* Puter is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
/**
|
|
* Email normalization + block-list check. Stateless; every path that matches,
|
|
* keys or blocks on an address uses these rules.
|
|
*
|
|
* CleanEmail('foo.bar+tag@gmail.com') === 'foobar@gmail.com'
|
|
* isBlockedEmail('temp@mailinator.com', ['mailinator.com']) === true
|
|
*/
|
|
|
|
import validator from 'validator';
|
|
import type { EventClient } from '../clients/event/EventClient';
|
|
import type {
|
|
EmailNotifyKind,
|
|
EmailNotifyValidateEvent,
|
|
} from '../clients/event/types';
|
|
|
|
type RuleName = 'dots_dont_matter' | 'remove_subaddressing';
|
|
|
|
interface Parts {
|
|
local: string;
|
|
domain: string;
|
|
}
|
|
|
|
const RULES: Record<RuleName, (p: Parts) => void> = {
|
|
dots_dont_matter: (p) => {
|
|
p.local = p.local.replace(/\./g, '');
|
|
},
|
|
remove_subaddressing: (p) => {
|
|
p.local = p.local.split('+')[0];
|
|
},
|
|
};
|
|
|
|
/** Rules each provider's semantics allow. Unlisted domains get none. */
|
|
const PROVIDERS: Record<string, { rules: RuleName[] }> = {
|
|
gmail: { rules: ['dots_dont_matter', 'remove_subaddressing'] },
|
|
// Dots are significant at Apple: `j.smith@` and `jsmith@` are two mailboxes.
|
|
icloud: { rules: ['remove_subaddressing'] },
|
|
outlook: { rules: ['remove_subaddressing'] },
|
|
proton: { rules: ['remove_subaddressing'] },
|
|
fastmail: { rules: ['remove_subaddressing'] },
|
|
zoho: { rules: ['remove_subaddressing'] },
|
|
yandex: { rules: ['remove_subaddressing'] },
|
|
// Listed to record the finding: yahoo makes `+` significant, using `-`.
|
|
yahoo: { rules: [] },
|
|
};
|
|
|
|
/** `brand.tld` for each whitespace-separated tld. */
|
|
const regional = (brand: string, tlds: string): string[] =>
|
|
tlds
|
|
.split(/\s+/)
|
|
.filter(Boolean)
|
|
.map((tld) => `${brand}.${tld}`);
|
|
|
|
/** Microsoft's consumer domains, regional ones included; one mailbox system. */
|
|
const MICROSOFT_DOMAINS = [
|
|
'msn.com',
|
|
'passport.com',
|
|
'windowslive.com',
|
|
...regional(
|
|
'outlook',
|
|
`com ae at be bg ch cl co co.id co.il co.in co.jp co.kr co.nz co.th co.uk
|
|
com.ar com.au com.br com.co com.mx com.my com.pe com.ph com.sg com.tr
|
|
com.vn cz de dk ee eg es fi fr gr hk hr hu ie in it jp kr lt lv my nl no
|
|
nz ph pk pl pt qa ro rs ru sa se sg si sk tw ua vn`,
|
|
),
|
|
...regional(
|
|
'hotmail',
|
|
`com at be ca ch cl co.il co.in co.jp co.kr co.nz co.th co.uk co.za
|
|
com.ar com.au com.br com.hk com.mx com.tr com.tw com.vn cz de dk es fi
|
|
fr gr hu ie it my nl no ph pt se sg sk`,
|
|
),
|
|
...regional(
|
|
'live',
|
|
`com at be ca ch cl cn co.kr co.uk co.za com.ar com.au com.mx com.my
|
|
com.ph com.pt com.sg de dk fi fr hk ie in it jp nl no ru se`,
|
|
),
|
|
];
|
|
|
|
const DOMAIN_TO_PROVIDER: Record<string, string> = {
|
|
'gmail.com': 'gmail',
|
|
'googlemail.com': 'gmail',
|
|
'icloud.com': 'icloud',
|
|
'me.com': 'icloud',
|
|
'mac.com': 'icloud',
|
|
...Object.fromEntries(MICROSOFT_DOMAINS.map((d) => [d, 'outlook'])),
|
|
'proton.me': 'proton',
|
|
'protonmail.com': 'proton',
|
|
'protonmail.ch': 'proton',
|
|
'pm.me': 'proton',
|
|
'fastmail.com': 'fastmail',
|
|
'fastmail.fm': 'fastmail',
|
|
'zoho.com': 'zoho',
|
|
'zohomail.com': 'zoho',
|
|
'zoho.eu': 'zoho',
|
|
'zohomail.eu': 'zoho',
|
|
'zoho.in': 'zoho',
|
|
'zohomail.in': 'zoho',
|
|
'yandex.com': 'yandex',
|
|
'yandex.ru': 'yandex',
|
|
'yandex.by': 'yandex',
|
|
'yandex.kz': 'yandex',
|
|
'yandex.ua': 'yandex',
|
|
'ya.ru': 'yandex',
|
|
'yahoo.com': 'yahoo',
|
|
'yahoo.co.uk': 'yahoo',
|
|
'yahoo.ca': 'yahoo',
|
|
'yahoo.com.au': 'yahoo',
|
|
};
|
|
|
|
/** Aliases that resolve to the same inbox on the provider side. */
|
|
const DOMAIN_NONDISTINCT: Record<string, string> = {
|
|
'googlemail.com': 'gmail.com',
|
|
};
|
|
|
|
/**
|
|
* Canonical form used for the `user.clean_email` column and for duplicate
|
|
* detection. Lowercases, collapses nondistinct domains, strips provider-
|
|
* insignificant characters.
|
|
*/
|
|
export function cleanEmail(email: string): string {
|
|
const lower = email.toLowerCase();
|
|
const [localRaw, domainRaw] = lower.split('@');
|
|
if (!domainRaw) return lower;
|
|
|
|
const parts: Parts = {
|
|
local: localRaw,
|
|
domain: DOMAIN_NONDISTINCT[domainRaw] ?? domainRaw,
|
|
};
|
|
|
|
// Nothing is assumed about a domain we don't know: lowercasing only.
|
|
const provider = PROVIDERS[DOMAIN_TO_PROVIDER[parts.domain] ?? ''];
|
|
for (const rule of provider?.rules ?? []) RULES[rule](parts);
|
|
|
|
return `${parts.local}@${parts.domain}`;
|
|
}
|
|
|
|
/**
|
|
* Whether a stored address is the one looked up, ignoring case. Re-check SQL
|
|
* email matches with this: the MySQL collations also fold accents (`á` = `a`).
|
|
*/
|
|
export function isSameAddress(
|
|
stored: string | null | undefined,
|
|
lookedUp: string,
|
|
): boolean {
|
|
return (
|
|
typeof stored === 'string' &&
|
|
stored.trim().toLowerCase() === lookedUp.trim().toLowerCase()
|
|
);
|
|
}
|
|
|
|
/** Storage gate; `isEmail` alone throws on non-strings and passes quoted parts. */
|
|
export function isStorableEmail(email: unknown): boolean {
|
|
return (
|
|
typeof email === 'string' &&
|
|
!/["<>]/u.test(email) &&
|
|
validator.isEmail(email)
|
|
);
|
|
}
|
|
|
|
/** Strips `+` on any domain. For abuse decisions only, never for identity. */
|
|
export function abuseKey(email: string): string {
|
|
const [local, domain] = cleanEmail(email).split('@');
|
|
return domain ? `${local.split('+')[0]}@${domain}` : local;
|
|
}
|
|
|
|
/** Whether we have asserted how this domain treats its own local parts. */
|
|
export function isProviderCanonicalized(email: string): boolean {
|
|
const domain = email.toLowerCase().split('@')[1] ?? '';
|
|
return Boolean(DOMAIN_TO_PROVIDER[DOMAIN_NONDISTINCT[domain] ?? domain]);
|
|
}
|
|
|
|
/**
|
|
* Returns true when the (cleaned) email matches any of the blocked domain
|
|
* suffixes. Suffix-match so `mailinator.com` blocks `foo@bar.mailinator.com`;
|
|
* case-insensitive on both sides.
|
|
*/
|
|
export function isBlockedEmail(
|
|
email: string,
|
|
blockedDomains: readonly string[] | undefined,
|
|
): boolean {
|
|
if (!blockedDomains || blockedDomains.length === 0) return false;
|
|
const clean = cleanEmail(email);
|
|
return blockedDomains.some((suffix) =>
|
|
clean.endsWith(suffix.toLowerCase()),
|
|
);
|
|
}
|
|
|
|
/** The addresses of `to` that no `email.notify.validate` listener declined. */
|
|
export async function mayEmailNotify(
|
|
events: Pick<EventClient, 'emitAndWait'>,
|
|
kind: EmailNotifyKind,
|
|
to: string[],
|
|
): Promise<Set<string>> {
|
|
const event: EmailNotifyValidateEvent = {
|
|
kind,
|
|
recipients: to.map((address) => ({ to: address, allow: true })),
|
|
};
|
|
// A listener that throws leaves `allow` as it was, so the email still goes.
|
|
if (to.length > 0) {
|
|
await events.emitAndWait('email.notify.validate', event, {});
|
|
}
|
|
return new Set(event.recipients.filter((r) => r.allow).map((r) => r.to));
|
|
}
|