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

cspscript for encore_entry_script_tags and cspstyle for encore_entry_link_tags #314

Open
thomas2411 opened this issue Aug 30, 2022 · 4 comments

Comments

@thomas2411
Copy link

cspscript and cspstyle works only for one script at a time. Both encore_entry_script_tags and encore_entry_link_tags generate multiple <script> and <style> tags by design. Do you think of any solution that could handle this situation? The only thing I can imagine is to add a new tags cspscripts and cspstyle to NelmioSecurityBundle which will handle multiple tags in a loop.
What do you think about it?

@sebbio
Copy link

sebbio commented Apr 5, 2024

Any idea?

@sebbio
Copy link

sebbio commented Apr 5, 2024

I make like this and work fine:
{{ encore_entry_link_tags('backend', null, '_default', {'nonce': csp_nonce('style')}) }}

@oleg-andreyev
Copy link

problem with csp_nonce it always generate new nonce and replaces prev. one and making it invalid.

imo it should behave similar to cspscript and cspstyle and pileup array of nonce.

@micheh
Copy link

micheh commented Aug 15, 2024

For Webpack Encore, you can use an EventSubscriber to automatically add the nonce to the script or link tags.

For example (only script tags):

final class EncoreNonceSubscriber implements EventSubscriberInterface
{
    private ContentSecurityPolicyListener $csp;

    public function __construct(ContentSecurityPolicyListener $csp)
    {
        $this->csp = $csp;
    }

    public static function getSubscribedEvents(): array
    {
        return [RenderAssetTagEvent::class => 'onRenderAssetTag'];
    }

    public function onRenderAssetTag(RenderAssetTagEvent $event): void
    {
        if ($event->isScriptTag()) {
            $event->setAttribute('nonce', $this->csp->getNonce('script'));
        }
    }
}

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

4 participants