Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Storage adapter getKeys method return empty array when set prefix = '' #15480

Closed
aa65535 opened this issue May 11, 2021 · 8 comments · Fixed by #15654
Closed

[BUG]: Storage adapter getKeys method return empty array when set prefix = '' #15480

aa65535 opened this issue May 11, 2021 · 8 comments · Fixed by #15654
Assignees
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report enhancement Enhancement to the framework status: low Low

Comments

@aa65535
Copy link

aa65535 commented May 11, 2021

Steps to reproduce the behavior:

use Phalcon\Storage\Adapter\Libmemcached;
use Phalcon\Storage\SerializerFactory;

$factory = new SerializerFactory();
$options = [
    'servers'           => [
        [
            'host'   => '127.0.0.1',
            'port'   => 11211,
            'weight' => 1
        ]
    ],
    'defaultSerializer' => 'Php',
    'lifetime'          => 3600,
    'prefix'            => '' // set empty string
];

$adapter = new Libmemcached($factory, $options);

$adapter->getKeys(); // return empty array

If patternis an empty string, keys should be returned directly.

protected function getFilteredKeys(var keys, string! prefix) -> array
{
var key, pattern;
array results;
let results = [],
pattern = this->prefix . prefix,
keys = !keys ? [] : keys;
for key in keys {
if Str::startsWith(key, pattern) {
let results[] = key;
}
}
return results;
}

Expected behavior
A clear and concise description of what you expected to happen.

When prefix is set to empty string, all keys should be returned

Details

  • Phalcon version: 4.1.2
  • PHP Version: 7.4.16
  • Operating System: CentOS 7
  • Installation type: Compiling from source
  • Zephir version (if any):
  • Server: Nginx
@aa65535 aa65535 added bug A bug report status: unverified Unverified labels May 11, 2021
@BeMySlaveDarlin
Copy link
Contributor

I suppose its related to Libmemcached adapter constructor

public function __construct(<SerializerFactory> factory, array! options = [])
{
if !isset options["servers"] {
let options["servers"] = [
0 : [
"host" : "127.0.0.1",
"port" : 11211,
"weight" : 1
]
];
}
let this->prefix = "ph-memc-",
this->options = options;
parent::__construct(factory, options);
}

As we can see, it sets this->prefix = "ph-memc-";, so pattern is never empty.

@aa65535
Copy link
Author

aa65535 commented May 11, 2021

parent::__construct(factory, options); 

If options["prefix"] = '' is set, the this->prefix will be reset in the parent class.

@niden
Copy link
Member

niden commented Jul 8, 2021

@aa65535 I am not convinced that this should happen.

By using the prefix we are namespacing so to speak the keys stored in the storage (Libmemcached in this case). If there are other processes using the same server, without the prefix, you will be able to retrieve keys that do not necessarily belong to your application.

So if I use say an application that stores data in memcached, and then use my Phalcon app with this adapter, without the prefix I will be able to read all the keys in memcached, even from the other application.

Thoughts?

@niden niden added discussion Request for comments and discussion and removed status: unverified Unverified labels Jul 8, 2021
@aa65535
Copy link
Author

aa65535 commented Jul 9, 2021

Of course, the best practice is to set a unique prefix for each application, but you can use the constructor to set the prefix to an empty string.
The problem is that when the prefix is an empty string, getKeys() does not return the expected value.

use Phalcon\Storage\Adapter\Libmemcached;
use Phalcon\Cache\Adapter\Redis;
use Phalcon\Storage\SerializerFactory;

$factory = new SerializerFactory();
$options = [
    'prefix' => '' // set empty string
];

$mem = new Libmemcached($factory, $options);

var_dump($mem->getPrefix()); // string(0) ""
var_dump($mem->getKeys()); // array(0) {}

$redis = new Redis($factory, $options);
var_dump($redis->getPrefix()); // string(0) ""
var_dump($redis->getKeys()); // array(0) {}

@niden
Copy link
Member

niden commented Aug 31, 2021

@aa65535 The example you gave is correct.

If you specify an empty prefix, Phalcon needs to internally set one in order to "namespace" the keys i.e. the keys stored only for your application. If it does not, you will be able to see all keys stored in Libmemcached or Redis or elsewhere.

This is done on purpose so as not to be able to access data that does not belong to you per se.

If you want to access all the keys in Memcached or Redis etc. you can instantiate the underlying adapter and then use the getKeys() on that object.

@niden niden added 5.0 The issues we want to solve in the 5.0 release wontfix The issue will not be fixed or implemented bug A bug report status: unverified Unverified and removed bug A bug report wontfix The issue will not be fixed or implemented labels Aug 31, 2021
@niden
Copy link
Member

niden commented Aug 31, 2021

I will write a couple of tests to ensure that the key can be changed if need be but never be empty. I do not recall the code right now but will check just in case.

@niden niden mentioned this issue Sep 9, 2021
5 tasks
@niden niden linked a pull request Sep 9, 2021 that will close this issue
5 tasks
@niden niden added enhancement Enhancement to the framework status: low Low and removed discussion Request for comments and discussion status: unverified Unverified labels Sep 9, 2021
@niden
Copy link
Member

niden commented Sep 9, 2021

After discussions with @Jeckerson we decided to offer this functionality. If the user wishes to not use a prefix, they can specify an empty prefix with the passed $options array.

@niden
Copy link
Member

niden commented Sep 9, 2021

Resolved in #15654

@niden niden closed this as completed Sep 9, 2021
@niden niden moved this to Released in Phalcon v5 Aug 25, 2022
@niden niden added this to Phalcon v5 Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report enhancement Enhancement to the framework status: low Low
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants