cloudflare-rails 6.0.0 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f52bcf9825a5051914653aa8b471e173896ca7b0345c88b30512a7f5ca5792ca
4
- data.tar.gz: ddfe50aa060ef4971b9722658ae75dee9580ce1b2c028973f5caa877e370e7a3
3
+ metadata.gz: '079b27f2bb751ae57b6ba12fa77c04abe62900241146ad7163b5c1f354a6a7a5'
4
+ data.tar.gz: 820820c94a3e782deae3ed94d67d9cc145c976b0a82fa313ee448e8f29708e3b
5
5
  SHA512:
6
- metadata.gz: ca4faedbdaf7f45af3212865abf420cdc1077bd9c0339f507425f3ce11a520101dedc9d8023b70bfe15d8c163fd6dd21d74a55f785255b95956bdb5fe6dbcc16
7
- data.tar.gz: 1f7fe45788911c43bf9897e17b9a410615d8554e75cd0e6971dd81a398c0c1fa420ecb6c1c036a4b37d1604bdfcd5aa6d928c5a2ce07e7761c3628f3263c1360
6
+ metadata.gz: a50b5891b48031f1a8a92ebfd08d63f4196df71abb14b5b4d24ab283f81170e3a68c6e04440d27a4c6ad7180030125bbbcff94ba708590536407b72b836acf82
7
+ data.tar.gz: 1bbfa4fd1c77980454d38c76cb9c9ec1da912ada1460d0554d4b1da534a41c3e8e1022876b9090c6161223ecfcf61ddd1ce9868cd6654dab7eeba3b34fd81c06
data/.rspec CHANGED
@@ -1,4 +1,2 @@
1
1
  --format d
2
2
  --color
3
- --format RspecJunitFormatter
4
- --out tmp/rspec/rspec-<%= File.basename ENV['BUNDLE_GEMFILE'] %><%= ENV["RACK_ATTACK"] ? "-rack-attack-#{ENV["RACK_ATTACK"]}" : '' %>.xml
data/.rubocop_todo.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2023-12-16 21:20:23 UTC using RuboCop version 1.59.0.
3
+ # on 2024-06-13 00:13:47 UTC using RuboCop version 1.64.1.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
@@ -11,7 +11,7 @@
11
11
  # AllowedMethods: enums
12
12
  Lint/ConstantDefinitionInBlock:
13
13
  Exclude:
14
- - 'spec/cloudflare/rails_spec.rb'
14
+ - "spec/cloudflare/rails_spec.rb"
15
15
 
16
16
  # Offense count: 1
17
17
  # This cop supports unsafe autocorrection (--autocorrect-all).
@@ -19,13 +19,13 @@ Lint/ConstantDefinitionInBlock:
19
19
  # AllowedMethods: instance_of?, kind_of?, is_a?, eql?, respond_to?, equal?, presence, present?
20
20
  Lint/RedundantSafeNavigation:
21
21
  Exclude:
22
- - 'spec/cloudflare/rails_spec.rb'
22
+ - "spec/cloudflare/rails_spec.rb"
23
23
 
24
24
  # Offense count: 1
25
25
  # Configuration parameters: AllowComments, AllowNil.
26
26
  Lint/SuppressedException:
27
27
  Exclude:
28
- - 'lib/cloudflare_rails/check_trusted_proxies.rb'
28
+ - "lib/cloudflare_rails/check_trusted_proxies.rb"
29
29
 
30
30
  # Offense count: 1
31
31
  # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
@@ -43,26 +43,26 @@ Metrics/MethodLength:
43
43
  # AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS
44
44
  Naming/FileName:
45
45
  Exclude:
46
- - 'lib/cloudflare-rails.rb'
46
+ - "lib/cloudflare-rails.rb"
47
47
 
48
48
  # Offense count: 2
49
49
  # Configuration parameters: ForbiddenDelimiters.
50
50
  # ForbiddenDelimiters: (?i-mx:(^|\s)(EO[A-Z]{1}|END)(\s|$))
51
51
  Naming/HeredocDelimiterNaming:
52
52
  Exclude:
53
- - 'lib/cloudflare_rails/fallback_ips.rb'
53
+ - "lib/cloudflare_rails/fallback_ips.rb"
54
54
 
55
55
  # Offense count: 3
56
56
  RSpec/AnyInstance:
57
57
  Exclude:
58
- - 'spec/cloudflare/rails_spec.rb'
58
+ - "spec/cloudflare/rails_spec.rb"
59
59
 
60
60
  # Offense count: 1
61
61
  # Configuration parameters: Prefixes, AllowedPatterns.
62
62
  # Prefixes: when, with, without
63
63
  RSpec/ContextWording:
64
64
  Exclude:
65
- - 'spec/cloudflare/rails_spec.rb'
65
+ - "spec/cloudflare/rails_spec.rb"
66
66
 
67
67
  # Offense count: 2
68
68
  # This cop supports safe autocorrection (--autocorrect).
@@ -70,19 +70,12 @@ RSpec/ContextWording:
70
70
  # DisallowedExamples: works
71
71
  RSpec/ExampleWording:
72
72
  Exclude:
73
- - 'spec/cloudflare/rails_spec.rb'
74
-
75
- # Offense count: 1
76
- # Configuration parameters: Include, CustomTransform, IgnoreMethods, SpecSuffixOnly.
77
- # Include: **/*_spec*rb*, **/spec/**/*
78
- RSpec/FilePath:
79
- Exclude:
80
- - 'spec/cloudflare/rails_spec.rb'
73
+ - "spec/cloudflare/rails_spec.rb"
81
74
 
82
75
  # Offense count: 1
83
76
  RSpec/LeakyConstantDeclaration:
84
77
  Exclude:
85
- - 'spec/cloudflare/rails_spec.rb'
78
+ - "spec/cloudflare/rails_spec.rb"
86
79
 
87
80
  # Offense count: 5
88
81
  RSpec/MultipleExpectations:
@@ -98,7 +91,7 @@ RSpec/MultipleMemoizedHelpers:
98
91
  # SupportedStyles: always, named_only
99
92
  RSpec/NamedSubject:
100
93
  Exclude:
101
- - 'spec/cloudflare/rails_spec.rb'
94
+ - "spec/cloudflare/rails_spec.rb"
102
95
 
103
96
  # Offense count: 16
104
97
  # Configuration parameters: AllowedGroups.
@@ -110,19 +103,19 @@ RSpec/NestedGroups:
110
103
  # Include: **/*_spec.rb
111
104
  RSpec/SpecFilePathFormat:
112
105
  Exclude:
113
- - 'spec/cloudflare/rails_spec.rb'
106
+ - "spec/cloudflare/rails_spec.rb"
114
107
 
115
108
  # Offense count: 1
116
109
  # This cop supports unsafe autocorrection (--autocorrect-all).
117
110
  Rails/ApplicationController:
118
111
  Exclude:
119
- - 'spec/cloudflare/rails_spec.rb'
112
+ - "spec/cloudflare/rails_spec.rb"
120
113
 
121
114
  # Offense count: 1
122
115
  # This cop supports unsafe autocorrection (--autocorrect-all).
123
116
  Rails/CompactBlank:
124
117
  Exclude:
125
- - 'lib/cloudflare_rails/importer.rb'
118
+ - "lib/cloudflare_rails/importer.rb"
126
119
 
127
120
  # Offense count: 3
128
121
  # This cop supports unsafe autocorrection (--autocorrect-all).
@@ -130,14 +123,14 @@ Rails/CompactBlank:
130
123
  # Include: **/Rakefile, **/*.rake
131
124
  Rails/RakeEnvironment:
132
125
  Exclude:
133
- - 'Rakefile'
126
+ - "Rakefile"
134
127
 
135
128
  # Offense count: 1
136
129
  # This cop supports safe autocorrection (--autocorrect).
137
130
  # Configuration parameters: AllowOnConstant, AllowOnSelfClass.
138
131
  Style/CaseEquality:
139
132
  Exclude:
140
- - 'lib/cloudflare_rails/check_trusted_proxies.rb'
133
+ - "lib/cloudflare_rails/check_trusted_proxies.rb"
141
134
 
142
135
  # Offense count: 2
143
136
  # This cop supports safe autocorrection (--autocorrect).
@@ -147,32 +140,22 @@ Style/CaseEquality:
147
140
  Style/FormatStringToken:
148
141
  EnforcedStyle: unannotated
149
142
 
150
- # Offense count: 15
143
+ # Offense count: 13
151
144
  # This cop supports unsafe autocorrection (--autocorrect-all).
152
145
  # Configuration parameters: EnforcedStyle.
153
146
  # SupportedStyles: always, always_true, never
154
147
  Style/FrozenStringLiteralComment:
155
148
  Exclude:
156
- - 'Appraisals'
157
- - 'Gemfile'
158
- - 'Rakefile'
159
- - 'cloudflare-rails.gemspec'
160
- - 'gemfiles/rails_6.1.gemfile'
161
- - 'gemfiles/rails_7.0.gemfile'
162
- - 'gemfiles/rails_7.1.gemfile'
163
- - 'lib/cloudflare_rails.rb'
164
- - 'lib/cloudflare_rails/check_trusted_proxies.rb'
165
- - 'lib/cloudflare_rails/importer.rb'
166
- - 'lib/cloudflare_rails/railtie.rb'
167
- - 'lib/cloudflare_rails/remote_ip_proxies.rb'
168
- - 'lib/cloudflare_rails/version.rb'
169
- - 'spec/cloudflare/rails_spec.rb'
170
- - 'spec/spec_helper.rb'
149
+ - "Appraisals"
150
+ - "Gemfile"
151
+ - "Rakefile"
152
+ - "cloudflare-rails.gemspec"
153
+ - "gemfiles/rails_7.1.gemfile"
171
154
 
172
155
  # Offense count: 1
173
156
  Style/MultilineBlockChain:
174
157
  Exclude:
175
- - 'lib/cloudflare_rails/railtie.rb'
158
+ - "lib/cloudflare_rails/railtie.rb"
176
159
 
177
160
  # Offense count: 2
178
161
  # This cop supports safe autocorrection (--autocorrect).
data/Appraisals CHANGED
@@ -3,7 +3,7 @@ appraise 'rails-7.1' do
3
3
  end
4
4
 
5
5
  appraise 'rails-7.2' do
6
- gem 'rails', github: 'rails/rails', branch: '7-2-stable'
6
+ gem 'rails', '~> 7.2.0'
7
7
  end
8
8
 
9
9
  appraise 'rails-8.0' do
data/CHANGELOG.md CHANGED
@@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file.
3
3
 
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+ ## [6.1.0]
7
+ - Add cloudflare? method to determine if request passed through CF (https://github.com/modosc/cloudflare-rails/pull/149)
8
+
6
9
  ## [6.0.0] - 2024-06-12
7
10
  - Drop support for `rails` version `6.1` and `7.0`, new minimum version is `7.1.0` (https://github.com/modosc/cloudflare-rails/pull/142)
8
11
  - Bump minimum ruby version to `3.1.0` in preparation for `rails` version `7.2` (https://github.com/modosc/cloudflare-rails/pull/142)
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # CloudflareRails [![Gem Version](https://badge.fury.io/rb/cloudflare-rails.svg)](https://badge.fury.io/rb/cloudflare-rails)
2
- This gem correctly configures Rails for [CloudFlare](https://www.cloudflare.com) so that `request.remote_ip` / `request.ip` both work correctly.
2
+ This gem correctly configures Rails for [CloudFlare](https://www.cloudflare.com) so that `request.remote_ip` / `request.ip` both work correctly. It also exposes a `#cloudflare?` method on `Rack::Request`.
3
3
 
4
4
  ## Rails Compatibility
5
5
 
@@ -14,7 +14,6 @@ This gem requires `railties`, `activesupport`, and `actionpack` >= `7.1`. For ol
14
14
  | 5.1 | 2.0.0 |
15
15
  | 5.0 | 2.0.0 |
16
16
  | 4.2 | 0.1.0 |
17
- | ----- | ------- |
18
17
 
19
18
  ## Installation
20
19
 
@@ -48,13 +47,31 @@ Unfortunately this does not fix `request.ip`. This method comes from the [Rack::
48
47
 
49
48
  These issues are why this gem patches both `Rack::Request::Helpers` and `ActionDispatch::RemoteIP` rather than using the built-in configuration methods.
50
49
 
50
+ ## Prerequisites
51
+ You must have a [`cache_store`](https://guides.rubyonrails.org/caching_with_rails.html#configuration) configured in your `rails` application.
52
+
51
53
  ## Usage
52
- You can configure the HTTP `timeout` and `expires_in` cache parameters inside of your rails config:
54
+ You can configure the HTTP `timeout` and `expires_in` cache parameters inside of your `rails` config:
53
55
  ```ruby
54
56
  config.cloudflare.expires_in = 12.hours # default value
55
57
  config.cloudflare.timeout = 5.seconds # default value
56
58
  ```
57
59
 
60
+ ## Blocking non-Cloudflare traffic
61
+ You can use the `#cloudfront?` method from this gem to block all non-Cloudflare traffic to your application. Here's an example of doing this with [`Rack::Attack`](https://github.com/rack/rack-attack):
62
+ ```ruby
63
+ Rack::Attack.blocklist('CloudFlare WAF bypass') do |req|
64
+ !req.cloudflare?
65
+ end
66
+ ```
67
+ Note that the request may optionally pass through additional trusted proxies, so it will return true for any of these scenarios:
68
+
69
+ * `REMOTE_ADDR: CloudFlare`
70
+ * `REMOTE_ADDR: trusted_proxy`, `X_HTTP_FORWARDED_FOR: CloudFlare`
71
+ * `REMOTE_ADDR: trusted_proxy`, `X_HTTP_FORWARDED_FOR: trusted_proxy2,CloudFlare,...`
72
+
73
+ but it will return false if CloudFlare comes after the trusted prefix of `X-Forwarded-For`.
74
+
58
75
  ## Alternatives
59
76
 
60
77
  [actionpack-cloudflare](https://github.com/customink/actionpack-cloudflare) simpler approach using the `CF-Connecting-IP` header.
@@ -23,12 +23,11 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency 'rack-attack', '~> 6.7.0'
24
24
  spec.add_development_dependency 'rake', '~> 13.2.1'
25
25
  spec.add_development_dependency 'rspec', '~> 3.13.0'
26
- spec.add_development_dependency 'rspec_junit_formatter', '~> 0.6.0'
27
- spec.add_development_dependency 'rspec-rails', '~> 6.1.2'
28
- spec.add_development_dependency 'rubocop', '~> 1.64.1'
29
- spec.add_development_dependency 'rubocop-performance', '~> 1.21.0'
30
- spec.add_development_dependency 'rubocop-rails', '~> 2.25.0'
31
- spec.add_development_dependency 'rubocop-rspec', '~> 2.26.1'
26
+ spec.add_development_dependency 'rspec-rails', '~> 7.0.1'
27
+ spec.add_development_dependency 'rubocop', '~> 1.66.1'
28
+ spec.add_development_dependency 'rubocop-performance', '~> 1.22.1'
29
+ spec.add_development_dependency 'rubocop-rails', '~> 2.26.1'
30
+ spec.add_development_dependency 'rubocop-rspec', '~> 3.0.1'
32
31
  spec.add_development_dependency 'webmock', '~> 3.23.1'
33
32
 
34
33
  spec.add_dependency 'actionpack', '>= 7.1.0', '< 8.1.0'
@@ -4,6 +4,6 @@
4
4
 
5
5
  source 'https://rubygems.org'
6
6
 
7
- gem 'rails', github: 'rails/rails', branch: '7-2-stable'
7
+ gem 'rails', '~> 7.2.0'
8
8
 
9
9
  gemspec path: '../'
@@ -1,13 +1,32 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CloudflareRails
2
4
  # patch rack::request::helpers to use our cloudflare ips - this way request.ip is
3
5
  # correct inside of rack and rails
4
6
  module CheckTrustedProxies
5
- def trusted_proxy?(ip)
6
- matching = Importer.cloudflare_ips.any? do |proxy|
7
+ def cloudflare_ip?(ip)
8
+ Importer.cloudflare_ips.any? do |proxy|
7
9
  proxy === ip
8
10
  rescue IPAddr::InvalidAddressError
9
11
  end
10
- matching || super
12
+ end
13
+
14
+ def trusted_proxy?(ip)
15
+ cloudflare_ip?(ip) || super
16
+ end
17
+
18
+ def cloudflare?
19
+ remote_addresses = split_header(get_header('REMOTE_ADDR'))
20
+ forwarded_for = self.forwarded_for || []
21
+
22
+ # Select only the trusted prefix of REMOTE_ADDR + X_HTTP_FORWARDED_FOR
23
+ trusted_proxies = (remote_addresses + forwarded_for).take_while do |ip|
24
+ trusted_proxy?(ip)
25
+ end
26
+
27
+ trusted_proxies.any? do |ip|
28
+ cloudflare_ip?(ip)
29
+ end
11
30
  end
12
31
  end
13
32
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'net/http'
2
4
  require 'uri'
3
5
 
@@ -19,9 +21,9 @@ module CloudflareRails
19
21
  end
20
22
  end
21
23
 
22
- BASE_URL = 'https://www.cloudflare.com'.freeze
23
- IPS_V4_URL = '/ips-v4/'.freeze
24
- IPS_V6_URL = '/ips-v6/'.freeze
24
+ BASE_URL = 'https://www.cloudflare.com'
25
+ IPS_V4_URL = '/ips-v4/'
26
+ IPS_V6_URL = '/ips-v6/'
25
27
 
26
28
  class << self
27
29
  def ips_v6
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/core_ext/integer/time'
2
4
 
3
5
  module CloudflareRails
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CloudflareRails
2
4
  # patch ActionDispatch::RemoteIP to use our cloudflare ips - this way
3
5
  # request.remote_ip is correct inside of rails
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CloudflareRails
2
- VERSION = '6.0.0'.freeze
4
+ VERSION = '6.1.0'
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'zeitwerk'
2
4
  loader = Zeitwerk::Loader.for_gem
3
5
  loader.ignore("#{__dir__}/cloudflare-rails.rb")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloudflare-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.0
4
+ version: 6.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jonathan schatz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-06-13 00:00:00.000000000 Z
11
+ date: 2024-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: appraisal
@@ -94,90 +94,76 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: 3.13.0
97
- - !ruby/object:Gem::Dependency
98
- name: rspec_junit_formatter
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - "~>"
102
- - !ruby/object:Gem::Version
103
- version: 0.6.0
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: 0.6.0
111
97
  - !ruby/object:Gem::Dependency
112
98
  name: rspec-rails
113
99
  requirement: !ruby/object:Gem::Requirement
114
100
  requirements:
115
101
  - - "~>"
116
102
  - !ruby/object:Gem::Version
117
- version: 6.1.2
103
+ version: 7.0.1
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
107
  requirements:
122
108
  - - "~>"
123
109
  - !ruby/object:Gem::Version
124
- version: 6.1.2
110
+ version: 7.0.1
125
111
  - !ruby/object:Gem::Dependency
126
112
  name: rubocop
127
113
  requirement: !ruby/object:Gem::Requirement
128
114
  requirements:
129
115
  - - "~>"
130
116
  - !ruby/object:Gem::Version
131
- version: 1.64.1
117
+ version: 1.66.1
132
118
  type: :development
133
119
  prerelease: false
134
120
  version_requirements: !ruby/object:Gem::Requirement
135
121
  requirements:
136
122
  - - "~>"
137
123
  - !ruby/object:Gem::Version
138
- version: 1.64.1
124
+ version: 1.66.1
139
125
  - !ruby/object:Gem::Dependency
140
126
  name: rubocop-performance
141
127
  requirement: !ruby/object:Gem::Requirement
142
128
  requirements:
143
129
  - - "~>"
144
130
  - !ruby/object:Gem::Version
145
- version: 1.21.0
131
+ version: 1.22.1
146
132
  type: :development
147
133
  prerelease: false
148
134
  version_requirements: !ruby/object:Gem::Requirement
149
135
  requirements:
150
136
  - - "~>"
151
137
  - !ruby/object:Gem::Version
152
- version: 1.21.0
138
+ version: 1.22.1
153
139
  - !ruby/object:Gem::Dependency
154
140
  name: rubocop-rails
155
141
  requirement: !ruby/object:Gem::Requirement
156
142
  requirements:
157
143
  - - "~>"
158
144
  - !ruby/object:Gem::Version
159
- version: 2.25.0
145
+ version: 2.26.1
160
146
  type: :development
161
147
  prerelease: false
162
148
  version_requirements: !ruby/object:Gem::Requirement
163
149
  requirements:
164
150
  - - "~>"
165
151
  - !ruby/object:Gem::Version
166
- version: 2.25.0
152
+ version: 2.26.1
167
153
  - !ruby/object:Gem::Dependency
168
154
  name: rubocop-rspec
169
155
  requirement: !ruby/object:Gem::Requirement
170
156
  requirements:
171
157
  - - "~>"
172
158
  - !ruby/object:Gem::Version
173
- version: 2.26.1
159
+ version: 3.0.1
174
160
  type: :development
175
161
  prerelease: false
176
162
  version_requirements: !ruby/object:Gem::Requirement
177
163
  requirements:
178
164
  - - "~>"
179
165
  - !ruby/object:Gem::Version
180
- version: 2.26.1
166
+ version: 3.0.1
181
167
  - !ruby/object:Gem::Dependency
182
168
  name: webmock
183
169
  requirement: !ruby/object:Gem::Requirement