forked from silverstripe/silverstripe-dynamodb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX AWS SDK PHP DynamoDb StandardSessionConnection binary safety
fixes silverstripe#32
- Loading branch information
Showing
5 changed files
with
127 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace SilverStripe\DynamoDb; | ||
|
||
/** | ||
* DynamoDbClient extension that handles sessions as binary strings rather than textual content. | ||
* | ||
* Current AWS SDK PHP (<= 3.102.1) implementation passes session data as 'S'. | ||
* This class overloads DynamoDbClient::updateItem method, intercepts the session table updates | ||
* and substitutes the data type from 'S' to 'B' (from string to binary). | ||
* PHP string is the php binary data type, so this should work seamlessly. | ||
* | ||
* @see https://github.com/silverstripe/silverstripe-dynamodb/issues/32 | ||
*/ | ||
class DynamoDbClient extends \Aws\DynamoDb\DynamoDbClient | ||
{ | ||
/** | ||
* The name of the DynamoDB table where | ||
* sessions are stored | ||
* | ||
* @var string | ||
*/ | ||
private $sessionTable; | ||
|
||
/** | ||
* The name of the data attribute of the session table | ||
* where sessions are stored | ||
* | ||
* @var string | ||
*/ | ||
private $dataAttribute; | ||
|
||
/** | ||
* Initialize the client with the session table | ||
* | ||
* @param string $sessionTable The session table name | ||
* | ||
* {@inheritdoc} | ||
*/ | ||
public function __construct($sessionTable, ...$args) | ||
{ | ||
$this->sessionTable = $sessionTable; | ||
parent::__construct(...$args); | ||
} | ||
|
||
/** | ||
* Initialize the client with the session data attribute (within the session table) | ||
* This method must be used to finish the client initialization, otherwise session updates will not be | ||
* intercepted and amended | ||
* | ||
* @param string $dataAttribute attribute of the session table where session data is persisted | ||
*/ | ||
public function setSessionTableDataAttribute($dataAttribute) | ||
{ | ||
$this->dataAttribute = $dataAttribute; | ||
} | ||
|
||
public function updateItem($attributes, ...$extra) | ||
{ | ||
$this->patchSessionUpdate($attributes); | ||
return parent::updateItem($attributes, ...$extra); | ||
} | ||
|
||
/** | ||
* Update the session data type from 'S' to 'B' (from string to binary) | ||
* | ||
* @param mixed &$data Data to be updated in-place | ||
*/ | ||
private function patchSessionUpdate(&$data) { | ||
if (!isset($data['TableName']) || $data['TableName'] !== $this->sessionTable) { | ||
return; | ||
} | ||
|
||
if (!isset($data['AttributeUpdates'][$this->dataAttribute]['Value']['S'])) { | ||
return; | ||
} | ||
|
||
$data['AttributeUpdates'][$this->dataAttribute]['Value']['B'] = $data['AttributeUpdates'][$this->dataAttribute]['Value']['S']; | ||
unset($data['AttributeUpdates'][$this->dataAttribute]['Value']['S']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace SilverStripe\DynamoDb; | ||
|
||
use Aws\DynamoDb\SessionConnectionInterface; | ||
|
||
/** | ||
* The only purpose of this class is to finish our custom | ||
* \SilverStripe\DynamoDb\DynamoDbClient initialization. | ||
* To do so it simply needs to pass session data attribute | ||
* from SessionConnection to DynamoDbClient::setSessionTableDataAttribute | ||
* so the latter may intercept session table updates intelligently. | ||
* | ||
* @see \SilverStripe\DynamoDb\DynamoDbClient | ||
*/ | ||
class SessionHandler extends \Aws\DynamoDb\SessionHandler | ||
{ | ||
/** | ||
* @var SessionConnectionInterface | ||
*/ | ||
private $connection; | ||
|
||
public static function fromClient(\Aws\DynamoDb\DynamoDbClient $client, array $config = []) | ||
{ | ||
$handler = parent::fromClient($client, $config); | ||
$client->setSessionTableDataAttribute($handler->connection->getDataAttribute()); | ||
|
||
return $handler; | ||
} | ||
|
||
public function __construct(SessionConnectionInterface $connection) | ||
{ | ||
$this->connection = $connection; | ||
parent::__construct($connection); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters