From 7e44e7e132c64168c314f39c388101a3f86f0fda Mon Sep 17 00:00:00 2001 From: Ruben Date: Tue, 4 Nov 2025 22:33:14 +0100 Subject: [PATCH] Add CSRF protection and rate limiting to contact form Improve contact form styling with dedicated CSS file Move contact form styles from base.css to separate file Add security measures to custom directory with .htaccess Update honeypot field styling and implementation --- content/kontakt/01-kontaktskjema.php | 24 +++++-- content/kontakt/styles.css | 97 ++++++++++++++++++++++++++++ custom/.htaccess | 10 +++ custom/styles/base.css | 87 ------------------------- 4 files changed, 126 insertions(+), 92 deletions(-) create mode 100644 content/kontakt/styles.css create mode 100644 custom/.htaccess diff --git a/content/kontakt/01-kontaktskjema.php b/content/kontakt/01-kontaktskjema.php index 48f0fbb..985d8aa 100644 --- a/content/kontakt/01-kontaktskjema.php +++ b/content/kontakt/01-kontaktskjema.php @@ -5,10 +5,25 @@ $formSuccess = false; $formErrors = []; $formData = ['name' => '', 'email' => '', 'message' => '']; +// Start session for CSRF token and rate limiting +if (session_status() === PHP_SESSION_NONE) { + session_start(); +} + +// Generate CSRF token if not exists +if (empty($_SESSION['csrf_token'])) { + $_SESSION['csrf_token'] = bin2hex(random_bytes(32)); +} + // Process form submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['contact_form_submit'])) { $formSubmitted = true; + // Security: CSRF Token Validation + if (!isset($_POST['csrf_token']) || !hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'])) { + $formErrors[] = 'Ugyldig sikkerhetskode. Vennligst prøv igjen.'; + } + // Spam Prevention 1: Honeypot field (should be empty) if (!empty($_POST['website'])) { $formErrors[] = 'Spam detected.'; @@ -32,10 +47,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['contact_form_submit'] } // Spam Prevention 4: Rate limiting (session-based) - if (session_status() === PHP_SESSION_NONE) { - session_start(); - } - $lastSubmitTime = isset($_SESSION['last_contact_submit']) ? $_SESSION['last_contact_submit'] : 0; if (time() - $lastSubmitTime < 60) { $formErrors[] = 'Vennligst vent litt før du sender inn igjen.'; @@ -208,11 +219,14 @@ $currentTime = time();
-