From 88693282f824806089c246a7faa2617e53467b56 Mon Sep 17 00:00:00 2001 From: Jonny Date: Mon, 12 Oct 2026 10:02:38 +0200 Subject: [PATCH] feat: Farb-Kontrastpruefung und SMTP-Passwort-Verschluesselung Co-Authored-By: Claude Opus 5.5 --- web/lib/firma/farben.test.ts | 47 +++++++++++++++++++++++++++++++ web/lib/firma/farben.ts | 33 ++++++++++++++++++++++ web/lib/firma/smtp-crypto.test.ts | 46 ++++++++++++++++++++++++++++++ web/lib/firma/smtp-crypto.ts | 36 +++++++++++++++++++++++ 4 files changed, 162 insertions(+) create mode 100644 web/lib/firma/farben.test.ts create mode 100644 web/lib/firma/farben.ts create mode 100644 web/lib/firma/smtp-crypto.test.ts create mode 100644 web/lib/firma/smtp-crypto.ts diff --git a/web/lib/firma/farben.test.ts b/web/lib/firma/farben.test.ts new file mode 100644 index 0000000..f80ad95 --- /dev/null +++ b/web/lib/firma/farben.test.ts @@ -0,0 +1,47 @@ +import { describe, expect, it } from "vitest"; +import { + MIN_KONTRAST, + STANDARD_FARBE_DUNKEL, + STANDARD_FARBE_PRIMAER, + kontrastZuWeiss, + normalisiereFarbe, + themeVariablen, +} from "./farben"; + +describe("normalisiereFarbe", () => { + it("akzeptiert #rrggbb und schreibt klein", () => { + expect(normalisiereFarbe(" #0A185C ")).toBe("#0a185c"); + }); + it("lehnt andere Formate ab", () => { + expect(normalisiereFarbe("#abc")).toBeNull(); + expect(normalisiereFarbe("0071ed")).toBeNull(); + expect(normalisiereFarbe("#gggggg")).toBeNull(); + expect(normalisiereFarbe("")).toBeNull(); + }); +}); + +describe("kontrastZuWeiss", () => { + it("liefert 21 fuer Schwarz und 1 fuer Weiss", () => { + expect(kontrastZuWeiss("#000000")).toBeCloseTo(21, 1); + expect(kontrastZuWeiss("#ffffff")).toBeCloseTo(1, 5); + }); + it("Standardfarben bestehen den Mindestkontrast", () => { + expect(kontrastZuWeiss(STANDARD_FARBE_PRIMAER)).toBeGreaterThanOrEqual(MIN_KONTRAST); + expect(kontrastZuWeiss(STANDARD_FARBE_DUNKEL)).toBeGreaterThanOrEqual(MIN_KONTRAST); + }); + it("Gelb faellt durch", () => { + expect(kontrastZuWeiss("#ffff00")).toBeLessThan(MIN_KONTRAST); + }); +}); + +describe("themeVariablen", () => { + it("setzt nur vorhandene Farben", () => { + expect(themeVariablen({ farbe_primaer: "#112233", farbe_dunkel: null })).toEqual({ + "--color-performance-blue": "#112233", + }); + expect(themeVariablen({ farbe_primaer: null, farbe_dunkel: "#000000" })).toEqual({ + "--color-night-blue": "#000000", + }); + expect(themeVariablen({ farbe_primaer: null, farbe_dunkel: null })).toEqual({}); + }); +}); diff --git a/web/lib/firma/farben.ts b/web/lib/firma/farben.ts new file mode 100644 index 0000000..772e4f5 --- /dev/null +++ b/web/lib/firma/farben.ts @@ -0,0 +1,33 @@ +export const STANDARD_FARBE_PRIMAER = "#0071ed"; +export const STANDARD_FARBE_DUNKEL = "#0a185c"; +// WCAG AA fuer normalen Text: Buttons tragen weisse Schrift auf der Hauptfarbe. +export const MIN_KONTRAST = 4.5; + +const HEX_REGEX = /^#[0-9a-f]{6}$/; + +export function normalisiereFarbe(eingabe: string): string | null { + const wert = eingabe.trim().toLowerCase(); + return HEX_REGEX.test(wert) ? wert : null; +} + +function kanal(wert: number): number { + const c = wert / 255; + return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4; +} + +// Kontrastverhaeltnis nach WCAG 2.x zwischen der Farbe und Weiss. +export function kontrastZuWeiss(hex: string): number { + const r = parseInt(hex.slice(1, 3), 16); + const g = parseInt(hex.slice(3, 5), 16); + const b = parseInt(hex.slice(5, 7), 16); + const luminanz = 0.2126 * kanal(r) + 0.7152 * kanal(g) + 0.0722 * kanal(b); + return 1.05 / (luminanz + 0.05); +} + +// CSS-Variablen aus globals.css (@theme), die das Layout am ueberschreibt. +export function themeVariablen(f: { farbe_primaer: string | null; farbe_dunkel: string | null }): Record { + const variablen: Record = {}; + if (f.farbe_primaer) variablen["--color-performance-blue"] = f.farbe_primaer; + if (f.farbe_dunkel) variablen["--color-night-blue"] = f.farbe_dunkel; + return variablen; +} diff --git a/web/lib/firma/smtp-crypto.test.ts b/web/lib/firma/smtp-crypto.test.ts new file mode 100644 index 0000000..0c42459 --- /dev/null +++ b/web/lib/firma/smtp-crypto.test.ts @@ -0,0 +1,46 @@ +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { entschluessele, verschluessele } from "./smtp-crypto"; + +const KEY = Buffer.alloc(32, 7).toString("base64"); + +describe("smtp-crypto", () => { + const alt = process.env.SMTP_VERSCHLUESSELUNG_KEY; + beforeEach(() => { + process.env.SMTP_VERSCHLUESSELUNG_KEY = KEY; + }); + afterEach(() => { + process.env.SMTP_VERSCHLUESSELUNG_KEY = alt; + }); + + it("Roundtrip liefert den Klartext", () => { + const wert = verschluessele("geheim-äöü"); + expect(wert.startsWith("v1:")).toBe(true); + expect(wert).not.toContain("geheim"); + expect(entschluessele(wert)).toBe("geheim-äöü"); + }); + + it("zwei Verschluesselungen desselben Texts unterscheiden sich (zufaelliger IV)", () => { + expect(verschluessele("x")).not.toBe(verschluessele("x")); + }); + + it("manipulierter Ciphertext scheitert", () => { + const [v, iv, tag, ct] = verschluessele("geheim").split(":"); + const kaputt = Buffer.from(ct, "base64"); + kaputt[0] ^= 1; + expect(() => entschluessele([v, iv, tag, kaputt.toString("base64")].join(":"))).toThrow(); + }); + + it("unbekanntes Format scheitert", () => { + expect(() => entschluessele("klartext")).toThrow("Unbekanntes Format"); + }); + + it("fehlender Schluessel scheitert mit klarer Meldung", () => { + delete process.env.SMTP_VERSCHLUESSELUNG_KEY; + expect(() => verschluessele("x")).toThrow("SMTP_VERSCHLUESSELUNG_KEY ist nicht gesetzt"); + }); + + it("Schluessel falscher Laenge scheitert", () => { + process.env.SMTP_VERSCHLUESSELUNG_KEY = Buffer.alloc(16).toString("base64"); + expect(() => verschluessele("x")).toThrow("32 Byte"); + }); +}); diff --git a/web/lib/firma/smtp-crypto.ts b/web/lib/firma/smtp-crypto.ts new file mode 100644 index 0000000..6c7276c --- /dev/null +++ b/web/lib/firma/smtp-crypto.ts @@ -0,0 +1,36 @@ +import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto"; + +const ALGORITHMUS = "aes-256-gcm"; + +function schluessel(): Buffer { + const roh = process.env.SMTP_VERSCHLUESSELUNG_KEY; + if (!roh) { + throw new Error("SMTP_VERSCHLUESSELUNG_KEY ist nicht gesetzt"); + } + const key = Buffer.from(roh, "base64"); + if (key.length !== 32) { + throw new Error("SMTP_VERSCHLUESSELUNG_KEY muss 32 Byte (base64) lang sein"); + } + return key; +} + +// Format v1:::, alles base64. Die Version erlaubt einen +// spaeteren Schluesselwechsel ohne Raten. +export function verschluessele(klartext: string): string { + const iv = randomBytes(12); + const cipher = createCipheriv(ALGORITHMUS, schluessel(), iv); + const ciphertext = Buffer.concat([cipher.update(klartext, "utf8"), cipher.final()]); + const tag = cipher.getAuthTag(); + return ["v1", iv.toString("base64"), tag.toString("base64"), ciphertext.toString("base64")].join(":"); +} + +export function entschluessele(wert: string): string { + const teile = wert.split(":"); + if (teile.length !== 4 || teile[0] !== "v1") { + throw new Error("Unbekanntes Format des verschluesselten SMTP-Passworts"); + } + const [, iv, tag, ciphertext] = teile; + const decipher = createDecipheriv(ALGORITHMUS, schluessel(), Buffer.from(iv, "base64")); + decipher.setAuthTag(Buffer.from(tag, "base64")); + return Buffer.concat([decipher.update(Buffer.from(ciphertext, "base64")), decipher.final()]).toString("utf8"); +}