diff --git a/.phpunit.result.cache b/.phpunit.result.cache index 2a4c270..bdaa4fc 100644 --- a/.phpunit.result.cache +++ b/.phpunit.result.cache @@ -1 +1 @@ -C:37:"PHPUnit\Runner\DefaultTestResultCache":4239:{a:2:{s:7:"defects";a:15:{s:85:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_if_encryption_decoding_is_working";i:4;s:85:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_if_encryption_encoding_is_working";i:4;s:104:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_encrypt_model_commands_encrypt_existing_records";i:4;s:90:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_where_in_query_builder_is_working";i:4;s:110:"ESolution\DBEncryption\Tests\EncryptedTest::it_assert_that_where_does_not_retrieve_a_user_with_incorrect_email";i:4;s:109:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_validation_rule_exists_when_record_exists_is_working";i:4;s:118:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_validation_rule_exists_when_record_does_not_exists_is_working";i:4;s:109:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_validation_rule_unique_when_record_exists_is_working";i:4;s:118:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_validation_rule_unique_when_record_does_not_exists_is_working";i:4;s:88:"ESolution\DBEncryption\Tests\EncryptedTest::it_tests_that_empty_values_are_not_encrypted";i:3;s:83:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_decrypt_command_is_working";i:4;s:96:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_encrypted_value_is_stored_in_lower_case";i:3;s:105:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_where_query_is_working_with_non_lowercase_values";i:4;s:88:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_convert_to_camelcase_is_working";i:4;s:84:"ESolution\DBEncryption\Tests\EncryptedTest::it_tests_that_empty_values_are_encrypted";i:4;}s:5:"times";a:15:{s:85:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_if_encryption_decoding_is_working";d:0.1710000000000000131006316905768471769988536834716796875;s:85:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_if_encryption_encoding_is_working";d:0.0189999999999999995281552145343084703199565410614013671875;s:104:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_encrypt_model_commands_encrypt_existing_records";d:0.024000000000000000499600361081320443190634250640869140625;s:90:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_where_in_query_builder_is_working";d:0.01600000000000000033306690738754696212708950042724609375;s:110:"ESolution\DBEncryption\Tests\EncryptedTest::it_assert_that_where_does_not_retrieve_a_user_with_incorrect_email";d:0.0210000000000000013045120539345589349977672100067138671875;s:109:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_validation_rule_exists_when_record_exists_is_working";d:0.022999999999999999611421941381195210851728916168212890625;s:118:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_validation_rule_exists_when_record_does_not_exists_is_working";d:0.0179999999999999986399767948341832379810512065887451171875;s:109:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_validation_rule_unique_when_record_exists_is_working";d:0.0200000000000000004163336342344337026588618755340576171875;s:118:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_validation_rule_unique_when_record_does_not_exists_is_working";d:0.01600000000000000033306690738754696212708950042724609375;s:88:"ESolution\DBEncryption\Tests\EncryptedTest::it_tests_that_empty_values_are_not_encrypted";d:0.021999999999999998723243521681069978512823581695556640625;s:83:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_decrypt_command_is_working";d:0.0259999999999999988065102485279567190445959568023681640625;s:96:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_encrypted_value_is_stored_in_lower_case";d:0.01299999999999999940325512426397835952229797840118408203125;s:105:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_where_query_is_working_with_non_lowercase_values";d:0.0189999999999999995281552145343084703199565410614013671875;s:88:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_convert_to_camelcase_is_working";d:0.022999999999999999611421941381195210851728916168212890625;s:84:"ESolution\DBEncryption\Tests\EncryptedTest::it_tests_that_empty_values_are_encrypted";d:0.0200000000000000004163336342344337026588618755340576171875;}}} \ No newline at end of file +C:37:"PHPUnit\Runner\DefaultTestResultCache":3640:{a:2:{s:7:"defects";a:16:{s:85:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_if_encryption_decoding_is_working";i:4;s:85:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_if_encryption_encoding_is_working";i:4;s:104:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_encrypt_model_commands_encrypt_existing_records";i:4;s:90:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_where_in_query_builder_is_working";i:4;s:110:"ESolution\DBEncryption\Tests\EncryptedTest::it_assert_that_where_does_not_retrieve_a_user_with_incorrect_email";i:4;s:109:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_validation_rule_exists_when_record_exists_is_working";i:4;s:118:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_validation_rule_exists_when_record_does_not_exists_is_working";i:4;s:109:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_validation_rule_unique_when_record_exists_is_working";i:4;s:118:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_validation_rule_unique_when_record_does_not_exists_is_working";i:4;s:88:"ESolution\DBEncryption\Tests\EncryptedTest::it_tests_that_empty_values_are_not_encrypted";i:3;s:83:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_decrypt_command_is_working";i:4;s:96:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_encrypted_value_is_stored_in_lower_case";i:3;s:105:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_where_query_is_working_with_non_lowercase_values";i:4;s:88:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_convert_to_camelcase_is_working";i:4;s:84:"ESolution\DBEncryption\Tests\EncryptedTest::it_tests_that_empty_values_are_encrypted";i:4;s:95:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_whereencrypted_can_handle_single_quote";i:4;}s:5:"times";a:16:{s:85:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_if_encryption_decoding_is_working";d:0.285;s:85:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_if_encryption_encoding_is_working";d:0.118;s:104:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_encrypt_model_commands_encrypt_existing_records";d:0.197;s:90:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_where_in_query_builder_is_working";d:0.131;s:110:"ESolution\DBEncryption\Tests\EncryptedTest::it_assert_that_where_does_not_retrieve_a_user_with_incorrect_email";d:0.129;s:109:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_validation_rule_exists_when_record_exists_is_working";d:0.137;s:118:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_validation_rule_exists_when_record_does_not_exists_is_working";d:0.11;s:109:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_validation_rule_unique_when_record_exists_is_working";d:0.131;s:118:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_validation_rule_unique_when_record_does_not_exists_is_working";d:0.128;s:88:"ESolution\DBEncryption\Tests\EncryptedTest::it_tests_that_empty_values_are_not_encrypted";d:0.022;s:83:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_decrypt_command_is_working";d:0.259;s:96:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_encrypted_value_is_stored_in_lower_case";d:0.013;s:105:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_where_query_is_working_with_non_lowercase_values";d:0.135;s:88:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_convert_to_camelcase_is_working";d:0.023;s:84:"ESolution\DBEncryption\Tests\EncryptedTest::it_tests_that_empty_values_are_encrypted";d:0.13;s:95:"ESolution\DBEncryption\Tests\EncryptedTest::it_test_that_whereencrypted_can_handle_single_quote";d:0.126;}}} \ No newline at end of file diff --git a/src/Builders/EncryptionEloquentBuilder.php b/src/Builders/EncryptionEloquentBuilder.php index b453718..e7fe26e 100644 --- a/src/Builders/EncryptionEloquentBuilder.php +++ b/src/Builders/EncryptionEloquentBuilder.php @@ -14,10 +14,10 @@ public function whereEncrypted($param1, $param2, $param3 = null) $filter->field = $param1; $filter->operation = isset($param3) ? $param2 : '='; $filter->value = isset($param3) ? $param3 : $param2; - + $salt = substr(hash('sha256', env('APP_KEY')), 0, 16); - return self::whereRaw("CONVERT(AES_DECRYPT(FROM_bASE64(`{$filter->field}`), '{$salt}') USING utf8mb4) {$filter->operation} '{$filter->value}' "); + return self::whereRaw("CONVERT(AES_DECRYPT(FROM_bASE64(`{$filter->field}`), '{$salt}') USING utf8mb4) {$filter->operation} ? ", [$filter->value]); } public function orWhereEncrypted($param1, $param2, $param3 = null) @@ -26,9 +26,9 @@ public function orWhereEncrypted($param1, $param2, $param3 = null) $filter->field = $param1; $filter->operation = isset($param3) ? $param2 : '='; $filter->value = isset($param3) ? $param3 : $param2; - + $salt = substr(hash('sha256', env('APP_KEY')), 0, 16); - return self::orWhereRaw("CONVERT(AES_DECRYPT(FROM_bASE64(`{$filter->field}`), '{$salt}') USING utf8mb4) {$filter->operation} '{$filter->value}' "); + return self::orWhereRaw("CONVERT(AES_DECRYPT(FROM_bASE64(`{$filter->field}`), '{$salt}') USING utf8mb4) {$filter->operation} ? ", [$filter->value]); } } \ No newline at end of file diff --git a/tests/Unit/EncryptedTest.php b/tests/Unit/EncryptedTest.php index d00114e..a03a80e 100644 --- a/tests/Unit/EncryptedTest.php +++ b/tests/Unit/EncryptedTest.php @@ -1,15 +1,18 @@ assertEquals($user->email, $email); $this->assertEquals($user->name, $name); - } /** @@ -39,8 +41,8 @@ public function it_test_if_encryption_encoding_is_working() /** - * @test - */ + * @test + */ public function it_test_that_encrypt_model_commands_encrypt_existing_records() { TestUser::$enableEncryption = false; @@ -58,8 +60,8 @@ public function it_test_that_encrypt_model_commands_encrypt_existing_records() /** - * @test - */ + * @test + */ public function it_test_that_where_in_query_builder_is_working() { $email = 'example@email.com'; @@ -68,12 +70,11 @@ public function it_test_that_where_in_query_builder_is_working() $user = TestUser::whereEncrypted('email', '=', $email)->first(); $this->assertNotNull($user); - } /** - * @test - */ + * @test + */ public function it_assert_that_where_does_not_retrieve_a_user_with_incorrect_email() { $this->createUser(); @@ -93,7 +94,7 @@ public function it_test_that_validation_rule_exists_when_record_exists_is_workin $this->createUser('Jhon Doe', $email); - $validator = validator(compact('email'), ['email'=>'exists_encrypted:test_users,email']); + $validator = validator(compact('email'), ['email' => 'exists_encrypted:test_users,email']); $this->assertFalse($validator->fails()); } @@ -106,8 +107,8 @@ public function it_test_that_validation_rule_exists_when_record_does_not_exists_ $this->createUser(); $validator = validator( - ['email'=>'non_existing@email.com'], - ['email'=>'exists_encrypted:test_users,email'] + ['email' => 'non_existing@email.com'], + ['email' => 'exists_encrypted:test_users,email'] ); $this->assertTrue($validator->fails()); @@ -123,7 +124,7 @@ public function it_test_that_validation_rule_unique_when_record_exists_is_workin $this->createUser('Jhon Doe', $email); - $validator = validator(compact('email'), ['email'=>'unique_encrypted:test_users,email']); + $validator = validator(compact('email'), ['email' => 'unique_encrypted:test_users,email']); $this->assertTrue($validator->fails()); } @@ -136,11 +137,11 @@ public function it_test_that_validation_rule_unique_when_record_does_not_exists_ $this->createUser(); $validator = validator( - ['email'=>'non_existing@email.com'], - ['email'=>'unique_encrypted:test_users,email'] + ['email' => 'non_existing@email.com'], + ['email' => 'unique_encrypted:test_users,email'] ); - $this->assertFalse( $validator->fails() ); + $this->assertFalse($validator->fails()); } /** @@ -148,17 +149,17 @@ public function it_test_that_validation_rule_unique_when_record_does_not_exists_ */ public function it_tests_that_empty_values_are_encrypted() { - $user = $this->createUser(null,'example@email.com'); + $user = $this->createUser(null, 'example@email.com'); $raw = DB::table('test_users')->select('*')->first(); - + $this->assertNotEmpty($raw->name); $this->assertEmpty($user->name); } /** - * @test - */ + * @test + */ public function it_test_that_decrypt_command_is_working() { TestUser::$enableEncryption = false; @@ -185,4 +186,16 @@ public function it_test_that_where_query_is_working_with_non_lowercase_values() $this->assertNotNull(TestUser::whereEncrypted('email', '=', 'JhOn@DoE.cOm')->first()); } -} \ No newline at end of file + /** + * @test + */ + public function it_test_that_whereencrypted_can_handle_single_quote() + { + $email = "JhOn@DoE.cOm'"; + $name = "Single's"; + $this->createUser($name, $email); + $query = TestUser::whereEncrypted('email', $email)->orWhereEncrypted('name', $name)->first(); + + $this->assertNotNull($query); + } +}