diff --git a/api/contact.php b/api/contact.php index 3674623..942ac8a 100644 --- a/api/contact.php +++ b/api/contact.php @@ -30,21 +30,35 @@ if (!empty($_POST['website'])) { ]); } +// Delai minimum de remplissage : un humain met plus de quelques secondes a ecrire un message. +// Reponse neutre pour ne pas indiquer au bot qu'il a ete detecte. +$formRenderedAt = (int) ($_SESSION['contact_form_rendered_at'] ?? 0); +if ($formRenderedAt === 0 || (time() - $formRenderedAt) < 4) { + error_log('[Contact] Envoi trop rapide ou formulaire non affiche, message ignore.'); + Security::jsonResponse([ + 'success' => true, + 'message' => 'Message envoye avec succes.', + ]); +} + // Google reCAPTCHA v3 (si configure) : filtre les bots via un score anti-spam. if (RECAPTCHA_ENABLED) { + $requestHost = preg_replace('/:\d+$/', '', (string) ($_SERVER['HTTP_HOST'] ?? '')) ?? ''; + $recaptchaOk = Security::verifyRecaptcha( (string) ($_POST['recaptcha_token'] ?? ''), RECAPTCHA_SECRET_KEY, Security::clientIp(), RECAPTCHA_MIN_SCORE, - 'contact' + 'contact', + $requestHost ); if (!$recaptchaOk) { http_response_code(422); Security::jsonResponse([ 'success' => false, - 'message' => 'Verification anti-robot echouee. Merci de reessayer.', + 'message' => 'Verification anti-robot echouee. Reessayez, ou ecrivez-moi directement a contact@tony-averty.dev.', ]); } } @@ -124,12 +138,39 @@ if (mb_strlen($message) < 10 || mb_strlen($message) > 5000) { ]); } -// Filtre anti-spam : les bots injectent souvent plusieurs liens ou du BBCode. +// Filtre anti-spam : les bots injectent souvent plusieurs liens, du BBCode, ou ecrivent +// dans des alphabets sans rapport avec une clientele francophone. $haystack = mb_strtolower($name . ' ' . $subject . ' ' . $message, 'UTF-8'); $linkCount = preg_match_all('~https?://|www\.|\[url|\[link|?a\b~i', $haystack); +$hasLinkInName = (bool) preg_match('~https?://|www\.|\.(com|net|ru|xyz|top|online)\b~i', $name); +$hasForeignScript = (bool) preg_match('/[\p{Cyrillic}\p{Han}\p{Hiragana}\p{Katakana}\p{Hangul}\p{Thai}]/u', $haystack); -if ($linkCount >= 4 || preg_match('~\[url|\[link|\[/url\]|xn--~i', $haystack)) { +// Signature des campagnes de spam observees : liens vers des hebergeurs de pages jetables +// ou raccourcisseurs, gros blocs de lignes vides, identifiant aleatoire en fin de message +// ("REFERENCE: a3aw0j4e8v5j0m3k..."), promesses de gains en dollars ou crypto. +$hasSpamHost = (bool) preg_match('~\b(telegra\.ph|t\.me|bit\.ly|tinyurl\.com|cutt\.ly|is\.gd|rebrand\.ly|shorturl\.at|goo\.su|clck\.ru)\b~i', $haystack); +$hasBlankPadding = (bool) preg_match('/(?:\r?\n[ \t]*){6,}/', $message); +$hasRandomToken = (bool) preg_match('/\b(?=[a-z0-9]*\d)(?=[a-z0-9]*[a-z])[a-z0-9]{40,}\b/i', $haystack); +$hasMoneyBait = (bool) preg_match('~\$\s?\d[\d,.\s]*(?:per|a|/)\s?day|\$>\$>|\$\s?\d{1,3}(?:,\d{3}){2,}|\b(?:crypto(?:currency)?|bitcoin|btc|usdt|forex|casino|airdrop|jackpot|lottery|hackathon prizes?)\b~i', $haystack); +// Campagne "Robertrow" : sujet anglais genere par gabarit ("Hello, writing about your price") +// avec des blocs d'espaces, message d'une ligne dans une langue tiree au hasard. +$hasTemplateGaps = (bool) preg_match('/\S {3,}\S/', $subject . "\n" . $name); +$hasPriceInquiryBot = (bool) preg_match('~\b(?:write|wrote|writing)\b.{0,30}\bprices?\b~i', $subject); + +if ( + $linkCount >= 3 + || $hasLinkInName + || $hasForeignScript + || $hasSpamHost + || $hasBlankPadding + || $hasRandomToken + || $hasMoneyBait + || $hasTemplateGaps + || $hasPriceInquiryBot + || preg_match('~\[url|\[link|\[/url\]|xn--~i', $haystack) +) { // On enregistre la tentative (rate limit) mais on ne notifie pas : reponse neutre. + error_log('[Contact] Message ignore par le filtre anti-spam (sujet : ' . mb_substr($subject, 0, 80) . ')'); Security::recordAttempt($db, 'contact', $ip); Security::jsonResponse([ 'success' => true, @@ -177,12 +218,14 @@ try { $cleanEmail ); + // L'accuse de reception ne reprend aucun texte saisi par l'expediteur : sinon un bot + // peut mettre l'adresse d'une victime et utiliser ce formulaire pour lui envoyer du spam. $confirmMailer = new Mailer(); $confirmMailer->sendHtml( $cleanEmail, 'Votre message a bien ete recu - Tony AVERTY', - buildClientConfirmationHtml($name, $subject, $message), - buildClientConfirmationText($name, $subject, $message), + buildClientConfirmationHtml(), + buildClientConfirmationText(), SMTP_FROM_EMAIL ); @@ -270,12 +313,8 @@ function buildAdminNotificationText(string $name, string $email, string $subject return implode("\n", $lines); } -function buildClientConfirmationHtml(string $name, string $subject, string $message): string +function buildClientConfirmationHtml(): string { - $safeName = Security::e($name); - $safeSubject = Security::e($subject !== '' ? $subject : 'votre message'); - $safeMessage = nl2br(Security::e($message)); - return << @@ -293,12 +332,8 @@ function buildClientConfirmationHtml(string $name, string $subject, string $mess
Bonjour {$safeName},
+Bonjour,
Merci pour votre message. Il a bien ete recu et je vous repondrai sous 24 heures.
-Sujet : {$safeSubject}
-Si vous n'etes pas a l'origine de cette demande, ignorez simplement cet email.