Skip to content

Beautiful Open-Source Form Builder

Enhance spam detection logic in EmailIntegration and CheckSpamEmailIntegrationPrompt

Julien Nahumeb8cc9a70e25
diff --git a/api/app/Service/AI/Prompts/Integration/CheckSpamEmailIntegrationPrompt.php b/api/app/Service/AI/Prompts/Integration/CheckSpamEmailIntegrationPrompt.php
index d47962ecf..d3e54ee73 100644
--- a/api/app/Service/AI/Prompts/Integration/CheckSpamEmailIntegrationPrompt.php
+++ b/api/app/Service/AI/Prompts/Integration/CheckSpamEmailIntegrationPrompt.php
@@ -28,6 +28,7 @@ class CheckSpamEmailIntegrationPrompt extends Prompt
<formContext>
Form title: {formTitle}
Form description: {formDescription}
+ Form appears to be default/dummy: {isDefaultForm}
</formContext>
<userInformation>
@@ -35,10 +36,24 @@ class CheckSpamEmailIntegrationPrompt extends Prompt
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.
+ {blockingHistory}
+
+ **CRITICAL ABUSE PATTERN - HIGH PRIORITY:**
+ 🚨 Watch for: Default/dummy forms (e.g., "Contact Form" with no description) paired with phishing emails.
+ This is a common abuse pattern: attacker creates dummy form, then sets up phishing email integration to mass-send to random people.
+
+ **BLOCK IMMEDIATELY (is_spam: true):**
+ - Phishing or brand impersonation content (login pages, credential harvesting, fake support)
+ - Clear mismatch: default/generic form with targeted phishing email
+ - Non-subscribed user with obvious phishing setup
+
+ **FLAG FOR ADMIN REVIEW (needs_admin_review: true):**
+ - Borderline contextual mismatch between form and email
+ - Unsubscribed user with suspicious (but not clearly malicious) email setup
+
+ **ALLOW (both false) - DEFAULT:**
+ - Email content matches form context (e.g., contact form with contact confirmation email)
+ - Subscribed users with legitimate integration setup
Respond with JSON: {"is_spam": boolean, "needs_admin_review": boolean, "reason": "brief explanation"}
EOD;
@@ -85,6 +100,9 @@ class CheckSpamEmailIntegrationPrompt extends Prompt
$userRegisteredSince = $this->form->creator->created_at->diffInDays(now());
$isSubscribed = $this->form->creator->is_subscribed ? 'yes' : 'no';
+ $isDefaultForm = $this->isDefaultDummyForm() ? 'yes' : 'no';
+
+ $blockingHistory = $this->buildBlockingHistoryContext();
return Str::of($template)
->replace('{subject}', $subject)
@@ -96,6 +114,52 @@ class CheckSpamEmailIntegrationPrompt extends Prompt
->replace('{formDescription}', (string) $this->form->description)
->replace('{userRegisteredSince}', (string) $userRegisteredSince)
->replace('{isSubscribed}', $isSubscribed)
+ ->replace('{isDefaultForm}', $isDefaultForm)
+ ->replace('{blockingHistory}', $blockingHistory)
->toString();
}
+
+ private function isDefaultDummyForm(): bool
+ {
+ // Detect if form appears to be a default/dummy form (hasn't been customized)
+ $form = $this->form;
+
+ // Check if title is the default contact form title and description is empty/default
+ $isDefaultTitle = $form->title === 'Contact Form' || $form->title === 'Untitled Form';
+ $hasNoDescription = empty($form->description) || $form->description === '';
+
+ // Check if form has very few fields (just the default ones)
+ $defaultFieldCount = count($form->properties ?? []) <= 4;
+
+ return $isDefaultTitle && $hasNoDescription && $defaultFieldCount;
+ }
+
+ private function buildBlockingHistoryContext(): string
+ {
+ $blockingHistory = $this->form->creator->meta['blocking_history'] ?? [];
+
+ if (empty($blockingHistory)) {
+ return '';
+ }
+
+ $lastBlock = end($blockingHistory);
+ if (!$lastBlock) {
+ return '';
+ }
+
+ // Only mention if user was recently manually unblocked
+ if (!is_null($lastBlock['unblocked_by']) && !is_null($lastBlock['unblocked_at'])) {
+ $daysSinceUnblock = \Carbon\Carbon::parse($lastBlock['unblocked_at'])->diffInDays(now());
+
+ if ($daysSinceUnblock <= 30) {
+ return "
+**⚠️ USER WAS MANUALLY UNBLOCKED BY ADMIN:**
+This user was unblocked {$daysSinceUnblock} days ago. Respect prior admin decision - only re-block for EXTREME violations (obvious phishing/credential theft).
+Use 'needs_admin_review' instead if borderline.
+";
+ }
+ }
+
+ return '';
+ }
}
diff --git a/api/app/Service/Integrations/EmailIntegrationSpamService.php b/api/app/Service/Integrations/EmailIntegrationSpamService.php
index 39fae9373..a18d11e35 100644
--- a/api/app/Service/Integrations/EmailIntegrationSpamService.php
+++ b/api/app/Service/Integrations/EmailIntegrationSpamService.php
@@ -4,32 +4,35 @@ namespace App\Service\Integrations;
use App\Models\Forms\Form;
use App\Models\Integration\FormIntegration;
+use App\Models\User;
use App\Service\AI\Prompts\Integration\CheckSpamEmailIntegrationPrompt;
+use App\Service\UserActionService;
+use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
class EmailIntegrationSpamService
{
- public function __construct()
+ public function __construct(protected UserActionService $userActionService)
{
}
public function checkForSpam(Form $form, FormIntegration $integration): ?array
{
- if (!$this->shouldCheck($form)) {
+ if (!$this->shouldCheck($form, $integration)) {
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'
- ]);
+ if ($result['is_spam'] ?? false) {
+ $this->userActionService->block(
+ $form->creator,
+ $result['reason'] ?? 'Your email integration was detected as spam/phishing',
+ null
+ );
+ } elseif ($result['needs_admin_review'] ?? false) {
+ $this->logAdminReview($integration, $result['reason'] ?? 'Email integration flagged for admin review');
}
return $result;
@@ -46,7 +49,7 @@ class EmailIntegrationSpamService
}
}
- private function shouldCheck(Form $form): bool
+ private function shouldCheck(Form $form, FormIntegration $integration): bool
{
// Skip if globally disabled
if (!config('spam.enabled')) {
@@ -62,15 +65,122 @@ class EmailIntegrationSpamService
return false;
}
+ // Skip if integration is not active
+ if ($integration->status !== FormIntegration::STATUS_ACTIVE) {
+ return false;
+ }
+
+ // Skip if user account is too old (user is established)
if ($form->creator->created_at->diffInMonths(now()) > 3) {
return false;
}
- if ($form->creator->is_risky) {
+ // Skip checking users who were manually unblocked by admin in the past 7 days
+ // This saves AI costs and prevents immediate re-blocking of reviewed users
+ if ($this->wasRecentlyManuallyUnblocked($form->creator, 7)) {
+ return false;
+ }
+
+ // Check integration content for obvious spam keywords first (cost optimization)
+ if ($this->containsKeywords($integration)) {
+ return true;
+ }
+
+ // Check risky users (unsubscribed + new)
+ 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
+ {
+ // 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(FormIntegration $integration): bool
+ {
+ $data = (array) ($integration->data ?? (object) []);
+ $content = strtolower(
+ json_encode($data) . ' ' .
+ ($data['subject'] ?? '') . ' ' .
+ ($data['email_content'] ?? '')
+ );
+
+ $keywords = config('spam.keywords', []);
+
+ foreach ($keywords as $keyword) {
+ if (str_contains($content, strtolower($keyword))) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private function wasRecentlyManuallyUnblocked(User $user, int $days = 7): bool
+ {
+ $history = $user->meta['blocking_history'] ?? [];
+ if (empty($history)) {
+ return false;
+ }
+
+ $recentDate = now()->subDays($days);
+
+ foreach (array_reverse($history) as $block) {
+ // Check if this block was manually unblocked
+ if (
+ !is_null($block['unblocked_by']) &&
+ !is_null($block['unblocked_at']) &&
+ \Carbon\Carbon::parse($block['unblocked_at'])->isAfter($recentDate)
+ ) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private function logAdminReview(FormIntegration $integration, string $reason): void
+ {
+ $cacheKey = "integration_admin_review_flagged_{$integration->id}";
+
+ // Check if we've already flagged this integration in the last 24 hours
+ if (Cache::has($cacheKey)) {
+ Log::info('Skipping duplicate admin review alert for integration', [
+ 'integration_id' => $integration->id,
+ 'reason' => 'Already flagged within 24 hours',
+ ]);
+ return;
+ }
+
+ // Cache this integration as flagged for 24 hours
+ Cache::put($cacheKey, true, now()->addDay());
+
+ Log::channel('slack_alerts')->info('🚨 Email integration flagged for admin review 🚨', [
+ 'integration_id' => $integration->id,
+ 'form_id' => $integration->form_id,
+ 'form_title' => $integration->form->title,
+ 'user_id' => $integration->form->creator->id,
+ 'user_email' => $integration->form->creator->email,
+ 'user_registered_days_ago' => $integration->form->creator->created_at->diffInDays(now()),
+ 'is_subscribed' => $integration->form->creator->is_subscribed,
+ 'total_forms' => $integration->form->creator->forms()->count(),
+ 'reason' => $reason,
+ 'actions' => [
+ 'View Form' => front_url('/forms/' . $integration->form->slug),
+ 'Admin Panel' => front_url('/admin?user_id=' . $integration->form->creator->id),
+ ],
+ ]);
+
+ Log::info('Email integration flagged for admin review', [
+ 'integration_id' => $integration->id,
+ 'form_id' => $integration->form_id,
+ 'user_id' => $integration->form->creator->id,
+ 'reason' => $reason,
+ ]);
+ }
}