File type icons contain an inline <style> without a CSP nonce
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 68/100
Research direction
Read templates/components/Icon.html.twig and inspect the SVGs in assets/icons/filetypes/, then trace how field-file-upload.js inserts the icon rendered by ea_fileupload_widget. Check how the layout applies csp_nonce('style') and how the icon styling is defined. Done when file type icons retain their base fill and stroke colors under an enforced CSP and the inline style no longer causes a violation.
Written by the indexing model from the issue text.
Description
Describe the bug
The SVG files in assets/icons/filetypes/ (generic.svg, pdf.svg, image.svg and the others) each start with an inline <style type="text/css"> element that defines .ea-icon-filetype-base-fill and .ea-icon-filetype-base-stroke, with a dark-mode variant.
templates/components/Icon.html.twig prints the SVG as it is ({{ icon.svgContents|raw }}), so the <style> element reaches the page without a nonce attribute.
Since #7381 and #7395 the layout calls csp_nonce('style'), so with NelmioSecurityBundle the response has a nonce in style-src. Browsers then ignore 'unsafe-inline' for <style> elements, and the style of these icons is refused.
In report-only mode a violation is reported each time a file is chosen in a file upload field. With an enforced policy the style is blocked, so the icon loses its base fill and stroke colours, in light and dark mode.
To Reproduce
EasyAdmin 5.10.4 (the files are the same on the 5.x branch today).
- Use NelmioSecurityBundle with a Content Security Policy whose
style-srcis'self'(report-only is enough to see it). - Have a field that uses
FileUploadTypein a CRUD controller. - Open the "new" page and choose a file that is not an image.
The browser reports:
Content Security Policy violation: style-src-elem, blocked-uri "inline",
source-file ".../bundles/easyadmin/field-file-upload.<hash>.js"
It happens when field-file-upload.js builds the card of the chosen file: #createCardElement() parses the data-icon-generic attribute of the widget (rendered by ea_fileupload_widget in crud/form_theme.html.twig) and appends it to the page.
I saw it with a custom field that uses FileUploadType; I did not try the stock FileField on a clean install, but it uses the same form type and script. I expect the same on an edit page that lists existing files, where ea_fileupload_widget renders <twig:ea:Icon name="filetypes:..."> directly, but I have only confirmed the case above in a browser.
(OPTIONAL) Additional context
Possible solutions:
- Move the two rules (and their dark-mode variant) to the bundle stylesheet and remove the
<style>element from the SVG files. This also avoids repeating the same rules once per icon. - Or add the nonce in the
Iconcomponent whencsp_nonceexists, as the layout does for its own<style>and<link>elements. Thedata-icon-*attributes then carry it too, because they are rendered with the same component.
Workaround: override templates/bundles/EasyAdminBundle/components/Icon.html.twig and add the nonce to the <style> element of the SVG:
{% set svg_contents = icon.svgContents %}
{% if '<style' in svg_contents %}
{% guard function csp_nonce %}
{% set svg_contents = svg_contents|replace({'<style': '<style nonce="' ~ csp_nonce('style') ~ '"'}) %}
{% endguard %}
{% endif %}
{{ svg_contents|raw }}
I can open a pull request for either solution if that helps.
- Dominant language
- PHP
- Stars
- 4.3k
- Forks
- 1.1k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 52
Getting set up
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from EasyCorp/EasyAdminBundle
-
design feature
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
EasyCorp/EasyAdminBundle#7930 ·
Maintainers usually reply within 2 days
-
design
Difficulty 2/5 Under an hour Newbie friendliness 88/100
EasyCorp/EasyAdminBundle#7879 ·
Maintainers usually reply within 2 days
-
design
Difficulty 1/5 Under an hour Newbie friendliness 90/100
EasyCorp/EasyAdminBundle#7875 ·
Maintainers usually reply within 2 days
-
#[AdminRoute]: the requirements of an action silently replace the ones of the classPossibly taken @anthonyhacart claimed this 1 day ago. Openfeature
Difficulty 3/5 Half a day Newbie friendliness 30/100
EasyCorp/EasyAdminBundle#7956 ·
Maintainers usually reply within 2 days
-
Difficulty 3/5 Half a day Newbie friendliness 42/100
EasyCorp/EasyAdminBundle#7954 ·
Maintainers usually reply within 2 days
All issues in EasyCorp/EasyAdminBundle
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 84/100
-
area/import type/enhancement when/later
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
parime/glpi-iso27001-management#1165 ·
Maintainers usually reply within 1 day
-
15.0
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
opnsense/plugins#5976 · 1 comment ·
Maintainers usually reply within 1 day