<commit_before><?php

/*
 * This file is part of the Nelmio SecurityBundle.
 *
 * (c) Nelmio <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Nelmio\SecurityBundle\Tests\Listener;

use Nelmio\SecurityBundle\EventListener\ForcedSslListener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class ForcedSslListenerTest extends \PHPUnit_Framework_TestCase
{
    private $kernel;

    protected function setUp()
    {
        $this->kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
    }

    /**
     * @dataProvider provideHstsHeaders
     */
    public function testHstsHeaders($hstsMaxAge, $hstsSubdomains, $hstsPreload, $result)
    {
        $listener = new ForcedSslListener($hstsMaxAge, $hstsSubdomains, $hstsPreload);

        $response = $this->callListenerResp($listener, 'https://localhost/', true);
        $this->assertSame($result, $response->headers->get('Strict-Transport-Security'));
    }

    public function provideHstsHeaders()
    {
        return array(
            array(60, true, false, 'max-age=60; includeSubDomains'),
            array(60, false, false, 'max-age=60'),
            array(3600, true, false, 'max-age=3600; includeSubDomains'),
            array(3600, false, false, 'max-age=3600'),
            array(3600, true, true, 'max-age=3600; includeSubDomains; preload'),
            array(3600, false, true, 'max-age=3600; preload'),
        );
    }

    public function testForcedSslSkipsSubReqs()
    {
        $listener = new ForcedSslListener(60, true);

        $response = $this->callListenerResp($listener, 'https://localhost/', false);
        $this->assertSame(null, $response->headers->get('Strict-Transport-Security'));
    }

    public function testForcedSslSkipsWhitelisted()
    {
        $listener = new ForcedSslListener(60, true, false, array('^/foo/', 'bar'));

        $response = $this->callListenerReq($listener, 'http://localhost/foo/lala', true);
        $this->assertSame(null, $response);

        $response = $this->callListenerReq($listener, 'http://localhost/lala/foo/lala', true);
        $this->assertSame('https://localhost/lala/foo/lala', $response->headers->get('Location'));

        $response = $this->callListenerReq($listener, 'https://localhost/lala/abarb', true);
        $this->assertSame(null, $response);
    }

    public function testForcedSslOnlyUsesHosts()
    {
        $listener = new ForcedSslListener(60, true, false, array(), array('^foo\.com$', '\.example\.org$'));

        $response = $this->callListenerReq($listener, 'http://afoo.com/foo/lala', true);
        $this->assertSame(null, $response);

        $response = $this->callListenerReq($listener, 'http://foo.com/foo/lala', true);
        $this->assertSame('https://foo.com/foo/lala', $response->headers->get('Location'));

        $response = $this->callListenerReq($listener, 'http://test.example.org/foo/lala', true);
        $this->assertSame('https://test.example.org/foo/lala', $response->headers->get('Location'));
    }

    protected function callListenerReq($listener, $uri, $masterReq)
    {
        $request = Request::create($uri);

        $event = new GetResponseEvent($this->kernel, $request, $masterReq ? HttpKernelInterface::MASTER_REQUEST : HttpKernelInterface::SUB_REQUEST);
        $listener->onKernelRequest($event);

        return $event->getResponse();
    }

    protected function callListenerResp(ForcedSslListener $listener, $uri, $masterReq)
    {
        $request = Request::create($uri);
        $response = new Response();

        $event = new FilterResponseEvent($this->kernel, $request, $masterReq ? HttpKernelInterface::MASTER_REQUEST : HttpKernelInterface::SUB_REQUEST, $response);
        $listener->onKernelResponse($event);

        return $response;
    }
}
<commit_msg>add test to assure hsts headers not being set for non-secure requests<commit_after><?php

/*
 * This file is part of the Nelmio SecurityBundle.
 *
 * (c) Nelmio <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Nelmio\SecurityBundle\Tests\Listener;

use Nelmio\SecurityBundle\EventListener\ForcedSslListener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class ForcedSslListenerTest extends \PHPUnit_Framework_TestCase
{
    private $kernel;

    protected function setUp()
    {
        $this->kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
    }

    /**
     * @dataProvider provideHstsHeaders
     */
    public function testHstsHeaders($hstsMaxAge, $hstsSubdomains, $hstsPreload, $result)
    {
        $listener = new ForcedSslListener($hstsMaxAge, $hstsSubdomains, $hstsPreload);

        $response = $this->callListenerResp($listener, 'https://localhost/', true);
        $this->assertSame($result, $response->headers->get('Strict-Transport-Security'));
    }

    /**
     * @dataProvider provideHstsHeaders
     */
    public function testHstsHeadersNotSetForNonSecureRequest($hstsMaxAge, $hstsSubdomains, $hstsPreload)
    {
        $listener = new ForcedSslListener($hstsMaxAge, $hstsSubdomains, $hstsPreload);

        $response = $this->callListenerResp($listener, 'http://localhost/', true);
        $this->assertSame(null, $response->headers->get('Strict-Transport-Security'));
    }

    public function provideHstsHeaders()
    {
        return array(
            array(60, true, false, 'max-age=60; includeSubDomains'),
            array(60, false, false, 'max-age=60'),
            array(3600, true, false, 'max-age=3600; includeSubDomains'),
            array(3600, false, false, 'max-age=3600'),
            array(3600, true, true, 'max-age=3600; includeSubDomains; preload'),
            array(3600, false, true, 'max-age=3600; preload'),
        );
    }

    public function testForcedSslSkipsSubReqs()
    {
        $listener = new ForcedSslListener(60, true);

        $response = $this->callListenerResp($listener, 'https://localhost/', false);
        $this->assertSame(null, $response->headers->get('Strict-Transport-Security'));
    }

    public function testForcedSslSkipsWhitelisted()
    {
        $listener = new ForcedSslListener(60, true, false, array('^/foo/', 'bar'));

        $response = $this->callListenerReq($listener, 'http://localhost/foo/lala', true);
        $this->assertSame(null, $response);

        $response = $this->callListenerReq($listener, 'http://localhost/lala/foo/lala', true);
        $this->assertSame('https://localhost/lala/foo/lala', $response->headers->get('Location'));

        $response = $this->callListenerReq($listener, 'https://localhost/lala/abarb', true);
        $this->assertSame(null, $response);
    }

    public function testForcedSslOnlyUsesHosts()
    {
        $listener = new ForcedSslListener(60, true, false, array(), array('^foo\.com$', '\.example\.org$'));

        $response = $this->callListenerReq($listener, 'http://afoo.com/foo/lala', true);
        $this->assertSame(null, $response);

        $response = $this->callListenerReq($listener, 'http://foo.com/foo/lala', true);
        $this->assertSame('https://foo.com/foo/lala', $response->headers->get('Location'));

        $response = $this->callListenerReq($listener, 'http://test.example.org/foo/lala', true);
        $this->assertSame('https://test.example.org/foo/lala', $response->headers->get('Location'));
    }

    protected function callListenerReq($listener, $uri, $masterReq)
    {
        $request = Request::create($uri);

        $event = new GetResponseEvent($this->kernel, $request, $masterReq ? HttpKernelInterface::MASTER_REQUEST : HttpKernelInterface::SUB_REQUEST);
        $listener->onKernelRequest($event);

        return $event->getResponse();
    }

    protected function callListenerResp(ForcedSslListener $listener, $uri, $masterReq)
    {
        $request = Request::create($uri);
        $response = new Response();

        $event = new FilterResponseEvent($this->kernel, $request, $masterReq ? HttpKernelInterface::MASTER_REQUEST : HttpKernelInterface::SUB_REQUEST, $response);
        $listener->onKernelResponse($event);

        return $response;
    }
}

{'fraction_non_alphanumeric': 0.15018315018315018, 'fraction_numerical': 0.010531135531135532, 'mean_word_length': 3.699838622915546, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 79, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '354813', 'n_tokens_mistral': 2660, 'n_tokens_neox': 2548, 'n_words': 465}