Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Transport/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ private function convertParamValue(mixed $value): string
$arrayValues = [];
foreach ($value as $val) {
if (is_string($val)) {
$escaped = str_replace(['\\', "'"], ['\\\\', "\\'"], $val);
$escaped = $this->convertParamValue($val);
$arrayValues[] = sprintf("'%s'", $escaped);
continue;
}
Expand All @@ -865,6 +865,9 @@ private function convertParamValue(mixed $value): string
if ($value === null) {
return '\\N';
}
if (is_string($value)) {
return str_replace(['\\', "'"], ['\\\\', "\\'"], $value);
}
return (string) $value;
}

Expand Down
56 changes: 56 additions & 0 deletions tests/NativeParamsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,62 @@ public function testSelectWithStringArrayParamInjectionAttempt(): void
$this->assertEquals(["x','injected','y"], $result->fetchOne('arr'));
}

public function testSelectWithStringParamTrailingBackslash(): void
{
$result = $this->client->selectWithParams(
'SELECT {search:String} as search',
['search' => 'hello\\']
);

$this->assertEquals('hello\\', $result->fetchOne('search'));
}

public function testSelectWithStringParamEmbeddedBackslash(): void
{
$result = $this->client->selectWithParams(
'SELECT {search:String} as search',
['search' => 'a\\b\\c']
);

$this->assertEquals('a\\b\\c', $result->fetchOne('search'));
}

public function testSelectWithStringParamSingleQuote(): void
{
$result = $this->client->selectWithParams(
'SELECT {name:String} as name',
['name' => "O'Brien"]
);

$this->assertEquals("O'Brien", $result->fetchOne('name'));
}

public function testSelectWithStringParamInjectionAttempt(): void
{
$result = $this->client->selectWithParams(
'SELECT {val:String} as val',
['val' => "x','injected','y"]
);

$this->assertEquals("x','injected','y", $result->fetchOne('val'));
}

public function testWriteWithStringParamTrailingBackslash(): void
{
$this->client->write("DROP TABLE IF EXISTS string_param_escape_test");
$this->client->write('CREATE TABLE IF NOT EXISTS string_param_escape_test (id UInt32, val String) ENGINE = Memory');

$this->client->writeWithParams(
'INSERT INTO string_param_escape_test VALUES ({id:UInt32}, {val:String})',
['id' => 1, 'val' => 'hello\\']
);

$st = $this->client->select('SELECT val FROM string_param_escape_test WHERE id = 1');
$this->assertEquals('hello\\', $st->fetchOne('val'));

$this->client->write("DROP TABLE IF EXISTS string_param_escape_test");
}

public function testSelectWithPerQuerySettings(): void
{
$result = $this->client->selectWithParams(
Expand Down
Loading