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

Adds csp nonce support for TagRenderer class #88

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

BruceGitHub
Copy link

@BruceGitHub BruceGitHub commented Apr 14, 2020

This pr. aim to resolve this
#10 (comment)
and this
#10 (comment)

Q A
Bug fix? no
New feature? yes
Doc updated no
BC breaks? no
Deprecations? yes
Tests pass? yes
Fixed tickets #10
License MIT

I'm not expert of PR, so sorry if I made mistakes in the process or in the code. But I will be happy to solve them in that case.

My problem was use csp-nonce in Symfony app when the nonce is generated from Nginx server.
I started from master branch but he already had problems... I can solve them ?

Todo

WIP

@BruceGitHub
Copy link
Author

BruceGitHub commented May 3, 2020

NOTE:
@ohartl
I found the solution without change the code a dirty solution but works!

//First need factory to change the default value for $defaultAttributes array 
<?php
declare(strict_types=1);

use Symfony\Component\Asset\Packages;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface;
use Symfony\WebpackEncoreBundle\Asset\TagRenderer;

class NonceFactory implements EventSubscriberInterface
{
    private $entrypointLookupCollection;
    private $packages;
    private static $nonce;

    public function __construct(
        EntrypointLookupCollectionInterface $entrypointLookupCollection,
        Packages $packages
    ) {
        $this->entrypointLookupCollection = $entrypointLookupCollection;
        $this->packages = $packages;
    }

    public static function generateNonce(RequestEvent $event)
    {
        static::$nonce = mt_rand(1, 1000);
    }

    public function create(array $defaultAttributes = []): TagRenderer
    {
        $defaultAttributes['nonce'] = static::$nonce;

        return new TagRenderer($this->entrypointLookupCollection, $this->packages, $defaultAttributes);
    }

    public function addNonceToHeader(ResponseEvent $event)
    {
        $response = $event->getResponse();
        $cspHeader = "script-src 'nonce-" . static::$nonce . "';";

        $response->headers->set('Content-Security-Policy', $cspHeader);
    }

    public static function getSubscribedEvents()
    {
        return [
            KernelEvents::REQUEST => 'generateNonce',
            KernelEvents::RESPONSE => 'addNonceToHeader',
        ];
    }
}

//Then declare the service definition

  webpack_encore.tag_renderer:
    class: Symfony\WebpackEncoreBundle\Asset\TagRenderer
    factory: ['@App\NonceFactory', 'create']

  App\NonceFactory:
    tags:
      - { name: kernel.event_subscriber }

This is the response header

HTTP/1.1 200 OK
Server: nginx
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/7.3.14
Cache-Control: max-age=0, must-revalidate, private
Date: Sun, 03 May 2020 21:21:21 GMT
Content-Security-Policy: script-src 'nonce-928'; <---- 
X-Debug-Token: 335f03
X-Robots-Tag: noindex
Expires: Sun, 03 May 2020 21:21:21 GMT
Content-Encoding: gzip

NOTE
The profiler uses by default the csp directive so must be turned off
because the nonce conflicts with nonce off app

web_profiler:
    toolbar: false
    intercept_redirects: false

@weaverryan weaverryan changed the base branch from master to main November 16, 2020 19:53
@BruceGitHub
Copy link
Author

@wouterj @weaverryan what think of this Pr
I close ?

@jrushlow jrushlow added the Feature New Feature label Jul 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New Feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants