Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

SessionManagerFactory + session validators via config makes validation always fail #7128

Closed

Conversation

Martin-P
Copy link
Contributor

When using Zend\Session\Service\SessionManagerFactory validators can be added via config:

'session_manager' => array(
    'validators' => array(
        'Zend\Session\Validator\HttpUserAgent',
        'Zend\Session\Validator\RemoteAddr',
    ),
),

Before security fix ddbf43a validators always returned true. After the security fix validators always fail. This PR fixes that issue with Zend\Session\Service\SessionManagerFactory (not to be confused with standalone usage of Zend\Session\SessionManager).

It is the same fix as in #7094, but it got ignored because of the security issue.

@@ -113,16 +112,14 @@ public function createService(ServiceLocatorInterface $services)
) {
$managerConfig = array_merge($managerConfig, $configService['session_manager']);
}
// Attach validators to session manager, if any
Copy link
Contributor

Choose a reason for hiding this comment

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

why was this comment removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The old code attached validators at that point ($chain->attach(...)). That code has been removed and there are no validators attached at that point anymore, which makes the comment obsolete.

@YDyachenko
Copy link

Hi guys.
This fix have one problem. He attach one validator two times (in SessionManager::initializeValidatorChain() and ValidatorChain::__construct())

Also i think what we should setup validators via ValidatorChain rather than SessionManager constructor.

So i created another patch for this issue: YDyachenko@feecb0a

@Martin-P
Copy link
Contributor Author

He attach one validator two times (in SessionManager::initializeValidatorChain() and ValidatorChain::__construct())

Can you specify where that happens? In my code nothing is attached at ValidatorChain::__construct().
See line 353 of Zend\Session\SessionManager:

$this->setValidatorChain(new ValidatorChain($this->getStorage()));

Perhaps you can post a failing unittest for that? That way it is easier to check if that actually happens.

@YDyachenko
Copy link

Can you specify where that happens? In my code nothing is attached at ValidatorChain::__construct().

If session contains validators, they will be attached:
Line 36 of Zend\Session\ValidatorChain.

Test:

    /**
     * @runInSeparateProcess
     */
    public function testFactoryDoesNotAttachValidatorTwoTimes()
    {
        $storage = new ArrayStorage();
        $storage->setMetadata('_VALID', array(
            'Zend\Session\Validator\RemoteAddr' => '127.0.0.1',
        ));
        $this->services->setService('Zend\Session\Storage\StorageInterface', $storage);

        $config = array(
            'session_manager' => array(
                'validators' => array(
                    'Zend\Session\Validator\RemoteAddr',
                ),
            ),
        );
        $this->services->setService('Config', $config);

        $manager = $this->services->get('Zend\Session\ManagerInterface');
        try {
            $manager->start();
        } catch (RuntimeException $e) {
            // pass
        }

        $this->assertEquals(1, $manager->getValidatorChain()->getListeners('session.validate')->count()); 
    }
  1. ZendTest\Session\Service\SessionManagerFactoryTest::testFactoryDoesNotAttachValidatorTwoTimes
    Failed asserting that 2 matches expected 1.

@Martin-P Martin-P force-pushed the hotfix/session-validators branch from 95cd0c3 to 9f2052f Compare February 15, 2015 01:00
@Martin-P
Copy link
Contributor Author

Did a rebase because I encountered some merge conflicts.

@YDyachenko Thanks! I added your test to this PR and fixed the issue for that specific test.

@weierophinney weierophinney modified the milestones: 2.3.5, 2.3.6, 2.4.0 Feb 18, 2015
@@ -85,6 +88,8 @@ public function testFactoryWillAddValidatorViaConfiguration()
$this->services->setService('Config', $config);
$manager = $this->services->get('Zend\Session\ManagerInterface');

$manager->start();
Copy link
Member

Choose a reason for hiding this comment

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

This changes the context for the test! The original test omitted it for a specific reason, based on the security issue we addressed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please see the previous discussion: #7094 (comment)

That is why I said it is a BC break, because the validators are now attached after the session has started. This makes sense, because before the session has not started there is no session data to compare with and thus the validators always fail.

In my opening post #7128 (comment) I explained the tests always passed before the security fix and always fail after the security fix. The tests added to detect the security fix still pass, which shows this PR does not compromise the security fix in any way.

This test is not related to the security issue, because:

  1. If it was here to detect the security issue, it should have failed before the security issue was even addressed. It did not and it always passed the test.
  2. The commit which fixed the security issue changed nothing about this test: ddbf43a

weierophinney added a commit that referenced this pull request Feb 24, 2015
SessionManagerFactory + session validators via config makes validation always fail
weierophinney added a commit that referenced this pull request Feb 24, 2015
@weierophinney
Copy link
Member

Merged to develop for release with 2.4.

@Martin-P Martin-P deleted the hotfix/session-validators branch February 28, 2015 19:25
weierophinney added a commit to zendframework/zend-session that referenced this pull request May 15, 2015
…fix/session-validators

SessionManagerFactory + session validators via config makes validation always fail
weierophinney added a commit to zendframework/zend-session that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants