rack-steady_etag 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: 4e9c4ec38a103ceb321da1a48a78a09872e7d47875fbd0fa1e15814116f79e64
4
- data.tar.gz: cb4abb1d12ac1c2a06427f7e6f63de34c4d8b99c56248287470f2c4aa3be655a
3
+ metadata.gz: d3047cb500ace97518e12a5667bad5602d27b493f24d6a8a6c8236372fe7ca36
4
+ data.tar.gz: d9c3afe27e063227577e1921f6f4ce4e170f7f5627be0126096f1ed846ad7d1e
5
5
  SHA512:
6
- metadata.gz: ab95c964994e61c962903a4a395be6715bc2a8d69e1cb66f0eb19371c4691972ff8d2cd76d8740129715652d515f0f0704238afbae5e30be9527065e0b127d59
7
- data.tar.gz: f10b144400e01f9587e51f7f20330cb8a3d14381c4183ecfa84f9180d301b01986eeb1df3b1f498115eabab2161763106e8dde26c96e3ce9fc031eb94c56016e
6
+ metadata.gz: b86c5ec23d0d27170406530421acea4a54be83caf4c0513de01004136e92d2842aa771927f9ab8074bd918b83f3f10efc651b3e815dc8c5c2036769767c28077
7
+ data.tar.gz: b28cddf4577f20d1c0c75cdcf7c58d9ce5765f2148ab6fde4b9c0533725d09aa4c596b1624cab70c4e36a5b909e67153fd14ff95a272e9ef8fa0ee5ac81800c8
data/CHANGELOG.md CHANGED
@@ -5,6 +5,10 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html
5
5
 
6
6
  ## Unreleased
7
7
 
8
+ ## 0.2.1 - 2022-05-12
9
+
10
+ - Only strip patterns for HTML and XHTML responses.
11
+
8
12
  ## 0.2.0 - 2022-05-12
9
13
 
10
14
  - Be more compatible with Rack 2.2.2:
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rack-steady_etag (0.2.0)
4
+ rack-steady_etag (0.2.1)
5
5
  activesupport (>= 3.2)
6
6
  rack (~> 2.0)
7
7
 
data/README.md CHANGED
@@ -20,17 +20,27 @@ By default Rails uses [`Rack::ETag`](https://rdoc.info/github/rack/rack/Rack/ETa
20
20
  You can add your own patterns:
21
21
 
22
22
  ```ruby
23
- Rack::SteadyETag::IGNORED_PATTERNS << /<meta name="XSRF-TOKEN" value="[^"]+">/
23
+ Rack::SteadyETag::STRIP_PATTERNS << /<meta name="XSRF-TOKEN" value="[^"]+">/
24
24
  ```
25
25
 
26
26
  You can also push lambda for arbitrary transformations:
27
27
 
28
28
  ```ruby
29
- Rack::SteadyETag::IGNORED_PATTERNS << -> { |text| text.gsub(/<meta name="XSRF-TOKEN" value="[^"]+">/, '') }
29
+ Rack::SteadyETag::STRIP_PATTERNS << -> { |text| text.gsub(/<meta name="XSRF-TOKEN" value="[^"]+">/, '') }
30
30
  ```
31
31
 
32
32
  Transformations are only applied for the `ETag` hash. The response body will not be changed.
33
33
 
34
+ ## What responses are processed
35
+
36
+ This middleware will process responses that match all of the following:
37
+
38
+ - Responses with a HTTP status of 200 or 201
39
+ - Responses with a `Content-Type` of `text/html` or `application/xhtml+xml`
40
+ - Responses with a body.
41
+
42
+ This middleware can also add a default `Cache-Control` header for responses it *didn't* process. This is passed as an argument during middleware initialization (see *Installation* below).
43
+
34
44
  ## Covered edge cases
35
45
 
36
46
  - Different `ETags` are generated when the same content is accessed with different Rack sessions.
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rack
4
4
  class SteadyEtag
5
- VERSION = "0.2.0"
5
+ VERSION = "0.2.1"
6
6
  end
7
7
  end
@@ -19,20 +19,25 @@ module Rack
19
19
  # Yes, Rack::ETag sets a default Cache-Control for responses that it can digest.
20
20
  DEFAULT_CACHE_CONTROL = "max-age=0, private, must-revalidate"
21
21
 
22
- IGNORE_PATTERNS = [
22
+ STRIP_PATTERNS = [
23
23
  /<meta\b[^>]*\bname=(["'])csrf-token\1[^>]+>/i,
24
24
  /<meta\b[^>]*\bname=(["'])csp-nonce\1[^>]+>/i,
25
25
  /<input\b[^>]*\bname=(["'])authenticity_token\1[^>]+>/i,
26
26
  lambda { |string| string.gsub(/(<script\b[^>]*)\bnonce=(["'])[^"']+\2+/i, '\1') }
27
27
  ]
28
28
 
29
+ STRIP_CONTENT_TYPES = %w[
30
+ text/html
31
+ application/xhtml+xml
32
+ ]
33
+
29
34
  def initialize(app, no_digest_cache_control = nil, digest_cache_control = DEFAULT_CACHE_CONTROL)
30
35
  @app = app
31
36
 
32
37
  @digest_cache_control = digest_cache_control
33
38
 
34
- # Rails sets a default `Cache-Control: no-cache` for responses that
35
- # we cannot digest.
39
+ # Rails sets a default `Cache-Control: no-cache` for responses that we cannot digest.
40
+ # See https://github.com/rails/rails/blob/d96609505511a76c618dc3adfa3ca4679317d008/railties/lib/rails/application/default_middleware_stack.rb#L81
36
41
  @no_digest_cache_control = no_digest_cache_control
37
42
  end
38
43
 
@@ -94,20 +99,14 @@ module Rack
94
99
  parts = []
95
100
  digest = nil
96
101
 
102
+ strippable_response = STRIP_CONTENT_TYPES.include?(headers['Content-Type'])
103
+
97
104
  body.each do |part|
98
105
  parts << part
99
106
 
100
107
  if part.present?
101
- part = strip_ignore_patterns(part)
102
-
103
- unless digest
104
- digest = Digest::SHA256.new
105
-
106
- if session && (session_id = session['session_id'])
107
- digest << session_id.to_s
108
- end
109
- end
110
-
108
+ digest ||= initialize_digest(session)
109
+ part = strip_patterns(part) if strippable_response
111
110
  digest << part
112
111
  end
113
112
  end
@@ -119,12 +118,22 @@ module Rack
119
118
  [digest, parts]
120
119
  end
121
120
 
122
- def strip_ignore_patterns(html)
123
- IGNORE_PATTERNS.each do |ignore_pattern|
124
- if ignore_pattern.respond_to?(:call)
125
- html = ignore_pattern.call(html)
121
+ def initialize_digest(session)
122
+ digest = Digest::SHA256.new
123
+
124
+ if session && (session_id = session['session_id'])
125
+ digest << session_id.to_s
126
+ end
127
+
128
+ digest
129
+ end
130
+
131
+ def strip_patterns(html)
132
+ STRIP_PATTERNS.each do |pattern|
133
+ if pattern.respond_to?(:call)
134
+ html = pattern.call(html)
126
135
  else
127
- html = html.gsub(ignore_pattern, '')
136
+ html = html.gsub(pattern, '')
128
137
  end
129
138
  end
130
139
  html
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-steady_etag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henning Koch