Skip to content
Snippets Groups Projects

Issue #2631220: Apply changes from MR 2210 to 11.x

Closed Kent Richards requested to merge issue/drupal-2631220:2631220-session-hardening-11.x into 11.x
Files
8
@@ -8,12 +8,12 @@
@@ -8,12 +8,12 @@
use Drupal\Core\Database\DatabaseException;
use Drupal\Core\Database\DatabaseException;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\AbstractSessionHandler;
/**
/**
* Default session handler.
* Default session handler.
*/
*/
class SessionHandler extends AbstractProxy implements \SessionHandlerInterface {
class SessionHandler extends AbstractSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface {
use DependencySerializationTrait;
use DependencySerializationTrait;
@@ -44,13 +44,13 @@ public function open(string $save_path, string $name): bool {
@@ -44,13 +44,13 @@ public function open(string $save_path, string $name): bool {
/**
/**
* {@inheritdoc}
* {@inheritdoc}
*/
*/
public function read(#[\SensitiveParameter] string $sid): string|false {
public function doRead(#[\SensitiveParameter] string $sessionId): string {
$data = '';
$data = '';
if (!empty($sid)) {
if (!empty($sessionId)) {
try {
try {
// Read the session data from the database.
// Read the session data from the database.
$query = $this->connection
$query = $this->connection
->queryRange('SELECT [session] FROM {sessions} WHERE [sid] = :sid', 0, 1, [':sid' => Crypt::hashBase64($sid)]);
->queryRange('SELECT [session] FROM {sessions} WHERE [sid] = :sid', 0, 1, [':sid' => Crypt::hashBase64($sessionId)]);
$data = (string) $query->fetchField();
$data = (string) $query->fetchField();
}
}
// Swallow the error if the table hasn't been created yet.
// Swallow the error if the table hasn't been created yet.
@@ -63,18 +63,18 @@ public function read(#[\SensitiveParameter] string $sid): string|false {
@@ -63,18 +63,18 @@ public function read(#[\SensitiveParameter] string $sid): string|false {
/**
/**
* {@inheritdoc}
* {@inheritdoc}
*/
*/
public function write(#[\SensitiveParameter] string $sid, string $value): bool {
public function doWrite(#[\SensitiveParameter] string $sessionId, string $data): bool {
$try_again = FALSE;
$try_again = FALSE;
$request = $this->requestStack->getCurrentRequest();
$request = $this->requestStack->getCurrentRequest();
$fields = [
$fields = [
'uid' => $request->getSession()->get('uid', 0),
'uid' => $request->getSession()->get('uid', 0),
'hostname' => $request->getClientIP(),
'hostname' => $request->getClientIP(),
'session' => $value,
'session' => $data,
'timestamp' => $this->time->getRequestTime(),
'timestamp' => $this->time->getRequestTime(),
];
];
$doWrite = fn() =>
$doWrite = fn() =>
$this->connection->merge('sessions')
$this->connection->merge('sessions')
->keys(['sid' => Crypt::hashBase64($sid)])
->keys(['sid' => Crypt::hashBase64($sessionId)])
->fields($fields)
->fields($fields)
->execute();
->execute();
try {
try {
@@ -106,11 +106,18 @@ public function close(): bool {
@@ -106,11 +106,18 @@ public function close(): bool {
/**
/**
* {@inheritdoc}
* {@inheritdoc}
*/
*/
public function destroy(#[\SensitiveParameter] string $sid): bool {
public function destroy(#[\SensitiveParameter] string $sessionId): bool {
 
return $this->doDestroy($sessionId);
 
}
 
 
/**
 
* {@inheritdoc}
 
*/
 
protected function doDestroy(#[\SensitiveParameter] string $sessionId): bool {
try {
try {
// Delete session data.
// Delete session data.
$this->connection->delete('sessions')
$this->connection->delete('sessions')
->condition('sid', Crypt::hashBase64($sid))
->condition('sid', Crypt::hashBase64($sessionId))
->execute();
->execute();
}
}
// Swallow the error if the table hasn't been created yet.
// Swallow the error if the table hasn't been created yet.
@@ -140,6 +147,16 @@ public function gc(int $lifetime): int|false {
@@ -140,6 +147,16 @@ public function gc(int $lifetime): int|false {
return FALSE;
return FALSE;
}
}
 
/**
 
* {@inheritdoc}
 
*/
 
public function updateTimestamp(#[\SensitiveParameter] string $sessionId, string $data): bool {
 
// This function is intentionally a no-op. Drupal manages session expiry in
 
// the MetadataBag, and the timestamp should not be updated here.
 
// @see \Drupal\Core\Session\MetadataBag::__construct()
 
return TRUE;
 
}
 
/**
/**
* Defines the schema for the session table.
* Defines the schema for the session table.
*
*
Loading