Add Content Security Policy (CSP) nonce support #7381
+97
−42
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR enables EasyAdmin to work with strict Content Security Policy headers, particularly when using NelmioSecurityBundle's CSP features.
Problem: Applications with strict CSP policies cannot use EasyAdmin without violations because:
nonceattributeonclickhandlers are blocked by CSP'sscript-srcdirectiveSolution:
1. Script Tag Nonce Support
All script tags now conditionally include a nonce attribute when the
csp_nonce()Twig function is available:{% guard function csp_nonce %} <script src="app.js" nonce="{{ csp_nonce('script') }}"></script> {% else %} <script src="app.js"></script> {% endguard %}2. Inline Event Handler Refactoring
Replaced CSP-violating
onclickhandlers with data attributes processed by JavaScript:data-ea-form-submit="formId"- triggers form submissiondata-ea-navigate="url"- triggers navigationBackward Compatibility
Fully backward compatible - the
{% guard %}tag gracefully skips nonce injection when NelmioSecurityBundle is not installed.Fixes #7213