Skip to content

Commit

Permalink
Add a new quoteBinary method (#23213)
Browse files Browse the repository at this point in the history
  • Loading branch information
csthomas authored and HLeithner committed Sep 17, 2019
1 parent 6c90418 commit 7d8b1b8
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 8 deletions.
15 changes: 15 additions & 0 deletions libraries/joomla/database/driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,21 @@ public function quote($text, $escape = true)
}
}

/**
* Quotes a binary string to database requirements for use in database queries.
*
* @param mixed $data A binary string to quote.
*
* @return string The binary quoted input string.
*
* @since __DEPLOY_VERSION__
*/
public function quoteBinary($data)
{
// SQL standard syntax for hexadecimal literals
return "X'" . bin2hex($data) . "'";
}

/**
* Wrap an SQL statement identifier name such as column, table or database names in quotes to prevent injection
* risks and reserved word conflicts.
Expand Down
14 changes: 14 additions & 0 deletions libraries/joomla/database/driver/pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -987,4 +987,18 @@ public function updateObject($table, &$object, $key, $nulls = false)

return $this->execute();
}

/**
* Quotes a binary string to database requirements for use in database queries.
*
* @param mixed $data A binary string to quote.
*
* @return string The binary quoted input string.
*
* @since __DEPLOY_VERSION__
*/
public function quoteBinary($data)
{
return "decode('" . bin2hex($data) . "', 'hex')";
}
}
14 changes: 14 additions & 0 deletions libraries/joomla/database/driver/postgresql.php
Original file line number Diff line number Diff line change
Expand Up @@ -1610,4 +1610,18 @@ protected function getCreateDatabaseQuery($options, $utf)
{
return 'CREATE DATABASE ' . $this->quoteName($options->db_name);
}

/**
* Quotes a binary string to database requirements for use in database queries.
*
* @param mixed $data A binary string to quote.
*
* @return string The binary quoted input string.
*
* @since __DEPLOY_VERSION__
*/
public function quoteBinary($data)
{
return "decode('" . bin2hex($data) . "', 'hex')";
}
}
15 changes: 15 additions & 0 deletions libraries/joomla/database/driver/sqlsrv.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,21 @@ public function quote($text, $escape = true)
return 'N\'' . ($escape ? $this->escape($text) : $text) . '\'';
}

/**
* Quotes a binary string to database requirements for use in database queries.
*
* @param mixed $data A binary string to quote.
*
* @return string The binary quoted input string.
*
* @since __DEPLOY_VERSION__
*/
public function quoteBinary($data)
{
// ODBC syntax for hexadecimal literals
return '0x' . bin2hex($data);
}

/**
* Determines if the connection to the server is active.
*
Expand Down
6 changes: 3 additions & 3 deletions libraries/joomla/session/storage/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function read($id)
$query = $db->getQuery(true)
->select($db->quoteName('data'))
->from($db->quoteName('#__session'))
->where($db->quoteName('session_id') . ' = ' . $db->quote($id));
->where($db->quoteName('session_id') . ' = ' . $db->quoteBinary($id));

$db->setQuery($query);

Expand Down Expand Up @@ -77,7 +77,7 @@ public function write($id, $data)
->update($db->quoteName('#__session'))
->set($db->quoteName('data') . ' = ' . $db->quote($data))
->set($db->quoteName('time') . ' = ' . time())
->where($db->quoteName('session_id') . ' = ' . $db->quote($id));
->where($db->quoteName('session_id') . ' = ' . $db->quoteBinary($id));

// Try to update the session data in the database table.
$db->setQuery($query);
Expand Down Expand Up @@ -114,7 +114,7 @@ public function destroy($id)
{
$query = $db->getQuery(true)
->delete($db->quoteName('#__session'))
->where($db->quoteName('session_id') . ' = ' . $db->quote($id));
->where($db->quoteName('session_id') . ' = ' . $db->quoteBinary($id));

// Remove a session from the database.
$db->setQuery($query);
Expand Down
2 changes: 1 addition & 1 deletion libraries/legacy/application/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ public function checkSession()
$query = $db->getQuery(true)
->select($db->quoteName('session_id'))
->from($db->quoteName('#__session'))
->where($db->quoteName('session_id') . ' = ' . $db->quote($session->getId()));
->where($db->quoteName('session_id') . ' = ' . $db->quoteBinary($session->getId()));

$db->setQuery($query, 0, 1);
$exists = $db->loadResult();
Expand Down
4 changes: 2 additions & 2 deletions libraries/src/Session/MetadataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function createRecordIfNonExisting(Session $session, User $user)
$query = $this->db->getQuery(true)
->select($this->db->quoteName('session_id'))
->from($this->db->quoteName('#__session'))
->where($this->db->quoteName('session_id') . ' = ' . $this->db->quote($session->getId()));
->where($this->db->quoteName('session_id') . ' = ' . $this->db->quoteBinary($session->getId()));

$this->db->setQuery($query, 0, 1);
$exists = $this->db->loadResult();
Expand All @@ -92,7 +92,7 @@ public function createRecordIfNonExisting(Session $session, User $user)
);

$values = array(
$this->db->quote($session->getId()),
$this->db->quoteBinary($session->getId()),
(int) $user->guest,
(int) $time,
(int) $user->id,
Expand Down
2 changes: 1 addition & 1 deletion plugins/privacy/user/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function onPrivacyRemoveData(PrivacyTableRequest $request, JUser $user =
foreach ($sessionIds as $sessionId)
{
$store->destroy($sessionId);
$quotedIds[] = $this->db->quote($sessionId);
$quotedIds[] = $this->db->quoteBinary($sessionId);
}

$this->db->setQuery(
Expand Down
2 changes: 1 addition & 1 deletion plugins/user/joomla/joomla.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function onUserLogin($user, $options = array())
// Purge the old session
$query = $this->db->getQuery(true)
->delete('#__session')
->where($this->db->quoteName('session_id') . ' = ' . $this->db->quote($oldSessionId));
->where($this->db->quoteName('session_id') . ' = ' . $this->db->quoteBinary($oldSessionId));

try
{
Expand Down

0 comments on commit 7d8b1b8

Please sign in to comment.