werkstaetten-app/src/lib/server/mail.ts
Friederich Loheide 3230618596 Sicherheit der Redaktion: 2FA, Passwort-Reset, CSP, dauerhafte Sperren
- Zwei-Faktor-Anmeldung (TOTP, RFC 6238 – gegen Testvektoren geprüft) mit
  QR-Code, 8 Wiederherstellungscodes, Schutz vor Code-Wiederverwendung;
  optional Pflicht für Administratoren; Admins können 2FA zurücksetzen
- „Passwort vergessen“ per E-Mail (SMTP über .env, sonst Link im Log),
  Link 1 Stunde gültig, verrät nicht, ob ein Konto existiert, beendet
  danach alle Sitzungen
- Sperren gegen zu viele Versuche in der Datenbank (Anmeldung, 2FA,
  Passwort-Reset, Kontaktformular) – überstehen Neustarts
- Content-Security-Policy (Skripte nur eigene/Nonce), Permissions-Policy,
  Cross-Origin-Opener-Policy

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-30 17:21:38 +00:00

38 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import nodemailer, { type Transporter } from 'nodemailer';
import { getSettings } from './settings';
/**
* E-Mail-Versand über SMTP (Einstellungen in der .env).
* Ohne SMTP-Konfiguration wird die Nachricht nur ins Server-Log geschrieben.
*/
const host = process.env.SMTP_HOST;
let transport: Transporter | null = null;
export const mailConfigured = () => !!host;
function transporter() {
if (!host) return null;
transport ??= nodemailer.createTransport({
host,
port: Number(process.env.SMTP_PORT ?? 587),
secure: process.env.SMTP_SECURE === 'true' || process.env.SMTP_PORT === '465',
auth: process.env.SMTP_USER ? { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS ?? '' } : undefined
});
return transport;
}
export async function sendMail({ to, subject, text }: { to: string; subject: string; text: string }): Promise<boolean> {
const t = transporter();
const from = process.env.SMTP_FROM ?? `${getSettings().appName} <no-reply@${new URL(process.env.ORIGIN ?? 'http://localhost').hostname}>`;
if (!t) {
console.log(`[mail] SMTP nicht eingerichtet – E-Mail an ${to} nicht versendet:\n Betreff: ${subject}\n ${text.replace(/\n/g, '\n ')}`);
return false;
}
try {
await t.sendMail({ from, to, subject, text });
return true;
} catch (e) {
console.error('[mail] Versand fehlgeschlagen:', (e as Error).message);
return false;
}
}