Beautiful Open-Source Form Builder
Enhance spam detection logic in CheckSpamFormPrompt and FormSpamService
Julien Nahum6c103de6b5de
diff --git a/api/app/Service/AI/Prompts/Form/CheckSpamFormPrompt.php b/api/app/Service/AI/Prompts/Form/CheckSpamFormPrompt.phpindex 9a8c5227f..b5b65320e 100644--- a/api/app/Service/AI/Prompts/Form/CheckSpamFormPrompt.php+++ b/api/app/Service/AI/Prompts/Form/CheckSpamFormPrompt.php@@ -13,8 +13,7 @@ class CheckSpamFormPrompt extends Promptprotected string $model = 'gpt-4.1-mini';public const PROMPT_TEMPLATE = <<<'EOD'- You are an AI assistant specialized in detecting spam, phishing, and fraudulent content in online forms.- Analyze the following form content and determine if it violates our policies.+ Analyze this form for spam/phishing. Be CONSERVATIVE - only block obvious threats.<formContent>{formContent}@@ -23,46 +22,31 @@ class CheckSpamFormPrompt extends Prompt<userInformation>User registered: {userRegisteredSince} days ago.User is subscribed: {isSubscribed}- Total forms created by user: {totalFormsCreated}+ Total forms created: {totalFormsCreated}</userInformation>- CRITICAL ANALYSIS GUIDELINES:-- **IMMEDIATE SPAM/PHISHING (Always Block):**- - ANY login forms claiming to be from major services (Gmail, Outlook, Yahoo, PayPal, Facebook, Microsoft, Google, Apple, etc.)- - Forms explicitly impersonating company authentication flows- - Any form asking for existing passwords or login credentials to "verify" accounts- - Forms with company branding/logos claiming to be official login pages-- **SUSPICIOUS PATTERNS (High Scrutiny):**- - New users (<7 days) creating forms with sensitive data collection- - Forms asking for passwords in combination with brand names- - Short forms (1-3 fields) requesting only sensitive information- - Forms collecting credit card, SSN, or banking details without clear business purpose-- **LEGITIMATE CASES (Generally Allowed):**- - Account creation/registration forms for legitimate services (password setup is normal)- - Employee onboarding forms requiring password creation- - Survey forms asking about security practices (research purposes)- - Event codes, conference codes, referral codes, discount codes- - Customer support forms for account assistance- - Secret codes for contests or events (NOT for account access)-- **KEY DECISION FACTORS:**- 1. **Login vs Registration**: Login forms are almost always phishing. Registration/signup forms mayb be legitimate.- 2. **Brand Impersonation**: Any clear impersonation of major companies is prohibited- 3. **User Trust**: Subscribed users and users with multiple forms get more benefit of doubt- 4. **Form Context**: Consider total fields, form purpose, and data types being collected-- **PASSWORD/SECRET GUIDELINES:**- - Password fields are acceptable for NEW account creation- - Password fields are NOT acceptable for login to existing accounts as it's not a login form, just a data collection form- - "Secret codes" are acceptable for events/contests but NOT for account recovery- - Never allow forms asking for existing passwords/credentials/PIN-- Based on this analysis, classify the form as spam or legitimate.-- Respond with a valid JSON object with your analysis.+ **BLOCK IMMEDIATELY (is_spam: true):**+ - Login forms impersonating major services (Gmail, PayPal, Facebook, Outlook, etc.)+ - Forms asking for existing passwords/credentials to "verify" accounts or for fake login+ - Clear brand impersonation with sensitive data collection++ **FLAG FOR ADMIN REVIEW (needs_admin_review: true):**+ - New users with vague titles but no clear phishing intent+ - Basic info collection (email/phone) without passwords+ - Borderline suspicious but possibly legitimate testing++ **ALLOW (both false):**+ - New account creation forms (password setup normal)+ - Surveys, event codes, referral forms+ - General contact/support forms++ **KEY RULES:**+ - Login forms = phishing, Registration forms = often legitimate+ - Subscribed users get benefit of doubt+ - When uncertain, prefer admin review over blocking++ Classify as: SPAM (block), ADMIN REVIEW (flag), or LEGITIMATE (allow).+ Respond with JSON: {"is_spam": boolean, "needs_admin_review": boolean, "reason": "explanation"}EOD;protected ?array $jsonSchema = [@@ -74,6 +58,10 @@ class CheckSpamFormPrompt extends Prompt'type' => 'boolean','description' => 'True if the form is considered spam/scam, false otherwise.'],+ 'needs_admin_review' => [+ 'type' => 'boolean',+ 'description' => 'True if the form is borderline suspicious and needs admin review but should not be automatically blocked. Only set when is_spam is false.'+ ],'reason' => ['type' => 'string','description' => 'A brief explanation for your decision.'@@ -166,7 +154,7 @@ class CheckSpamFormPrompt extends Prompt}$totalBlocks = count($history);- $manualUnblocks = collect($history)->filter(fn ($block) => !is_null($block['unblocked_by']))->count();+ $manualUnblocks = collect($history)->filter(fn($block) => !is_null($block['unblocked_by']))->count();$summary = [];$summary[] = "Total times blocked: {$totalBlocks}";diff --git a/api/app/Service/Forms/FormSpamService.php b/api/app/Service/Forms/FormSpamService.phpindex acd85a264..c1f4cdd33 100644--- a/api/app/Service/Forms/FormSpamService.php+++ b/api/app/Service/Forms/FormSpamService.php@@ -25,6 +25,8 @@ class FormSpamServiceif ($result['is_spam']) {$this->userActionService->block($form->creator, $result['reason'] ?? 'Your form was detected as spam', null);+ } elseif ($result['needs_admin_review'] ?? false) {+ $this->logAdminReview($form, $result['reason'] ?? 'Form flagged for admin review');}} catch (\Exception $e) {Log::error('Failed to check form for spam.', [@@ -72,4 +74,26 @@ class FormSpamServicereturn false;}++ private function logAdminReview(Form $form, string $reason): void+ {+ Log::channel('slack_churn')->warning('Form flagged for admin review 🚨', [+ 'form_id' => $form->id,+ 'form_title' => $form->title,+ 'user_id' => $form->creator->id,+ 'user_email' => $form->creator->email,+ 'user_registered_days_ago' => $form->creator->created_at->diffInDays(now()),+ 'is_subscribed' => $form->creator->is_subscribed,+ 'total_forms' => $form->creator->forms()->count(),+ 'reason' => $reason,+ 'form_url' => config('app.client_url') . '/forms/' . $form->slug,+ ]);++ Log::info('Form flagged for admin review', [+ 'form_id' => $form->id,+ 'form_title' => $form->title,+ 'user_id' => $form->creator->id,+ 'reason' => $reason,+ ]);+ }}