File type icons contain an inline <style> without a CSP nonce

Open Beginner friendly
#7,958 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
php, symfony
Domain
backend, security

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

bug design

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).

  1. Use NelmioSecurityBundle with a Content Security Policy whose style-src is 'self' (report-only is enough to see it).
  2. Have a field that uses FileUploadType in a CRUD controller.
  3. 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 Icon component when csp_nonce exists, as the layout does for its own <style> and <link> elements. The data-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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from EasyCorp/EasyAdminBundle

All issues in EasyCorp/EasyAdminBundle

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.