Skip to content

Commit 073d430

Browse files
committed
Fix Plausible CSP violations with strict-dynamic
Add CSP nonces to Plausible script tags and stop fragment-caching the partial so nonces stay per-request. Remove unused plausible.io host allowlist from script-src since strict-dynamic ignores host sources.
1 parent cc1f277 commit 073d430

6 files changed

Lines changed: 7 additions & 9 deletions

File tree

app/views/application/_plausible.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<% if ENV.key?('PLAUSIBLE_DOMAIN') && Rails.env == "production" %>
2-
<script async src="https://plausible.io/js/<%= ENV['PLAUSIBLE_DOMAIN'] %>.js"></script>
3-
<script>
2+
<script nonce="<%= request.content_security_policy_nonce %>" async src="https://plausible.io/js/<%= ENV['PLAUSIBLE_DOMAIN'] %>.js"></script>
3+
<script nonce="<%= request.content_security_policy_nonce %>">
44
window.plausible=window.plausible||function(){(plausible.q=plausible.q||[]).push(arguments)},plausible.init=plausible.init||function(i){plausible.o=i||{}};
55
plausible.init()
66
</script>

app/views/layouts/application.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<%= render partial: "shared/layout_icons", cached: true %>
1212

1313
<%= render partial: "application/ga", cached: true %>
14-
<%= render partial: "application/plausible", cached: true %>
14+
<%= render partial: "application/plausible" %>
1515

1616
<%= csrf_meta_tags %>
1717
<%= csp_meta_tag if defined?(csp_meta_tag) %>

app/views/layouts/bare.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<%= render partial: "shared/layout_icons", cached: true %>
1212

1313
<%= render partial: "application/ga", cached: true %>
14-
<%= render partial: "application/plausible", cached: true %>
14+
<%= render partial: "application/plausible" %>
1515

1616
<%= csrf_meta_tags %>
1717
<%= csp_meta_tag if defined?(csp_meta_tag) %>

app/views/layouts/login.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<%= render partial: "shared/layout_icons", cached: true %>
1212

1313
<%= render partial: "application/ga", cached: true %>
14-
<%= render partial: "application/plausible", cached: true %>
14+
<%= render partial: "application/plausible" %>
1515

1616
<%= csrf_meta_tags %>
1717
<%= csp_meta_tag if defined?(csp_meta_tag) %>

app/views/layouts/naked.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<%= render partial: "shared/layout_icons", cached: true %>
1212

1313
<%= render partial: "application/ga", cached: true %>
14-
<%= render partial: "application/plausible", cached: true %>
14+
<%= render partial: "application/plausible" %>
1515

1616
<%= csrf_meta_tags %>
1717
<%= csp_meta_tag if defined?(csp_meta_tag) %>

config/initializers/content_security_policy.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
policy.img_src :self, :https, :http, :data, :blob
1414
policy.media_src :self, :https, :http, :data, :blob
1515
policy.object_src :none
16-
script_src = [:strict_dynamic, :self, :https, :http]
17-
script_src << "https://plausible.io" if ENV.key?("PLAUSIBLE_DOMAIN")
18-
policy.script_src(*script_src)
16+
policy.script_src :strict_dynamic, :self, :https, :http
1917
policy.style_src :self, :https, :http, :unsafe_inline
2018
policy.style_src_attr :unsafe_inline
2119
policy.connect_src :self, :https, :http, :ws, :wss

0 commit comments

Comments
 (0)