From 97b0ed264c145df17f607e4397fa9d2f407da408 Mon Sep 17 00:00:00 2001 From: divine <48183131+divine@users.noreply.github.com> Date: Fri, 18 Dec 2020 01:29:14 +0300 Subject: [PATCH] Backport of #1992 to 3.6 Add preg_quote to value in getCount for Unique validation with DatabasePresenceVerifier.php in order to correctly escape regex chars in values from DB (eg. "+") Co-Authored-By: andrei-gafton-rtgt <61140282+andrei-gafton-rtgt@users.noreply.github.com> --- CHANGELOG.md | 2 ++ .../Validation/DatabasePresenceVerifier.php | 2 +- tests/ValidationTest.php | 20 +++++++++++++++++++ tests/models/User.php | 1 + 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3d6d836e..3d52882f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ All notable changes to this project will be documented in this file. ### Changed - MongodbQueueServiceProvider does not use the DB Facade anymore [#2149](https://github.com/jenssegers/laravel-mongodb/pull/2149) by [@curosmj](https://github.com/curosmj) +- Add escape regex chars to DB Presence Verifier [#1992](https://github.com/jenssegers/laravel-mongodb/pull/1992) by [@andrei-gafton-rtgt](https://github.com/andrei-gafton-rtgt). + ## [3.6.6] - 2020-10-29 diff --git a/src/Jenssegers/Mongodb/Validation/DatabasePresenceVerifier.php b/src/Jenssegers/Mongodb/Validation/DatabasePresenceVerifier.php index a7d57a89b..8ed85fd7f 100644 --- a/src/Jenssegers/Mongodb/Validation/DatabasePresenceVerifier.php +++ b/src/Jenssegers/Mongodb/Validation/DatabasePresenceVerifier.php @@ -16,7 +16,7 @@ class DatabasePresenceVerifier extends \Illuminate\Validation\DatabasePresenceVe */ public function getCount($collection, $column, $value, $excludeId = null, $idColumn = null, array $extra = []) { - $query = $this->table($collection)->where($column, 'regex', "/$value/i"); + $query = $this->table($collection)->where($column, 'regex', "/" . preg_quote($value) . "/i"); if ($excludeId !== null && $excludeId != 'NULL') { $query->where($idColumn ?: 'id', '<>', $excludeId); diff --git a/tests/ValidationTest.php b/tests/ValidationTest.php index 638398866..aed2278ca 100644 --- a/tests/ValidationTest.php +++ b/tests/ValidationTest.php @@ -41,6 +41,26 @@ public function testUnique(): void ['name' => 'required|unique:users'] ); $this->assertFalse($validator->fails()); + + User::create(['name' => 'Johnny Cash', 'email' => 'johnny.cash+200@gmail.com']); + + $validator = Validator::make( + ['email' => 'johnny.cash+200@gmail.com'], + ['email' => 'required|unique:users'] + ); + $this->assertTrue($validator->fails()); + + $validator = Validator::make( + ['email' => 'johnny.cash+20@gmail.com'], + ['email' => 'required|unique:users'] + ); + $this->assertFalse($validator->fails()); + + $validator = Validator::make( + ['email' => 'johnny.cash+1@gmail.com'], + ['email' => 'required|unique:users'] + ); + $this->assertFalse($validator->fails()); } public function testExists(): void diff --git a/tests/models/User.php b/tests/models/User.php index 1217af762..1f40ba367 100644 --- a/tests/models/User.php +++ b/tests/models/User.php @@ -13,6 +13,7 @@ * Class User * @property string $_id * @property string $name + * @property string $email * @property string $title * @property int $age * @property \Carbon\Carbon $birthday