diff --git a/app/Community/Livewire/Forms/ForumTopicCommentForm.php b/app/Community/Livewire/Forms/ForumTopicCommentForm.php index e93076e55d..9716b434d1 100644 --- a/app/Community/Livewire/Forms/ForumTopicCommentForm.php +++ b/app/Community/Livewire/Forms/ForumTopicCommentForm.php @@ -5,10 +5,10 @@ namespace App\Community\Livewire\Forms; use App\Models\ForumTopicComment; +use App\Support\Rules\ContainsRegularCharacter; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Http\RedirectResponse; use Livewire\Attributes\Locked; -use Livewire\Attributes\Validate; use Livewire\Features\SupportRedirects\Redirector; use Livewire\Form; @@ -16,7 +16,6 @@ class ForumTopicCommentForm extends Form { use AuthorizesRequests; - #[Validate('required|max:60000')] public string $body = ''; #[Locked] @@ -31,7 +30,14 @@ public function setForumTopicComment(ForumTopicComment $forumTopicComment): void public function update(): RedirectResponse|Redirector { $this->authorize('update', [ForumTopicComment::class, $this->forumTopicComment]); - $this->validate(); + $this->validate([ + 'body' => [ + 'required', + 'string', + 'max:60000', + new ContainsRegularCharacter(), + ], + ]); editTopicComment($this->forumTopicComment->id, $this->body); diff --git a/app/Community/Requests/MessageRequest.php b/app/Community/Requests/MessageRequest.php index 4108243fbc..4375f7ae10 100644 --- a/app/Community/Requests/MessageRequest.php +++ b/app/Community/Requests/MessageRequest.php @@ -4,6 +4,7 @@ namespace App\Community\Requests; +use App\Support\Rules\ContainsRegularCharacter; use Illuminate\Foundation\Http\FormRequest; class MessageRequest extends FormRequest @@ -11,7 +12,12 @@ class MessageRequest extends FormRequest public function rules(): array { return [ - 'body' => 'required|string|max:60000', + 'body' => [ + 'required', + 'string', + 'max:60000', + new ContainsRegularCharacter(), + ], 'recipient' => 'required_without:thread_id|exists:UserAccounts,User', 'thread_id' => 'nullable|integer', 'title' => 'required_without:thread_id|string|max:255', diff --git a/app/Support/Rules/ContainsRegularCharacter.php b/app/Support/Rules/ContainsRegularCharacter.php new file mode 100644 index 0000000000..6c1b299799 --- /dev/null +++ b/app/Support/Rules/ContainsRegularCharacter.php @@ -0,0 +1,19 @@ +translate(); + } + } +} diff --git a/lang/en/validation.php b/lang/en/validation.php index 529a37b66a..c02fee2953 100755 --- a/lang/en/validation.php +++ b/lang/en/validation.php @@ -133,7 +133,7 @@ /* * Strict validation rules */ - + 'contains_regular_character' => 'The :attribute must contain at least one regular character.', 'ctype_alnum' => 'The :attribute must only contain unaccented letters and numbers.', /* diff --git a/public/request/comment/create.php b/public/request/comment/create.php index 29b9073b38..43e43d0dea 100644 --- a/public/request/comment/create.php +++ b/public/request/comment/create.php @@ -6,6 +6,7 @@ use App\Models\Comment; use App\Models\Ticket; use App\Models\User; +use App\Support\Rules\ContainsRegularCharacter; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Validator; @@ -14,7 +15,12 @@ } $input = Validator::validate(Arr::wrap(request()->post()), [ - 'body' => 'required|string|max:2000', + 'body' => [ + 'required', + 'string', + 'max:2000', + new ContainsRegularCharacter(), + ], 'commentable_id' => 'required|integer', 'commentable_type' => 'required|integer', ]); diff --git a/public/request/forum-topic-comment/create.php b/public/request/forum-topic-comment/create.php index f94131ae5a..9e6f89d357 100644 --- a/public/request/forum-topic-comment/create.php +++ b/public/request/forum-topic-comment/create.php @@ -2,6 +2,7 @@ use App\Models\ForumTopic; use App\Models\User; +use App\Support\Rules\ContainsRegularCharacter; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Validator; @@ -11,7 +12,12 @@ $input = Validator::validate(Arr::wrap(request()->post()), [ 'topic' => 'required|integer|exists:ForumTopic,ID', - 'body' => 'required|string|max:60000', + 'body' => [ + 'required', + 'string', + 'max:60000', + new ContainsRegularCharacter(), + ], ]); $userModel = User::firstWhere('User', $user); diff --git a/tests/Unit/Rules/ContainsRegularCharacterTest.php b/tests/Unit/Rules/ContainsRegularCharacterTest.php new file mode 100644 index 0000000000..31fef821aa --- /dev/null +++ b/tests/Unit/Rules/ContainsRegularCharacterTest.php @@ -0,0 +1,54 @@ + 'This is a valid comment with letters and symbols!']; + + $validator = Validator::make($data, [ + 'body' => ['required', 'string', new ContainsRegularCharacter()], + ]); + + $this->assertFalse($validator->fails()); + } + + public function testItFailsWhenInputOnlyContainsControlCharacters(): void + { + $data = ['body' => "\u{200B}\u{200E}\u{200F}"]; + + $validator = Validator::make($data, [ + 'body' => ['required', 'string', new ContainsRegularCharacter()], + ]); + + $this->assertTrue($validator->fails()); + } + + public function testItFailsWhenInputIsEmpty(): void + { + $data = ['body' => '']; + + $validator = Validator::make($data, [ + 'body' => ['required', 'string', new ContainsRegularCharacter()], + ]); + + $this->assertTrue($validator->fails()); + } + + public function testItPassesWhenInputIsOnlySymbols(): void + { + $data = ['body' => '***!!!']; + + $validator = Validator::make($data, [ + 'body' => ['required', 'string', new ContainsRegularCharacter()], + ]); + + $this->assertFalse($validator->fails()); + } +}