From b37c966aea22b690225f45d217d0ada07cb20286 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 28 Feb 2012 10:06:53 -0600 Subject: [PATCH] improve session ID assignment to avoid possible overlaps. Signed-off-by: Taylor Otwell --- laravel/session/drivers/apc.php | 2 +- laravel/session/drivers/cookie.php | 2 +- laravel/session/drivers/database.php | 2 +- laravel/session/drivers/driver.php | 47 ++++++++++++++++++++++++--- laravel/session/drivers/file.php | 2 +- laravel/session/drivers/memcached.php | 2 +- laravel/session/drivers/redis.php | 2 +- laravel/session/payload.php | 16 +++------ 8 files changed, 53 insertions(+), 22 deletions(-) diff --git a/laravel/session/drivers/apc.php b/laravel/session/drivers/apc.php index a45efaa0..d3148366 100644 --- a/laravel/session/drivers/apc.php +++ b/laravel/session/drivers/apc.php @@ -1,6 +1,6 @@ $this->id(), 'data' => array( + ':new:' => array(), + ':old:' => array(), + )); + } + + /** + * Get a new session ID that isn't assigned to any current session. + * + * @return string + */ + public function id() + { + $session = array(); + + // We'll containue generating random IDs until we find an ID that is + // not currently assigned to a session. This is almost definitely + // going to happen on the first iteration. + do { + + $session = $this->load($id = Str::random(40)); + + } while ( ! is_null($session)); + + return $id; + } } \ No newline at end of file diff --git a/laravel/session/drivers/file.php b/laravel/session/drivers/file.php index f2df8622..d7e3b98f 100644 --- a/laravel/session/drivers/file.php +++ b/laravel/session/drivers/file.php @@ -1,6 +1,6 @@ session) or static::expired($this->session)) { $this->exists = false; - $this->session = array('id' => Str::random(40), 'data' => array( - ':new:' => array(), - ':old:' => array(), - )); + $this->session = $this->driver->fresh(); } // A CSRF token is stored in every session. The token is used by the Form // class and the "csrf" filter to protect the application from cross-site - // request forgery attacks. The token is simply a long, random string - // which should be posted with each request to the application. + // request forgery attacks. The token is simply a random string. if ( ! $this->has(Session::csrf_token)) { $this->put(Session::csrf_token, Str::random(40)); @@ -125,8 +120,7 @@ public function get($key, $default = null) // We check for the item in the general session data first, and if it // does not exist in that data, we will attempt to find it in the new - // and old flash data. If none of those arrays contain the requested - // item, we will just return the default value. + // and old flash data, or finally return the default value. if ( ! is_null($value = array_get($session, $key))) { return $value; @@ -247,7 +241,7 @@ public function flush() */ public function regenerate() { - $this->session['id'] = Str::random(40); + $this->session['id'] = $this->driver->id(); $this->exists = false; }