Skip to content

Commit f26f286

Browse files
committed
minor #20362 [AssetMapper] Document usage of strict-dynamic in a CSP (nicwortel)
This PR was merged into the 6.4 branch. Discussion ---------- [AssetMapper] Document usage of `strict-dynamic` in a CSP AssetMapper will include special importmap entries for CSS files, which get resolved to `data:application/javascript`. See https://symfony.com/doc/current/frontend/asset_mapper.html#handling-css. Browsers will report those as CSP violations, as `data:` scripts can also be used for XSS attacks. For the same reason, allowing `data:` in the CSP is not a safe solution. symfony/symfony#58416 (comment) provides a solution: using `strict-dynamic` in the `script-src` directive will allow the importmap to include other resources. This PR adds that solution to the documentation. Commits ------- 358ece7 [AssetMapper] Document usage of `strict-dynamic` in a CSP
2 parents fae981f + 358ece7 commit f26f286

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

frontend/asset_mapper.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ from inside ``app.js``:
402402
// things on "window" become global variables
403403
window.$ = $;
404404
405+
.. _asset-mapper-handling-css:
406+
405407
Handling CSS
406408
------------
407409

@@ -1103,6 +1105,24 @@ it in the CSP header, and then pass the same nonce to the Twig function:
11031105
{# the csp_nonce() function is defined by the NelmioSecurityBundle #}
11041106
{{ importmap('app', {'nonce': csp_nonce('script')}) }}
11051107
1108+
Content Security Policy and CSS Files
1109+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1110+
1111+
If your importmap includes CSS files, AssetMapper uses a trick to load those by
1112+
adding ``data:application/javascript`` to the rendered importmap (see
1113+
:ref:`Handling CSS <asset-mapper-handling-css>`).
1114+
This can cause browsers to report CSP violations and block the CSS files from
1115+
being loaded.
1116+
To prevent this, you can add `strict-dynamic`_ to the ``script-src`` directive
1117+
of your Content Security Policy, to tell the browser that the importmap is
1118+
allowed to load other resources.
1119+
1120+
.. note::
1121+
1122+
When using ``strict-dynamic``, the browser will ignore any other sources in
1123+
``script-src`` such as ``'self'`` or ``'unsafe-inline'``, so any other
1124+
``<script>`` tags will also need to be trusted via a nonce.
1125+
11061126
The AssetMapper Component Caching System in dev
11071127
-----------------------------------------------
11081128

@@ -1186,5 +1206,6 @@ command as part of your CI to be warned anytime a new vulnerability is found.
11861206
.. _`package.json configuration file`: https://docs.npmjs.com/creating-a-package-json-file
11871207
.. _Content Security Policy: https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
11881208
.. _NelmioSecurityBundle: https://symfony.com/bundles/NelmioSecurityBundle/current/index.html#nonce-for-inline-script-handling
1209+
.. _strict-dynamic: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#strict-dynamic
11891210
.. _kocal/biome-js-bundle: https://github.com/Kocal/BiomeJsBundle
11901211
.. _`SensioLabs Minify Bundle`: https://github.com/sensiolabs/minify-bundle

0 commit comments

Comments
 (0)