fix(andreknie.de): harden personal data lifecycle

This commit is contained in:
2026-07-28 11:13:27 +02:00
parent 772a63c622
commit b31da6175a
11 changed files with 276 additions and 124 deletions
+13 -4
View File
@@ -8,6 +8,7 @@ import talkRequestRouter from './routes/talk-request.js'
import newsletterRouter from './routes/newsletter.js'
import resourceDownloadRouter from './routes/resource-download.js'
import speakerCvRouter from './routes/speaker-cv.js'
import { isMailerReady } from './services/mailer.js'
const __dirname = dirname(fileURLToPath(import.meta.url))
const PORT = process.env.PORT || 3003
@@ -30,7 +31,11 @@ app.use(express.json({ limit: '10kb' }))
// Routes
app.get('/api/health', (_req, res) => {
res.json({ status: 'ok', time: new Date().toISOString() })
res.json({
status: isMailerReady() ? 'ok' : 'degraded',
mailer: { ready: isMailerReady() },
time: new Date().toISOString(),
})
})
app.use('/api/contact', contactRouter)
@@ -45,6 +50,10 @@ app.use((err, _req, res, _next) => {
res.status(500).json({ error: 'Interner Serverfehler.' })
})
app.listen(PORT, () => {
console.log(`[andreknie.de Backend] Running on port ${PORT}`)
})
if (process.env.NODE_ENV !== 'test') {
app.listen(PORT, () => {
console.log(`[andreknie.de Backend] Running on port ${PORT}`)
})
}
export { app }
@@ -0,0 +1,36 @@
import { beforeAll, afterAll, describe, expect, it } from 'vitest'
let server
let baseUrl
beforeAll(async () => {
const { app } = await import('./index.js')
server = app.listen(0)
await new Promise(resolve => server.once('listening', resolve))
baseUrl = `http://127.0.0.1:${server.address().port}`
})
afterAll(() => server?.close())
describe('backend mail safety', () => {
it('reports degraded health without SMTP configuration', async () => {
const response = await fetch(`${baseUrl}/api/health`)
const body = await response.json()
expect(response.status).toBe(200)
expect(body.status).toBe('degraded')
expect(body.mailer).toEqual({ ready: false })
expect(JSON.stringify(body)).not.toContain('SMTP_PASS')
})
it('does not report success when contact mail cannot be sent', async () => {
const response = await fetch(`${baseUrl}/api/contact`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ name: 'Test', email: 'test@example.invalid', message: 'Synthetic test' }),
})
expect(response.status).toBe(503)
expect((await response.json()).error).toBeTruthy()
})
})
+21 -14
View File
@@ -1,8 +1,8 @@
import { Router } from 'express'
import { contactLimiter } from '../middleware/rateLimiter.js'
import { antiSpam } from '../middleware/antiSpam.js'
import { createToken, verifyToken } from '../services/confirmationToken.js'
import { sendConfirmationEmail, sendStakeholderNotification } from '../services/mailer.js'
import { createToken, reserveToken, completeToken, releaseToken } from '../services/confirmationToken.js'
import { sendConfirmationEmail, sendStakeholderNotification, MailerNotReadyError } from '../services/mailer.js'
const router = Router()
@@ -11,27 +11,34 @@ router.post('/', contactLimiter, antiSpam, async (req, res) => {
const errors = {}
if (!name || name.trim().length === 0) errors.name = 'Name ist erforderlich.'
if (name && name.length > 100) errors.name = 'Max. 100 Zeichen.'
if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) errors.email = 'Gültige E-Mail erforderlich.'
if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) errors.email = 'Gueltige E-Mail erforderlich.'
if (!message || message.trim().length === 0) errors.message = 'Nachricht ist erforderlich.'
if (message && message.length > 2000) errors.message = 'Max. 2000 Zeichen.'
if (Object.keys(errors).length > 0) {
return res.status(400).json({ errors })
}
if (Object.keys(errors).length > 0) return res.status(400).json({ errors })
const token = await createToken('contact', { name, email, message }, 24)
await sendConfirmationEmail(email, 'contact', token)
res.status(201).json({ ok: true, message: 'Bestätigungs-E-Mail gesendet.' })
try {
const token = await createToken('contact', { name, email, message }, 24)
await sendConfirmationEmail(email, 'contact', token)
res.status(201).json({ ok: true, message: 'Bestaetigungs-E-Mail gesendet.' })
} catch (error) {
res.status(error instanceof MailerNotReadyError ? 503 : 502).json({ error: 'Die Anfrage konnte derzeit nicht versendet werden.' })
}
})
router.get('/confirm/:token', async (req, res) => {
const baseUrl = process.env.BASE_URL || ''
const entry = await verifyToken(req.params.token)
if (!entry) {
return res.redirect(baseUrl + '/bestaetigung?status=error')
const entry = await reserveToken(req.params.token)
if (!entry) return res.redirect(baseUrl + '/bestaetigung?status=error')
try {
await sendStakeholderNotification('contact', entry.data)
await completeToken(req.params.token)
res.redirect(baseUrl + '/bestaetigung?status=success')
} catch (error) {
await releaseToken(req.params.token)
res.status(error instanceof MailerNotReadyError ? 503 : 502).redirect(baseUrl + '/bestaetigung?status=error')
}
await sendStakeholderNotification('contact', entry.data)
res.redirect(baseUrl + '/bestaetigung?status=success')
})
export default router
@@ -5,8 +5,8 @@ import { fileURLToPath } from 'url'
import { Mutex } from 'async-mutex'
import { newsletterLimiter } from '../middleware/rateLimiter.js'
import { antiSpam } from '../middleware/antiSpam.js'
import { createToken, verifyToken } from '../services/confirmationToken.js'
import { sendConfirmationEmail } from '../services/mailer.js'
import { createToken, reserveToken, completeToken, releaseToken } from '../services/confirmationToken.js'
import { sendConfirmationEmail, MailerNotReadyError } from '../services/mailer.js'
const __dirname = dirname(fileURLToPath(import.meta.url))
const SUBS_FILE = join(__dirname, '..', 'data', 'subscribers.json')
@@ -27,38 +27,46 @@ const router = Router()
router.post('/', newsletterLimiter, antiSpam, async (req, res) => {
const { email } = req.body
if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
return res.status(400).json({ errors: { email: 'Gültige E-Mail erforderlich.' } })
return res.status(400).json({ errors: { email: 'Gueltige E-Mail erforderlich.' } })
}
// Check for existing subscription
const subs = loadSubscribers()
if (subs.find(s => s.email === email && s.active)) {
return res.status(409).json({ error: 'Diese E-Mail ist bereits abonniert.' })
}
const token = await createToken('newsletter', { email }, 48)
await sendConfirmationEmail(email, 'newsletter', token)
res.status(201).json({ ok: true, message: 'Bestätigungs-E-Mail gesendet.' })
try {
const token = await createToken('newsletter', { email }, 48)
await sendConfirmationEmail(email, 'newsletter', token)
res.status(201).json({ ok: true, message: 'Bestaetigungs-E-Mail gesendet.' })
} catch (error) {
res.status(error instanceof MailerNotReadyError ? 503 : 502).json({ error: 'Die Anfrage konnte derzeit nicht versendet werden.' })
}
})
router.get('/confirm/:token', async (req, res) => {
const baseUrl = process.env.BASE_URL || ''
const entry = await verifyToken(req.params.token)
if (!entry) {
return res.redirect(baseUrl + '/bestaetigung?status=error')
}
const entry = await reserveToken(req.params.token)
if (!entry) return res.redirect(baseUrl + '/bestaetigung?status=error')
await subsMutex.runExclusive(async () => {
const subs = loadSubscribers()
subs.push({
email: entry.data.email,
subscribedAt: new Date().toISOString(),
active: true,
try {
await subsMutex.runExclusive(async () => {
const subs = loadSubscribers()
if (!subs.find(s => s.email === entry.data.email && s.active)) {
subs.push({
email: entry.data.email,
subscribedAt: new Date().toISOString(),
active: true,
})
saveSubscribers(subs)
}
})
saveSubscribers(subs)
})
res.redirect(baseUrl + '/bestaetigung?status=success')
await completeToken(req.params.token)
res.redirect(baseUrl + '/bestaetigung?status=success')
} catch {
await releaseToken(req.params.token)
res.status(502).redirect(baseUrl + '/bestaetigung?status=error')
}
})
export default router
@@ -38,15 +38,22 @@ router.post('/', resourceLimiter, antiSpam, async (req, res) => {
}
const lead = { name, email, message, timestamp: new Date().toISOString() }
await leadsMutex.runExclusive(async () => {
const leads = loadLeads()
leads.push(lead)
saveLeads(leads)
})
try {
const result = await sendSpeakerCv(email, lead, PDF_PATH)
if (result.pdfMissing) {
return res.status(502).json({ error: 'Der Speaker CV ist derzeit nicht verfügbar.' })
}
await sendSpeakerCv(email, lead, PDF_PATH)
await leadsMutex.runExclusive(async () => {
const leads = loadLeads()
leads.push(lead)
saveLeads(leads)
})
res.status(201).json({ ok: true, message: 'Danke! Der Speaker CV ist auf dem Weg in dein Postfach.' })
res.status(201).json({ ok: true, message: 'Danke! Der Speaker CV ist auf dem Weg in dein Postfach.' })
} catch {
res.status(503).json({ error: 'Der Versand ist derzeit nicht verfügbar.' })
}
})
export default router
@@ -1,8 +1,8 @@
import { Router } from 'express'
import { talkRequestLimiter } from '../middleware/rateLimiter.js'
import { antiSpam } from '../middleware/antiSpam.js'
import { createToken, verifyToken } from '../services/confirmationToken.js'
import { sendConfirmationEmail, sendStakeholderNotification } from '../services/mailer.js'
import { createToken, reserveToken, completeToken, releaseToken } from '../services/confirmationToken.js'
import { sendConfirmationEmail, sendStakeholderNotification, MailerNotReadyError } from '../services/mailer.js'
const router = Router()
@@ -10,29 +10,36 @@ router.post('/', talkRequestLimiter, antiSpam, async (req, res) => {
const { name, email, event_name, topic, message } = req.body
const errors = {}
if (!name || name.trim().length === 0) errors.name = 'Name ist erforderlich.'
if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) errors.email = 'Gültige E-Mail erforderlich.'
if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) errors.email = 'Gueltige E-Mail erforderlich.'
if (!event_name || event_name.trim().length === 0) errors.event_name = 'Eventname ist erforderlich.'
if (!topic || topic.trim().length === 0) errors.topic = 'Thema ist erforderlich.'
if (!message || message.trim().length === 0) errors.message = 'Nachricht ist erforderlich.'
if (message && message.length > 2000) errors.message = 'Max. 2000 Zeichen.'
if (Object.keys(errors).length > 0) {
return res.status(400).json({ errors })
}
if (Object.keys(errors).length > 0) return res.status(400).json({ errors })
const token = await createToken('talk-request', { name, email, event_name, topic, message }, 48)
await sendConfirmationEmail(email, 'talk-request', token)
res.status(201).json({ ok: true, message: 'Bestätigungs-E-Mail gesendet.' })
try {
const token = await createToken('talk-request', { name, email, event_name, topic, message }, 48)
await sendConfirmationEmail(email, 'talk-request', token)
res.status(201).json({ ok: true, message: 'Bestaetigungs-E-Mail gesendet.' })
} catch (error) {
res.status(error instanceof MailerNotReadyError ? 503 : 502).json({ error: 'Die Anfrage konnte derzeit nicht versendet werden.' })
}
})
router.get('/confirm/:token', async (req, res) => {
const baseUrl = process.env.BASE_URL || ''
const entry = await verifyToken(req.params.token)
if (!entry) {
return res.redirect(baseUrl + '/bestaetigung?status=error')
const entry = await reserveToken(req.params.token)
if (!entry) return res.redirect(baseUrl + '/bestaetigung?status=error')
try {
await sendStakeholderNotification('talk-request', entry.data)
await completeToken(req.params.token)
res.redirect(baseUrl + '/bestaetigung?status=success')
} catch (error) {
await releaseToken(req.params.token)
res.status(error instanceof MailerNotReadyError ? 503 : 502).redirect(baseUrl + '/bestaetigung?status=error')
}
await sendStakeholderNotification('talk-request', entry.data)
res.redirect(baseUrl + '/bestaetigung?status=success')
})
export default router
@@ -9,7 +9,6 @@ const DATA_DIR = join(__dirname, '..', 'data')
const TOKENS_FILE = join(DATA_DIR, 'pending-tokens.json')
const tokenMutex = new Mutex()
// Ensure data directory exists
mkdirSync(DATA_DIR, { recursive: true })
function loadTokens() {
@@ -22,13 +21,6 @@ function saveTokens(tokens) {
writeFileSync(TOKENS_FILE, JSON.stringify(tokens, null, 2), 'utf-8')
}
/**
* Create a confirmation token.
* @param {string} type - 'contact' | 'talk-request' | 'newsletter'
* @param {object} data - The form data to store
* @param {number} expiresInHours - Token expiration (default: 48h)
* @returns {string} The generated token
*/
export async function createToken(type, data, expiresInHours = 48) {
const token = crypto.randomUUID()
await tokenMutex.runExclusive(async () => {
@@ -39,51 +31,64 @@ export async function createToken(type, data, expiresInHours = 48) {
data,
createdAt: new Date().toISOString(),
expiresAt: new Date(Date.now() + expiresInHours * 60 * 60 * 1000).toISOString(),
confirmed: false,
status: 'pending',
})
saveTokens(tokens)
})
return token
}
/**
* Verify and consume a token.
* @returns {object|null} The stored data if valid, null if expired/invalid
*/
export async function verifyToken(token) {
/** Reserve a valid token for exactly one confirmation flow. */
export async function reserveToken(token) {
return await tokenMutex.runExclusive(async () => {
const tokens = loadTokens()
const idx = tokens.findIndex(t => t.token === token && !t.confirmed)
const idx = tokens.findIndex(t => t.token === token && (t.status || (t.confirmed ? 'completed' : 'pending')) === 'pending')
if (idx === -1) return null
const entry = tokens[idx]
if (new Date(entry.expiresAt) < new Date()) {
// Expired — remove it
if (new Date(entry.expiresAt) <= new Date()) {
tokens.splice(idx, 1)
saveTokens(tokens)
return null
}
// Mark as confirmed
tokens[idx].confirmed = true
tokens[idx].status = 'processing'
saveTokens(tokens)
return entry
})
}
/**
* Clean up expired tokens (run periodically).
*/
/** Remove a successfully processed token and its personal payload. */
export async function completeToken(token) {
await tokenMutex.runExclusive(async () => {
const tokens = loadTokens()
const remaining = tokens.filter(t => t.token !== token)
if (remaining.length !== tokens.length) saveTokens(remaining)
})
}
/** Return a failed confirmation to pending so it can be retried. */
export async function releaseToken(token) {
await tokenMutex.runExclusive(async () => {
const tokens = loadTokens()
const entry = tokens.find(t => t.token === token)
if (!entry) return
entry.status = 'pending'
saveTokens(tokens)
})
}
// Compatibility for code that only needs the reservation behavior.
export const verifyToken = reserveToken
export async function cleanupExpiredTokens() {
await tokenMutex.runExclusive(async () => {
const tokens = loadTokens()
const now = new Date()
const valid = tokens.filter(t => new Date(t.expiresAt) > now || t.confirmed)
if (valid.length !== tokens.length) {
saveTokens(valid)
}
const valid = tokens.filter(t => !t.confirmed && new Date(t.expiresAt) > now)
if (valid.length !== tokens.length) saveTokens(valid)
})
}
// Run cleanup every hour
setInterval(cleanupExpiredTokens, 60 * 60 * 1000)
const cleanupTimer = setInterval(cleanupExpiredTokens, 60 * 60 * 1000)
cleanupTimer.unref?.()
@@ -0,0 +1,65 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { existsSync, readFileSync, unlinkSync } from 'fs'
import { join } from 'path'
import { createToken, reserveToken, completeToken, releaseToken, cleanupExpiredTokens } from './confirmationToken.js'
const tokensFile = join(process.cwd(), 'server', 'data', 'pending-tokens.json')
function removeTokensFile() {
if (existsSync(tokensFile)) unlinkSync(tokensFile)
}
beforeEach(removeTokensFile)
afterEach(removeTokensFile)
describe('confirmation token lifecycle', () => {
it('creates a pending token and reserves it once', async () => {
const token = await createToken('contact', { name: 'Test', email: 'test@example.invalid' }, 1)
const first = await reserveToken(token)
const second = await reserveToken(token)
expect(first.status).toBe('processing')
expect(second).toBeNull()
expect(JSON.parse(readFileSync(tokensFile, 'utf8')).find(entry => entry.token === token).status).toBe('processing')
})
it('removes an expired token instead of returning it', async () => {
const token = await createToken('contact', { email: 'expired@example.invalid' }, -1)
expect(await reserveToken(token)).toBeNull()
expect(JSON.parse(readFileSync(tokensFile, 'utf8'))).toEqual([])
})
it('removes a successfully processed token and its payload', async () => {
const token = await createToken('contact', { email: 'done@example.invalid' })
await reserveToken(token)
await completeToken(token)
expect(JSON.parse(readFileSync(tokensFile, 'utf8'))).toEqual([])
})
it('releases a failed processing attempt for a controlled retry', async () => {
const token = await createToken('contact', { email: 'retry@example.invalid' })
await reserveToken(token)
await releaseToken(token)
expect((await reserveToken(token)).status).toBe('processing')
})
it('allows exactly one parallel reservation', async () => {
const token = await createToken('contact', { email: 'parallel@example.invalid' })
const results = await Promise.all([reserveToken(token), reserveToken(token)])
expect(results.filter(Boolean)).toHaveLength(1)
})
it('cleans expired pending and processing entries', async () => {
const pending = await createToken('contact', { email: 'pending@example.invalid' }, -1)
const processing = await createToken('contact', { email: 'processing@example.invalid' }, -1)
await reserveToken(processing)
await cleanupExpiredTokens()
expect(pending).toBeTruthy()
expect(JSON.parse(readFileSync(tokensFile, 'utf8'))).toEqual([])
})
})
@@ -9,6 +9,18 @@ const BASE_URL = process.env.BASE_URL || 'https://andreknie.de'
let transporter = null
export class MailerNotReadyError extends Error {
constructor() {
super('Mailer ist nicht konfiguriert.')
this.name = 'MailerNotReadyError'
this.code = 'MAILER_NOT_READY'
}
}
export function isMailerReady() {
return Boolean(SMTP_HOST && SMTP_USER && SMTP_PASS)
}
function getTransporter() {
if (!transporter && SMTP_HOST && SMTP_USER && SMTP_PASS) {
transporter = nodemailer.createTransport({
@@ -23,10 +35,7 @@ function getTransporter() {
export async function sendConfirmationEmail(to, type, token) {
const t = getTransporter()
if (!t) {
console.log(`[Mailer] SMTP not configured. Would send ${type} confirmation to ${to}`)
return
}
if (!t) throw new MailerNotReadyError()
const confirmUrl = `${BASE_URL}/api/${type}/confirm/${token}`
const subjects = {
@@ -45,10 +54,7 @@ export async function sendConfirmationEmail(to, type, token) {
export async function sendStakeholderNotification(type, data) {
const t = getTransporter()
if (!t) {
console.log(`[Mailer] SMTP not configured. Would notify stakeholder about ${type}`)
return
}
if (!t) throw new MailerNotReadyError()
const subjects = {
contact: `Neue Kontaktanfrage von ${data.name}`,
@@ -80,11 +86,7 @@ export async function sendSpeakerCv(to, lead, pdfPath) {
const t = getTransporter()
const pdfMissing = !pdfPath || !existsSync(pdfPath)
if (!t) {
console.log(`[Mailer] SMTP not configured. Would send speaker CV to ${to} (pdfMissing=${pdfMissing})`)
await notifyStakeholderSpeakerCv(lead, pdfMissing)
return { sent: false, pdfMissing }
}
if (!t) throw new MailerNotReadyError()
if (!pdfMissing) {
await t.sendMail({
@@ -112,10 +114,7 @@ async function notifyStakeholderSpeakerCv(lead, pdfMissing) {
: 'CV wurde automatisch an den Anfragenden gesendet.',
].join('\n')
if (!t) {
console.log(`[Mailer] SMTP not configured. Would notify stakeholder about speaker-cv lead:\n${body}`)
return
}
if (!t) throw new MailerNotReadyError()
await t.sendMail({
from: SMTP_USER,
@@ -0,0 +1,12 @@
import { describe, expect, it } from 'vitest'
import { isMailerReady, MailerNotReadyError, sendConfirmationEmail } from './mailer.js'
describe('mailer readiness', () => {
it('reports missing SMTP configuration without exposing credentials', async () => {
expect(isMailerReady()).toBe(false)
await expect(sendConfirmationEmail('test@example.invalid', 'contact', 'synthetic-token'))
.rejects.toBeInstanceOf(MailerNotReadyError)
await expect(sendConfirmationEmail('test@example.invalid', 'contact', 'synthetic-token'))
.rejects.toMatchObject({ code: 'MAILER_NOT_READY' })
})
})