Skip to content

Commit

Permalink
Merge pull request #11 from kirame09/hotfix/single-quote
Browse files Browse the repository at this point in the history
Hotfix/single quote
  • Loading branch information
elgibor-solution authored Aug 30, 2021
2 parents 0426ce3 + d8e772f commit 1ab554a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -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;}}}
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;}}}
8 changes: 4 additions & 4 deletions src/Builders/EncryptionEloquentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]);
}
}
59 changes: 36 additions & 23 deletions tests/Unit/EncryptedTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<?php

namespace ESolution\DBEncryption\Tests;

use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Testing\RefreshDatabase;

class EncryptedTest extends TestCase {
class EncryptedTest extends TestCase
{

use RefreshDatabase;

/**
* @test
*/
* @test
*/
public function it_test_if_encryption_decoding_is_working()
{
$name = 'Jhon';
Expand All @@ -19,7 +22,6 @@ public function it_test_if_encryption_decoding_is_working()

$this->assertEquals($user->email, $email);
$this->assertEquals($user->name, $name);

}

/**
Expand All @@ -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;
Expand All @@ -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 = '[email protected]';
Expand All @@ -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();
Expand All @@ -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());
}
Expand All @@ -106,8 +107,8 @@ public function it_test_that_validation_rule_exists_when_record_does_not_exists_
$this->createUser();

$validator = validator(
['email'=>'[email protected]'],
['email'=>'exists_encrypted:test_users,email']
['email' => '[email protected]'],
['email' => 'exists_encrypted:test_users,email']
);

$this->assertTrue($validator->fails());
Expand All @@ -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());
}
Expand All @@ -136,29 +137,29 @@ public function it_test_that_validation_rule_unique_when_record_does_not_exists_
$this->createUser();

$validator = validator(
['email'=>'[email protected]'],
['email'=>'unique_encrypted:test_users,email']
['email' => '[email protected]'],
['email' => 'unique_encrypted:test_users,email']
);

$this->assertFalse( $validator->fails() );
$this->assertFalse($validator->fails());
}

/**
* @test
*/
public function it_tests_that_empty_values_are_encrypted()
{
$user = $this->createUser(null,'[email protected]');
$user = $this->createUser(null, '[email protected]');
$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;
Expand All @@ -185,4 +186,16 @@ public function it_test_that_where_query_is_working_with_non_lowercase_values()
$this->assertNotNull(TestUser::whereEncrypted('email', '=', '[email protected]')->first());
}

}
/**
* @test
*/
public function it_test_that_whereencrypted_can_handle_single_quote()
{
$email = "[email protected]'";
$name = "Single's";
$this->createUser($name, $email);
$query = TestUser::whereEncrypted('email', $email)->orWhereEncrypted('name', $name)->first();

$this->assertNotNull($query);
}
}

0 comments on commit 1ab554a

Please sign in to comment.