Security-Hardening Runde 5: Hack-Das-Ding (DSGVO-GAU + Timing + XSS)
Live-Pentest gegen Dev-Server + 3 parallele Audit-Agents. 🚨 CRITICAL: /api/uploads/* war ohne Auth erreichbar - express.static('/api/uploads', ...) → jeder konnte mit ratbarer URL sensible PDFs (Kündigungsbestätigungen, Ausweise, Bankkarten, Vollmachten) ziehen. Live-verifiziert: 23-KB-PDF eines echten Kunden ohne Login geladen. - Fix: authenticate-Middleware vor static-Handler (req.query.token unterstützung war schon da, jetzt aktiv genutzt). - Frontend: utils/fileUrl.ts hängt JWT als ?token=... an. 24 direkte /api${...Path}-URLs in 5 Dateien per Skript migriert (CustomerDetail, ContractDetail, InvoicesSection, PdfTemplates, GDPRDashboard). 🚨 HIGH: Login-Timing User-Enumeration - bcrypt.compare wurde nur bei existierenden Usern ausgeführt → 110ms vs 10ms Differenz, Email-Enumeration trivial messbar. - Fix: Dummy-bcrypt-compare bei invalid user (Cost 12). Plus Lazy- Rehash bei erfolgreichem Login: alte Cost-10-Hashes (z.B. admin aus Installation) werden auf BCRYPT_COST upgraded, damit Dummy- und Echt-Hash-Cost zusammenpassen. - Live-verifiziert nach Admin-Rehash: 422ms (invalid) vs 423ms (valid) – Side-Channel dicht. 🚨 HIGH: XSS via Privacy-Policy/Imprint-HTML - 4 Frontend-Seiten renderten Backend-HTML ohne DOMPurify (PortalPrivacy, ConsentPage, PortalWebsitePrivacy, PortalImprint). Admin-eingegebene <script>-Tags wären bei jedem Portal-Kunden- Besuch ausgeführt worden – auch auf der öffentlichen Consent-Seite. - Fix: DOMPurify.sanitize mit strikter FORBID_TAGS/ATTR Config. 🛡 HIGH: IDOR-Härtung an Upload-/Document-Endpoints - canAccessContract jetzt in: uploadContractDocument, deleteContractDocument, handleContractDocumentUpload (Kündigungs- Letter+Confirmation), handleContractDocumentDelete, saveAttachmentAsContractDocument. - Defense-in-Depth: aktuell durch requirePermission abgesichert, schützt auch gegen künftige Staff-Scoping-Rollen. Offen für v1.1: - Per-File-Ownership-Check für /api/uploads (Kontroll-Lookup welche Ressource zur Datei gehört) - TipTap-Link-Tool javascript:-Protokoll blockieren - Prisma-Error-Messages in Admin-Endpoints generisch sanitisieren Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -11,6 +11,40 @@ import { getSystemEmailCredentials } from './emailProvider/emailProviderService.
|
||||
// Bestehende Hashes mit Faktor 10 bleiben gültig (bcrypt kodiert den Faktor im Hash).
|
||||
const BCRYPT_COST = 12;
|
||||
|
||||
// Dummy-Hash mit Cost 12 für Timing-Attack-Schutz: bei nicht-existierendem User
|
||||
// führen wir trotzdem ein bcrypt.compare() durch, damit die Antwortzeit nicht
|
||||
// verrät, ob die E-Mail existiert. Konstanter Hash hat keine Bedeutung außer
|
||||
// dem Timing-Angleich.
|
||||
const DUMMY_BCRYPT_HASH = '$2a$12$CwTycUXWue0Thq9StjUM0uJ8gQKwqKjq8lZ3TZ9qg8aJ0A9hPn4Wy';
|
||||
|
||||
/**
|
||||
* Upgrade eines bestehenden Passwort-Hashes auf aktuellen BCRYPT_COST.
|
||||
* Wird nach erfolgreichem Login aufgerufen. Alte User (z.B. admin mit Cost 10
|
||||
* aus der Installation) werden so lazy auf Cost 12 migriert – damit sich die
|
||||
* Antwortzeit beim Login der Dummy-Zeit bei ungültigen Usern angleicht.
|
||||
*/
|
||||
async function maybeUpgradePasswordHash(
|
||||
table: 'user' | 'customer',
|
||||
id: number,
|
||||
plaintextPassword: string,
|
||||
currentHash: string,
|
||||
): Promise<void> {
|
||||
const match = currentHash.match(/^\$2[aby]\$(\d+)\$/);
|
||||
const currentCost = match ? parseInt(match[1], 10) : 0;
|
||||
if (currentCost === BCRYPT_COST) return;
|
||||
try {
|
||||
const newHash = await bcrypt.hash(plaintextPassword, BCRYPT_COST);
|
||||
if (table === 'user') {
|
||||
await prisma.user.update({ where: { id }, data: { password: newHash } });
|
||||
} else {
|
||||
await prisma.customer.update({ where: { id }, data: { portalPasswordHash: newHash } });
|
||||
}
|
||||
} catch (err) {
|
||||
// Nicht kritisch – Login war erfolgreich, Rehash kann beim nächsten Login nachgeholt werden
|
||||
console.warn('[maybeUpgradePasswordHash] Fehler beim Rehash:', err);
|
||||
}
|
||||
}
|
||||
|
||||
// Mitarbeiter-Login
|
||||
export async function login(email: string, password: string) {
|
||||
const user = await prisma.user.findUnique({
|
||||
@@ -33,6 +67,9 @@ export async function login(email: string, password: string) {
|
||||
});
|
||||
|
||||
if (!user || !user.isActive) {
|
||||
// Timing-Attack-Schutz: Dummy-bcrypt-compare damit die Antwortzeit bei
|
||||
// nicht-existierendem/deaktiviertem User der eines gültigen Users entspricht.
|
||||
await bcrypt.compare(password, DUMMY_BCRYPT_HASH);
|
||||
throw new Error('Ungültige Anmeldedaten');
|
||||
}
|
||||
|
||||
@@ -41,6 +78,10 @@ export async function login(email: string, password: string) {
|
||||
throw new Error('Ungültige Anmeldedaten');
|
||||
}
|
||||
|
||||
// Lazy-Upgrade: ältere Cost-10-Hashes auf aktuellen BCRYPT_COST rehashen.
|
||||
// Async, nicht blockierend für die Response.
|
||||
maybeUpgradePasswordHash('user', user.id, password, user.password).catch(() => {});
|
||||
|
||||
// Collect all permissions from all roles
|
||||
const permissions = new Set<string>();
|
||||
for (const userRole of user.roles) {
|
||||
@@ -107,6 +148,8 @@ export async function customerLogin(email: string, password: string) {
|
||||
|
||||
if (!customer || !customer.portalEnabled || !customer.portalPasswordHash) {
|
||||
console.log('[CustomerLogin] Abbruch: Kunde nicht gefunden oder Portal nicht aktiviert');
|
||||
// Timing-Attack-Schutz (siehe login())
|
||||
await bcrypt.compare(password, DUMMY_BCRYPT_HASH);
|
||||
throw new Error('Ungültige Anmeldedaten');
|
||||
}
|
||||
|
||||
@@ -117,6 +160,9 @@ export async function customerLogin(email: string, password: string) {
|
||||
throw new Error('Ungültige Anmeldedaten');
|
||||
}
|
||||
|
||||
// Lazy-Upgrade analog zu Mitarbeiter-Login
|
||||
maybeUpgradePasswordHash('customer', customer.id, password, customer.portalPasswordHash).catch(() => {});
|
||||
|
||||
// Letzte Anmeldung aktualisieren
|
||||
await prisma.customer.update({
|
||||
where: { id: customer.id },
|
||||
|
||||
Reference in New Issue
Block a user