Session handler broke on PHP 7.3.0
Apparently the problem lies in RedisSessionHandler.php:L143
if ($this->mustRegenerate($session_id)) {
session_id($session_id = $this->create_sid());
// ...
}
Up until PHP 7.2 calling session_id($session_id) inside RedisSessionHandler::read() updates the ID in PHP's internals, so that when the session is closed later on and PHP calls RedisSessionHandler::write(), $session_id is the new ID. On PHP 7.3.0 the $session_id received in RedisSessionHandler::write() is still the old value (the one that has to change), so the handler generates a new ID, puts it in the Cookie header and sends it back to the client, but in Redis the stored key is the stale one. In other words, session_id($session_id) no longer does anything.
Example
$ curl -i -H "Cookie: PHPSESSID=madeupkey;" php73:32768/visit-counter.php
HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Thu, 20 Dec 2018 20:03:59 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/7.3.0
Set-Cookie: PHPSESSID=3m5qrhtk9upe63069ilf2h3ogr; path=/ <------ new (but lost) ID
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
1
Redis MONITOR log:
1545336238.057942 [0 172.18.0.6:57078] "EXISTS" "PHPREDIS_SESSION:madeupkey"
1545336238.587852 [0 172.18.0.6:57078] "SET" "PHPREDIS_SESSION:3m5qrhtk9upe63069ilf2h3ogr_lock" "" "nx"
1545336239.123639 [0 172.18.0.6:57078] "SETEX" "PHPREDIS_SESSION:madeupkey" "1440" "visits|i:1;"
1545336239.595592 [0 172.18.0.6:57078] "DEL" "PHPREDIS_SESSION:3m5qrhtk9upe63069ilf2h3ogr_lock"
Keys in Redis:
127.0.0.1:6379> KEYS *
1) "PHPREDIS_SESSION:madeupkey" <---- successful session fixation attack
127.0.0.1:6379> GET PHPREDIS_SESSION:madeupkey
"visits|i:1;"
Test suite results on PHP 7.3.0:
290ed5384ec4:/var/www/redis-sessions.test# TARGET=php73 vendor/bin/phpunit
PHPUnit 7.2.6 by Sebastian Bergmann and contributors.
...FF....F...... 16 / 16 (100%)
Time: 604 ms, Memory: 6.00MB
There were 3 failures:
1) UMA\RedisSessions\Tests\E2E\BasicTest::testMaliciousRequest
Failed asserting that 'visits|i:1;' is false.
/var/www/redis-sessions.test/tests/e2e/BasicTest.php:113
2) UMA\RedisSessions\Tests\E2E\BasicTest::testMaliciousRequestWithCustomCookieParams
Failed asserting that 'visits|i:1;' is false.
/var/www/redis-sessions.test/tests/e2e/BasicTest.php:136
3) UMA\RedisSessions\Tests\E2E\ConcurrentTest::testMaliciousRequests
Failed asserting that 1 is identical to 200.
/var/www/redis-sessions.test/tests/e2e/ConcurrentTest.php:69
FAILURES!
Tests: 16, Assertions: 55, Failures: 3.
Reported https://bugs.php.net/bug.php?id=77330
If the problem is not solved in the engine a possible workaround would be to store the regenerated $session_id in a new private attribute, and inside RedisSessionHandler::write() use that attribute (if it has been set) as the session key instead of the PHP supplied $session_id.
You cannot use session_id() to set new session ID for active sessions. session_id('new_id') updates session module's internal C struct value. Most C written save handlers do not expect changing session ID for active sessions and can behave badly.
If you would like to set 'custom session ID', you must use proper API. i.e. Use PS_FUNCS_SID or better use PS_FUNCS_UPDATE_TIMESTAMP, and define PS_CREATE_SID_FUNC for custom session ID.
If you really need to change session ID in user(PHP) script, close old one, set new ID, then start new session.
Although PHP 7.3 disallows any harmful usages that can cause "side effect", users can set any session ID for inactive sessions.
I'll explore that last option then. Thank you for dropping by :-)
Hi, @1ma. I found the resolution for this problem. Just need implement SessionIdInterface, SessionUpdateTimestampHandlerInterface interfaces and simplify read() method like in this example:
public function read($session_id)
{
$this->acquireLockOn($session_id);
if ($this->isNew($session_id)) {
return '';
}
return (string)$this->redis->get($session_id);
}
public function validateId($sessionId)
{
return !$this->mustRegenerate($sessionId);
}
public function updateTimestamp($sessionId, $sessionData){
return parent::updateTimestamp($sessionId, $sessionData);
}
Thanks @waverage.
Actually I'm thinking about abandoning the library, because last time I checked php-redis was already passing all my integration tests (with redis.session.locking_enabled=1 and session.use_strict_mode=1).
I just haven't taken the time to pin down the specific version that fixed all the issues and document that properly. But if you run their latest release you don't really need uma/redis-session-handler anymore :slightly_smiling_face: (with the above config)