Skip to content

Commit

Permalink
[10.x] Throw exception when trying to escape array for database conne…
Browse files Browse the repository at this point in the history
…ction (#48836)

* Throw error when trying to escape array

* Add tests to test escaping array throwing exception

* Update Connection.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
sidneyprins and taylorotwell authored Oct 30, 2023
1 parent 6f06ad4 commit 0aeecec
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,8 @@ public function escape($value, $binary = false)
return (string) $value;
} elseif (is_bool($value)) {
return $this->escapeBool($value);
} elseif (is_array($value)) {
throw new RuntimeException('The database connection does not support escaping arrays.');
} else {
if (str_contains($value, "\00")) {
throw new RuntimeException('Strings with null bytes cannot be escaped. Use the binary escape option.');
Expand Down
7 changes: 7 additions & 0 deletions tests/Integration/Database/MySql/EscapeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,11 @@ public function testEscapeStringNullByte()

$this->app['db']->escape("I am hiding a \00 byte");
}

public function testEscapeArray()
{
$this->expectException(RuntimeException::class);

$this->app['db']->escape(['a', 'b']);
}
}
7 changes: 7 additions & 0 deletions tests/Integration/Database/Postgres/EscapeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,11 @@ public function testEscapeStringNullByte()

$this->app['db']->escape("I am hiding a \00 byte");
}

public function testEscapeArray()
{
$this->expectException(RuntimeException::class);

$this->app['db']->escape(['a', 'b']);
}
}
7 changes: 7 additions & 0 deletions tests/Integration/Database/SqlServer/EscapeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ public function testEscapeStringNullByte()

$this->app['db']->escape("I am hiding a \00 byte");
}

public function testEscapeArray()
{
$this->expectException(RuntimeException::class);

$this->app['db']->escape(['a', 'b']);
}
}
7 changes: 7 additions & 0 deletions tests/Integration/Database/Sqlite/EscapeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,11 @@ public function testEscapeStringNullByte()

$this->app['db']->escape("I am hiding a \00 byte");
}

public function testEscapeArray()
{
$this->expectException(RuntimeException::class);

$this->app['db']->escape(['a', 'b']);
}
}

0 comments on commit 0aeecec

Please sign in to comment.