2024-03-28 18:14:30 +01:00
|
|
|
<?php
|
|
|
|
|
|
2024-06-05 15:35:46 +02:00
|
|
|
namespace App\Integrations\Handlers;
|
2024-03-28 18:14:30 +01:00
|
|
|
|
Email spam security (#641)
* Add hCaptcha on register page
* register page captcha test cases
* Refactor integration validation rules to include form context
- Updated the `getValidationRules` method in various integration handlers (Discord, Email, Google Sheets, Slack, Webhook, Zapier) to accept an optional `Form` parameter, allowing for context-aware validation.
- Enhanced the `EmailIntegration` handler to enforce restrictions based on user plans, ensuring free users can only create one email integration per form and can only send to a single email address.
- Added a new test suite for `EmailIntegration` to validate the new restrictions and ensure proper functionality for both free and pro users.
- Introduced loading state management in the `IntegrationModal` component to improve user experience during save operations.
These changes improve the flexibility and user experience of form integrations, particularly for email handling.
* for self-hosted ignore emil validation for spam
* fix pint
* ignore register throttle for testing env
* support new migration for mysql also
* Register page captcha enable if captcha key set
* fix test case
* fix test case
* fix test case
* fix pint
* Refactor RegisterController middleware and update TestCase setup
- Removed environment check for throttling middleware in RegisterController, ensuring consistent rate limiting for the registration endpoint.
- Updated TestCase to disable throttle middleware during tests, allowing for more flexible testing scenarios without rate limiting interference.
* Enhance hCaptcha integration in tests and configuration
- Added hCaptcha site and secret keys to phpunit.xml for testing purposes.
- Updated RegisterTest to configure hCaptcha secret key dynamically, ensuring proper token validation in production environment.
These changes improve the testing setup for hCaptcha, facilitating more accurate simulation of production conditions.
---------
Co-authored-by: Julien Nahum <julien@nahum.net>
2024-12-18 13:16:27 +01:00
|
|
|
use App\Models\Forms\Form;
|
|
|
|
|
use App\Models\Integration\FormIntegration;
|
2024-10-22 10:34:29 +02:00
|
|
|
use App\Notifications\Forms\FormEmailNotification;
|
2024-03-28 18:14:30 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
use Illuminate\Support\Facades\Notification;
|
2024-10-22 10:34:29 +02:00
|
|
|
use App\Open\MentionParser;
|
|
|
|
|
use App\Service\Forms\FormSubmissionFormatter;
|
Email spam security (#641)
* Add hCaptcha on register page
* register page captcha test cases
* Refactor integration validation rules to include form context
- Updated the `getValidationRules` method in various integration handlers (Discord, Email, Google Sheets, Slack, Webhook, Zapier) to accept an optional `Form` parameter, allowing for context-aware validation.
- Enhanced the `EmailIntegration` handler to enforce restrictions based on user plans, ensuring free users can only create one email integration per form and can only send to a single email address.
- Added a new test suite for `EmailIntegration` to validate the new restrictions and ensure proper functionality for both free and pro users.
- Introduced loading state management in the `IntegrationModal` component to improve user experience during save operations.
These changes improve the flexibility and user experience of form integrations, particularly for email handling.
* for self-hosted ignore emil validation for spam
* fix pint
* ignore register throttle for testing env
* support new migration for mysql also
* Register page captcha enable if captcha key set
* fix test case
* fix test case
* fix test case
* fix pint
* Refactor RegisterController middleware and update TestCase setup
- Removed environment check for throttling middleware in RegisterController, ensuring consistent rate limiting for the registration endpoint.
- Updated TestCase to disable throttle middleware during tests, allowing for more flexible testing scenarios without rate limiting interference.
* Enhance hCaptcha integration in tests and configuration
- Added hCaptcha site and secret keys to phpunit.xml for testing purposes.
- Updated RegisterTest to configure hCaptcha secret key dynamically, ensuring proper token validation in production environment.
These changes improve the testing setup for hCaptcha, facilitating more accurate simulation of production conditions.
---------
Co-authored-by: Julien Nahum <julien@nahum.net>
2024-12-18 13:16:27 +01:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2024-03-28 18:14:30 +01:00
|
|
|
|
2025-01-14 11:53:31 +01:00
|
|
|
class EmailIntegration extends AbstractIntegrationHandler
|
2024-03-28 18:14:30 +01:00
|
|
|
{
|
2024-10-22 10:34:29 +02:00
|
|
|
public const RISKY_USERS_LIMIT = 120;
|
|
|
|
|
|
Email spam security (#641)
* Add hCaptcha on register page
* register page captcha test cases
* Refactor integration validation rules to include form context
- Updated the `getValidationRules` method in various integration handlers (Discord, Email, Google Sheets, Slack, Webhook, Zapier) to accept an optional `Form` parameter, allowing for context-aware validation.
- Enhanced the `EmailIntegration` handler to enforce restrictions based on user plans, ensuring free users can only create one email integration per form and can only send to a single email address.
- Added a new test suite for `EmailIntegration` to validate the new restrictions and ensure proper functionality for both free and pro users.
- Introduced loading state management in the `IntegrationModal` component to improve user experience during save operations.
These changes improve the flexibility and user experience of form integrations, particularly for email handling.
* for self-hosted ignore emil validation for spam
* fix pint
* ignore register throttle for testing env
* support new migration for mysql also
* Register page captcha enable if captcha key set
* fix test case
* fix test case
* fix test case
* fix pint
* Refactor RegisterController middleware and update TestCase setup
- Removed environment check for throttling middleware in RegisterController, ensuring consistent rate limiting for the registration endpoint.
- Updated TestCase to disable throttle middleware during tests, allowing for more flexible testing scenarios without rate limiting interference.
* Enhance hCaptcha integration in tests and configuration
- Added hCaptcha site and secret keys to phpunit.xml for testing purposes.
- Updated RegisterTest to configure hCaptcha secret key dynamically, ensuring proper token validation in production environment.
These changes improve the testing setup for hCaptcha, facilitating more accurate simulation of production conditions.
---------
Co-authored-by: Julien Nahum <julien@nahum.net>
2024-12-18 13:16:27 +01:00
|
|
|
public static function getValidationRules(?Form $form): array
|
2024-03-28 18:14:30 +01:00
|
|
|
{
|
Email spam security (#641)
* Add hCaptcha on register page
* register page captcha test cases
* Refactor integration validation rules to include form context
- Updated the `getValidationRules` method in various integration handlers (Discord, Email, Google Sheets, Slack, Webhook, Zapier) to accept an optional `Form` parameter, allowing for context-aware validation.
- Enhanced the `EmailIntegration` handler to enforce restrictions based on user plans, ensuring free users can only create one email integration per form and can only send to a single email address.
- Added a new test suite for `EmailIntegration` to validate the new restrictions and ensure proper functionality for both free and pro users.
- Introduced loading state management in the `IntegrationModal` component to improve user experience during save operations.
These changes improve the flexibility and user experience of form integrations, particularly for email handling.
* for self-hosted ignore emil validation for spam
* fix pint
* ignore register throttle for testing env
* support new migration for mysql also
* Register page captcha enable if captcha key set
* fix test case
* fix test case
* fix test case
* fix pint
* Refactor RegisterController middleware and update TestCase setup
- Removed environment check for throttling middleware in RegisterController, ensuring consistent rate limiting for the registration endpoint.
- Updated TestCase to disable throttle middleware during tests, allowing for more flexible testing scenarios without rate limiting interference.
* Enhance hCaptcha integration in tests and configuration
- Added hCaptcha site and secret keys to phpunit.xml for testing purposes.
- Updated RegisterTest to configure hCaptcha secret key dynamically, ensuring proper token validation in production environment.
These changes improve the testing setup for hCaptcha, facilitating more accurate simulation of production conditions.
---------
Co-authored-by: Julien Nahum <julien@nahum.net>
2024-12-18 13:16:27 +01:00
|
|
|
$rules = [
|
|
|
|
|
'send_to' => ['required'],
|
2024-10-22 10:34:29 +02:00
|
|
|
'sender_name' => 'required',
|
|
|
|
|
'sender_email' => 'email|nullable',
|
|
|
|
|
'subject' => 'required',
|
|
|
|
|
'email_content' => 'required',
|
|
|
|
|
'include_submission_data' => 'boolean',
|
|
|
|
|
'include_hidden_fields_submission_data' => ['nullable', 'boolean'],
|
|
|
|
|
'reply_to' => 'nullable',
|
2024-03-28 18:14:30 +01:00
|
|
|
];
|
Email spam security (#641)
* Add hCaptcha on register page
* register page captcha test cases
* Refactor integration validation rules to include form context
- Updated the `getValidationRules` method in various integration handlers (Discord, Email, Google Sheets, Slack, Webhook, Zapier) to accept an optional `Form` parameter, allowing for context-aware validation.
- Enhanced the `EmailIntegration` handler to enforce restrictions based on user plans, ensuring free users can only create one email integration per form and can only send to a single email address.
- Added a new test suite for `EmailIntegration` to validate the new restrictions and ensure proper functionality for both free and pro users.
- Introduced loading state management in the `IntegrationModal` component to improve user experience during save operations.
These changes improve the flexibility and user experience of form integrations, particularly for email handling.
* for self-hosted ignore emil validation for spam
* fix pint
* ignore register throttle for testing env
* support new migration for mysql also
* Register page captcha enable if captcha key set
* fix test case
* fix test case
* fix test case
* fix pint
* Refactor RegisterController middleware and update TestCase setup
- Removed environment check for throttling middleware in RegisterController, ensuring consistent rate limiting for the registration endpoint.
- Updated TestCase to disable throttle middleware during tests, allowing for more flexible testing scenarios without rate limiting interference.
* Enhance hCaptcha integration in tests and configuration
- Added hCaptcha site and secret keys to phpunit.xml for testing purposes.
- Updated RegisterTest to configure hCaptcha secret key dynamically, ensuring proper token validation in production environment.
These changes improve the testing setup for hCaptcha, facilitating more accurate simulation of production conditions.
---------
Co-authored-by: Julien Nahum <julien@nahum.net>
2024-12-18 13:16:27 +01:00
|
|
|
|
|
|
|
|
if ($form->is_pro || config('app.self_hosted')) {
|
|
|
|
|
return $rules;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Free plan users can only send to a single email address (avoid spam)
|
|
|
|
|
$rules['send_to'][] = function ($attribute, $value, $fail) use ($form) {
|
|
|
|
|
if (count(explode("\n", trim($value))) > 1 || count(explode(',', $value)) > 1) {
|
|
|
|
|
$fail('You can only send to a single email address on the free plan. Please upgrade to the Pro plan to create a new integration.');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Free plan users can only have a single email integration per form (avoid spam)
|
|
|
|
|
if (!request()->route('integrationid')) {
|
|
|
|
|
$existingEmailIntegrations = FormIntegration::where('form_id', $form->id)
|
|
|
|
|
->where('integration_id', 'email')
|
|
|
|
|
->count();
|
|
|
|
|
if ($existingEmailIntegrations > 0) {
|
|
|
|
|
throw ValidationException::withMessages([
|
|
|
|
|
'settings.send_to' => ['Free users are limited to 1 email integration per form.']
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $rules;
|
2024-03-28 18:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function shouldRun(): bool
|
|
|
|
|
{
|
2024-10-24 12:02:23 +02:00
|
|
|
return $this->integrationData?->send_to && parent::shouldRun() && !$this->riskLimitReached();
|
2024-10-22 10:34:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// To avoid phishing abuse we limit this feature for risky users
|
|
|
|
|
private function riskLimitReached(): bool
|
|
|
|
|
{
|
|
|
|
|
// This is a per-workspace limit for risky workspaces
|
|
|
|
|
if ($this->form->workspace->is_risky) {
|
|
|
|
|
if ($this->form->workspace->submissions_count >= self::RISKY_USERS_LIMIT) {
|
|
|
|
|
Log::error('!!!DANGER!!! Dangerous user detected! Attempting many email sending.', [
|
|
|
|
|
'form_id' => $this->form->id,
|
|
|
|
|
'workspace_id' => $this->form->workspace->id,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2024-03-28 18:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function handle(): void
|
|
|
|
|
{
|
|
|
|
|
if (!$this->shouldRun()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 10:34:29 +02:00
|
|
|
if ($this->form->is_pro) { // For Send to field Mentions are Pro feature
|
|
|
|
|
$formatter = (new FormSubmissionFormatter($this->form, $this->submissionData))->outputStringsOnly();
|
2024-10-24 12:02:23 +02:00
|
|
|
$parser = new MentionParser($this->integrationData?->send_to, $formatter->getFieldsWithValue());
|
2024-11-06 17:48:10 +01:00
|
|
|
$sendTo = $parser->parseAsText();
|
2024-10-22 10:34:29 +02:00
|
|
|
} else {
|
2024-10-24 12:02:23 +02:00
|
|
|
$sendTo = $this->integrationData?->send_to;
|
2024-10-22 10:34:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$recipients = collect(preg_split("/\r\n|\n|\r/", $sendTo))
|
2024-03-28 18:14:30 +01:00
|
|
|
->filter(function ($email) {
|
|
|
|
|
return filter_var($email, FILTER_VALIDATE_EMAIL);
|
|
|
|
|
});
|
2025-01-14 11:53:31 +01:00
|
|
|
Log::info('Sending email notification', [
|
2024-10-22 10:34:29 +02:00
|
|
|
'recipients' => $recipients->toArray(),
|
2024-03-28 18:14:30 +01:00
|
|
|
'form_id' => $this->form->id,
|
|
|
|
|
'form_slug' => $this->form->slug,
|
|
|
|
|
]);
|
2024-11-06 17:48:10 +01:00
|
|
|
|
2024-10-22 10:34:29 +02:00
|
|
|
$recipients->each(function ($subscriber) {
|
2024-03-28 18:14:30 +01:00
|
|
|
Notification::route('mail', $subscriber)->notify(
|
2025-01-14 11:53:31 +01:00
|
|
|
new FormEmailNotification($this->event, $this->integrationData)
|
2024-03-28 18:14:30 +01:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|