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

Fix session_regenerate_id issue when using redis as the session handler #12331

Conversation

thilinah
Copy link

Fix session_regenerate_id issue when using redis as the session handler in php7. No session handler should return null for read method. it should return an empty string is the key is not found

…er in php7. No session handler should return null for read method. it should return an empty string is the key is not found
@Jurigag
Copy link
Contributor

Jurigag commented Oct 17, 2016

Some test if it's fixed could be good.

@@ -118,7 +118,8 @@ class Redis extends Adapter
*/
public function read(sessionId) -> string
{
return (string) this->_redis->get(sessionId, this->_lifetime);
let res = (string) this->_redis->get(sessionId, this->_lifetime);
return res == null ? "" : res;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after let res = (string) .... the res wont be a null.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also you missed var res

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to instantiate the 'res' variable before this line:

var res;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and we use tabs for zep files instead of spaces

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

php -a
Interactive mode enabled
$res = '';
php > var_dump($res == null);
php shell code:1:
bool(true)

@sergeyklay sergeyklay closed this Oct 17, 2016
@thilinah
Copy link
Author

There is a problem with current implementation. The line

return (string) this->_redis->get(sessionId, this->_lifetime);

is returning null when the session key is not in store (when using redis session handler)

This causes a fatal error when calling session_regenerate_id

I was using phalcon with php7 and redis session handler and had to extend it and use the extended php session handler with following method.

    public function read($sessionId)
    {
        $val = $this->_redis->get($sessionId, $this->_lifetime);
        return empty($val) ? "" : $val;
    }

May be someone can apply a fix based on this

@Jurigag
Copy link
Contributor

Jurigag commented Oct 17, 2016

How this is returning null when it should be casted to (string) ?

@thilinah
Copy link
Author

It could be a problem with zephir in php7. Some how I'm certain redis.zep read function returning null when the sessino key is not found.

branch: 3.0.x
php version: 7

screen shot 2016-10-17 at 12 23 17

@Jurigag
Copy link
Contributor

Jurigag commented Oct 17, 2016

Why you didn't added (string) here..... ?

@sergeyklay
Copy link
Contributor

Fixed in the 3.0.x branch.

git clone git@github.com:phalcon/cphalcon.git
cd cphalcon
git checkout 3.0.x

zephir fullclean
zephir build

@sergeyklay
Copy link
Contributor

// always will return string, not null
return (string) $this->_redis->get($sessionId, $this->_lifetime);

// just try
var_dump(null);
var_dump((string) null);

@sergeyklay
Copy link
Contributor

@thilinah
Copy link
Author

thilinah commented Oct 17, 2016

@sergeyklay when the null casted to string you might not be getting a null in zephir. But when the redis.zep is extended and called in php7 it is retuning null to php7. Please check the screenshot attached.

@Jurigag the code in screenshot is php. $this->_redis->get calls the get function in phalcon redis adapter compiled from branch 3.0.x. But as you can see it is retuning null. (even though you have casted the result from redis store to string). This breaks "session_regenerate_id" in php7.

I extended the pahlcon redis session handler and used it as my application session handler to fix this issue.

@sergeyklay
Copy link
Contributor

@thilinah Please see at public function read(sessionId) -> string .
The -> string will prevent to return non string

@sergeyklay
Copy link
Contributor

Provide please small script to return non-string, real null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants