Skip to content

Commit 1ab554a

Browse files
Merge pull request #11 from kirame09/hotfix/single-quote
Hotfix/single quote
2 parents 0426ce3 + d8e772f commit 1ab554a

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

.phpunit.result.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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;}}}
1+
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;}}}

src/Builders/EncryptionEloquentBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public function whereEncrypted($param1, $param2, $param3 = null)
1414
$filter->field = $param1;
1515
$filter->operation = isset($param3) ? $param2 : '=';
1616
$filter->value = isset($param3) ? $param3 : $param2;
17-
17+
1818
$salt = substr(hash('sha256', env('APP_KEY')), 0, 16);
1919

20-
return self::whereRaw("CONVERT(AES_DECRYPT(FROM_bASE64(`{$filter->field}`), '{$salt}') USING utf8mb4) {$filter->operation} '{$filter->value}' ");
20+
return self::whereRaw("CONVERT(AES_DECRYPT(FROM_bASE64(`{$filter->field}`), '{$salt}') USING utf8mb4) {$filter->operation} ? ", [$filter->value]);
2121
}
2222

2323
public function orWhereEncrypted($param1, $param2, $param3 = null)
@@ -26,9 +26,9 @@ public function orWhereEncrypted($param1, $param2, $param3 = null)
2626
$filter->field = $param1;
2727
$filter->operation = isset($param3) ? $param2 : '=';
2828
$filter->value = isset($param3) ? $param3 : $param2;
29-
29+
3030
$salt = substr(hash('sha256', env('APP_KEY')), 0, 16);
3131

32-
return self::orWhereRaw("CONVERT(AES_DECRYPT(FROM_bASE64(`{$filter->field}`), '{$salt}') USING utf8mb4) {$filter->operation} '{$filter->value}' ");
32+
return self::orWhereRaw("CONVERT(AES_DECRYPT(FROM_bASE64(`{$filter->field}`), '{$salt}') USING utf8mb4) {$filter->operation} ? ", [$filter->value]);
3333
}
3434
}

tests/Unit/EncryptedTest.php

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
<?php
2+
23
namespace ESolution\DBEncryption\Tests;
4+
35
use Illuminate\Support\Facades\DB;
46
use Illuminate\Foundation\Testing\RefreshDatabase;
57

6-
class EncryptedTest extends TestCase {
8+
class EncryptedTest extends TestCase
9+
{
710

811
use RefreshDatabase;
912

1013
/**
11-
* @test
12-
*/
14+
* @test
15+
*/
1316
public function it_test_if_encryption_decoding_is_working()
1417
{
1518
$name = 'Jhon';
@@ -19,7 +22,6 @@ public function it_test_if_encryption_decoding_is_working()
1922

2023
$this->assertEquals($user->email, $email);
2124
$this->assertEquals($user->name, $name);
22-
2325
}
2426

2527
/**
@@ -39,8 +41,8 @@ public function it_test_if_encryption_encoding_is_working()
3941

4042

4143
/**
42-
* @test
43-
*/
44+
* @test
45+
*/
4446
public function it_test_that_encrypt_model_commands_encrypt_existing_records()
4547
{
4648
TestUser::$enableEncryption = false;
@@ -58,8 +60,8 @@ public function it_test_that_encrypt_model_commands_encrypt_existing_records()
5860

5961

6062
/**
61-
* @test
62-
*/
63+
* @test
64+
*/
6365
public function it_test_that_where_in_query_builder_is_working()
6466
{
6567
$email = '[email protected]';
@@ -68,12 +70,11 @@ public function it_test_that_where_in_query_builder_is_working()
6870
$user = TestUser::whereEncrypted('email', '=', $email)->first();
6971

7072
$this->assertNotNull($user);
71-
7273
}
7374

7475
/**
75-
* @test
76-
*/
76+
* @test
77+
*/
7778
public function it_assert_that_where_does_not_retrieve_a_user_with_incorrect_email()
7879
{
7980
$this->createUser();
@@ -93,7 +94,7 @@ public function it_test_that_validation_rule_exists_when_record_exists_is_workin
9394

9495
$this->createUser('Jhon Doe', $email);
9596

96-
$validator = validator(compact('email'), ['email'=>'exists_encrypted:test_users,email']);
97+
$validator = validator(compact('email'), ['email' => 'exists_encrypted:test_users,email']);
9798

9899
$this->assertFalse($validator->fails());
99100
}
@@ -106,8 +107,8 @@ public function it_test_that_validation_rule_exists_when_record_does_not_exists_
106107
$this->createUser();
107108

108109
$validator = validator(
109-
['email'=>'[email protected]'],
110-
['email'=>'exists_encrypted:test_users,email']
110+
['email' => '[email protected]'],
111+
['email' => 'exists_encrypted:test_users,email']
111112
);
112113

113114
$this->assertTrue($validator->fails());
@@ -123,7 +124,7 @@ public function it_test_that_validation_rule_unique_when_record_exists_is_workin
123124

124125
$this->createUser('Jhon Doe', $email);
125126

126-
$validator = validator(compact('email'), ['email'=>'unique_encrypted:test_users,email']);
127+
$validator = validator(compact('email'), ['email' => 'unique_encrypted:test_users,email']);
127128

128129
$this->assertTrue($validator->fails());
129130
}
@@ -136,29 +137,29 @@ public function it_test_that_validation_rule_unique_when_record_does_not_exists_
136137
$this->createUser();
137138

138139
$validator = validator(
139-
['email'=>'[email protected]'],
140-
['email'=>'unique_encrypted:test_users,email']
140+
['email' => '[email protected]'],
141+
['email' => 'unique_encrypted:test_users,email']
141142
);
142143

143-
$this->assertFalse( $validator->fails() );
144+
$this->assertFalse($validator->fails());
144145
}
145146

146147
/**
147148
* @test
148149
*/
149150
public function it_tests_that_empty_values_are_encrypted()
150151
{
151-
$user = $this->createUser(null,'[email protected]');
152+
$user = $this->createUser(null, '[email protected]');
152153
$raw = DB::table('test_users')->select('*')->first();
153-
154+
154155
$this->assertNotEmpty($raw->name);
155156
$this->assertEmpty($user->name);
156157
}
157158

158159

159160
/**
160-
* @test
161-
*/
161+
* @test
162+
*/
162163
public function it_test_that_decrypt_command_is_working()
163164
{
164165
TestUser::$enableEncryption = false;
@@ -185,4 +186,16 @@ public function it_test_that_where_query_is_working_with_non_lowercase_values()
185186
$this->assertNotNull(TestUser::whereEncrypted('email', '=', '[email protected]')->first());
186187
}
187188

188-
}
189+
/**
190+
* @test
191+
*/
192+
public function it_test_that_whereencrypted_can_handle_single_quote()
193+
{
194+
$email = "[email protected]'";
195+
$name = "Single's";
196+
$this->createUser($name, $email);
197+
$query = TestUser::whereEncrypted('email', $email)->orWhereEncrypted('name', $name)->first();
198+
199+
$this->assertNotNull($query);
200+
}
201+
}

0 commit comments

Comments
 (0)