Skip to content

Commit

Permalink
FIX DatabaseStore binary safety
Browse files Browse the repository at this point in the history
 # Conflicts:
 #	src/Store/DatabaseStore.php
  • Loading branch information
dnsl48 authored and robbieaverill committed Jul 23, 2019
1 parent 19ae6fa commit bed3833
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions code/HybridSessionStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ public function read($session_id) {

if ($result && $result->numRecords()) {
$data = $result->first();
return $data['Data'];
$decoded = $this->binaryDataJsonDecode($data['Data']);
return is_null($decoded) ? $data['Data'] : $decoded;
}
}

Expand All @@ -379,12 +380,52 @@ public function write($session_id, $session_data) {
ON DUPLICATE KEY UPDATE "Expiry" = %2$u, "Data" = \'%3$s\'',
Convert::raw2sql($session_id),
$expiry,
Convert::raw2sql($session_data)
Convert::raw2sql($this->binaryDataJsonEncode($session_data))
));

return true;
}

/**
* Encode binary data into ASCII string (a subset of UTF-8)
*
* Silverstripe <= 4.4 does not have a binary db field implementation, so we have to store
* binary data as text
*
* @param string $data This is a binary blob
*
* @return string
*/
private function binaryDataJsonEncode($data)
{
return json_encode([
self::class,
base64_encode($data)
]);
}

/**
* Decode ASCII string into original binary data (a php string)
*
* Silverstripe <= 4.4 does not have a binary db field implementation, so we have to store
* binary data as text
*
* @param string $text
*
* @param null|string
*/
private function binaryDataJsonDecode($text)
{
$struct = json_decode($text, true, 2);
if (!is_array($struct) || count($struct) !== 2) {
return null;
}
if (!isset($struct[0]) || !isset($struct[1]) || $struct[0] !== self::class) {
return null;
}
return base64_decode($struct[1]);
}

public function destroy($session_id) {
// NOP
}
Expand Down

0 comments on commit bed3833

Please sign in to comment.