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

LockKey doesn't get prefixed with prefix properly + patch #249

Closed
hoshsadiq opened this issue Feb 25, 2016 · 14 comments
Closed

LockKey doesn't get prefixed with prefix properly + patch #249

hoshsadiq opened this issue Feb 25, 2016 · 14 comments
Labels
dx Developer eXperience enhancement Improves existing functionality
Milestone

Comments

@hoshsadiq
Copy link

The RedisSessionHandler::$lockKey gets prefixed with just the prefix without the colon ($this->prefix.$lockKey), but every other session related sessions get prefixed with $this->prefix.':'.$sessionId). Lock key should also have a colon after the prefix ($this->prefix.':'.$this->lockKey). I've generated a patch to fix this. I'd give you a PR but I'm not sure where to point the PR to. You can apply this by saving it as diff.patch and running: patch -p0 < diff.patch.

This was asked about in #181 as well. There's already a PR to master #158 but this is for 1.1.

Below is the patch:

diff --git Session/Storage/Handler/RedisSessionHandler.php Session/Storage/Handler/RedisSessionHandler.php
index b6ad274..092ebfd 100644
--- Session/Storage/Handler/RedisSessionHandler.php
+++ Session/Storage/Handler/RedisSessionHandler.php
@@ -108,10 +108,10 @@ class RedisSessionHandler implements \SessionHandlerInterface

         $this->lockKey = $sessionId.'.lock';
         for ($i=0;$i<$attempts;$i++) {
-            $success = $this->redis->setnx($this->prefix.$this->lockKey, '1');
+            $success = $this->redis->setnx($this->getLockKey(), '1');
             if ($success) {
                 $this->locked = true;
-                $this->redis->expire($this->prefix.$this->lockKey, $this->lockMaxWait + 1);
+                $this->redis->expire($this->getLockKey(), $this->lockMaxWait + 1);
                 return true;
             }
             usleep($this->spinLockWait);
@@ -122,7 +122,7 @@ class RedisSessionHandler implements \SessionHandlerInterface

     private function unlockSession()
     {
-        $this->redis->del($this->prefix.$this->lockKey);
+        $this->redis->del($this->getLockKey());
         $this->locked = false;
     }

@@ -213,6 +213,11 @@ class RedisSessionHandler implements \SessionHandlerInterface
         return $this->prefix . ':' . $sessionId;
     }

+    public function getLockKey()
+    {
+        return $this->getRedisKey($this->lockKey);
+    }
+
     /**
      * Destructor
      */
@hoshsadiq hoshsadiq changed the title LockKey doesn't get prefixed with prefix properly LockKey doesn't get prefixed with prefix properly + patch Feb 25, 2016
@curry684 curry684 added this to the 3.0 milestone Apr 8, 2018
@curry684 curry684 added dx Developer eXperience enhancement Improves existing functionality labels Apr 8, 2018
@curry684
Copy link
Collaborator

curry684 commented Apr 9, 2018

@Seldaek I think this "issue" still exists after #404 right?

@Seldaek
Copy link
Collaborator

Seldaek commented Apr 10, 2018

Yes I believe it does.

@curry684
Copy link
Collaborator

@hoshsadiq given that we released 3.0.0-RC2 and gearing towards 3.0.0 I think we should consider pushing this fix in there still. If you want to make the PR you'd only need to target master now as 2.x is heading for deprecation the day we get 3.x out there.

@B-Galati
Copy link
Collaborator

@curry684 @Seldaek I think we should deprecate this feature in v3 in favor of the Symfony redis handler and then update the doc to explain how to use symfony redis session handler. What do you think?

I'll do the proposed patch.

@curry684
Copy link
Collaborator

Makes sense.

B-Galati added a commit to B-Galati/SncRedisBundle that referenced this issue Feb 21, 2019
@B-Galati
Copy link
Collaborator

What do yout think @dkarlovi?

@dkarlovi
Copy link

If you're asking about

I think we should deprecate this feature in v3 in favor of the Symfony redis handler

Sounds good to me, the handler there should work just as well.

Ironically, I'm not using it even though I wrote it since this bundle provides direct access to the Redis client which I needed explicitly so switched the session handler too. :)

If some issues come up, I pledge to fix them in Symfony. So, from me, 👍

@B-Galati
Copy link
Collaborator

Thanks a lot @dkarlovi!

I am now wondering what's the difference between ours and the one you implemented in Symfony. It looks like it's all about the lock behavior for write and read but I don't get why it's needed. Could be to mitigate some security issue?

@dkarlovi
Copy link

Session locks were added later IIRC, by @nicolas-grekas, it's a feature for all Symfony's session handlers, don't know the specifics.

Overall, the handler from Symfony is trivial, but works and is used: some people already fixed a few small bugs since it was added. You should be pretty safe to use it, IMO.

@B-Galati
Copy link
Collaborator

It looks like session locking was not implemented yet. See symfony/symfony#4976.
It has been recently implemented in phpredis extension but not compatible with cluster mode.
It looks like it's something really tricky :D.
I guess we could contribute our handler lock logic to Symfony before deprecating ours.

@curry684
Copy link
Collaborator

I think it's pretty pointless to do it on our end while it should be pretty much Symfony native behavior.

@B-Galati
Copy link
Collaborator

@curry684 Yes, what do you think about contributing a lock logic to the Symfony RedisSessionHandler?

@curry684
Copy link
Collaborator

Sounds like a lot of work :D But yeah I think we should eventually do it as it's likely the most used non-file session handler in Symfony.

@curry684
Copy link
Collaborator

Is fixed in 3.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dx Developer eXperience enhancement Improves existing functionality
Projects
None yet
Development

No branches or pull requests

5 participants