wagtail: A list of widgets breaking strict Content-Security-Policy (CSP) directives

Is your proposal related to a problem?

See #1288. Wagtail uses inline javascript and styles to provide useful feedback to the user. This causes conflicts with CSP directives.

This issue attempts to list all of the potential places this occurs in the Wagtail codebase.

Directives to be reviewed:

  • script-src ((completed 2021-04-21))
  • img-src ((completed 2024-09-27))
  • ~font-src~ (no longer relevant)
  • style-src ((completed 2021-04-21))

Extra Notes

  • The JS library modernizer-2.6.2.min.js is blocked by the style-src: 'self' CSP. You can trigger this by visiting /admin/

img-src

img-src 'self' data: blob:;

See #12368.

font-src

No Wagtail font files as of a few years ago.

script-src: ‘self’

Wagtail Admin

Wagtail Admin Templates

  1. wagtail/admin/templates/wagtailadmin/

  2. wagtail/admin/templates/wagtailadmin/edit_handlers/

  3. wagtail/admin/templates/wagtailadmin/home/

  4. wagtail/admin/templates/wagtailadmin/pages/

  5. wagtail/admin/templates/wagtailadmin/permissions/includes/

  6. wagtail/admin/templates/wagtailadmin/reports/

  7. wagtail/admin/templates/wagtailadmin/shared/

  8. wagtail/admin/templates/wagtailadmin/widgets/

  9. wagtail/admin/templates/wagtailadmin/workflows/

Wagtail Contrib

Wagtail Documents

Wagtail Images

Wagtail Snippets

Wagtail Users

Wagtail Utils

style-src: ‘self’

Wagtail Embeds

Wagtail Admin

Contrib

Documents

Embeds

Images

Users

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 19 (13 by maintainers)

Commits related to this issue

Most upvoted comments

As far as I can see, aside from #12368 for img-src, we’ll only need fixes for style-src and script-src from now on. There are few remaining, so I’ve taken the time to now split them across separate issues so it’s easier for contributors to take on, and will now close this issue – we’re getting there!

CSP testing

Here is the March 2025 “strictest simplest CSP” which I believe can be used for the Wagtail admin:

default-src 'self';
script-src 'self' 'unsafe-inline';
style-src 'self' 'unsafe-inline';
img-src 'self' data: blob:;

And here is a variation compatible with the bakerydemo, which I would recommend contributors interested in CSP support make use of:

default-src 'self';
script-src 'self' 'report-sample';
style-src 'self' 'report-sample';
img-src 'self' blob: i.ytimg.com www.gravatar.com;
connect-src 'self' releases.wagtail.org;
frame-src 'self' www.youtube.com;

CSP scanner

I’ve also set up a pretty basic CSP scanner with browser automation, which I hope we could reuse to add QA checks for this in Wagtail at some point. Here is what that reported for bakerydemo, with each row being a separate instance of a CSP violation, and the row number in the corresponding file / HTML response.

Scan results

2025-03-01 bakerydemo style-src

url,line
icons.js,0
vendor.js,1
/media/images/vik.original.jpg,0
/documents/multiple/add/,551
/images/43/,783
/images/43/generate_url/,18
/images/multiple/add/,557
/pages/1/add_subpage/,298
/pages/1/add_subpage/,315
/pages/1/add_subpage/,332
/pages/1/add_subpage/,349
/pages/1/add_subpage/,366
/pages/1/add_subpage/,383
/pages/1/add_subpage/,400
/pages/1/add_subpage/,417
/styleguide/,1776
/styleguide/,3158
/styleguide/,3282
/styleguide/,3290
/styleguide/,694
/styleguide/,703
/styleguide/,712

2025-03-01 bakerydemo script-src

file,line
vendor.js,1
/forms/,601
/forms/,603
/forms/submissions/69/,576
/forms/submissions/69/,578
/groups/edit/1/,1613
/groups/edit/2/,1617
/pages/,606
/pages/,608
/pages/3/edit/,1820
/pages/34/edit/,1856
/pages/34/edit/,2223
/pages/60/,699
/pages/60/,701
/pages/60/edit/,1895
/pages/60/edit/,2148
/pages/60/edit/,2326
/pages/60/edit/,2764
/pages/60/edit/,2964
/pages/60/edit/,3164
/pages/60/history/,682
/pages/60/history/,684
/pages/61/edit/,1820
/pages/62/edit/,1930
/pages/62/edit/,2089
/pages/62/edit/,2320
/pages/62/edit/,2554
/pages/63/edit/,1820
/pages/65/edit/,1855
/pages/65/edit/,2331
/pages/65/edit/,2388
/pages/65/edit/,2639
/pages/65/edit/,2696
/pages/65/edit/,2947
/pages/65/edit/,3004
/pages/65/edit/,3255
/pages/65/edit/,3312
/pages/65/edit/,3563
/pages/65/edit/,3620
/pages/65/edit/,3871
/pages/65/edit/,3928
/pages/65/edit/,4179
/pages/65/edit/,4236
/pages/65/edit/,4640
/pages/68/edit/,1970
/pages/68/edit/,2129
/pages/68/edit/,2360
/pages/68/edit/,2594
/pages/69/edit/,1800
/pages/69/edit/,4560
/pages/70/edit/,1898
/pages/72/edit/,1930
/pages/72/edit/,2089
/pages/72/edit/,2320
/pages/72/edit/,2554
/pages/81/edit/,1699
/pages/81/edit/,2329
/pages/81/edit/,2563
/pages/82/copy/,476
/pages/82/edit/,1860
/pages/82/edit/,2490
/pages/82/edit/,2724
/pages/82/history/,728
/pages/82/history/,730
/pages/82/move/,401
/pages/add/base/formpage/1/,1342
/pages/add/base/formpage/1/,1932
/pages/add/base/gallerypage/1/,1456
/pages/add/base/homepage/1/,1312
/pages/add/base/homepage/1/,1565
/pages/add/base/homepage/1/,1743
/pages/add/base/homepage/1/,2181
/pages/add/base/homepage/1/,2381
/pages/add/base/homepage/1/,2581
/pages/add/base/standardpage/1/,1456
/pages/add/blog/blogindexpage/1/,1371
/pages/usage/base/formpage/,615
/pages/usage/base/formpage/,617
/redirects/1/,751
/redirects/add/,713
/reports/aging-pages/,623
/reports/locked/,616
/reports/locked/,618
/reports/site-history/,720
/reports/site-history/,722
/reports/workflow_tasks/,816
/reports/workflow_tasks/,818
/reports/workflow_tasks/,865
/reports/workflow_tasks/,867
/reports/workflow/,805
/reports/workflow/,807
/searchpicks/1/,75
/searchpicks/1/,814
/searchpicks/add/,75
/searchpicks/reports/search-terms/,586
/searchpicks/reports/search-terms/,588
/sites/edit/2/,750
/sites/new/,705
/snippets/base/footertext/,654
/snippets/base/footertext/,656
/snippets/base/person/,810
/snippets/base/person/,812
/snippets/base/person/add/,1245
/snippets/base/person/edit/1/,1598
/snippets/base/person/history/1/,716
/snippets/base/person/history/1/,718
/snippets/breads/breadingredient/,646
/snippets/breads/breadingredient/,648
/snippets/breads/breadtype/,582
/snippets/breads/breadtype/,584
/styleguide/,2068
/styleguide/,2110
/styleguide/,2152
/styleguide/,2659
/styleguide/,2772
/styleguide/,2886
/styleguide/,3000
/users/,691
/users/,693
/workflows/add/,818
/workflows/add/,994
/workflows/edit/1/,1009
/workflows/edit/1/,1192
/workflows/edit/1/,1311
/workflows/edit/1/,779
/workflows/usage/1/,655
/workflows/usage/1/,657

Thank you @thibaudcolas - this is a great report, we’ve had some incredible momentum on this in the last few years.

Thanks for splitting out the remaining tasks.

Reading up / based on our discussion in Slack, we’ll need to get rid of those style="" attributes in HTML/Python, and in JavaScript switch any usage of setAttribute('style') to adding individual properties on the style DOM attribute: https://stackoverflow.com/a/29089970/1798491.

I couldn’t see any setAttribute('style') in Wagtail’s JS code, but there could be in our dependencies.

For style attributes in HTML, I could see two based on the list above that will be tricky to change:

Does some clever math. This would either require a <style> attribute with a nonce and adding styles via a unique id to each iframe… or I think we might be able to refactor this to use the more modern aspect-ratio CSS property.

This might be easier by setting the values via JS.


All the other style attributes in HTML can either be replaced with CSS classes, or the hidden HTML attribute.