Skip to content

Commit

Permalink
Suggestion of 2 new functions for SQL-safe CSV-values-handling for IN…
Browse files Browse the repository at this point in the history
… (....) usages

Instead of using:

`" AND columnname IN " . implode( $array )`

and have escaping outside the query (and hard to security-review), I suggest:

`" AND columname IN (" . $db->safeArrayOfIntegersToCSV($array) . ")"`

Which makes security-reviews much easier (and automatable). This function and its generalized use would have avoided vulnerability in joomla#42 joomla/cms-security#42 (comment)
  • Loading branch information
beat committed Dec 16, 2015
1 parent 952f2a6 commit 8a8f987
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions libraries/joomla/database/driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1850,6 +1850,48 @@ public function quoteName($name, $as = null)
}
}

/**
* Sanitizes an array of (int) to a safe CSV-formatted string
*
* @param array $array Array to sanitize out
*
* @return string string with safe integers, e.g. '1, 2, 3'
*
* @since 3.4.7
*/
public function safeArrayOfIntegersToCSV($array)
{
return implode(', ', array_map(
function ($v)
{
return (int) $v;
},
$array
)
);
}

/**
* Sanitizes an array of (string) to a safe CSV-formatted string of escaped and quoted values
*
* @param string[] $array Array to sanitize out
*
* @return string string with safe database-quoted strings e.g. "'A', 'B', 'C'""
*
* @since 3.4.7
*/
public function safeArrayOfStringsToCSV($array)
{
return implode(', ', array_map(
function ($v)
{
return $this->quote($v);
},
$array
)
);
}

/**
* Quote strings coming from quoteName call.
*
Expand Down

0 comments on commit 8a8f987

Please sign in to comment.