feat(deploy): add umami tracking, fix CSP, and lock JSON writes

This commit is contained in:
2026-07-22 15:23:28 +02:00
parent f3cfacfd11
commit 1ab41bf3ec
6 changed files with 76 additions and 18 deletions
@@ -2,11 +2,13 @@ import { Router } from 'express'
import { readFileSync, writeFileSync, existsSync } from 'fs'
import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
import { Mutex } from 'async-mutex'
import { resourceLimiter } from '../middleware/rateLimiter.js'
import { antiSpam } from '../middleware/antiSpam.js'
const __dirname = dirname(fileURLToPath(import.meta.url))
const LEADS_FILE = join(__dirname, '..', 'data', 'leads.json')
const leadsMutex = new Mutex()
// Only alphanumeric + hyphen resource IDs are allowed. This prevents path
// traversal (e.g. "../../etc/passwd") when building the download URL.
@@ -24,7 +26,7 @@ function saveLeads(leads) {
const router = Router()
router.post('/', resourceLimiter, antiSpam, (req, res) => {
router.post('/', resourceLimiter, antiSpam, async (req, res) => {
const { name, email, company, resource_id } = req.body
const errors = {}
if (!name || name.trim().length === 0) errors.name = 'Name ist erforderlich.'
@@ -38,15 +40,17 @@ router.post('/', resourceLimiter, antiSpam, (req, res) => {
return res.status(400).json({ errors })
}
const leads = loadLeads()
leads.push({
name,
email,
company,
resource_id,
timestamp: new Date().toISOString(),
await leadsMutex.runExclusive(async () => {
const leads = loadLeads()
leads.push({
name,
email,
company,
resource_id,
timestamp: new Date().toISOString(),
})
saveLeads(leads)
})
saveLeads(leads)
// Return download URL (resource files are in public/resources/)
res.json({ ok: true, download_url: `/resources/${resource_id}.pdf` })
@@ -2,6 +2,7 @@ import { Router } from 'express'
import { readFileSync, writeFileSync, existsSync } from 'fs'
import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
import { Mutex } from 'async-mutex'
import { resourceLimiter } from '../middleware/rateLimiter.js'
import { antiSpam } from '../middleware/antiSpam.js'
import { sendSpeakerCv } from '../services/mailer.js'
@@ -9,6 +10,7 @@ import { sendSpeakerCv } from '../services/mailer.js'
const __dirname = dirname(fileURLToPath(import.meta.url))
const LEADS_FILE = join(__dirname, '..', 'data', 'speaker-cv-leads.json')
const PDF_PATH = join(__dirname, '..', '..', 'public', 'resources', 'speaker-cv-andre-knie.pdf')
const leadsMutex = new Mutex()
function loadLeads() {
if (!existsSync(LEADS_FILE)) return []
@@ -36,9 +38,11 @@ router.post('/', resourceLimiter, antiSpam, async (req, res) => {
}
const lead = { name, email, message, timestamp: new Date().toISOString() }
const leads = loadLeads()
leads.push(lead)
saveLeads(leads)
await leadsMutex.runExclusive(async () => {
const leads = loadLeads()
leads.push(lead)
saveLeads(leads)
})
await sendSpeakerCv(email, lead, PDF_PATH)