Skip to content

Commit 6dcd66f

Browse files
Merge pull request #36 from aeris/csp-nonce
Support for nonce CSP
2 parents c57b4b3 + 16473ad commit 6dcd66f

File tree

1 file changed

+9
-8
lines changed
  • lib/rails_live_reload/middleware

1 file changed

+9
-8
lines changed

lib/rails_live_reload/middleware/base.rb

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ def call!(env)
1515
@app.call(env)
1616
end
1717
else
18-
request = Rack::Request.new(env)
18+
request = ActionDispatch::Request.new(env)
1919
status, headers, body = @app.call(env)
2020

2121
if html?(headers) && (status == 500 || (status.to_s =~ /20./ && request.get?))
22-
return inject_rails_live_reload(status, headers, body)
22+
return inject_rails_live_reload(request, status, headers, body)
2323
end
2424

2525
[status, headers, body]
@@ -28,25 +28,26 @@ def call!(env)
2828

2929
private
3030

31-
def inject_rails_live_reload(status, headers, body)
31+
def inject_rails_live_reload(request, status, headers, body)
3232
response = Rack::Response.new([], status, headers)
33-
33+
34+
nonce = request&.content_security_policy_nonce
3435
if String === body
35-
response.write make_new_response(body)
36+
response.write make_new_response(body, nonce)
3637
else
37-
body.each { |fragment| response.write make_new_response(fragment) }
38+
body.each { |fragment| response.write make_new_response(fragment, nonce) }
3839
end
3940
body.close if body.respond_to?(:close)
4041
response.finish
4142
end
4243

43-
def make_new_response(body)
44+
def make_new_response(body, nonce)
4445
index = body.rindex(/<\/body>/i) || body.rindex(/<\/html>/i)
4546
return body if index.nil?
4647

4748
body.insert(index, <<~HTML.html_safe)
4849
<script defer type="text/javascript" src="#{RailsLiveReload.config.url}/script"></script>
49-
<script id="rails-live-reload-options" type="application/json">
50+
<script id="rails-live-reload-options" type="application/json" nonce="#{nonce}">
5051
#{{
5152
files: CurrentRequest.current.data.to_a,
5253
time: Time.now.to_i,

0 commit comments

Comments
 (0)