Skip to content

Commit b72fe17

Browse files
committed
svelte/1: Add csp_script_nonce and csp_style_nonce parameters
1 parent e07aede commit b72fe17

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

lib/live_svelte.ex

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ defmodule LiveSvelte do
3030

3131
attr :csp_nonce, :string,
3232
default: nil,
33-
doc: "A Content-Security-Policy nonce for the <script> and <style> tags"
33+
doc: "A Content-Security-Policy nonce for the generated <script> and <style> tags"
34+
35+
attr :csp_script_nonce, :string,
36+
default: nil,
37+
doc: "A Content-Security-Policy nonce for the generated <script> tag"
38+
39+
attr :csp_style_nonce, :string,
40+
default: nil,
41+
doc: "A Content-Security-Policy nonce for the generated <style> tag"
3442

3543
attr :ssr, :boolean,
3644
default: true,
@@ -87,17 +95,22 @@ defmodule LiveSvelte do
8795
end
8896
end
8997

98+
csp_attrs = if(nonce = assigns.csp_nonce, do: [nonce: nonce])
99+
90100
assigns =
91101
assign(assigns,
92-
csp_attrs: if(nonce = assigns[:csp_nonce], do: [nonce: nonce]),
102+
csp_script_attrs:
103+
csp_attrs || if(nonce = assigns.csp_script_nonce, do: [nonce: nonce], else: []),
104+
csp_style_attrs:
105+
csp_attrs || if(nonce = assigns.csp_style_nonce, do: [nonce: nonce], else: []),
93106
init: init,
94107
slots: slots,
95108
ssr_render: ssr_code
96109
)
97110

98111
~H"""
99112
<.live_json live_json_props={@live_json_props}>
100-
<script {@csp_attrs}>
113+
<script {@csp_script_attrs}>
101114
<%= raw(@ssr_render["head"]) %>
102115
</script>
103116
<div
@@ -114,7 +127,7 @@ defmodule LiveSvelte do
114127
class={@class}
115128
>
116129
<%= raw(@ssr_render["head"]) %>
117-
<style {@csp_attrs}>
130+
<style {@csp_style_attrs}>
118131
<%= raw(@ssr_render["css"]["code"]) %>
119132
</style>
120133
<%= raw(@ssr_render["html"]) %>

0 commit comments

Comments
 (0)