Skip to content

Commit 7e9402d

Browse files
committed
added sanitize
1 parent 1ed7a77 commit 7e9402d

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Umbrellio\Postgres\Helpers;
6+
7+
class PostgresTextSanitizer
8+
{
9+
/**
10+
* Удаляет запрещённые/опасные символы из строки для PostgreSQL.
11+
*
12+
* Удаляются:
13+
* - \x00 (нулевой байт)
14+
* - управляющие символы ASCII от \x01 до \x1F, кроме \x09, \x0A, \x0D
15+
*/
16+
public static function sanitize(?string $input): ?string
17+
{
18+
if ($input === null) {
19+
return null;
20+
}
21+
22+
// Удаляем управляющие символы, кроме: табуляция (\x09), \n (\x0A), \r (\x0D)
23+
$input = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F]/u', '', $input);
24+
25+
return $input;
26+
}
27+
}

src/PostgresConnection.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PDO;
1212
use Umbrellio\Postgres\Extensions\AbstractExtension;
1313
use Umbrellio\Postgres\Extensions\Exceptions\ExtensionInvalidException;
14+
use Umbrellio\Postgres\Helpers\PostgresTextSanitizer;
1415
use Umbrellio\Postgres\Schema\Builder;
1516
use Umbrellio\Postgres\Schema\Grammars\PostgresGrammar;
1617
use Umbrellio\Postgres\Schema\Types\NumericType;
@@ -98,6 +99,9 @@ public function prepareBindings(array $bindings)
9899
if ($value instanceof DateTimeInterface) {
99100
$bindings[$key] = $value->format($grammar->getDateFormat());
100101
}
102+
if (is_string($value)) {
103+
$bindings[$key] = PostgresTextSanitizer::sanitize($value);
104+
}
101105
}
102106

103107
return $bindings;

0 commit comments

Comments
 (0)