symfony: Web Debug Toolbar script is blocked by CSP
Symfony version(s) affected
7.1.3
Description
UPDATE: errors do occur even if CSP is is completely disabled (https://github.com/symfony/symfony/issues/58223#issuecomment-2360043257)
The CSP is handled by the NelmioSecurityBundle.
When navigating using UX Turbo Drive the WDT does not show up and these errors arise:
Content-Security-Policy: The page’s settings blocked an inline script (script-src-elem) from being executed because it violates the following directive: “script-src https://127.0.0.1:5173/ 'nonce-Hie7bQmr1f4mayneR7+x+w==' 'unsafe-inline' 'nonce-1dca403eefc12cf2b625d960af5ce5c1'”
Sometimes it also shows this error:
Uncaught TypeError: ajaxToolbarPanel is null
renderAjaxRequests (index):521
finishAjaxRequest (index):521
load (index):521
onreadystatechange (index):521
request (index):521
load (index):521
loadToolbar (index):521
finishAjaxRequest (index):521
fetch (index):521
promise callback*window.fetch (index):521
fetchWithTurboHeaders turbo.es2017-esm.js:692
perform turbo.es2017-esm.js:823
setLater turbo.es2017-esm.js:1016
setTimeout handler*setLater turbo.es2017-esm.js:1015
LinkPrefetchObserver turbo.es2017-esm.js:3158
LinkPrefetchObserver turbo.es2017-esm.js:3124
start turbo.es2017-esm.js:3103
start turbo.es2017-esm.js:5153
start turbo.es2017-esm.js:5601
<anonymous> turbo.es2017-esm.js:6578
And I have a related question: Why is the WDT generating a second nonce? Why not use the existing one?
How to reproduce
symfony/web-profiler-bundle 7.1.3 nelmio/security-bundle 3.4.0 symfony/ux-turbo 2.18.0
DevSubscriber.php
<?php
namespace App\EventSubscriber;
use Symfony\Component\DependencyInjection\Attribute\When;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
#[When(env: 'dev')]
class DevSubscriber implements EventSubscriberInterface
{
private bool $isAjaxTurboDrive = false;
public function onKernelRequest(RequestEvent $event)
{
if (!$event->isMainRequest()) {return;}
$request = $event->getRequest();
// Turbo
$turboRequestID = $request->headers->get('x-turbo-request-id');
if ($turboRequestID)
{
$this->isAjaxTurboDrive = true;
}
}
public function onKernelResponse(ResponseEvent $event)
{
$response = $event->getResponse();
// These two lines make the WDT to show up, but errors remain. The nonce value is weak for testing purposes.
$response->headers->set('X-SymfonyProfiler-Style-Nonce', 'fffff');
$response->headers->set('X-SymfonyProfiler-Script-Nonce', 'fffff');
if ($this->isAjaxTurboDrive)
{
$response->headers->set('Symfony-Debug-Toolbar-Replace', '1');
}
}
public static function getSubscribedEvents() : array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
KernelEvents::RESPONSE => 'onKernelResponse',
];
}
}
?>
excerpt from nelmio_security.yaml:
...
csp:
hosts: []
content_types: []
enforce:
level1_fallback: false
browser_adaptive:
enabled: false
# report-uri: '%router.request_context.base_url%/nelmio/csp/report'
default-src:
- 'none'
style-src:
- 'self'
- https://www.gstatic.com https://fonts.googleapis.com # Google Fonts
font-src:
- 'self'
- https://fonts.googleapis.com https://fonts.gstatic.com # Google Fonts
img-src:
- 'self'
- 'data:'
form-action:
- 'self'
frame-src:
- 'self'
- 'blob:'
- https://www.google.com
frame-ancestors:
- 'self'
object-src:
- 'self'
media-src:
- 'self'
connect-src:
- 'self'
- 'wss:'
block-all-mixed-content: true # defaults to false, blocks HTTP content over HTTPS transport
# upgrade-insecure-requests: true # defaults to false, upgrades HTTP requests to HTTPS transport
...
Possible Solution
I managed to make the toolbar show up by setting both X-SymfonyProfiler-Style-Nonce and X-SymfonyProfiler-Script-Nonce (see DevSubscriber.php in “How to reproduce” section). However, I am still encountering the same errors in the console.
So possible solution is to make WDT reuse the same nonce when UX Turbo is installed. Here it is in more detail:
In method getNonces (in the ContentSecurityPolicyHandler) there it will check if the current request is coming from Turbo Drive:
$request->headers->has('x-turbo-request-id');
a) If not, then it presumes this request is an initial page load and continues to check if UX Turbo is installed and if yes, it sets a new nonce and saves it to session.
b) If this is a Turbo Drive request, then it will reuse nonce from the session. And here it should also trigger update of the WDT by setting the Symfony-Debug-Toolbar-Replace header (https://symfony.com/doc/current/profiler.html#updating-the-web-debug-toolbar-after-ajax-requests).
Additional Context
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 34 (14 by maintainers)
Created a new PR to solve this issue: https://github.com/symfony/ux/pull/2633