Skip to content
Snippets Groups Projects
Commit fde02516 authored by Al Munnings's avatar Al Munnings
Browse files

Qualify the collection on the queries, ensure search is against preview_token in serialize

parent 2ff7dae2
No related branches found
No related tags found
No related merge requests found
Pipeline #141243 passed
...@@ -23,6 +23,21 @@ class DatabaseStorageExpirableToken extends DatabaseStorageExpirable { ...@@ -23,6 +23,21 @@ class DatabaseStorageExpirableToken extends DatabaseStorageExpirable {
return parent::doSetWithExpire($key, $value, $expire); return parent::doSetWithExpire($key, $value, $expire);
} }
/**
* Escape the token value for searching within a serialized blob.
*
* @param string|null $token
* The token to escape.
*
* @return string
* The escaped token.
*/
protected function escapeToken(?string $token): string {
$token = $token ?: '';
$token = preg_replace('/[^a-zA-Z0-9_-]/', '', $token);
return sprintf('%%;s:13:"preview_token";s:%d:"%s";%%', strlen($token), $token);
}
/** /**
* Load by token. * Load by token.
* *
...@@ -33,14 +48,12 @@ class DatabaseStorageExpirableToken extends DatabaseStorageExpirable { ...@@ -33,14 +48,12 @@ class DatabaseStorageExpirableToken extends DatabaseStorageExpirable {
* The value. * The value.
*/ */
public function getKeyByToken(?string $token) { public function getKeyByToken(?string $token) {
$token_length = strlen($token ?? '');
$token_pattern = sprintf('%%s:%s:"%s";%%', $token_length, $token);
try { try {
return $this->connection->query( return $this->connection->query(
'SELECT [name] FROM {' . $this->connection->escapeTable($this->table) . '} WHERE value LIKE :token', 'SELECT [name] FROM {' . $this->connection->escapeTable($this->table) . '} WHERE value LIKE :token AND collection = :collection',
[ [
':token' => $token_pattern, ':token' => $this->escapeToken($token),
':collection' => $this->collection,
])->fetchField(); ])->fetchField();
} }
catch (\Exception $e) { catch (\Exception $e) {
...@@ -61,14 +74,13 @@ class DatabaseStorageExpirableToken extends DatabaseStorageExpirable { ...@@ -61,14 +74,13 @@ class DatabaseStorageExpirableToken extends DatabaseStorageExpirable {
public function getTokenByKey($name) { public function getTokenByKey($name) {
try { try {
$preview_blob = $this->connection->query( $preview_blob = $this->connection->query(
'SELECT [value] FROM {' . $this->connection->escapeTable($this->table) . '} WHERE name = :name', 'SELECT [value] FROM {' . $this->connection->escapeTable($this->table) . '} WHERE name = :name AND collection = :collection',
[ [
':name' => $name, ':name' => $name,
':collection' => $this->collection,
])->fetchField(); ])->fetchField();
$preview_object = $this->serializer->decode($preview_blob); return $this->serializer->decode($preview_blob)?->preview_token ?? NULL;
return $preview_object?->preview_token ?? NULL;
} }
catch (\Exception $e) { catch (\Exception $e) {
$this->catchException($e); $this->catchException($e);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment