Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent DB escaping functions from affecting non-sql taints #9019

Merged
merged 2 commits into from
Dec 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions stubs/CoreGenericClasses.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,36 @@ final class WeakMap implements ArrayAccess, Countable, IteratorAggregate, Traver
public function offsetUnset($offset) {}
}

class mysqli
{
/**
* @psalm-pure
*
* @psalm-taint-escape sql
* @psalm-flow ($string) -> return
*/
function escape_string($string) {}

/**
* @psalm-pure
*
* @psalm-taint-escape sql
* @psalm-flow ($string) -> return
*/
function real_escape_string($string) {}
}

class SQLite3
{
/**
* @psalm-pure
*
* @psalm-taint-escape sql
* @psalm-flow ($string) -> return
*/
static function escapeString($string) {}
}


#[Attribute(Attribute::TARGET_METHOD)]
final class ReturnTypeWillChange
Expand Down
64 changes: 64 additions & 0 deletions stubs/CoreGenericFunctions.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -1492,3 +1492,67 @@ function mb_convert_encoding(array|string $string, string $to_encoding, array|st
* @psalm-suppress ReferenceConstraintViolation
*/
function stream_select(null|array &$read, null|array &$write, null|array &$except, null|int $seconds, null|int $microseconds = null) : bool|int {}

/**
* @psalm-pure
*
* @psalm-taint-escape sql
* @psalm-flow ($string) -> return
*/
function mysqli_escape_string($string) {}

/**
* @psalm-pure
*
* @psalm-taint-escape sql
* @psalm-flow ($string) -> return
*/
function mysqli_real_escape_string($string) {}

/**
* @psalm-pure
*
* @psalm-taint-escape sql
* @psalm-flow ($string) -> return
*/
function db2_escape_string($string) {}

/**
* @psalm-pure
*
* @psalm-taint-escape sql
* @psalm-flow ($string) -> return
*/
function cubrid_real_escape_string($string) {}

/**
* @psalm-pure
*
* @psalm-taint-escape sql
* @psalm-flow ($string1, $string2) -> return
*/
function pg_escape_bytea($string1, $string2 = null) {}

/**
* @psalm-pure
*
* @psalm-taint-escape sql
* @psalm-flow ($string1, $string2) -> return
*/
function pg_escape_identifier($string1, $string2 = null) {}

/**
* @psalm-pure
*
* @psalm-taint-escape sql
* @psalm-flow ($string1, $string2) -> return
*/
function pg_escape_literal($string1, $string2 = null) {}

/**
* @psalm-pure
*
* @psalm-taint-escape sql
* @psalm-flow ($string1, $string2) -> return
*/
function pg_escape_string($string1, $string2 = null) {}
88 changes: 88 additions & 0 deletions tests/TaintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,17 @@ function bar(array $arr): void {
echo urlencode($_GET["bad"]);
',
],
'mysqliEscapeFunctions' => [
'code' => '<?php
$mysqli = new mysqli();

$a = $mysqli->escape_string($_GET["a"]);
$b = mysqli_escape_string($_GET["b"]);
$c = $mysqli->real_escape_string($_GET["c"]);
$d = mysqli_real_escape_string($_GET["d"]);

$mysqli->query("$a$b$c$d");',
],
];
}

Expand Down Expand Up @@ -2389,6 +2400,83 @@ public static function getPrevious(string $s): string {
',
'error_message' => 'TaintedShell',
],
'assertMysqliOnlyEscapesSqlTaints1' => [
'code' => '<?php
$mysqli = new mysqli();
echo $mysqli->escape_string($_GET["a"]);',
'error_message' => 'TaintedHtml',
],
'assertMysqliOnlyEscapesSqlTaints2' => [
'code' => '<?php
$mysqli = new mysqli();
echo $mysqli->real_escape_string($_GET["a"]);',
'error_message' => 'TaintedHtml',
],
'assertMysqliOnlyEscapesSqlTaints3' => [
'code' => '<?php
echo mysqli_escape_string($_GET["a"]);',
'error_message' => 'TaintedHtml',
],
'assertMysqliOnlyEscapesSqlTaints4' => [
'code' => '<?php
echo mysqli_real_escape_string($_GET["a"]);',
'error_message' => 'TaintedHtml',
],
'assertDb2OnlyEscapesSqlTaints' => [
'code' => '<?php
echo db2_escape_string($_GET["a"]);',
'error_message' => 'TaintedHtml',
],
'assertCubridOnlyEscapesSqlTaints' => [
'code' => '<?php
echo cubrid_real_escape_string($_GET["a"]);',
'error_message' => 'TaintedHtml',
],
'assertSQLiteOnlyEscapesSqlTaints' => [
'code' => '<?php
echo SQLite3::escapeString($_GET["a"]);',
'error_message' => 'TaintedHtml',
],
'assertPGOnlyEscapesSqlTaints1' => [
'code' => '<?php
echo pg_escape_bytea($_GET["a"]);',
'error_message' => 'TaintedHtml',
],
'assertPGOnlyEscapesSqlTaints2' => [
'code' => '<?php
echo pg_escape_bytea($conn, $_GET["a"]);',
'error_message' => 'TaintedHtml',
],
'assertPGOnlyEscapesSqlTaints3' => [
'code' => '<?php
echo pg_escape_identifier($_GET["a"]);',
'error_message' => 'TaintedHtml',
],
'assertPGOnlyEscapesSqlTaints4' => [
'code' => '<?php
echo pg_escape_identifier($conn, $_GET["a"]);',
'error_message' => 'TaintedHtml',
],
'assertPGOnlyEscapesSqlTaints5' => [
'code' => '<?php
echo pg_escape_literal($_GET["a"]);',
'error_message' => 'TaintedHtml',
],
'assertPGOnlyEscapesSqlTaints6' => [
'code' => '<?php
echo pg_escape_literal($conn, $_GET["a"]);',
'error_message' => 'TaintedHtml',
],
'assertPGOnlyEscapesSqlTaints7' => [
'code' => '<?php
echo pg_escape_string($_GET["a"]);',
'error_message' => 'TaintedHtml',
],
'assertPGOnlyEscapesSqlTaints8' => [
'code' => '<?php
echo pg_escape_string($conn, $_GET["a"]);',
'error_message' => 'TaintedHtml',
],
];
}

Expand Down