Skip to content
This repository was archived by the owner on Mar 2, 2023. It is now read-only.

Commit 118a164

Browse files
author
Ion Ghițun
committed
master - fixed sql injection
1 parent c2f1229 commit 118a164

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

src/Models/BaseModel.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
use Illuminate\Database\Eloquent\Builder;
77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Support\Arr;
9+
use Illuminate\Support\Facades\DB;
10+
use function array_key_exists;
11+
use function array_slice;
12+
use function chr;
13+
use function count;
14+
use function in_array;
15+
use function strlen;
916

1017
/**
1118
* @method static Builder|self whereEncrypted($column, $value)
@@ -46,13 +53,13 @@ public function getAttribute($key)
4653
{
4754
$value = parent::getAttribute($key);
4855

49-
if (\array_key_exists($key, $this->relations) || method_exists($this, $key)) {
56+
if (array_key_exists($key, $this->relations) || method_exists($this, $key)) {
5057
return $value;
5158
}
5259

5360
$value = parent::getAttribute($key);
5461

55-
if (\in_array($key, $this->encrypted, true)) {
62+
if (in_array($key, $this->encrypted, true)) {
5663
return $this->aesDecrypt($value);
5764
}
5865

@@ -86,9 +93,9 @@ public function aesDecrypt($val, string $cypher = 'aes-128-ecb', bool $mySqlKey
8693
*/
8794
private function generateMysqlAesKey($key): string
8895
{
89-
$generatedKey = str_repeat(\chr(0), 16);
96+
$generatedKey = str_repeat(chr(0), 16);
9097

91-
for ($i = 0, $len = \strlen($key); $i < $len; $i++) {
98+
for ($i = 0, $len = strlen($key); $i < $len; $i++) {
9299
$generatedKey[$i % 16] = $generatedKey[$i % 16] ^ $key[$i];
93100
}
94101

@@ -105,7 +112,7 @@ private function generateMysqlAesKey($key): string
105112
*/
106113
public function setAttribute($key, $value)
107114
{
108-
if (\in_array($key, $this->encrypted, true)) {
115+
if (in_array($key, $this->encrypted, true)) {
109116
$value = $this->aesEncrypt($value);
110117
}
111118

@@ -158,7 +165,7 @@ public function attributesToArray(): array
158165
*/
159166
public function getOriginal($key = null, $default = null)
160167
{
161-
if (\in_array($key, $this->encrypted, true)) {
168+
if (in_array($key, $this->encrypted, true)) {
162169
return $this->aesDecrypt(Arr::get($this->original, $key, $default));
163170
}
164171

@@ -195,11 +202,11 @@ public function anonymize($locale = null): void
195202
$faker = Factory::create($locale ?? (getenv('FAKER_LOCALE') ?? Factory::DEFAULT_LOCALE));
196203

197204
foreach ($this->anonymizable as $field => $type) {
198-
if (\in_array($field, $this->attributes, true)) {
205+
if (in_array($field, $this->attributes, true)) {
199206
$method = $type[0];
200207

201-
if (\count($type) > 1) {
202-
$this->$field = $faker->$method(\array_slice($type, 1));
208+
if (count($type) > 1) {
209+
$this->$field = $faker->$method(array_slice($type, 1));
203210
} else {
204211
$this->$field = $faker->$method;
205212
}
@@ -218,7 +225,7 @@ public function anonymize($locale = null): void
218225
*/
219226
public function scopeWhereEncrypted($query, $column, $value)
220227
{
221-
return $query->whereRaw('AES_DECRYPT('.$column.', "'.getenv('ENCRYPTION_KEY').'") LIKE ? COLLATE utf8mb4_general_ci', [$value]);
228+
return $query->whereRaw('AES_DECRYPT('.$column.', "'.getenv('ENCRYPTION_KEY').'") LIKE '.DB::getPdo()->quote($value).' COLLATE utf8mb4_general_ci');
222229
}
223230

224231
/**
@@ -232,7 +239,7 @@ public function scopeWhereEncrypted($query, $column, $value)
232239
*/
233240
public function scopeWhereNotEncrypted($query, $column, $value)
234241
{
235-
return $query->whereRaw('AES_DECRYPT('.$column.', "'.getenv('ENCRYPTION_KEY').'") NOT LIKE ? COLLATE utf8mb4_general_ci', [$value]);
242+
return $query->whereRaw('AES_DECRYPT('.$column.', "'.getenv('ENCRYPTION_KEY').'") NOT LIKE '.DB::getPdo()->quote($value).' COLLATE utf8mb4_general_ci');
236243
}
237244

238245
/**
@@ -246,7 +253,7 @@ public function scopeWhereNotEncrypted($query, $column, $value)
246253
*/
247254
public function scopeOrWhereEncrypted($query, $column, $value)
248255
{
249-
return $query->orWhereRaw('AES_DECRYPT('.$column.', "'.getenv('ENCRYPTION_KEY').'") LIKE ? COLLATE utf8mb4_general_ci', [$value]);
256+
return $query->orWhereRaw('AES_DECRYPT('.$column.', "'.getenv('ENCRYPTION_KEY').'") LIKE '.DB::getPdo()->quote($value).' COLLATE utf8mb4_general_ci');
250257
}
251258

252259
/**
@@ -260,7 +267,7 @@ public function scopeOrWhereEncrypted($query, $column, $value)
260267
*/
261268
public function scopeOrWhereNotEncrypted($query, $column, $value)
262269
{
263-
return $query->orWhereRaw('AES_DECRYPT('.$column.', "'.getenv('ENCRYPTION_KEY').'") NOT LIKE ? COLLATE utf8mb4_general_ci', [$value]);
270+
return $query->orWhereRaw('AES_DECRYPT('.$column.', "'.getenv('ENCRYPTION_KEY').'") NOT LIKE '.DB::getPdo()->quote($value).' COLLATE utf8mb4_general_ci');
264271
}
265272

266273
/**

src/MysqlEncryptionServiceProvider.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ private function addValidators(): void
3636
$field = isset($parameters[1]) ? $parameters[1] : $attribute;
3737
$ignore = isset($parameters[2]) ? $parameters[2] : null;
3838

39-
$items = DB::select("SELECT count(*) as aggregate FROM `".$parameters[0]."` WHERE AES_DECRYPT(`".$field."`, '".getenv("ENCRYPTION_KEY")."') LIKE ? COLLATE utf8mb4_general_ci".($ignore ? " AND id != ".$ignore : ''),
40-
[$value]);
39+
$items = DB::select("SELECT count(*) as aggregate FROM `".$parameters[0]."` WHERE AES_DECRYPT(`".$field."`, '".getenv("ENCRYPTION_KEY")."') LIKE '".DB::getPdo()->quote($value)."' COLLATE utf8mb4_general_ci".($ignore ? " AND id != ".$ignore : ''));
4140

4241
return $items[0]->aggregate === 0;
4342
});
@@ -52,8 +51,7 @@ private function addValidators(): void
5251

5352
$field = isset($parameters[1]) ? $parameters[1] : $attribute;
5453

55-
$items = DB::select("SELECT count(*) as aggregate FROM `".$parameters[0]."` WHERE AES_DECRYPT(`".$field."`, '".getenv("ENCRYPTION_KEY")."') LIKE ? COLLATE utf8mb4_general_ci",
56-
[$value]);
54+
$items = DB::select("SELECT count(*) as aggregate FROM `".$parameters[0]."` WHERE AES_DECRYPT(`".$field."`, '".getenv("ENCRYPTION_KEY")."') LIKE '".DB::getPdo()->quote($value)."' COLLATE utf8mb4_general_ci");
5755

5856
return $items[0]->aggregate > 0;
5957
});

0 commit comments

Comments
 (0)