Skip to content

Beautiful Open-Source Form Builder

AI for check form and block user if needed (#822)

Chirag Chhatralada77a2dd0b6e
diff --git a/api/app/Events/Forms/FormSaved.php b/api/app/Events/Forms/FormSaved.php
new file mode 100644
index 000000000..64f23fec3
--- /dev/null
+++ b/api/app/Events/Forms/FormSaved.php
@@ -0,0 +1,17 @@
+<?php
+
+namespace App\Events\Forms;
+
+use App\Models\Forms\Form;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+
+class FormSaved
+{
+ use Dispatchable;
+ use SerializesModels;
+
+ public function __construct(public Form $form)
+ {
+ }
+}
diff --git a/api/app/Jobs/Form/CheckFormForSpam.php b/api/app/Jobs/Form/CheckFormForSpam.php
new file mode 100644
index 000000000..c43a14eb9
--- /dev/null
+++ b/api/app/Jobs/Form/CheckFormForSpam.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace App\Jobs\Form;
+
+use App\Models\Forms\Form;
+use App\Service\Forms\FormSpamService;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+
+class CheckFormForSpam implements ShouldQueue
+{
+ use Dispatchable;
+ use InteractsWithQueue;
+ use Queueable;
+ use SerializesModels;
+
+ public function __construct(public Form $form)
+ {
+ }
+
+ public function handle(FormSpamService $formSpamService): void
+ {
+ $formSpamService->checkForm($this->form);
+ }
+}
diff --git a/api/app/Listeners/Forms/FormSpamCheckListener.php b/api/app/Listeners/Forms/FormSpamCheckListener.php
new file mode 100644
index 000000000..381e034cf
--- /dev/null
+++ b/api/app/Listeners/Forms/FormSpamCheckListener.php
@@ -0,0 +1,15 @@
+<?php
+
+namespace App\Listeners\Forms;
+
+use App\Events\Forms\FormSaved;
+use App\Jobs\Form\CheckFormForSpam;
+
+class FormSpamCheckListener
+{
+ public function handle(FormSaved $event): void
+ {
+ // We can add logic here to not queue for certain users if needed
+ CheckFormForSpam::dispatch($event->form)->onQueue('default');
+ }
+}
diff --git a/api/app/Models/Forms/Form.php b/api/app/Models/Forms/Form.php
index afecd4b20..cb6ec2030 100644
--- a/api/app/Models/Forms/Form.php
+++ b/api/app/Models/Forms/Form.php
@@ -20,6 +20,7 @@ use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Stevebauman\Purify\Facades\Purify;
use Carbon\Carbon;
+use App\Events\Forms\FormSaved;
class Form extends Model implements CachableAttributes
{
@@ -142,6 +143,7 @@ class Form extends Model implements CachableAttributes
*/
protected $dispatchesEvents = [
'created' => FormCreated::class,
+ 'saved' => FormSaved::class,
];
public function getIsProAttribute()
diff --git a/api/app/Models/User.php b/api/app/Models/User.php
index cbbbfe7f3..74c348cf1 100644
--- a/api/app/Models/User.php
+++ b/api/app/Models/User.php
@@ -149,14 +149,14 @@ class User extends Authenticatable implements JWTSubject
return !is_null($this->blocked_at);
}
- public function blockUser(string $reason, int $moderatorId): void
+ public function blockUser(string $reason, ?int $moderatorId): void
{
$this->blocked_at = now();
$history = $this->meta['blocking_history'] ?? [];
$history[] = [
'reason' => $reason,
'blocked_at' => $this->blocked_at,
- 'blocked_by' => $moderatorId,
+ 'blocked_by' => $moderatorId ?? null,
'unblock_reason' => null,
'unblocked_at' => null,
'unblocked_by' => null,
diff --git a/api/app/Providers/EventServiceProvider.php b/api/app/Providers/EventServiceProvider.php
index 6791a7c04..7d0069530 100644
--- a/api/app/Providers/EventServiceProvider.php
+++ b/api/app/Providers/EventServiceProvider.php
@@ -5,6 +5,7 @@ namespace App\Providers;
use App\Events\Billing\SubscriptionCreated;
use App\Events\Billing\SubscriptionUpdated;
use App\Events\Forms\FormSubmitted;
+use App\Events\Forms\FormSaved;
use App\Events\Models\FormCreated;
use App\Events\Models\FormIntegrationCreated;
use App\Events\Models\FormIntegrationsEventCreated;
@@ -14,6 +15,7 @@ use App\Listeners\Forms\FormCreationConfirmation;
use App\Listeners\Forms\FormIntegrationCreatedHandler;
use App\Listeners\Forms\FormIntegrationsEventListener;
use App\Listeners\Forms\NotifyFormSubmission;
+use App\Listeners\Forms\FormSpamCheckListener;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
@@ -32,6 +34,9 @@ class EventServiceProvider extends ServiceProvider
FormCreated::class => [
FormCreationConfirmation::class,
],
+ FormSaved::class => [
+ FormSpamCheckListener::class,
+ ],
FormSubmitted::class => [
NotifyFormSubmission::class
],
diff --git a/api/app/Service/AI/Prompts/Form/CheckSpamFormPrompt.php b/api/app/Service/AI/Prompts/Form/CheckSpamFormPrompt.php
new file mode 100644
index 000000000..a69d4ccc6
--- /dev/null
+++ b/api/app/Service/AI/Prompts/Form/CheckSpamFormPrompt.php
@@ -0,0 +1,125 @@
+<?php
+
+namespace App\Service\AI\Prompts\Form;
+
+use App\Models\Forms\Form;
+use App\Service\AI\Prompts\Prompt;
+use Illuminate\Support\Str;
+
+class CheckSpamFormPrompt extends Prompt
+{
+ protected ?float $temperature = 0.2;
+ protected ?int $maxTokens = 500;
+ protected 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.
+
+ <formContent>
+ {formContent}
+ </formContent>
+
+ <userInformation>
+ User registered: {userRegisteredSince} days ago.
+ User is subscribed: {isSubscribed}
+ Total forms created by user: {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.
+ EOD;
+
+ protected ?array $jsonSchema = [
+ 'type' => 'object',
+ 'required' => ['is_spam', 'reason'],
+ 'additionalProperties' => false,
+ 'properties' => [
+ 'is_spam' => [
+ 'type' => 'boolean',
+ 'description' => 'True if the form is considered spam/scam, false otherwise.'
+ ],
+ 'reason' => [
+ 'type' => 'string',
+ 'description' => 'A brief explanation for your decision.'
+ ]
+ ]
+ ];
+
+ public function __construct(
+ public Form $form
+ ) {
+ parent::__construct();
+ }
+
+ protected function getPromptTemplate(): string
+ {
+ return self::PROMPT_TEMPLATE;
+ }
+
+ protected function buildPrompt(): string
+ {
+ $template = $this->getPromptTemplate();
+ $formContent = $this->extractFormContent();
+ $userRegisteredSince = $this->form->creator->created_at->diffInDays(now());
+ $isSubscribed = $this->form->creator->is_subscribed ? 'yes' : 'no';
+ $totalFormsCreated = $this->form->creator->forms()->count();
+
+ return Str::of($template)
+ ->replace('{formContent}', $formContent)
+ ->replace('{userRegisteredSince}', $userRegisteredSince)
+ ->replace('{isSubscribed}', $isSubscribed)
+ ->replace('{totalFormsCreated}', $totalFormsCreated)
+ ->toString();
+ }
+
+ private function extractFormContent(): string
+ {
+ $content = [];
+ $content[] = "Title: " . $this->form->title;
+ $content[] = "Description: " . $this->form->description;
+
+ foreach ($this->form->properties as $field) {
+ $content[] = "- Field Name: " . ($field['name'] ?? 'N/A');
+ $content[] = " Field Type: " . ($field['type'] ?? 'N/A');
+ $content[] = " Placeholder: " . ($field['placeholder'] ?? 'N/A');
+ }
+
+ return implode("\n", $content);
+ }
+}
diff --git a/api/app/Service/Forms/FormSpamService.php b/api/app/Service/Forms/FormSpamService.php
new file mode 100644
index 000000000..acd85a264
--- /dev/null
+++ b/api/app/Service/Forms/FormSpamService.php
@@ -0,0 +1,75 @@
+<?php
+
+namespace App\Service\Forms;
+
+use App\Models\Forms\Form;
+use App\Models\User;
+use App\Service\AI\Prompts\Form\CheckSpamFormPrompt;
+use App\Service\UserActionService;
+use Illuminate\Support\Facades\Log;
+
+class FormSpamService
+{
+ public function __construct(protected UserActionService $userActionService)
+ {
+ }
+
+ public function checkForm(Form $form): void
+ {
+ if (!config('spam.enabled') || !$this->shouldCheck($form)) {
+ return;
+ }
+
+ try {
+ $result = CheckSpamFormPrompt::run($form);
+
+ if ($result['is_spam']) {
+ $this->userActionService->block($form->creator, $result['reason'] ?? 'Your form was detected as spam', null);
+ }
+ } catch (\Exception $e) {
+ Log::error('Failed to check form for spam.', [
+ 'form_id' => $form->id,
+ 'exception' => $e->getMessage(),
+ ]);
+ }
+ }
+
+ private function shouldCheck(Form $form): bool
+ {
+ if ($form->creator->is_blocked || $form->creator->admin || $form->creator->moderator) {
+ return false;
+ }
+
+ if ($this->containsKeywords($form)) {
+ return true;
+ }
+
+ if ($this->isRiskyUser($form->creator)) {
+ return true;
+ }
+
+ // Random check for other users
+ return (rand(1, 100) <= config('spam.random_check_percentage', 0));
+ }
+
+ private function isRiskyUser(User $user): bool
+ {
+ // Example: Free user registered in the last N days
+ return !$user->is_subscribed && $user->created_at->diffInDays(now()) <= config('spam.risky_user_days', 7);
+ }
+
+ private function containsKeywords(Form $form): bool
+ {
+ $content = json_encode($form->fields) . ' ' . $form->title . ' ' . $form->description;
+ $content = strtolower($content);
+ $keywords = config('spam.keywords', []);
+
+ foreach ($keywords as $keyword) {
+ if (str_contains($content, strtolower($keyword))) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+}
diff --git a/api/app/Service/UserActionService.php b/api/app/Service/UserActionService.php
index fcb0e0fa6..9e158d524 100644
--- a/api/app/Service/UserActionService.php
+++ b/api/app/Service/UserActionService.php
@@ -11,7 +11,7 @@ use Illuminate\Support\Facades\Log;
class UserActionService
{
- public function block(User $user, string $reason, int $moderatorId): User
+ public function block(User $user, string $reason, ?int $moderatorId): User
{
$user->blockUser($reason, $moderatorId);
diff --git a/api/config/spam.php b/api/config/spam.php
new file mode 100644
index 000000000..688605936
--- /dev/null
+++ b/api/config/spam.php
@@ -0,0 +1,114 @@
+<?php
+
+return [
+ /*
+ |--------------------------------------------------------------------------
+ | Spam Detection Settings
+ |--------------------------------------------------------------------------
+ |
+ | This file contains settings for the spam detection feature.
+ |
+ */
+
+ // Enable or disable the entire feature
+ 'enabled' => env('SPAM_DETECTION_ENABLED', true),
+
+ // Definition of a "risky" user in days since registration
+ 'risky_user_days' => 7,
+
+ // Percentage of non-risky users to check randomly
+ 'random_check_percentage' => 10,
+
+ // Keywords that trigger a mandatory spam check
+ 'keywords' => [
+ "password",
+ "passphrase",
+ "secret question",
+ "security question",
+ "mother's maiden name",
+ "pin",
+ "2fa code",
+ "mfa code",
+ "authentication code",
+ "backup code",
+ "recovery code",
+ "credit card number",
+ "card number",
+ "cvv",
+ "cvc",
+ "csc",
+ "card security code",
+ "expiration date",
+ "expiry date",
+ "bank account number",
+ "routing number",
+ "iban",
+ "swift code",
+ "online banking password",
+ "social security number",
+ "ssn",
+ "tax id",
+ "username",
+ "userid",
+ "user id",
+ "login",
+ "signin",
+ "sign in",
+ "account number",
+ "member id",
+ "customer id",
+ "email address",
+ "phone number",
+ "google",
+ "microsoft",
+ "apple",
+ "amazon",
+ "facebook",
+ "meta",
+ "instagram",
+ "netflix",
+ "paypal",
+ "stripe",
+ "dropbox",
+ "linkedin",
+ "yahoo",
+ "adobe",
+ "office 365",
+ "icloud",
+ "outlook",
+ "gmail",
+ "visa",
+ "mastercard",
+ "american express",
+ "discover",
+ "bank of america",
+ "chase",
+ "wells fargo",
+ "citibank",
+ "hsbc",
+ "irs",
+ "hmrc",
+ "cra",
+ "dhl",
+ "fedex",
+ "ups",
+ "usps",
+ "verify your account",
+ "confirm your identity",
+ "account suspended",
+ "account locked",
+ "unusual activity",
+ "security alert",
+ "action required",
+ "update your details",
+ "login required",
+ "secure your account",
+ "account validation",
+ "service cancellation",
+ "password reset",
+ "claim your reward",
+ "prize winner",
+ "lottery winner",
+ "free access"
+ ],
+];
diff --git a/client/components/pages/admin/BlockUser.vue b/client/components/pages/admin/BlockUser.vue
index d56e856df..777b651c9 100644
--- a/client/components/pages/admin/BlockUser.vue
+++ b/client/components/pages/admin/BlockUser.vue
@@ -4,18 +4,15 @@
:icon="isBlocked ? 'heroicons:lock-open-20-solid' : 'heroicons:no-symbol-20-solid'"
>
<div class="space-y-6 flex flex-col justify-between">
- <p class="text-xs text-neutral-500">
- <template v-if="isBlocked">
- This will unblock the user and allow them to log in again. Their forms will remain in draft status.
- <br>
- <b>Blocked on:</b> {{ new Date(user.blocked_at).toLocaleString() }}
- <br>
- <b>Reason:</b> {{ lastBlock?.reason }}
+ <UAlert
+ :icon="alertContent.icon"
+ :color="alertContent.color"
+ :variant="alertContent.variant"
+ >
+ <template #description>
+ <div v-html="alertContent.content" />
</template>
- <template v-else>
- This will block the user from accessing their account and set all their forms to draft.
- </template>
- </p>
+ </UAlert>
<VForm @submit.prevent="submit">
<TextAreaInput
@@ -85,7 +82,10 @@ const historyColumns = [
},
{
accessorKey: 'blocked_by',
- header: 'Blocked By'
+ header: 'Blocked By',
+ cell: ({ row }) => {
+ return row.original.blocked_by ? row.original.blocked_by : 'AI'
+ }
},
{
accessorKey: 'reason',
@@ -117,6 +117,34 @@ const lastBlock = computed(() => {
return blockingHistory.value[blockingHistory.value.length - 1]
})
+const alertContent = computed(() => {
+ if (isBlocked.value) {
+ const blockedBy = lastBlock.value?.blocked_by || 'Automatically blocked by our AI'
+ return {
+ icon: 'i-heroicons-exclamation-triangle',
+ color: 'error',
+ variant: 'subtle',
+ content: `
+ This will unblock the user and allow them to log in again. Their forms will remain in draft status.
+ <div class="mt-2">
+ <b>Blocked on:</b> ${new Date(props.user.blocked_at).toLocaleString()}
+ <br>
+ <b>Blocked by:</b> ${blockedBy}
+ <br>
+ <b>Reason:</b> ${lastBlock.value?.reason || ''}
+ </div>
+ `
+ }
+ } else {
+ return {
+ icon: 'i-heroicons-exclamation-triangle',
+ color: 'warning',
+ variant: 'subtle',
+ content: 'This will block the user from accessing their account and set all their forms to draft.'
+ }
+ }
+})
+
async function submit() {
loading.value = true