data_style_sanitizer 0.2.0 → 0.2.2

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: 22f6d0b814ca0d3bf8ecefe93afae4fd0833c3680d82f175e5bc1ba4bc8e04a6
4
- data.tar.gz: 72a1418a6b7941951c213ec3f53b4f5a7d319161fd8c4fad9a899493670c7e29
3
+ metadata.gz: a99ffd99fabc59fadd239621056e2bbcf385002c72a7f5a21b782318f2cc5561
4
+ data.tar.gz: d5e162d48cbb7c9817501129c4d5ab671a295b3c451914b95f6b57b2a5699ff5
5
5
  SHA512:
6
- metadata.gz: d7cfd17b311c9a945b004b20d60ea49127353c9db3029493c8c2c77fa48d9d0283c1d6d6d8d7f5fb0c064ea55b35d60bbcc855f06e1844047d02e7eeb070d2fd
7
- data.tar.gz: 5c6eacde4af0df37b358fa248dc68fcdefd2bc7956648cdc175f86f2c3d14069267a23d89a3fa1d45da32f01d4481297d24f9f5eef552488610b67381fc6a76c
6
+ metadata.gz: 076ecfb8d8c5b82f675cef01bcac30a09cedbcbe0902dde8f86ef0fb7fb8d56ef308e923ec9c2cf94c6a6907ea647868218ea97ce7475b160bbc653662590c1d
7
+ data.tar.gz: cbcd19bd0e21e3b44221fe871d1757e74a9509d6e0e63b0cbc07415c0bf1ae0feece7766f5f711a72b45eb7c60f11df4758f97d0b4ad7acc8373a6a2f69fa5a0
@@ -1,4 +1,4 @@
1
- require_relative "data_style_sanitizer/processor"
1
+ require_relative "processor"
2
2
 
3
3
  module DataStyleSanitizer
4
4
  class Middleware
@@ -32,8 +32,6 @@ module DataStyleSanitizer
32
32
  def extract_nonce_from_env(env)
33
33
  if env["action_dispatch.content_security_policy_nonce"].respond_to?(:call)
34
34
  env["action_dispatch.content_security_policy_nonce"].call(:style)
35
- else
36
- nil
37
35
  end
38
36
  end
39
37
  end
@@ -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?('data-style')
15
+ return unless html_response? && response.body.include?("data-style")
16
16
 
17
- nonce = content_security_policy_nonce(:style) rescue nil
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!('</head>', "#{style_block}</head>")
25
+ response.body.sub!("</head>", "#{style_block}</head>")
22
26
  end
23
27
 
24
28
  def html_response?
25
- response.content_type == 'text/html'
29
+ response.content_type == "text/html"
26
30
  end
27
31
  end
28
32
  end
@@ -1,4 +1,4 @@
1
- require 'rails/railtie'
1
+ require "rails/railtie"
2
2
 
3
3
  module DataStyleSanitizer
4
4
  class Railtie < Rails::Railtie
@@ -11,28 +11,37 @@ 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.sanitize_html(body, nonce: nonce)
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
- env.dig("action_dispatch.content_security_policy_nonce", :style)
35
+ if env.respond_to?(:dig)
36
+ env.dig("action_dispatch.content_security_policy_nonce", :style)
37
+ else
38
+ # get nonce from meta tag
39
+ # This is a fallback for older versions of Rails
40
+ meta_tag = env["rack.session"]&.dig("meta_tags", "csp-nonce")
41
+ if meta_tag
42
+ meta_tag.match(/nonce="([^"]+)"/)[1] if /nonce="([^"]+)"/.match?(meta_tag)
43
+ end
44
+ end
36
45
  end
37
- end
46
+ end
38
47
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DataStyleSanitizer
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.2"
5
5
  end
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "data_style_sanitizer/processor"
4
- require "data_style_sanitizer/railtie" if defined?(Rails)
3
+ require_relative "data_style_sanitizer/processor"
4
+ require_relative "data_style_sanitizer/railtie"
5
5
  require_relative "data_style_sanitizer/version"
6
+ require_relative "data_style_sanitizer/railtie"
7
+ require_relative "data_style_sanitizer/middleware"
6
8
 
7
9
  module DataStyleSanitizer
8
10
  class Error < StandardError; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_style_sanitizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - tedaford
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '7.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '7.1'
41
55
  description: This is a gem that converts data-style attributes into CSP-compliant
42
56
  nonced style blocks. It is designed to work with Rails applications and provides
43
57
  a simple interface for sanitizing HTML content.