data_style_sanitizer 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 92d2a1679e57bb4634ab85c0e4883314fde9f7380f0da02a81ef7939e7f7833c
|
4
|
+
data.tar.gz: 0a71f5c5c1ef2abf82d5630a055ff663d0597b663467b449a115119a3ac149f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb8c460227b1839353264d939293563ed458b7e6546c306784d1c69e95e5fb925ebf5e33109272cd3720184d2fed2d4791158864ec695b828608038cc15b077f
|
7
|
+
data.tar.gz: cbedd72c8a7601e7ff045055e75519db982cb7a100aeb8253b041512f451cdae3a703e9d44ac52c3916a4f2b91350a515b660c9d1aa7d7142d4991c4d737a3cb
|
@@ -12,17 +12,21 @@ module DataStyleSanitizer
|
|
12
12
|
private
|
13
13
|
|
14
14
|
def inject_data_style_sanitizer_styles
|
15
|
-
return unless html_response? && response.body.include?(
|
15
|
+
return unless html_response? && response.body.include?("data-style")
|
16
16
|
|
17
|
-
nonce =
|
17
|
+
nonce = begin
|
18
|
+
content_security_policy_nonce(:style)
|
19
|
+
rescue
|
20
|
+
nil
|
21
|
+
end
|
18
22
|
style_block = DataStyleSanitizer::Renderer.generate_style_block(response.body, nonce: nonce)
|
19
23
|
|
20
24
|
# Inject into <head>
|
21
|
-
response.body.sub!(
|
25
|
+
response.body.sub!("</head>", "#{style_block}</head>")
|
22
26
|
end
|
23
27
|
|
24
28
|
def html_response?
|
25
|
-
response.content_type ==
|
29
|
+
response.content_type == "text/html"
|
26
30
|
end
|
27
31
|
end
|
28
32
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "rails/railtie"
|
2
2
|
|
3
3
|
module DataStyleSanitizer
|
4
4
|
class Railtie < Rails::Railtie
|
@@ -11,28 +11,28 @@ module DataStyleSanitizer
|
|
11
11
|
def initialize(app)
|
12
12
|
@app = app
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
def call(env)
|
16
16
|
status, headers, response = @app.call(env)
|
17
|
-
|
17
|
+
|
18
18
|
if headers["Content-Type"]&.include?("text/html")
|
19
19
|
body = +""
|
20
20
|
response.each { |part| body << part }
|
21
|
-
|
21
|
+
|
22
22
|
nonce = extract_nonce(env)
|
23
|
-
new_body = DataStyleSanitizer.
|
24
|
-
|
23
|
+
new_body = DataStyleSanitizer.process(body, nonce: nonce)
|
24
|
+
|
25
25
|
headers["Content-Length"] = new_body.bytesize.to_s
|
26
26
|
[status, headers, [new_body]]
|
27
27
|
else
|
28
28
|
[status, headers, response]
|
29
29
|
end
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
private
|
33
|
-
|
33
|
+
|
34
34
|
def extract_nonce(env)
|
35
35
|
env.dig("action_dispatch.content_security_policy_nonce", :style)
|
36
36
|
end
|
37
|
-
end
|
37
|
+
end
|
38
38
|
end
|