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

[CSP] Allow nonce passed through header (to support Turbolinks/PJAX) #136

Closed
ruudk opened this issue Feb 12, 2017 · 6 comments
Closed

[CSP] Allow nonce passed through header (to support Turbolinks/PJAX) #136

ruudk opened this issue Feb 12, 2017 · 6 comments

Comments

@ruudk
Copy link
Contributor

ruudk commented Feb 12, 2017

We are running a Symfony2 application with Turbolinks and want to use CSP. When we use the csp_nonce() method to generate a nonce, it will be different for every request. But since we use Turbolinks, navigating to other pages will just load the new HTML through AJAX and embed it in the current document. When a new page includes a <script> or <style> tag it will be blocked.

So I was thinking about the following solution:

In ContentSecurityPolicyListener:

    public function onKernelRequest(GetResponseEvent $event)
    {
        // ..

        if ($event->getRequest()->headers->has('X-CSP-Nonce')) {
            // @todo maybe add some regex validation
            $this->nonce = $event->getRequest()->headers->get('X-CSP-Nonce');
        }
    }

Then in my HTML I set this:

<meta name="csp_nonce" content="{{ csp_nonce() }}">

And in my app.js I set something like this:

    $.ajaxSetup({
        headers: {
            'X-CSP-NONCE': document.querySelector('meta[name="csp_nonce"]').getAttribute('content'),
        }
    })

This works, but what about security? Am I going to do something really stupid/bad?

@ruudk ruudk changed the title [CSP [CSP] Allow nonce passed through header (to support Turbolinks/PJAX) Feb 12, 2017
@sstok
Copy link

sstok commented Feb 12, 2017

For the meta you need http-equiv not name as this an HTTP header, if your not sure you test this with something that should fail in that situation, load a script without a proper nonce. Assuming this works or believing it works because someone says yes is a dangerous practice.

@romainneutron
Copy link
Collaborator

With #139 , you could $container->get('nelmio_security.twig_extension')->getCSPNonce().

@romainneutron
Copy link
Collaborator

OH, you can currently do

$container->get('nelmio_security.csp_listener')->getNonce()

And it's much more cleaner

@stof
Copy link
Contributor

stof commented Jun 7, 2019

@romainneutron I don't think closing this is right. The request is not to allow reading the nonce, buit about setting it.

@florimondmanca
Copy link

florimondmanca commented Feb 13, 2023

Edit: moved to #321

@ruudk
Copy link
Contributor Author

ruudk commented Feb 13, 2023

Please open a new issue, don't reply in something from 2017.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants