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:
Chirag Chhatrala
2024-12-18 17:46:27 +05:30
committed by GitHub
parent c1ee072b71
commit 7365479c83
18 changed files with 375 additions and 25 deletions

View File

@@ -0,0 +1,175 @@
<?php
use App\Models\Integration\FormIntegration;
test('free user can create one email integration', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
// First email integration should succeed
$response = $this->postJson(route('open.forms.integration.create', $form), [
'integration_id' => 'email',
'status' => true,
'settings' => [
'send_to' => 'test@example.com',
'sender_name' => 'Test Sender',
'subject' => 'Test Subject',
'email_content' => 'Test Content',
'include_submission_data' => true
]
]);
$response->assertSuccessful();
expect(FormIntegration::where('form_id', $form->id)->count())->toBe(1);
// Second email integration should fail
$response = $this->postJson(route('open.forms.integration.create', $form), [
'integration_id' => 'email',
'status' => true,
'settings' => [
'send_to' => 'another@example.com',
'sender_name' => 'Test Sender',
'subject' => 'Test Subject',
'email_content' => 'Test Content',
'include_submission_data' => true
]
]);
$response->assertStatus(422)
->assertJson([
'errors' => [
'settings.send_to' => ['Free users are limited to 1 email integration per form.']
]
]);
});
test('pro user can create multiple email integrations', function () {
$user = $this->actingAsProUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
// First email integration
$response = $this->postJson(route('open.forms.integration.create', $form), [
'integration_id' => 'email',
'status' => true,
'settings' => [
'send_to' => 'test@example.com',
'sender_name' => 'Test Sender',
'subject' => 'Test Subject',
'email_content' => 'Test Content',
'include_submission_data' => true
]
]);
$response->assertSuccessful();
// Second email integration should also succeed for pro users
$response = $this->postJson(route('open.forms.integration.create', $form), [
'integration_id' => 'email',
'status' => true,
'settings' => [
'send_to' => 'another@example.com',
'sender_name' => 'Test Sender',
'subject' => 'Test Subject',
'email_content' => 'Test Content',
'include_submission_data' => true
]
]);
$response->assertSuccessful();
expect(FormIntegration::where('form_id', $form->id)->count())->toBe(2);
});
test('free user cannot add multiple emails', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$response = $this->postJson(route('open.forms.integration.create', $form), [
'integration_id' => 'email',
'status' => true,
'settings' => [
'send_to' => "test@example.com\nanother@example.com",
'sender_name' => 'Test Sender',
'subject' => 'Test Subject',
'email_content' => 'Test Content',
'include_submission_data' => true
]
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['settings.send_to'])
->assertJson([
'errors' => [
'settings.send_to' => ['You can only send to a single email address on the free plan. Please upgrade to the Pro plan to create a new integration.']
]
]);
});
test('pro user can add multiple emails', function () {
$user = $this->actingAsProUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
$response = $this->postJson(route('open.forms.integration.create', $form), [
'integration_id' => 'email',
'status' => true,
'settings' => [
'send_to' => "test@example.com\nanother@example.com\nthird@example.com",
'sender_name' => 'Test Sender',
'subject' => 'Test Subject',
'email_content' => 'Test Content',
'include_submission_data' => true
]
]);
$response->assertSuccessful();
$integration = FormIntegration::where('form_id', $form->id)->first();
expect($integration)->not->toBeNull();
expect($integration->data->send_to)->toContain('test@example.com');
expect($integration->data->send_to)->toContain('another@example.com');
expect($integration->data->send_to)->toContain('third@example.com');
});
test('free user can update their single email integration', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);
$form = $this->createForm($user, $workspace);
// Create initial integration
$response = $this->postJson(route('open.forms.integration.create', $form), [
'integration_id' => 'email',
'status' => true,
'settings' => [
'send_to' => 'test@example.com',
'sender_name' => 'Test Sender',
'subject' => 'Test Subject',
'email_content' => 'Test Content',
'include_submission_data' => true
]
]);
$response->assertSuccessful();
$integrationId = $response->json('form_integration.id');
// Update the integration
$response = $this->putJson(route('open.forms.integration.update', [$form, $integrationId]), [
'integration_id' => 'email',
'status' => true,
'settings' => [
'send_to' => 'updated@example.com',
'sender_name' => 'Updated Sender',
'subject' => 'Updated Subject',
'email_content' => 'Updated Content',
'include_submission_data' => true
]
]);
$response->assertSuccessful();
$integration = FormIntegration::find($integrationId);
expect($integration->data->send_to)->toBe('updated@example.com');
expect($integration->data->sender_name)->toBe('Updated Sender');
});

View File

@@ -1,8 +1,15 @@
<?php
use App\Models\User;
use App\Rules\ValidHCaptcha;
use Illuminate\Support\Facades\Http;
it('can register', function () {
Http::fake([
ValidHCaptcha::H_CAPTCHA_VERIFY_URL => Http::response(['success' => true])
]);
$this->postJson('/register', [
'name' => 'Test User',
'email' => 'test@test.app',
@@ -10,13 +17,15 @@ it('can register', function () {
'password' => 'secret',
'password_confirmation' => 'secret',
'agree_terms' => true,
'h-captcha-response' => 'test-token', // Mock token for testing
])
->assertSuccessful()
->assertJsonStructure(['id', 'name', 'email']);
$this->assertDatabaseHas('users', [
'name' => 'Test User',
'email' => 'test@test.app',
]);
$user = User::where('email', 'test@test.app')->first();
expect($user)->not->toBeNull();
expect($user->meta)->toHaveKey('registration_ip');
expect($user->meta['registration_ip'])->toBe(request()->ip());
});
it('cannot register with existing email', function () {
@@ -27,12 +36,17 @@ it('cannot register with existing email', function () {
'email' => 'test@test.app',
'password' => 'secret',
'password_confirmation' => 'secret',
'h-captcha-response' => 'test-token',
])
->assertStatus(422)
->assertJsonValidationErrors(['email']);
});
it('cannot register with disposable email', function () {
Http::fake([
ValidHCaptcha::H_CAPTCHA_VERIFY_URL => Http::response(['success' => true])
]);
// Select random email
$email = [
'dumliyupse@gufum.com',
@@ -48,6 +62,7 @@ it('cannot register with disposable email', function () {
'password' => 'secret',
'password_confirmation' => 'secret',
'agree_terms' => true,
'h-captcha-response' => 'test-token',
])
->assertStatus(422)
->assertJsonValidationErrors(['email'])
@@ -60,3 +75,22 @@ it('cannot register with disposable email', function () {
],
]);
});
it('requires hcaptcha token in production', function () {
config(['services.h_captcha.secret_key' => 'test-key']);
Http::fake([
ValidHCaptcha::H_CAPTCHA_VERIFY_URL => Http::response(['success' => true])
]);
$this->postJson('/register', [
'name' => 'Test User',
'email' => 'test@test.app',
'hear_about_us' => 'google',
'password' => 'secret',
'password_confirmation' => 'secret',
'agree_terms' => true,
])
->assertStatus(422)
->assertJsonValidationErrors(['h-captcha-response']);
});

View File

@@ -2,10 +2,15 @@
use App\Models\UserInvite;
use Carbon\Carbon;
use App\Rules\ValidHCaptcha;
use Illuminate\Support\Facades\Http;
beforeEach(function () {
$this->user = $this->actingAsProUser();
$this->workspace = $this->createUserWorkspace($this->user);
Http::fake([
ValidHCaptcha::H_CAPTCHA_VERIFY_URL => Http::response(['success' => true])
]);
});
@@ -31,6 +36,7 @@ it('can register with invite token', function () {
'password_confirmation' => 'secret',
'agree_terms' => true,
'invite_token' => $token,
'h-captcha-response' => 'test-token',
]);
$response->assertSuccessful();
expect($this->workspace->users()->count())->toBe(2);
@@ -59,6 +65,7 @@ it('cannot register with expired invite token', function () {
'password_confirmation' => 'secret',
'agree_terms' => true,
'invite_token' => $token,
'h-captcha-response' => 'test-token',
]);
$response->assertStatus(400)->assertJson([
'message' => 'Invite token has expired.',
@@ -88,6 +95,7 @@ it('cannot re-register with accepted invite token', function () {
'password_confirmation' => 'secret',
'agree_terms' => true,
'invite_token' => $token,
'h-captcha-response' => 'test-token',
]);
$response->assertSuccessful();
expect($this->workspace->users()->count())->toBe(2);
@@ -104,6 +112,7 @@ it('cannot re-register with accepted invite token', function () {
'password_confirmation' => 'secret',
'agree_terms' => true,
'invite_token' => $token,
'h-captcha-response' => 'test-token',
]);
$response->assertStatus(422)->assertJson([
@@ -138,6 +147,7 @@ it('can cancel user invite', function () {
'password_confirmation' => 'secret',
'agree_terms' => true,
'invite_token' => $token,
'h-captcha-response' => 'test-token',
]);
$response->assertStatus(400)->assertJson([
'message' => 'Invite token is invalid.',

View File

@@ -4,10 +4,19 @@ namespace Tests;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Routing\Middleware\ThrottleRequests;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use RefreshDatabase;
use TestHelpers;
protected function setUp(): void
{
parent::setUp();
$this->withoutMiddleware(
ThrottleRequests::class
);
}
}