Skip to content

Commit

Permalink
Added the "key" validation for the MemcacheD commands, to prevent inj…
Browse files Browse the repository at this point in the history
…ections.
  • Loading branch information
AlexeyPlodenko committed Apr 1, 2024
1 parent 9d91d16 commit 0da3c16
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
20 changes: 20 additions & 0 deletions src/Library/Command/AbstractMemcached.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Library\Command;

use Exception;

abstract class AbstractMemcached
{
/**
* @throws Exception
*/
protected function validateKey(string $key)
{
if (!preg_match('/^[^\x00-\x1F\x7F\s]{1,250}$/', $key)) {
throw new Exception(
'Key must be a string up to 250 symbols long, and not containing control and whitespace characters.'
);
}
}
}
19 changes: 17 additions & 2 deletions src/Library/Command/Memcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// https://www.php.net/manual/en/memcache.installation.php
use \Memcache as MemcachePecl;

class Memcache implements CommandInterface
class Memcache extends AbstractMemcached implements CommandInterface
{
/**
* @var App|null
Expand Down Expand Up @@ -160,9 +160,12 @@ public function items($server, $port, $slab)
* @param string $key Key to retrieve
*
* @return string
* @throws Exception
*/
public function get($server, $port, $key)
{
$this->validateKey($key);

# Adding server
self::$_memcache->addServer($server, $port);

Expand All @@ -184,9 +187,12 @@ public function get($server, $port, $key)
* @param integer $duration Duration
*
* @return string
* @throws Exception
*/
function set($server, $port, $key, $data, $duration)
{
$this->validateKey($key);

# Adding server
self::$_memcache->addServer($server, $port);

Expand All @@ -206,9 +212,12 @@ function set($server, $port, $key, $data, $duration)
* @param string $key Key to delete
*
* @return string
* @throws Exception
*/
public function delete($server, $port, $key)
{
$this->validateKey($key);

# Adding server
self::$_memcache->addServer($server, $port);

Expand All @@ -229,9 +238,12 @@ public function delete($server, $port, $key)
* @param integer $value Value to increment
*
* @return string
* @throws Exception
*/
function increment($server, $port, $key, $value)
{
$this->validateKey($key);

# Adding server
self::$_memcache->addServer($server, $port);

Expand All @@ -252,9 +264,12 @@ function increment($server, $port, $key, $value)
* @param integer $value Value to decrement
*
* @return string
* @throws Exception
*/
function decrement($server, $port, $key, $value)
{
$this->validateKey($key);

# Adding server
self::$_memcache->addServer($server, $port);

Expand Down Expand Up @@ -318,4 +333,4 @@ function telnet($server, $port, $command)
{
throw new Exception('PECL Memcache does not support telnet, use Server instead');
}
}
}
19 changes: 17 additions & 2 deletions src/Library/Command/Memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
// https://www.php.net/manual/en/memcached.installation.php
use \Memcached as MemcachedPecl;

class Memcached implements CommandInterface
class Memcached extends AbstractMemcached implements CommandInterface
{
/**
* @var Memcached
Expand Down Expand Up @@ -131,9 +131,12 @@ public function items($server, $port, $slab)
* @param string $key Key to retrieve
*
* @return string
* @throws Exception
*/
public function get($server, $port, $key)
{
$this->validateKey($key);

# Adding server
self::$_memcache->addServer($server, $port);

Expand All @@ -155,9 +158,12 @@ public function get($server, $port, $key)
* @param integer $duration Duration
*
* @return string
* @throws Exception
*/
function set($server, $port, $key, $data, $duration)
{
$this->validateKey($key);

# Adding server
self::$_memcache->addServer($server, $port);

Expand All @@ -180,9 +186,12 @@ function set($server, $port, $key, $data, $duration)
* @param string $key Key to delete
*
* @return string
* @throws Exception
*/
public function delete($server, $port, $key)
{
$this->validateKey($key);

# Adding server
self::$_memcache->addServer($server, $port);

Expand All @@ -201,9 +210,12 @@ public function delete($server, $port, $key)
* @param integer $value Value to increment
*
* @return string
* @throws Exception
*/
function increment($server, $port, $key, $value)
{
$this->validateKey($key);

# Adding server
self::$_memcache->addServer($server, $port);

Expand All @@ -224,9 +236,12 @@ function increment($server, $port, $key, $value)
* @param integer $value Value to decrement
*
* @return string
* @throws Exception
*/
function decrement($server, $port, $key, $value)
{
$this->validateKey($key);

# Adding server
self::$_memcache->addServer($server, $port);

Expand Down Expand Up @@ -289,4 +304,4 @@ function telnet($server, $port, $command)
{
throw new Exception('PECL Memcached does not support telnet, use Server instead');
}
}
}
19 changes: 18 additions & 1 deletion src/Library/Command/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
use App\Library\App;
use App\Library\Data\Analysis;
use App\Library\Data\Errors;
use Exception;

class Server implements CommandInterface
class Server extends AbstractMemcached implements CommandInterface
{
/**
* @var App|null
Expand Down Expand Up @@ -373,9 +374,12 @@ public function items($server, $port, $slab)
* @param string $key Key to retrieve
*
* @return string|null
* @throws Exception
*/
public function get($server, $port, $key): ?string
{
$this->validateKey($key);

# Executing command : get
$string = $this->exec("get $key", $server, $port);
if ($string) {
Expand All @@ -399,9 +403,12 @@ public function get($server, $port, $key): ?string
* @param integer $duration Duration
*
* @return string
* @throws Exception
*/
function set($server, $port, $key, $data, $duration)
{
$this->validateKey($key);

# Formatting data
$data = preg_replace('/\r/', '', $data);

Expand All @@ -421,9 +428,12 @@ function set($server, $port, $key, $data, $duration)
* @param string $key Key to delete
*
* @return string
* @throws Exception
*/
public function delete($server, $port, $key)
{
$this->validateKey($key);

# Executing command : delete
if (($result = $this->exec('delete ' . $key, $server, $port))) {
return $result;
Expand All @@ -441,9 +451,12 @@ public function delete($server, $port, $key)
* @param integer $value Value to increment
*
* @return string
* @throws Exception
*/
function increment($server, $port, $key, $value)
{
$this->validateKey($key);

# Executing command : increment
if (($result = $this->exec('incr ' . $key . ' ' . $value, $server, $port))) {
return $result;
Expand All @@ -461,9 +474,12 @@ function increment($server, $port, $key, $value)
* @param integer $value Value to decrement
*
* @return string
* @throws Exception
*/
function decrement($server, $port, $key, $value)
{
$this->validateKey($key);

# Executing command : decrement
if (($result = $this->exec('decr ' . $key . ' ' . $value, $server, $port))) {
return $result;
Expand Down Expand Up @@ -501,6 +517,7 @@ function flush_all($server, $port, $delay)
* @param bool $more More action
*
* @return array
* @throws Exception
*/
function search($server, $port, $search, $level = false, $more = false): array
{
Expand Down

0 comments on commit 0da3c16

Please sign in to comment.