forked from fahrPool/fahrPool
fix: SMTP-SSRF-Schutz, Port-Whitelist, Passwort nur bei gleichem Ziel behalten
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
9b90bf6335
commit
c797fe62ec
|
|
@ -4,8 +4,16 @@ import { revalidatePath } from "next/cache";
|
|||
import { createClient } from "@/lib/supabase/server";
|
||||
import { createAdminClient } from "@/lib/supabase/admin";
|
||||
import { ladeAdminKontext } from "@/lib/firma/admin-kontext";
|
||||
import { firmenSmtpZuConfig, ladeFirmenSmtp, validateSmtpEingabe } from "@/lib/firma/smtp";
|
||||
import {
|
||||
SMTP_ZIEL_NICHT_ERLAUBT,
|
||||
SmtpZielFehler,
|
||||
firmenSmtpZuConfig,
|
||||
ladeFirmenSmtp,
|
||||
pinneSmtpZiel,
|
||||
validateSmtpEingabe,
|
||||
} from "@/lib/firma/smtp";
|
||||
import { verschluessele } from "@/lib/firma/smtp-crypto";
|
||||
import { pruefeSmtpZiel } from "@/lib/firma/smtp-ziel";
|
||||
import { sendeProtokollMail } from "@/lib/notifications/email";
|
||||
|
||||
export type ActionResult = { success: true } | { success: false; error: string };
|
||||
|
|
@ -29,15 +37,22 @@ export async function speichereSmtp(formData: FormData): Promise<ActionResult> {
|
|||
absenderAdresse: formData.get("absender_adresse"),
|
||||
absenderName: formData.get("absender_name"),
|
||||
},
|
||||
Boolean(bestehend?.passwort_verschluesselt)
|
||||
bestehend
|
||||
);
|
||||
if (!validation.valid) return { success: false, error: validation.error };
|
||||
const w = validation.werte;
|
||||
|
||||
const ziel = await pruefeSmtpZiel(w.host);
|
||||
if (!ziel.ok) return { success: false, error: `${SMTP_ZIEL_NICHT_ERLAUBT}.` };
|
||||
|
||||
let passwortVerschluesselt: string | null = null;
|
||||
if (w.benutzer) {
|
||||
try {
|
||||
passwortVerschluesselt = w.neuesPasswort ? verschluessele(w.neuesPasswort) : bestehend?.passwort_verschluesselt ?? null;
|
||||
passwortVerschluesselt = w.neuesPasswort
|
||||
? verschluessele(w.neuesPasswort)
|
||||
: w.passwortBehalten
|
||||
? (bestehend?.passwort_verschluesselt ?? null)
|
||||
: null;
|
||||
} catch (fehler) {
|
||||
return { success: false, error: fehler instanceof Error ? fehler.message : "Verschlüsselung fehlgeschlagen." };
|
||||
}
|
||||
|
|
@ -86,8 +101,9 @@ export async function sendeTestmail(): Promise<ActionResult> {
|
|||
|
||||
let cfg;
|
||||
try {
|
||||
cfg = firmenSmtpZuConfig(zeile);
|
||||
cfg = await pinneSmtpZiel(firmenSmtpZuConfig(zeile));
|
||||
} catch (fehler) {
|
||||
if (fehler instanceof SmtpZielFehler) return { success: false, error: `${SMTP_ZIEL_NICHT_ERLAUBT}.` };
|
||||
return { success: false, error: fehler instanceof Error ? fehler.message : "Konfiguration ungültig." };
|
||||
}
|
||||
|
||||
|
|
@ -97,5 +113,8 @@ export async function sendeTestmail(): Promise<ActionResult> {
|
|||
text: "Diese Testmail bestätigt, dass der SMTP-Versand Ihrer Firma funktioniert.",
|
||||
attachments: [],
|
||||
});
|
||||
return ergebnis.ok ? { success: true } : { success: false, error: ergebnis.fehler.slice(0, 500) };
|
||||
if (ergebnis.ok) return { success: true };
|
||||
// Kein Rohtext an den Admin: Fehlermeldungen koennten interne Ziele verraten.
|
||||
console.error("Testmail fehlgeschlagen:", ergebnis.fehler);
|
||||
return { success: false, error: "Verbindung zum SMTP-Server fehlgeschlagen." };
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,71 @@
|
|||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("node:dns/promises", () => ({ lookup: vi.fn() }));
|
||||
|
||||
import { lookup } from "node:dns/promises";
|
||||
import { pruefeSmtpZiel } from "./smtp-ziel";
|
||||
|
||||
const lookupMock = vi.mocked(lookup) as unknown as ReturnType<typeof vi.fn>;
|
||||
|
||||
describe("pruefeSmtpZiel", () => {
|
||||
beforeEach(() => {
|
||||
lookupMock.mockReset();
|
||||
});
|
||||
|
||||
it.each([
|
||||
"127.0.0.1",
|
||||
"10.0.0.1",
|
||||
"0.0.0.0",
|
||||
"100.64.0.1",
|
||||
"169.254.169.254",
|
||||
"172.16.5.4",
|
||||
"192.168.1.1",
|
||||
"198.18.0.1",
|
||||
"224.0.0.1",
|
||||
"255.255.255.255",
|
||||
"::",
|
||||
"::1",
|
||||
"fc00::1",
|
||||
"fd12:3456::1",
|
||||
"fe80::1",
|
||||
"ff02::1",
|
||||
"::ffff:127.0.0.1",
|
||||
"::ffff:7f00:1",
|
||||
"::ffff:169.254.169.254",
|
||||
])("lehnt das IP-Literal %s ab", async (ip) => {
|
||||
expect(await pruefeSmtpZiel(ip)).toEqual({ ok: false });
|
||||
expect(lookupMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("akzeptiert eine oeffentliche IP direkt und pinnt sie", async () => {
|
||||
expect(await pruefeSmtpZiel("93.184.216.34")).toEqual({ ok: true, ip: "93.184.216.34" });
|
||||
expect(await pruefeSmtpZiel("2606:4700::1111")).toEqual({ ok: true, ip: "2606:4700::1111" });
|
||||
expect(lookupMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("lehnt einen Namen ab, der privat aufloest", async () => {
|
||||
lookupMock.mockResolvedValue([{ address: "10.1.2.3", family: 4 }]);
|
||||
expect(await pruefeSmtpZiel("intern.example.de")).toEqual({ ok: false });
|
||||
expect(lookupMock).toHaveBeenCalledWith("intern.example.de", { all: true });
|
||||
});
|
||||
|
||||
it("lehnt ab, wenn nur eine von mehreren Adressen privat ist", async () => {
|
||||
lookupMock.mockResolvedValue([
|
||||
{ address: "93.184.216.34", family: 4 },
|
||||
{ address: "127.0.0.1", family: 4 },
|
||||
]);
|
||||
expect(await pruefeSmtpZiel("mix.example.de")).toEqual({ ok: false });
|
||||
});
|
||||
|
||||
it("akzeptiert einen Namen mit oeffentlicher Adresse und liefert die erste IP", async () => {
|
||||
lookupMock.mockResolvedValue([{ address: "93.184.216.34", family: 4 }]);
|
||||
expect(await pruefeSmtpZiel("smtp.example.de")).toEqual({ ok: true, ip: "93.184.216.34" });
|
||||
});
|
||||
|
||||
it("lehnt ab, wenn die Aufloesung scheitert oder nichts liefert", async () => {
|
||||
lookupMock.mockRejectedValueOnce(new Error("ENOTFOUND"));
|
||||
expect(await pruefeSmtpZiel("gibt-es-nicht.example.de")).toEqual({ ok: false });
|
||||
lookupMock.mockResolvedValueOnce([]);
|
||||
expect(await pruefeSmtpZiel("leer.example.de")).toEqual({ ok: false });
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
import { lookup } from "node:dns/promises";
|
||||
import { BlockList, isIP } from "node:net";
|
||||
|
||||
// Nicht oeffentliche Ziele (SSRF-Schutz fuer den firmeneigenen SMTP-Server).
|
||||
const GESPERRT = new BlockList();
|
||||
for (const [netz, laenge] of [
|
||||
["0.0.0.0", 8],
|
||||
["10.0.0.0", 8],
|
||||
["100.64.0.0", 10],
|
||||
["127.0.0.0", 8],
|
||||
["169.254.0.0", 16],
|
||||
["172.16.0.0", 12],
|
||||
["192.0.0.0", 24],
|
||||
["192.168.0.0", 16],
|
||||
["198.18.0.0", 15],
|
||||
["224.0.0.0", 4],
|
||||
["240.0.0.0", 4],
|
||||
] as const) {
|
||||
GESPERRT.addSubnet(netz, laenge, "ipv4");
|
||||
}
|
||||
GESPERRT.addAddress("::", "ipv6");
|
||||
GESPERRT.addAddress("::1", "ipv6");
|
||||
GESPERRT.addSubnet("fc00::", 7, "ipv6");
|
||||
GESPERRT.addSubnet("fe80::", 10, "ipv6");
|
||||
GESPERRT.addSubnet("ff00::", 8, "ipv6");
|
||||
|
||||
// ::ffff:a.b.c.d bzw. ::ffff:xxxx:yyyy auf die eingebettete IPv4-Adresse abbilden.
|
||||
function normalisiere(ip: string): string {
|
||||
const punkt = /^::ffff:(\d{1,3}(?:\.\d{1,3}){3})$/i.exec(ip);
|
||||
if (punkt) return punkt[1];
|
||||
const hex = /^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i.exec(ip);
|
||||
if (hex) {
|
||||
const hoch = parseInt(hex[1], 16);
|
||||
const tief = parseInt(hex[2], 16);
|
||||
return `${hoch >> 8}.${hoch & 255}.${tief >> 8}.${tief & 255}`;
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
function istOeffentlich(ip: string): boolean {
|
||||
const norm = normalisiere(ip);
|
||||
const art = isIP(norm);
|
||||
if (art === 0) return false;
|
||||
return !GESPERRT.check(norm, art === 4 ? "ipv4" : "ipv6");
|
||||
}
|
||||
|
||||
export type SmtpZielErgebnis = { ok: true; ip: string } | { ok: false };
|
||||
|
||||
// Loest den Host auf und liefert die zu verwendende (gepinnte) IP. Alle Adressen
|
||||
// muessen oeffentlich sein; sonst ok:false (ohne Grund, kein Orakel fuer Admins).
|
||||
export async function pruefeSmtpZiel(host: string): Promise<SmtpZielErgebnis> {
|
||||
if (isIP(host) !== 0) {
|
||||
return istOeffentlich(host) ? { ok: true, ip: host } : { ok: false };
|
||||
}
|
||||
let adressen: { address: string }[];
|
||||
try {
|
||||
adressen = await lookup(host, { all: true });
|
||||
} catch {
|
||||
return { ok: false };
|
||||
}
|
||||
if (adressen.length === 0 || !adressen.every((a) => istOeffentlich(a.address))) {
|
||||
return { ok: false };
|
||||
}
|
||||
return { ok: true, ip: adressen[0].address };
|
||||
}
|
||||
|
|
@ -1,6 +1,16 @@
|
|||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("node:dns/promises", () => ({ lookup: vi.fn() }));
|
||||
import { lookup } from "node:dns/promises";
|
||||
import type { SupabaseClient } from "@supabase/supabase-js";
|
||||
import { ermittleEmailConfig, firmenSmtpZuConfig, validateSmtpEingabe, type FirmenSmtpZeile } from "./smtp";
|
||||
import {
|
||||
SmtpZielFehler,
|
||||
ermittleEmailConfig,
|
||||
firmenSmtpZuConfig,
|
||||
validateSmtpEingabe,
|
||||
type BestehendSmtp,
|
||||
type FirmenSmtpZeile,
|
||||
} from "./smtp";
|
||||
import { verschluessele } from "./smtp-crypto";
|
||||
|
||||
const KEY = Buffer.alloc(32, 3).toString("base64");
|
||||
|
|
@ -15,9 +25,16 @@ const EINGABE = {
|
|||
absenderName: "Firma B",
|
||||
};
|
||||
|
||||
const BESTEHEND: BestehendSmtp = {
|
||||
host: "smtp.firma.de",
|
||||
port: 587,
|
||||
benutzer: "mailer",
|
||||
passwort_verschluesselt: "v1:x:y:z",
|
||||
};
|
||||
|
||||
describe("validateSmtpEingabe", () => {
|
||||
it("uebernimmt gueltige Werte", () => {
|
||||
expect(validateSmtpEingabe(EINGABE, false)).toEqual({
|
||||
expect(validateSmtpEingabe(EINGABE, null)).toEqual({
|
||||
valid: true,
|
||||
werte: {
|
||||
host: "smtp.firma.de",
|
||||
|
|
@ -25,35 +42,62 @@ describe("validateSmtpEingabe", () => {
|
|||
secure: false,
|
||||
benutzer: "mailer",
|
||||
neuesPasswort: "geheim",
|
||||
passwortBehalten: false,
|
||||
absender_adresse: "noreply@firma.de",
|
||||
absender_name: "Firma B",
|
||||
},
|
||||
});
|
||||
});
|
||||
it("Host ist Pflicht", () => {
|
||||
expect(validateSmtpEingabe({ ...EINGABE, host: " " }, false)).toEqual({ valid: false, error: "Bitte den SMTP-Server angeben." });
|
||||
expect(validateSmtpEingabe({ ...EINGABE, host: " " }, null)).toEqual({ valid: false, error: "Bitte den SMTP-Server angeben." });
|
||||
});
|
||||
it("Port muss 1 bis 65535 sein", () => {
|
||||
expect(validateSmtpEingabe({ ...EINGABE, port: "70000" }, false)).toEqual({
|
||||
it.each(["70000", "22", "80", "abc", "587.5", ""])("Port %s ist nicht erlaubt", (port) => {
|
||||
expect(validateSmtpEingabe({ ...EINGABE, port }, null)).toEqual({
|
||||
valid: false,
|
||||
error: "Bitte einen gültigen Port (1–65535) angeben.",
|
||||
error: "Bitte einen gültigen Port (25, 465, 587 oder 2525) angeben.",
|
||||
});
|
||||
});
|
||||
it.each(["25", "465", "587", "2525"])("Port %s ist erlaubt", (port) => {
|
||||
expect(validateSmtpEingabe({ ...EINGABE, port }, null).valid).toBe(true);
|
||||
});
|
||||
it("Absenderadresse muss eine E-Mail sein", () => {
|
||||
expect(validateSmtpEingabe({ ...EINGABE, absenderAdresse: "kein mail" }, false)).toEqual({
|
||||
expect(validateSmtpEingabe({ ...EINGABE, absenderAdresse: "kein mail" }, null)).toEqual({
|
||||
valid: false,
|
||||
error: "Bitte eine gültige Absenderadresse angeben.",
|
||||
});
|
||||
});
|
||||
it("Benutzer ohne neues und ohne gespeichertes Passwort scheitert", () => {
|
||||
expect(validateSmtpEingabe({ ...EINGABE, passwort: "" }, false)).toEqual({ valid: false, error: "Bitte ein Passwort eingeben." });
|
||||
expect(validateSmtpEingabe({ ...EINGABE, passwort: "" }, null)).toEqual({ valid: false, error: "Bitte ein Passwort eingeben." });
|
||||
});
|
||||
it("leeres Passwort mit gespeichertem Passwort bleibt unveraendert (null)", () => {
|
||||
const e = validateSmtpEingabe({ ...EINGABE, passwort: "" }, true);
|
||||
it("gleiches Ziel + leeres Passwort: gespeichertes Passwort bleibt", () => {
|
||||
const e = validateSmtpEingabe({ ...EINGABE, passwort: "" }, BESTEHEND);
|
||||
expect(e.valid && e.werte.neuesPasswort).toBeNull();
|
||||
expect(e.valid && e.werte.passwortBehalten).toBe(true);
|
||||
});
|
||||
it("geaenderter Host + leeres Passwort scheitert", () => {
|
||||
expect(validateSmtpEingabe({ ...EINGABE, host: "evil.example.de", passwort: "" }, BESTEHEND)).toEqual({
|
||||
valid: false,
|
||||
error: "Bitte ein Passwort eingeben.",
|
||||
});
|
||||
});
|
||||
it("geaenderter Port + leeres Passwort scheitert", () => {
|
||||
expect(validateSmtpEingabe({ ...EINGABE, port: "465", passwort: "" }, BESTEHEND).valid).toBe(false);
|
||||
});
|
||||
it("geaenderter Benutzer + leeres Passwort scheitert", () => {
|
||||
expect(validateSmtpEingabe({ ...EINGABE, benutzer: "anderer", passwort: "" }, BESTEHEND).valid).toBe(false);
|
||||
});
|
||||
it("geaenderter Host mit neuem Passwort ist gueltig", () => {
|
||||
const e = validateSmtpEingabe({ ...EINGABE, host: "neu.example.de", passwort: "neu" }, BESTEHEND);
|
||||
expect(e.valid && e.werte.neuesPasswort).toBe("neu");
|
||||
expect(e.valid && e.werte.passwortBehalten).toBe(false);
|
||||
});
|
||||
it("bestehende Zeile ohne gespeichertes Passwort zaehlt nicht", () => {
|
||||
expect(
|
||||
validateSmtpEingabe({ ...EINGABE, passwort: "" }, { ...BESTEHEND, passwort_verschluesselt: null }).valid
|
||||
).toBe(false);
|
||||
});
|
||||
it("ohne Benutzer wird das Passwort ignoriert", () => {
|
||||
const e = validateSmtpEingabe({ ...EINGABE, benutzer: "", passwort: "x" }, false);
|
||||
const e = validateSmtpEingabe({ ...EINGABE, benutzer: "", passwort: "x" }, null);
|
||||
expect(e.valid && e.werte.benutzer).toBeNull();
|
||||
expect(e.valid && e.werte.neuesPasswort).toBeNull();
|
||||
});
|
||||
|
|
@ -63,6 +107,8 @@ describe("firmenSmtpZuConfig / ermittleEmailConfig", () => {
|
|||
const alt = process.env.SMTP_VERSCHLUESSELUNG_KEY;
|
||||
beforeEach(() => {
|
||||
process.env.SMTP_VERSCHLUESSELUNG_KEY = KEY;
|
||||
(lookup as unknown as ReturnType<typeof vi.fn>).mockReset();
|
||||
(lookup as unknown as ReturnType<typeof vi.fn>).mockResolvedValue([{ address: "93.184.216.34", family: 4 }]);
|
||||
});
|
||||
afterEach(() => {
|
||||
if (alt === undefined) {
|
||||
|
|
@ -100,9 +146,21 @@ describe("firmenSmtpZuConfig / ermittleEmailConfig", () => {
|
|||
});
|
||||
});
|
||||
|
||||
it("nimmt den Firmen-SMTP vor dem Fallback", async () => {
|
||||
it("nimmt den Firmen-SMTP vor dem Fallback und pinnt die geprueft aufgeloeste IP", async () => {
|
||||
const fallback = () => ({ host: "global", port: 25, secure: false, from: "g@x.de", auth: null });
|
||||
expect((await ermittleEmailConfig(fakeAdmin(zeile()), "f2", fallback))?.host).toBe("smtp.firma.de");
|
||||
const cfg = await ermittleEmailConfig(fakeAdmin(zeile()), "f2", fallback);
|
||||
expect(cfg?.host).toBe("93.184.216.34");
|
||||
expect(cfg?.servername).toBe("smtp.firma.de");
|
||||
});
|
||||
|
||||
it("wirft SmtpZielFehler, wenn der Firmen-SMTP privat aufloest", async () => {
|
||||
(lookup as unknown as ReturnType<typeof vi.fn>).mockResolvedValue([{ address: "10.0.0.5", family: 4 }]);
|
||||
await expect(ermittleEmailConfig(fakeAdmin(zeile()), "f2", () => null)).rejects.toBeInstanceOf(SmtpZielFehler);
|
||||
});
|
||||
|
||||
it("prueft den globalen Fallback nicht", async () => {
|
||||
const fallback = () => ({ host: "127.0.0.1", port: 1025, secure: false, from: "g@x.de", auth: null });
|
||||
expect((await ermittleEmailConfig(fakeAdmin(null), "f2", fallback))?.host).toBe("127.0.0.1");
|
||||
});
|
||||
|
||||
it("faellt ohne Firmen-SMTP auf den globalen zurueck", async () => {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import type { SupabaseClient } from "@supabase/supabase-js";
|
||||
import { getEmailConfig, type EmailConfig } from "@/lib/notifications/email-config";
|
||||
import { entschluessele } from "./smtp-crypto";
|
||||
import { pruefeSmtpZiel } from "./smtp-ziel";
|
||||
|
||||
export type FirmenSmtpZeile = {
|
||||
firma_id: string;
|
||||
|
|
@ -21,10 +22,29 @@ export type SmtpWerte = {
|
|||
secure: boolean;
|
||||
benutzer: string | null;
|
||||
neuesPasswort: string | null;
|
||||
// true: kein neues Passwort, das gespeicherte bleibt (Ziel und Benutzer unveraendert).
|
||||
passwortBehalten: boolean;
|
||||
absender_adresse: string;
|
||||
absender_name: string | null;
|
||||
};
|
||||
|
||||
export type BestehendSmtp = {
|
||||
host: string;
|
||||
port: number;
|
||||
benutzer: string | null;
|
||||
passwort_verschluesselt: string | null;
|
||||
};
|
||||
|
||||
export const ERLAUBTE_SMTP_PORTS = [25, 465, 587, 2525];
|
||||
export const SMTP_ZIEL_NICHT_ERLAUBT = "SMTP-Ziel nicht erlaubt";
|
||||
|
||||
export class SmtpZielFehler extends Error {
|
||||
constructor() {
|
||||
super(SMTP_ZIEL_NICHT_ERLAUBT);
|
||||
this.name = "SmtpZielFehler";
|
||||
}
|
||||
}
|
||||
|
||||
type Eingabe = {
|
||||
host: unknown;
|
||||
port: unknown;
|
||||
|
|
@ -41,14 +61,14 @@ function text(wert: unknown): string {
|
|||
|
||||
export function validateSmtpEingabe(
|
||||
input: Eingabe,
|
||||
passwortGespeichert: boolean
|
||||
bestehend: BestehendSmtp | null
|
||||
): { valid: true; werte: SmtpWerte } | { valid: false; error: string } {
|
||||
const host = text(input.host);
|
||||
if (!host) return { valid: false, error: "Bitte den SMTP-Server angeben." };
|
||||
|
||||
const port = Number(text(input.port));
|
||||
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
||||
return { valid: false, error: "Bitte einen gültigen Port (1–65535) angeben." };
|
||||
if (!ERLAUBTE_SMTP_PORTS.includes(port)) {
|
||||
return { valid: false, error: "Bitte einen gültigen Port (25, 465, 587 oder 2525) angeben." };
|
||||
}
|
||||
|
||||
const absender = text(input.absenderAdresse);
|
||||
|
|
@ -59,7 +79,12 @@ export function validateSmtpEingabe(
|
|||
const benutzer = text(input.benutzer) || null;
|
||||
// Passwort nicht trimmen: Leerzeichen koennen Teil des Passworts sein.
|
||||
const passwort = typeof input.passwort === "string" && input.passwort !== "" ? input.passwort : null;
|
||||
if (benutzer && !passwort && !passwortGespeichert) {
|
||||
// Das gespeicherte Passwort gilt nur fuer genau dieses Ziel und diesen Benutzer;
|
||||
// sonst koennte es an einen anderen Server geschickt werden.
|
||||
const zielGleich =
|
||||
bestehend !== null && bestehend.host === host && bestehend.port === port && bestehend.benutzer === benutzer;
|
||||
const passwortBehalten = Boolean(benutzer && !passwort && zielGleich && bestehend?.passwort_verschluesselt);
|
||||
if (benutzer && !passwort && !passwortBehalten) {
|
||||
return { valid: false, error: "Bitte ein Passwort eingeben." };
|
||||
}
|
||||
|
||||
|
|
@ -71,6 +96,7 @@ export function validateSmtpEingabe(
|
|||
secure: input.secure,
|
||||
benutzer,
|
||||
neuesPasswort: benutzer ? passwort : null,
|
||||
passwortBehalten,
|
||||
absender_adresse: absender,
|
||||
absender_name: text(input.absenderName) || null,
|
||||
},
|
||||
|
|
@ -100,6 +126,14 @@ export function firmenSmtpZuConfig(zeile: FirmenSmtpZeile): EmailConfig {
|
|||
};
|
||||
}
|
||||
|
||||
// Prueft das Ziel und pinnt die Verbindung auf die geprueft aufgeloeste IP
|
||||
// (verhindert DNS-Rebinding zwischen Pruefung und Verbindungsaufbau).
|
||||
export async function pinneSmtpZiel(cfg: EmailConfig): Promise<EmailConfig> {
|
||||
const ziel = await pruefeSmtpZiel(cfg.host);
|
||||
if (!ziel.ok) throw new SmtpZielFehler();
|
||||
return { ...cfg, host: ziel.ip, servername: cfg.host };
|
||||
}
|
||||
|
||||
// Firmen-SMTP vor globalem Fallback (SMTP_* aus der .env); null = keiner vorhanden.
|
||||
export async function ermittleEmailConfig(
|
||||
admin: SupabaseClient,
|
||||
|
|
@ -107,5 +141,6 @@ export async function ermittleEmailConfig(
|
|||
fallback: () => EmailConfig | null = getEmailConfig
|
||||
): Promise<EmailConfig | null> {
|
||||
const zeile = await ladeFirmenSmtp(admin, firmaId);
|
||||
return zeile ? firmenSmtpZuConfig(zeile) : fallback();
|
||||
// Nur der Firmen-SMTP wird geprueft; der globale Fallback kommt vom Betreiber (.env).
|
||||
return zeile ? pinneSmtpZiel(firmenSmtpZuConfig(zeile)) : fallback();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,9 @@ export type EmailConfig = {
|
|||
secure: boolean;
|
||||
from: string;
|
||||
auth: { user: string; pass: string } | null;
|
||||
// Gesetzt, wenn host auf eine geprueft aufgeloeste IP gepinnt wurde: TLS prueft
|
||||
// das Zertifikat dann gegen den urspruenglichen Namen.
|
||||
servername?: string;
|
||||
};
|
||||
|
||||
// Liest die SMTP-Konfiguration aus den Umgebungsvariablen. null = kein globaler SMTP;
|
||||
|
|
|
|||
|
|
@ -131,6 +131,21 @@ describe("sendeProtokollMail", () => {
|
|||
expect(createTransport.mock.calls[0][0]).toMatchObject({ requireTLS: true, auth: { user: "u", pass: "p" } });
|
||||
});
|
||||
|
||||
it("pinnt auf die IP und prueft das Zertifikat gegen den urspruenglichen Namen", async () => {
|
||||
sendMail.mockResolvedValue({ messageId: "m" });
|
||||
await sendeProtokollMail({ ...cfg, host: "93.184.216.34", servername: "smtp.firma.de" }, mail);
|
||||
expect(createTransport.mock.calls[0][0]).toMatchObject({
|
||||
host: "93.184.216.34",
|
||||
tls: { servername: "smtp.firma.de" },
|
||||
});
|
||||
});
|
||||
|
||||
it("ohne servername keine tls-Option", async () => {
|
||||
sendMail.mockResolvedValue({ messageId: "m" });
|
||||
await sendeProtokollMail(cfg, mail);
|
||||
expect((createTransport.mock.calls[0][0] as { tls?: unknown }).tls).toBeUndefined();
|
||||
});
|
||||
|
||||
it("ohne Auth: kein auth, kein requireTLS", async () => {
|
||||
sendMail.mockResolvedValue({ messageId: "m" });
|
||||
await sendeProtokollMail({ ...cfg, auth: null }, mail);
|
||||
|
|
|
|||
|
|
@ -60,6 +60,7 @@ export async function sendeProtokollMail(cfg: EmailConfig, mail: ProtokollMail):
|
|||
port: cfg.port,
|
||||
secure: cfg.secure,
|
||||
auth: cfg.auth ?? undefined,
|
||||
tls: cfg.servername ? { servername: cfg.servername } : undefined,
|
||||
// Mit Zugangsdaten niemals im Klartext senden: STARTTLS erzwingen, sonst
|
||||
// koennte ein Angreifer STARTTLS entfernen und die Credentials mitlesen.
|
||||
requireTLS: cfg.auth !== null && !cfg.secure,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import { describe, expect, it, vi } from "vitest";
|
||||
import type { SupabaseClient } from "@supabase/supabase-js";
|
||||
import { protokollMailHandler, whatsappHandler } from "./handler";
|
||||
import { SmtpZielFehler } from "@/lib/firma/smtp";
|
||||
import type { OutboxRow } from "./dispatch";
|
||||
import type { EmailConfig } from "./email-config";
|
||||
import type { WhatsAppConfig } from "./config";
|
||||
|
|
@ -144,6 +145,20 @@ describe("whatsappHandler (Empfaenger pro Firma)", () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe("protokollMailHandler mit geblocktem SMTP-Ziel", () => {
|
||||
it("meldet nur den generischen Text und erzeugt kein PDF", async () => {
|
||||
const erzeugePdf = vi.fn();
|
||||
const res = await protokollMailHandler({} as never, {
|
||||
erzeugePdf,
|
||||
ladeConfig: async () => {
|
||||
throw new SmtpZielFehler();
|
||||
},
|
||||
})(row());
|
||||
expect(res).toEqual({ ok: false, fehler: "SMTP-Ziel nicht erlaubt" });
|
||||
expect(erzeugePdf).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("protokollMailHandler ohne SMTP", () => {
|
||||
it("meldet Kein SMTP konfiguriert (Retry, nicht endgueltig) und erzeugt kein PDF", async () => {
|
||||
const erzeugePdf = vi.fn();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import type { SupabaseClient } from "@supabase/supabase-js";
|
||||
import { erzeugeProtokollPdf } from "@/lib/auftraege/protokoll-pdf";
|
||||
import { ermittleEmailConfig } from "@/lib/firma/smtp";
|
||||
import { ermittleEmailConfig, SMTP_ZIEL_NICHT_ERLAUBT, SmtpZielFehler } from "@/lib/firma/smtp";
|
||||
import type { WhatsAppConfig } from "./config";
|
||||
import type { Handler } from "./dispatch";
|
||||
import type { EmailConfig } from "./email-config";
|
||||
|
|
@ -49,6 +49,10 @@ export function protokollMailHandler(
|
|||
try {
|
||||
cfg = await ladeConfig(admin, row.firma_id);
|
||||
} catch (fehler) {
|
||||
// Admins lesen notification_outbox: bei geblocktem Ziel kein Rohtext (kein Orakel).
|
||||
if (fehler instanceof SmtpZielFehler) {
|
||||
return { ok: false, fehler: SMTP_ZIEL_NICHT_ERLAUBT };
|
||||
}
|
||||
return {
|
||||
ok: false,
|
||||
fehler: `SMTP-Konfiguration fehlerhaft: ${fehler instanceof Error ? fehler.message : String(fehler)}`,
|
||||
|
|
|
|||
Loading…
Reference in New Issue