rack-disable_css_animations 0.3.0 → 0.5.0

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: 066fb57f2552ab1537853e3133bcaaf887f15aa475192748c8ac14a5b69372f8
4
- data.tar.gz: b2006f786cbe9d6c4f7e2fb300e34cecfb9e9244964f79e282386766fd3f48e2
3
+ metadata.gz: ea999d1e3559763383af7d5ee86a43a11d39db3b96a94f6ddd9b4e475948217c
4
+ data.tar.gz: 93fe65b137aeab7996bf2251eea15533a96954654113c754c908ca3408d7f7c8
5
5
  SHA512:
6
- metadata.gz: de2919ae4ef71a3a6f18f286ae746113963f3e7606a90da80b99650b1b7d7ab93775966548f0f29e106cbcb6cd01b97c2bc780fe38e2dd21208ec49029335255
7
- data.tar.gz: 1a8c171793683cb954db7598324fa3982901e606144b1b34ef7c948ac84bc4ccc0b0abbb736acdd5c91aaa4f3b0e363af45409805709bd9dfa977e542d5f85e8
6
+ metadata.gz: 33e7ed70f886c5b1ac11fcb98bc4783afdcd902dfae88f007180dcc773f9655115f02736decdb5fd530ad35a1a885c02f8fb53755c57ae2f9b7f412f18788cec
7
+ data.tar.gz: 252f743b815f325d8173caf19338987efa87a0e027b9208521eac101c4dcc9c931dfc2f6cf912fac8161b94acaa842dc1665a8c3ebe1501a132ea3a6db749177
@@ -0,0 +1,18 @@
1
+ name: CI
2
+ on: [push, pull_request]
3
+ jobs:
4
+ test:
5
+ strategy:
6
+ matrix:
7
+ ruby: [ "3.3", "3.4", "4.0" ]
8
+
9
+ runs-on: ubuntu-latest
10
+ steps:
11
+ - uses: actions/checkout@v5
12
+ - name: Set up Ruby
13
+ uses: ruby/setup-ruby@v1
14
+ with:
15
+ ruby-version: ${{ matrix.ruby }}
16
+ bundler-cache: true
17
+ - name: Run the default task
18
+ run: bundle exec rake
data/CHANGELOG.md ADDED
@@ -0,0 +1,28 @@
1
+ # Changelog
2
+
3
+ ## 0.5.0
4
+
5
+ - Add CSP nonce support: when the response's `Content-Security-Policy` header sets a `style-src 'nonce-…'`, the injected `<style>` tag now carries a matching `nonce` attribute so it is not blocked by CSP.
6
+
7
+ ## 0.4.0
8
+
9
+ - Add stub for manual requiring.
10
+ - Automatically add to the middleware stack when required after Rails.
11
+
12
+ ## 0.3.0
13
+
14
+ - Disable `scroll-behavior` as well.
15
+ - CSS prefixes are no longer needed.
16
+
17
+ ## 0.2.0
18
+
19
+ - Actually disable the animations.
20
+ - `0` is not a valid value for the `animation-duration` property.
21
+
22
+ ## 0.1.1
23
+
24
+ - Use prefix methods too (PhantomJS needed the `-webkit` prefix).
25
+
26
+ ## 0.1.0
27
+
28
+ - Initial release.
data/README.md CHANGED
@@ -8,21 +8,9 @@ Add this line to your application's Gemfile:
8
8
 
9
9
  gem 'rack-disable_css_animations'
10
10
 
11
- And then execute:
12
-
13
- $ bundle
14
-
15
- Or install it yourself as:
16
-
17
- $ gem install rack-disable_css_animations
18
-
19
11
  ## Usage
20
12
 
21
- If using Rails, add the middleware to your test environment in `config/environments/test.rb`:
22
-
23
- ```ruby
24
- config.middleware.use Rack::DisableCSSAnimations
25
- ```
13
+ If using Rails, this will be automatically added to your middleware stack when required after Rails, so only require it in the environments you want it in.
26
14
 
27
15
  ## Contributing
28
16
 
data/Rakefile CHANGED
@@ -1,2 +1,10 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rake/testtask"
2
3
 
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/test_*.rb"]
8
+ end
9
+
10
+ task default: :test
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class DisableCSSAnimations
3
- VERSION = "0.3.0"
3
+ VERSION = "0.5.0"
4
4
  end
5
5
  end
@@ -2,6 +2,12 @@ require "rack/disable_css_animations/version"
2
2
 
3
3
  module Rack
4
4
  class DisableCSSAnimations
5
+ if defined?(Rails)
6
+ class Rails < Rails::Railtie
7
+ config.app_middleware.use DisableCSSAnimations
8
+ end
9
+ end
10
+
5
11
  def initialize app
6
12
  @app = app
7
13
  end
@@ -10,6 +16,8 @@ module Rack
10
16
  @status, @headers, @body = @app.call(env)
11
17
  return [@status, @headers, @body] unless html?
12
18
 
19
+ @style_nonce = directive_nonces["style-src"]
20
+
13
21
  response = Rack::Response.new([], @status, @headers)
14
22
  @body.each do |fragment|
15
23
  response.write inject(fragment)
@@ -25,9 +33,27 @@ module Rack
25
33
  @headers["Content-Type"] =~ /html/
26
34
  end
27
35
 
36
+ def csp_header
37
+ @headers["Content-Security-Policy"] || @headers["content-security-policy"] || ""
38
+ end
39
+
40
+ def directive_nonces
41
+ csp_header.split(";").each_with_object({}) do |directive, nonces|
42
+ tokens = directive.split
43
+ name = tokens.shift
44
+ next unless name
45
+ nonce = tokens.find { |t| t =~ /\A'nonce-(.+)'\z/ } && $1
46
+ nonces[name] = nonce if nonce
47
+ end
48
+ end
49
+
50
+ def style_tag
51
+ @style_nonce ? %(<style nonce="#{@style_nonce}">) : "<style>"
52
+ end
53
+
28
54
  def inject response
29
55
  markup = <<-CSS
30
- <style>
56
+ #{style_tag}
31
57
  * {
32
58
  animation-delay: 0s !important;
33
59
  animation-duration: 0.01s !important;
@@ -0,0 +1 @@
1
+ require "rack/disable_css_animations"
@@ -22,4 +22,6 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_development_dependency "bundler"
24
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "minitest"
26
+ spec.add_development_dependency "rack-test"
25
27
  end
@@ -0,0 +1,84 @@
1
+ require "minitest/autorun"
2
+ require "rack/test"
3
+ require "rack/disable_css_animations"
4
+
5
+ class TestDisableCSSAnimations < Minitest::Test
6
+ include Rack::Test::Methods
7
+
8
+ HTML_BODY = "<html><head><title>Test</title></head><body>hi</body></html>"
9
+
10
+ attr_accessor :response_status, :response_headers, :response_body
11
+
12
+ def app
13
+ outer_self = self
14
+ Rack::DisableCSSAnimations.new(lambda do |_env|
15
+ [outer_self.response_status, outer_self.response_headers, [outer_self.response_body]]
16
+ end)
17
+ end
18
+
19
+ def setup
20
+ self.response_status = 200
21
+ self.response_headers = { "Content-Type" => "text/html" }
22
+ self.response_body = HTML_BODY
23
+ end
24
+
25
+ def test_non_html_response_is_passed_through_unchanged
26
+ self.response_headers = { "Content-Type" => "application/json" }
27
+ self.response_body = %({"foo":"bar"})
28
+
29
+ get "/"
30
+
31
+ assert_equal %({"foo":"bar"}), last_response.body
32
+ end
33
+
34
+ def test_html_response_injects_style_tag
35
+ get "/"
36
+
37
+ assert_includes last_response.body, "<style>"
38
+ assert_includes last_response.body, "animation-duration: 0.01s !important"
39
+ refute_includes last_response.body, "nonce="
40
+ end
41
+
42
+ def test_injects_before_closing_head
43
+ get "/"
44
+
45
+ style_index = last_response.body.index("<style")
46
+ head_close_index = last_response.body.index("</head>")
47
+ assert style_index < head_close_index
48
+ end
49
+
50
+ def test_style_src_nonce_is_copied_onto_style_tag
51
+ self.response_headers["Content-Security-Policy"] = "style-src 'nonce-abc123' 'self'; script-src 'nonce-xyz789'"
52
+
53
+ get "/"
54
+
55
+ assert_includes last_response.body, %(<style nonce="abc123">)
56
+ refute_includes last_response.body, "<style>"
57
+ end
58
+
59
+ def test_csp_without_style_src_nonce_injects_plain_style_tag
60
+ self.response_headers["Content-Security-Policy"] = "default-src 'self'; script-src 'nonce-xyz789'"
61
+
62
+ get "/"
63
+
64
+ assert_includes last_response.body, "<style>"
65
+ refute_includes last_response.body, "nonce="
66
+ end
67
+
68
+ def test_csp_with_style_src_but_no_nonce_injects_plain_style_tag
69
+ self.response_headers["Content-Security-Policy"] = "style-src 'self' 'unsafe-inline'"
70
+
71
+ get "/"
72
+
73
+ assert_includes last_response.body, "<style>"
74
+ refute_includes last_response.body, "nonce="
75
+ end
76
+
77
+ def test_lowercase_csp_header_is_also_recognized
78
+ self.response_headers = { "Content-Type" => "text/html", "content-security-policy" => "style-src 'nonce-lower1'" }
79
+
80
+ get "/"
81
+
82
+ assert_includes last_response.body, %(<style nonce="lower1">)
83
+ end
84
+ end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-disable_css_animations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2022-09-22 00:00:00.000000000 Z
10
+ date: 2026-04-17 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rack
@@ -52,6 +51,34 @@ dependencies:
52
51
  - - ">="
53
52
  - !ruby/object:Gem::Version
54
53
  version: '0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: minitest
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ - !ruby/object:Gem::Dependency
69
+ name: rack-test
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
55
82
  description: Rack middleware to disable CSS animations sitewide. Useful for making
56
83
  acceptance tests quicker and more deterministic.
57
84
  email:
@@ -61,20 +88,23 @@ executables:
61
88
  extensions: []
62
89
  extra_rdoc_files: []
63
90
  files:
91
+ - ".github/workflows/main.yml"
64
92
  - ".gitignore"
93
+ - CHANGELOG.md
65
94
  - Gemfile
66
95
  - LICENSE.txt
67
96
  - README.md
68
97
  - Rakefile
69
98
  - bin/setup
99
+ - lib/rack-disable_css_animations.rb
70
100
  - lib/rack/disable_css_animations.rb
71
101
  - lib/rack/disable_css_animations/version.rb
72
102
  - rack-disable_css_animations.gemspec
103
+ - test/test_disable_css_animations.rb
73
104
  homepage: https://github.com/botandrose/rack-disable_css_animations
74
105
  licenses:
75
106
  - MIT
76
107
  metadata: {}
77
- post_install_message:
78
108
  rdoc_options: []
79
109
  require_paths:
80
110
  - lib
@@ -89,8 +119,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
119
  - !ruby/object:Gem::Version
90
120
  version: '0'
91
121
  requirements: []
92
- rubygems_version: 3.2.32
93
- signing_key:
122
+ rubygems_version: 3.6.2
94
123
  specification_version: 4
95
124
  summary: Rack middleware to disable CSS animations sitewide.
96
- test_files: []
125
+ test_files:
126
+ - test/test_disable_css_animations.rb