Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3f6372d

Browse filesBrowse files
author
Benjamin Zikarsky
committed
MongoDbSessionHandler::read() now checks for valid session age
1 parent 69b07e9 commit 3f6372d
Copy full SHA for 3f6372d

File tree

Expand file treeCollapse file tree

2 files changed

+40
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+40
-1
lines changed

‎src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MongoDbSessionHandler.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MongoDbSessionHandler.php
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,11 @@ public function write($sessionId, $data)
159159
*/
160160
public function read($sessionId)
161161
{
162+
$timeoutThreshold = new \MongoDate(time() - (int) ini_get('session.gc_maxlifetime'));
163+
162164
$dbData = $this->getCollection()->findOne(array(
163-
$this->options['id_field'] => $sessionId,
165+
$this->options['id_field'] => $sessionId,
166+
$this->options['time_field'] => array('$gt' => $timeoutThreshold),
164167
));
165168

166169
return null === $dbData ? '' : $dbData[$this->options['data_field']]->bin;

‎src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,42 @@ public function testCloseMethodAlwaysReturnTrue()
7373
$this->assertTrue($this->storage->close(), 'The "close" method should always return true');
7474
}
7575

76+
public function testRead()
77+
{
78+
$collection = $this->createMongoCollectionMock();
79+
80+
$this->mongo->expects($this->once())
81+
->method('selectCollection')
82+
->with($this->options['database'], $this->options['collection'])
83+
->will($this->returnValue($collection));
84+
85+
$that = $this;
86+
87+
// defining the timeout before the actual method call
88+
// allows to test for "greater than" values in the $criteria
89+
$testTimeout = time() - (int) ini_get('session.gc_maxlifetime');
90+
91+
$collection->expects($this->once())
92+
->method('findOne')
93+
->will($this->returnCallback(function ($criteria) use ($that, $testTimeout) {
94+
$that->assertArrayHasKey($that->options['id_field'], $criteria);
95+
$that->assertEquals($criteria[$that->options['id_field']], 'foo');
96+
97+
$that->assertArrayHasKey($that->options['time_field'], $criteria);
98+
$that->assertArrayHasKey('$gt', $criteria[$that->options['time_field']]);
99+
$that->assertInstanceOf('MongoDate', $criteria[$that->options['time_field']]['$gt']);
100+
$that->assertGreaterThanOrEqual($criteria[$that->options['time_field']]['$gt']->sec, $testTimeout);
101+
102+
return array(
103+
$that->options['id_field'] => 'foo',
104+
$that->options['data_field'] => new \MongoBinData('bar', \MongoBinData::BYTE_ARRAY),
105+
$that->options['id_field'] => new \MongoDate(),
106+
);
107+
}));
108+
109+
$this->assertEquals('bar', $this->storage->read('foo'));
110+
}
111+
76112
public function testWrite()
77113
{
78114
$collection = $this->createMongoCollectionMock();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.