Skip to content

Beautiful Open-Source Form Builder

Track Email Integration spam (#942)

Chirag Chhatralaf4ec2ecc13db
diff --git a/api/app/Events/Models/FormIntegrationSaved.php b/api/app/Events/Models/FormIntegrationSaved.php
new file mode 100644
index 000000000..ec70c66dd
--- /dev/null
+++ b/api/app/Events/Models/FormIntegrationSaved.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace App\Events\Models;
+
+use App\Models\Integration\FormIntegration;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+
+class FormIntegrationSaved
+{
+ use Dispatchable;
+ use InteractsWithSockets;
+ use SerializesModels;
+
+ public function __construct(
+ public FormIntegration $formIntegration
+ ) {
+ }
+}
diff --git a/api/app/Listeners/Integration/FormIntegrationSpamCheckListener.php b/api/app/Listeners/Integration/FormIntegrationSpamCheckListener.php
new file mode 100644
index 000000000..6f6a92a21
--- /dev/null
+++ b/api/app/Listeners/Integration/FormIntegrationSpamCheckListener.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace App\Listeners\Integration;
+
+use App\Events\Models\FormIntegrationSaved;
+use App\Service\Integrations\EmailIntegrationSpamService;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Support\Facades\App;
+
+class FormIntegrationSpamCheckListener implements ShouldQueue
+{
+ use InteractsWithQueue;
+
+ public function __construct()
+ {
+ }
+
+ public function handle(FormIntegrationSaved $event): void
+ {
+ if (App::environment('testing')) {
+ return;
+ }
+
+ if ($event->formIntegration->integration_id === 'email') {
+ $emailIntegrationSpamService = app(EmailIntegrationSpamService::class);
+ $emailIntegrationSpamService->checkForSpam($event->formIntegration->form, $event->formIntegration);
+ }
+
+ return;
+ }
+}
diff --git a/api/app/Models/Integration/FormIntegration.php b/api/app/Models/Integration/FormIntegration.php
index 806e5dbf5..c00d632a6 100644
--- a/api/app/Models/Integration/FormIntegration.php
+++ b/api/app/Models/Integration/FormIntegration.php
@@ -3,14 +3,18 @@
namespace App\Models\Integration;
use App\Events\Models\FormIntegrationCreated;
+use App\Events\Models\FormIntegrationSaved;
use App\Models\Forms\Form;
use App\Models\OAuthProvider;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
class FormIntegration extends Model
{
use HasFactory;
+ use SoftDeletes;
+
public const STATUS_ACTIVE = 'active';
public const STATUS_INACTIVE = 'inactive';
@@ -33,6 +37,7 @@ class FormIntegration extends Model
protected $dispatchesEvents = [
'created' => FormIntegrationCreated::class,
+ 'saved' => FormIntegrationSaved::class,
];
/**
@@ -62,11 +67,4 @@ class FormIntegration extends Model
{
return self::getAllIntegrations()[$key] ?? null;
}
-
- public static function booted(): void
- {
- static::deleting(function (FormIntegration $integration) {
- $integration->events()->delete();
- });
- }
}
diff --git a/api/app/Models/User.php b/api/app/Models/User.php
index 0be9d8dd2..f172666c9 100644
--- a/api/app/Models/User.php
+++ b/api/app/Models/User.php
@@ -343,11 +343,7 @@ class User extends Authenticatable implements JWTSubject, CachableAttributes
public function getIsRiskyAttribute()
{
- return $this->created_at->isAfter(now()->subDays(3)) || // created in last 3 days
- $this->subscriptions()->where(function ($q) {
- $q->where('stripe_status', 'trialing')
- ->orWhere('stripe_status', 'active');
- })->first()?->onTrial();
+ return $this->created_at->isAfter(now()->subDays(3));
}
public function flushCache()
diff --git a/api/app/Providers/EventServiceProvider.php b/api/app/Providers/EventServiceProvider.php
index a1623642e..e0a0dedda 100644
--- a/api/app/Providers/EventServiceProvider.php
+++ b/api/app/Providers/EventServiceProvider.php
@@ -9,6 +9,7 @@ use App\Events\Models\FormSubmissionDeleting;
use App\Events\Forms\FormSaved;
use App\Events\Models\FormCreated;
use App\Events\Models\FormIntegrationCreated;
+use App\Events\Models\FormIntegrationSaved;
use App\Events\Models\FormIntegrationsEventCreated;
use App\Listeners\Billing\HandleSubscriptionCreated;
use App\Listeners\Billing\RemoveWorkspaceGuestsIfNeeded;
@@ -18,6 +19,7 @@ use App\Listeners\Forms\FormIntegrationsEventListener;
use App\Listeners\Forms\NotifyFormSubmission;
use App\Listeners\Forms\DeleteFormSubmissionFiles;
use App\Listeners\Forms\FormSpamCheckListener;
+use App\Listeners\Integration\FormIntegrationSpamCheckListener;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
@@ -49,6 +51,9 @@ class EventServiceProvider extends ServiceProvider
FormIntegrationCreated::class => [
FormIntegrationCreatedHandler::class,
],
+ FormIntegrationSaved::class => [
+ FormIntegrationSpamCheckListener::class,
+ ],
FormIntegrationsEventCreated::class => [
FormIntegrationsEventListener::class,
],
diff --git a/api/app/Service/AI/Prompts/Integration/CheckSpamEmailIntegrationPrompt.php b/api/app/Service/AI/Prompts/Integration/CheckSpamEmailIntegrationPrompt.php
new file mode 100644
index 000000000..d47962ecf
--- /dev/null
+++ b/api/app/Service/AI/Prompts/Integration/CheckSpamEmailIntegrationPrompt.php
@@ -0,0 +1,101 @@
+<?php
+
+namespace App\Service\AI\Prompts\Integration;
+
+use App\Models\Forms\Form;
+use App\Models\Integration\FormIntegration;
+use App\Service\AI\Prompts\Prompt;
+use Illuminate\Support\Str;
+
+class CheckSpamEmailIntegrationPrompt extends Prompt
+{
+ protected ?float $temperature = 0.2;
+ protected ?int $maxTokens = 500;
+ protected string $model = 'gpt-4.1-mini';
+
+ public const PROMPT_TEMPLATE = <<<'EOD'
+ Analyze this email notification integration for spam/phishing or unrelated content.
+
+ <emailIntegration>
+ Subject: {subject}
+ Body:
+ {emailContent}
+ Includes submission data: {includesSubmissionData}
+ Reply-to: {replyTo}
+ Link edit submission: {linkEditSubmission}
+ </emailIntegration>
+
+ <formContext>
+ Form title: {formTitle}
+ Form description: {formDescription}
+ </formContext>
+
+ <userInformation>
+ User registered: {userRegisteredSince} days ago.
+ User is subscribed: {isSubscribed}
+ </userInformation>
+
+ Guidelines:
+ - BLOCK immediately if email content is phishing, brand impersonation, login credential harvesting, or clearly unrelated to the form context.
+ - FLAG FOR ADMIN REVIEW only when borderline suspicious, but not clearly malicious.
+ - ALLOW by default for normal notifications about the form or its submissions.
+
+ Respond with JSON: {"is_spam": boolean, "needs_admin_review": boolean, "reason": "brief explanation"}
+ EOD;
+
+ protected ?array $jsonSchema = [
+ 'type' => 'object',
+ 'required' => ['is_spam', 'needs_admin_review', 'reason'],
+ 'additionalProperties' => false,
+ 'properties' => [
+ 'is_spam' => [
+ 'type' => 'boolean',
+ ],
+ 'needs_admin_review' => [
+ 'type' => 'boolean',
+ ],
+ 'reason' => [
+ 'type' => 'string',
+ ],
+ ],
+ ];
+
+ public function __construct(
+ public Form $form,
+ public FormIntegration $integration
+ ) {
+ parent::__construct();
+ }
+
+ protected function getPromptTemplate(): string
+ {
+ return self::PROMPT_TEMPLATE;
+ }
+
+ protected function buildPrompt(): string
+ {
+ $template = $this->getPromptTemplate();
+ $data = (array) ($this->integration->data ?? (object) []);
+
+ $subject = (string) ($data['subject'] ?? '');
+ $emailContent = (string) ($data['email_content'] ?? '');
+ $includesSubmissionData = isset($data['include_submission_data']) && $data['include_submission_data'] ? 'yes' : 'no';
+ $replyTo = (string) ($data['reply_to'] ?? '');
+ $linkEditSubmission = isset($data['link_edit_submission']) && $data['link_edit_submission'] ? 'yes' : 'no';
+
+ $userRegisteredSince = $this->form->creator->created_at->diffInDays(now());
+ $isSubscribed = $this->form->creator->is_subscribed ? 'yes' : 'no';
+
+ return Str::of($template)
+ ->replace('{subject}', $subject)
+ ->replace('{emailContent}', $emailContent)
+ ->replace('{includesSubmissionData}', $includesSubmissionData)
+ ->replace('{replyTo}', $replyTo)
+ ->replace('{linkEditSubmission}', $linkEditSubmission)
+ ->replace('{formTitle}', (string) $this->form->title)
+ ->replace('{formDescription}', (string) $this->form->description)
+ ->replace('{userRegisteredSince}', (string) $userRegisteredSince)
+ ->replace('{isSubscribed}', $isSubscribed)
+ ->toString();
+ }
+}
diff --git a/api/app/Service/Integrations/EmailIntegrationSpamService.php b/api/app/Service/Integrations/EmailIntegrationSpamService.php
new file mode 100644
index 000000000..39fae9373
--- /dev/null
+++ b/api/app/Service/Integrations/EmailIntegrationSpamService.php
@@ -0,0 +1,76 @@
+<?php
+
+namespace App\Service\Integrations;
+
+use App\Models\Forms\Form;
+use App\Models\Integration\FormIntegration;
+use App\Service\AI\Prompts\Integration\CheckSpamEmailIntegrationPrompt;
+use Illuminate\Support\Facades\Log;
+
+class EmailIntegrationSpamService
+{
+ public function __construct()
+ {
+ }
+
+ public function checkForSpam(Form $form, FormIntegration $integration): ?array
+ {
+ if (!$this->shouldCheck($form)) {
+ return null;
+ }
+
+ try {
+ $result = CheckSpamEmailIntegrationPrompt::run($form, $integration);
+
+ if (($result['is_spam'] ?? false) === true || ($result['needs_admin_review'] ?? false) === true) {
+ Log::channel('slack_admin')->info('⚠️ Email integration flagged for admin review', [
+ 'form_id' => $form->id,
+ 'integration_id' => $integration->id,
+ 'user_id' => $form->creator->id,
+ 'user_email' => $form->creator->email,
+ 'reason' => $result['reason'] ?? 'Email integration detected as spam/phishing'
+ ]);
+ }
+
+ return $result;
+ } catch (\Exception $e) {
+ Log::error('Failed to check email integration for spam.', [
+ 'form_id' => $form->id,
+ 'integration_id' => $integration->id,
+ 'user_id' => $form->creator->id,
+ 'user_email' => $form->creator->email,
+ 'exception' => $e->getMessage(),
+ ]);
+
+ return null;
+ }
+ }
+
+ private function shouldCheck(Form $form): bool
+ {
+ // Skip if globally disabled
+ if (!config('spam.enabled')) {
+ return false;
+ }
+
+ // Check if creator exists before accessing its properties
+ if (!$form->creator) {
+ return false;
+ }
+
+ if ($form->creator->is_blocked || $form->creator->admin || $form->creator->moderator) {
+ return false;
+ }
+
+ if ($form->creator->created_at->diffInMonths(now()) > 3) {
+ return false;
+ }
+
+ if ($form->creator->is_risky) {
+ return true;
+ }
+
+ // Random check for other users
+ return (rand(1, 100) <= config('spam.random_check_percentage', 0));
+ }
+}
diff --git a/api/database/migrations/2025_10_31_000200_add_soft_deletes_to_form_integrations_table.php b/api/database/migrations/2025_10_31_000200_add_soft_deletes_to_form_integrations_table.php
new file mode 100644
index 000000000..aaaa777a3
--- /dev/null
+++ b/api/database/migrations/2025_10_31_000200_add_soft_deletes_to_form_integrations_table.php
@@ -0,0 +1,27 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class () extends Migration {
+ /**
+ * Run the migrations.
+ */
+ public function up(): void
+ {
+ Schema::table('form_integrations', function (Blueprint $table) {
+ $table->softDeletes();
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void
+ {
+ Schema::table('form_integrations', function (Blueprint $table) {
+ $table->dropSoftDeletes();
+ });
+ }
+};
diff --git a/api/tests/Feature/Zapier/IntegrationsTest.php b/api/tests/Feature/Zapier/IntegrationsTest.php
index d81a94576..b6c6e9064 100644
--- a/api/tests/Feature/Zapier/IntegrationsTest.php
+++ b/api/tests/Feature/Zapier/IntegrationsTest.php
@@ -5,6 +5,7 @@ use App\Models\User;
use Laravel\Sanctum\Sanctum;
use function Pest\Laravel\assertDatabaseCount;
+use function Pest\Laravel\assertSoftDeleted;
use function Pest\Laravel\delete;
use function Pest\Laravel\post;
use function PHPUnit\Framework\assertEquals;
@@ -100,7 +101,7 @@ test('delete an integration', function () {
])
->assertOk();
- assertDatabaseCount('form_integrations', 0);
+ assertSoftDeleted('form_integrations', ['id' => $integration->id]);
});
test('cannot delete an integration with an incorrect hook url', function () {