Commit 71838159 authored by catch's avatar catch

Issue #2252033 by olli, dawehner: Don't serialize database connection info

parent 1ee1495c
......@@ -1273,8 +1273,9 @@ public function quote($string, $parameter_type = \PDO::PARAM_STR) {
*/
public function serialize() {
$connection = clone $this;
// Don't serialize the PDO connection and other lazy-instantiated members.
unset($connection->connection, $connection->schema, $connection->driverClasses);
// Don't serialize the PDO connection as well as everything else which
// depends on settings.php.
unset($connection->connection, $connection->connectionOptions, $connection->schema, $connection->prefixes, $connection->prefixReplace, $connection->driverClasses);
return serialize(get_object_vars($connection));
}
......@@ -1286,6 +1287,8 @@ public function unserialize($serialized) {
foreach ($data as $key => $value) {
$this->{$key} = $value;
}
$this->connectionOptions = Database::getConnectionInfo($this->key)[$this->target];
// Re-establish the PDO connection using the original options.
$this->connection = static::open($this->connectionOptions);
......@@ -1293,6 +1296,8 @@ public function unserialize($serialized) {
if (!empty($this->statementClass)) {
$this->connection->setAttribute(\PDO::ATTR_STATEMENT_CLASS, array($this->statementClass, array($this)));
}
$this->setPrefix(isset($this->connectionOptions['prefix']) ? $this->connectionOptions['prefix'] : '');
}
}
......@@ -7,7 +7,9 @@
namespace Drupal\system\Tests\Database;
use Doctrine\Common\Reflection\StaticReflectionProperty;
use Drupal\Core\Database\Database;
use Drupal\Core\Site\Settings;
use Drupal\simpletest\KernelTestBase;
/**
......@@ -268,6 +270,32 @@ public function testConnectionSerialization() {
}
}
// By using "key", we ensure that its not a key used in the serialized PHP.
$not_serialized_properties = ['"connection"', '"connectionOptions"', '"schema"', '"prefixes"', '"prefixReplace"', '"driverClasses"'];
foreach ($not_serialized_properties as $property) {
$this->assertIdentical(FALSE, strpos($serialized, $property));
}
// Serialize the DB connection again, but this time change the connection
// information under the hood.
$serialized = serialize($db);
$db_connection_info = Database::getAllConnectionInfo();
// Use reflection to empty out $databaseInfo.
$reflection_class = new \ReflectionClass('Drupal\Core\Database\Database');
$database_info_reflection = $reflection_class->getProperty('databaseInfo');
$database_info_reflection->setAccessible(TRUE);
$database_info_reflection->setValue(NULL, []);
// Setup a different DB connection which should be picked up after the
// unserialize.
$db_connection_info['default']['default']['extra'] = 'value';
Database::setMultipleConnectionInfo($db_connection_info);
/** @var \Drupal\Core\Database\Connection $db */
$db = unserialize($serialized);
$this->assertEqual($db->getConnectionOptions()['extra'], 'value');
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment