Skip to content

Conversation

@oxess
Copy link

@oxess oxess commented Jan 18, 2026

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:

  • Script tags lack the required nonce attribute
  • Inline onclick handlers are blocked by CSP's script-src directive

Solution:

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 onclick handlers with data attributes processed by JavaScript:

  • data-ea-form-submit="formId" - triggers form submission
  • data-ea-navigate="url" - triggers navigation

Backward Compatibility

Fully backward compatible - the {% guard %} tag gracefully skips nonce injection when NelmioSecurityBundle is not installed.

Fixes #7213

Enable EasyAdmin to work with strict CSP policies when using NelmioSecurityBundle.

Script Tag Nonces:
- Add nonce attribute to all script tags using `{% guard function csp_nonce %}`
- Gracefully falls back when NelmioSecurityBundle is not installed
- Applied to layout, crud, login templates, and dynamic JS assets

Inline Handler Refactoring:
- Replace CSP-violating `onclick` handlers with data attributes
- `data-ea-form-submit`: triggers form submission via JS event listener
- `data-ea-navigate`: triggers navigation via JS event listener
- New `#createCspCompliantEventHandlers()` method in app.js

Fully backward compatible - works with or without NelmioSecurityBundle.

Fixes EasyCorp#7213
@oxess
Copy link
Author

oxess commented Jan 21, 2026

@javiereguiluz what do you think?

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

Successfully merging this pull request may close these issues.

Support for Content Security Policy (CSP)

1 participant