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>
This commit is contained in:
@@ -12,6 +12,7 @@ use Illuminate\Foundation\Auth\RegistersUsers;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
use App\Rules\ValidHCaptcha;
|
||||
|
||||
class RegisterController extends Controller
|
||||
{
|
||||
@@ -27,6 +28,9 @@ class RegisterController extends Controller
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('guest');
|
||||
|
||||
$this->middleware('throttle:5,1')->only('register'); // 5 attempts per minute
|
||||
$this->middleware('throttle:30,60')->only('register'); // 30 attempts per hour
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,7 +60,7 @@ class RegisterController extends Controller
|
||||
*/
|
||||
protected function validator(array $data)
|
||||
{
|
||||
return Validator::make($data, [
|
||||
$rules = [
|
||||
'name' => 'required|max:255',
|
||||
'email' => 'required|email:filter|max:255|unique:users|indisposable',
|
||||
'password' => 'required|min:6|confirmed',
|
||||
@@ -64,8 +68,14 @@ class RegisterController extends Controller
|
||||
'agree_terms' => ['required', Rule::in([true])],
|
||||
'appsumo_license' => ['nullable'],
|
||||
'invite_token' => ['nullable', 'string'],
|
||||
'utm_data' => ['nullable', 'array']
|
||||
], [
|
||||
'utm_data' => ['nullable', 'array'],
|
||||
];
|
||||
|
||||
if (config('services.h_captcha.secret_key')) {
|
||||
$rules['h-captcha-response'] = [new ValidHCaptcha()];
|
||||
}
|
||||
|
||||
return Validator::make($data, $rules, [
|
||||
'agree_terms' => 'Please agree with the terms and conditions.',
|
||||
]);
|
||||
}
|
||||
@@ -84,6 +94,7 @@ class RegisterController extends Controller
|
||||
'password' => bcrypt($data['password']),
|
||||
'hear_about_us' => $data['hear_about_us'],
|
||||
'utm_data' => array_key_exists('utm_data', $data) ? $data['utm_data'] : null,
|
||||
'meta' => ['registration_ip' => request()->ip()],
|
||||
]);
|
||||
|
||||
// Add relation with user
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Requests\Integration;
|
||||
|
||||
use App\Models\Forms\Form;
|
||||
use App\Models\Integration\FormIntegration;
|
||||
use App\Rules\IntegrationLogicRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
@@ -14,9 +15,11 @@ class FormIntegrationsRequest extends FormRequest
|
||||
public array $integrationRules = [];
|
||||
|
||||
private ?string $integrationClassName = null;
|
||||
private ?Form $form = null;
|
||||
|
||||
public function __construct(Request $request)
|
||||
{
|
||||
$this->form = Form::findOrFail(request()->route('id'));
|
||||
if ($request->integration_id) {
|
||||
// Load integration class, and get rules
|
||||
$integration = FormIntegration::getIntegration($request->integration_id);
|
||||
@@ -77,7 +80,7 @@ class FormIntegrationsRequest extends FormRequest
|
||||
|
||||
private function loadIntegrationRules()
|
||||
{
|
||||
foreach ($this->integrationClassName::getValidationRules() as $key => $value) {
|
||||
foreach ($this->integrationClassName::getValidationRules($this->form) as $key => $value) {
|
||||
$this->integrationRules['settings.' . $key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ abstract class AbstractIntegrationHandler
|
||||
Http::throw()->post($this->getWebhookUrl(), $this->getWebhookData());
|
||||
}
|
||||
|
||||
abstract public static function getValidationRules(): array;
|
||||
abstract public static function getValidationRules(?Form $form): array;
|
||||
|
||||
public static function isOAuthRequired(): bool
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Integrations\Handlers;
|
||||
|
||||
use App\Models\Forms\Form;
|
||||
use App\Open\MentionParser;
|
||||
use App\Service\Forms\FormSubmissionFormatter;
|
||||
use Illuminate\Support\Arr;
|
||||
@@ -9,7 +10,7 @@ use Vinkla\Hashids\Facades\Hashids;
|
||||
|
||||
class DiscordIntegration extends AbstractIntegrationHandler
|
||||
{
|
||||
public static function getValidationRules(): array
|
||||
public static function getValidationRules(?Form $form): array
|
||||
{
|
||||
return [
|
||||
'discord_webhook_url' => 'required|url|starts_with:https://discord.com/api/webhooks',
|
||||
|
||||
@@ -2,20 +2,23 @@
|
||||
|
||||
namespace App\Integrations\Handlers;
|
||||
|
||||
use App\Models\Forms\Form;
|
||||
use App\Models\Integration\FormIntegration;
|
||||
use App\Notifications\Forms\FormEmailNotification;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use App\Open\MentionParser;
|
||||
use App\Service\Forms\FormSubmissionFormatter;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class EmailIntegration extends AbstractEmailIntegrationHandler
|
||||
{
|
||||
public const RISKY_USERS_LIMIT = 120;
|
||||
|
||||
public static function getValidationRules(): array
|
||||
public static function getValidationRules(?Form $form): array
|
||||
{
|
||||
return [
|
||||
'send_to' => 'required',
|
||||
$rules = [
|
||||
'send_to' => ['required'],
|
||||
'sender_name' => 'required',
|
||||
'sender_email' => 'email|nullable',
|
||||
'subject' => 'required',
|
||||
@@ -24,6 +27,31 @@ class EmailIntegration extends AbstractEmailIntegrationHandler
|
||||
'include_hidden_fields_submission_data' => ['nullable', 'boolean'],
|
||||
'reply_to' => 'nullable',
|
||||
];
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
protected function shouldRun(): bool
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Integrations\Handlers;
|
||||
|
||||
use App\Events\Forms\FormSubmitted;
|
||||
use App\Integrations\Google\Google;
|
||||
use App\Models\Forms\Form;
|
||||
use App\Models\Integration\FormIntegration;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
@@ -22,11 +23,9 @@ class GoogleSheetsIntegration extends AbstractIntegrationHandler
|
||||
$this->client = new Google($formIntegration);
|
||||
}
|
||||
|
||||
public static function getValidationRules(): array
|
||||
public static function getValidationRules(?Form $form): array
|
||||
{
|
||||
return [
|
||||
|
||||
];
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function isOAuthRequired(): bool
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Integrations\Handlers;
|
||||
|
||||
use App\Models\Forms\Form;
|
||||
use App\Open\MentionParser;
|
||||
use App\Service\Forms\FormSubmissionFormatter;
|
||||
use Illuminate\Support\Arr;
|
||||
@@ -9,7 +10,7 @@ use Vinkla\Hashids\Facades\Hashids;
|
||||
|
||||
class SlackIntegration extends AbstractIntegrationHandler
|
||||
{
|
||||
public static function getValidationRules(): array
|
||||
public static function getValidationRules(?Form $form): array
|
||||
{
|
||||
return [
|
||||
'slack_webhook_url' => 'required|url|starts_with:https://hooks.slack.com/',
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace App\Integrations\Handlers;
|
||||
|
||||
use App\Models\Forms\Form;
|
||||
|
||||
class WebhookIntegration extends AbstractIntegrationHandler
|
||||
{
|
||||
public static function getValidationRules(): array
|
||||
public static function getValidationRules(?Form $form): array
|
||||
{
|
||||
return [
|
||||
'webhook_url' => 'required|url'
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Integrations\Handlers;
|
||||
|
||||
use App\Events\Forms\FormSubmitted;
|
||||
use App\Models\Forms\Form;
|
||||
use App\Models\Integration\FormIntegration;
|
||||
use Exception;
|
||||
|
||||
@@ -16,7 +17,7 @@ class ZapierIntegration extends AbstractIntegrationHandler
|
||||
parent::__construct($event, $formIntegration, $integration);
|
||||
}
|
||||
|
||||
public static function getValidationRules(): array
|
||||
public static function getValidationRules(?Form $form): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ class User extends Authenticatable implements JWTSubject
|
||||
'password',
|
||||
'hear_about_us',
|
||||
'utm_data',
|
||||
'meta'
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -44,6 +45,7 @@ class User extends Authenticatable implements JWTSubject
|
||||
'password',
|
||||
'remember_token',
|
||||
'hear_about_us',
|
||||
'meta'
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -56,6 +58,7 @@ class User extends Authenticatable implements JWTSubject
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'utm_data' => 'array',
|
||||
'meta' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user