rack-dedos 0.4.1 → 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: a6136473af715369f93cdd16ed44fbbbdc241ee0efa1c7743a5590735ec5d392
4
- data.tar.gz: cd541b5cfb7cd9dfdb4262670d8190bf942215a65e6a984e48e9e7e7f7db1f6c
3
+ metadata.gz: ec975fa6b00c228589811832180fae90d61ba898ef4b08881c9f7897d06ee4d1
4
+ data.tar.gz: 51d214aa83afb5fb9c3d1397be8ef7ba35c26bf9dfc77a0dd1d9ea3b69864558
5
5
  SHA512:
6
- metadata.gz: caf872690a89e1e502974fa7560a5e5aa6499545a213cb82b2e03b788cda2075822ae1a8be23e23ff67a220087d56c6fa6d76a7388d6b4887c8f0a6e51f10f0c
7
- data.tar.gz: 14a31dd383c251c9333968744c91ebb9833e7dc501b68db2cda7e2c1c10b4f44090dafbdb1edf1de1f9042dba0338c44279852478a48a1bbd1a5394b05078b4a
6
+ metadata.gz: 20594ffae8646abd32f009a6bf203cc464720489e60885e1ebd60e60054c84c0ad0563c3f50dc7e248277bbc239db8592c699faf1e5c395f71572da4ef08ff78
7
+ data.tar.gz: 859501a8bb90c6823c413b4bc30bb597491699eb009820e9d61875eee15e4e4f970225bd66b8206cd81c78f24fa8f1cd97a355b070222dee78db0e7543833290
data/CHANGELOG.md CHANGED
@@ -2,6 +2,21 @@
2
2
 
3
3
  Nothing so far
4
4
 
5
+ ## 0.5.0
6
+
7
+ ### Changes
8
+ * Update to Ruby 4.0
9
+ * Require Minitest >= 6
10
+
11
+ ## 0.4.2
12
+
13
+ ### Changes
14
+ * Use [BreakVer](https://www.taoensso.com/break-versioning) from this point forward
15
+
16
+ ### Additions
17
+ * Include the requested URL in warnings
18
+ * Add `only_paths` and `except_paths` options
19
+
5
20
  ## 0.4.1
6
21
 
7
22
  ### Fixes
data/README.md CHANGED
@@ -31,14 +31,56 @@ And then install the bundle:
31
31
  bundle install
32
32
  ```
33
33
 
34
- ## Configuration
34
+ ⚠️ This gem initially followed [SemVer](https://semver.org) loosely but transitioned to [BreakVer](https://www.taoensso.com/break-versioning) starting from version 0.4.2 onwards.
35
35
 
36
- Given the drastic nature of the filters, you should use this middleware for production environments only and/or if an environment variable like `UNDER_ATTACK` is set to true.
36
+ ## Usage
37
+
38
+ You should use an environment variable such as `UNDER_ATTACK` to enable Rack::Dedos only when your app is actually under attack.
39
+
40
+ ### Rackup
41
+
42
+ ```ruby
43
+ #!/usr/bin/env rackup
44
+ require 'rack/dedos'
45
+
46
+ if %w(true t on 1).include? ENV['UNDER_ATTACK']
47
+ use Rack::Dedos
48
+ end
49
+
50
+ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, world!\n"] }
51
+ ```
52
+
53
+ ### Hanami
54
+
55
+ ```ruby
56
+ # config/settings.rb
57
+
58
+ module MyApp
59
+ class Settings < Hanami::Settings
60
+ setting :under_attack, default: false, constructor: Types::Params::Bool
61
+ end
62
+ end
63
+ ```
64
+
65
+ ```ruby
66
+ # config/app.rb
67
+
68
+ module MyApp
69
+ class App < Hanami::App
70
+ environment(:production) do
71
+ if setting.under_attack
72
+ middleware.use Rack::Dedos
73
+ end
74
+ end
75
+ end
76
+ end
77
+ ```
37
78
 
38
79
  ### Rails
39
80
 
40
81
  ```ruby
41
82
  # config/application.rb
83
+
42
84
  class Application < Rails::Application
43
85
  if Rails.env.production? && ActiveModel::Type::Boolean.new.cast(ENV['UNDER_ATTACK'])
44
86
  config.middleware.use Rack::Dedos
@@ -46,19 +88,26 @@ class Application < Rails::Application
46
88
  end
47
89
  ```
48
90
 
49
- ### Rackup
91
+ ## Configuration
50
92
 
51
- ```ruby
52
- #!/usr/bin/env rackup
53
- require 'rack/dedos'
93
+ Given the drastic nature of the filters, you should use this middleware for production environments only and/or if an environment variable like `UNDER_ATTACK` is set to true.
54
94
 
55
- if %w(true t on 1).include? ENV['UNDER_ATTACK']
56
- use Rack::Dedos
57
- end
95
+ ### Request
58
96
 
59
- run lambda { |env| [200, {'Content-Type' => 'text/plain'}, "Hello, world!\n"] }
97
+ By default, filters are applied to all request paths. You can norrow this to only certain matching request paths:
98
+
99
+ ```ruby
100
+ use Rack::Dedos,
101
+ only_paths: [%r(^/search), %r(\.xml$)]
102
+
103
+ use Rack::Dedos,
104
+ except_paths: [%r(^/$)]
60
105
  ```
61
106
 
107
+ If you set both `only_paths` and `except_paths`, the latter take precedence.
108
+
109
+ ⚠️ The request path does not include GET parameters.
110
+
62
111
  ### Response
63
112
 
64
113
  If a request is classified as malicious by at least one filter, the middleware responds with:
@@ -6,9 +6,11 @@ module Rack
6
6
  class Base
7
7
 
8
8
  DEFAULT_OPTIONS = {
9
+ only_paths: [],
10
+ except_paths: [],
9
11
  status: 403,
10
12
  text: 'Forbidden (Temporarily Blocked by Rules)'
11
- }
13
+ }.freeze
12
14
 
13
15
  attr_reader :app
14
16
  attr_reader :options
@@ -25,10 +27,10 @@ module Rack
25
27
  def call(env)
26
28
  request = Rack::Request.new(env)
27
29
  ip = real_ip(request)
28
- if allowed?(request, ip)
30
+ if !apply?(request) || allowed?(request, ip)
29
31
  app.call(env)
30
32
  else
31
- message = "rack-dedos: request from #{ip} blocked by #{self.class}"
33
+ message = "rack-dedos: request #{request.path} from #{ip} blocked by #{name}"
32
34
  warn([message, details].compact.join(": "))
33
35
  [options[:status], { 'Content-Type' => 'text/plain' }, [options[:text]]]
34
36
  end
@@ -40,6 +42,12 @@ module Rack
40
42
  Rack::Dedos.config
41
43
  end
42
44
 
45
+ def apply?(request)
46
+ return false if @options[:except_paths].any? { request.path.match? _1 }
47
+ return true if @options[:only_paths].none?
48
+ @options[:only_paths].any? { request.path.match? _1 }
49
+ end
50
+
43
51
  # Get the real IP of the client
44
52
  #
45
53
  # If containers and/or proxies such as Cloudflare are in the mix, the
@@ -7,6 +7,10 @@ module Rack
7
7
  module Filters
8
8
  class Country < Base
9
9
 
10
+ def name
11
+ :country
12
+ end
13
+
10
14
  # @option options [String] :maxmind_db_file MaxMind database file
11
15
  # @option options [Symbol, Array<Symbol>] :allowed_countries ISO 3166-1 alpha 2
12
16
  # @option options [Symbol, Array<Symbol>] :denied_countries ISO 3166-1 alpha 2
@@ -5,6 +5,10 @@ module Rack
5
5
  module Filters
6
6
  class UserAgent < Base
7
7
 
8
+ def name
9
+ :user_agent
10
+ end
11
+
8
12
  # @option options [String] :cache_url URL of the cache backend
9
13
  # @option options [Integer] :cache_period how long to retain cached IP
10
14
  # addresses in seconds (default: 900)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Rack
4
4
  module Dedos
5
- VERSION = "0.4.1"
5
+ VERSION = "0.5.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-dedos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Schwyn
@@ -85,16 +85,16 @@ dependencies:
85
85
  requirements:
86
86
  - - ">="
87
87
  - !ruby/object:Gem::Version
88
- version: '0'
88
+ version: 6.0.0
89
89
  type: :development
90
90
  prerelease: false
91
91
  version_requirements: !ruby/object:Gem::Requirement
92
92
  requirements:
93
93
  - - ">="
94
94
  - !ruby/object:Gem::Version
95
- version: '0'
95
+ version: 6.0.0
96
96
  - !ruby/object:Gem::Dependency
97
- name: minitest-flash
97
+ name: minitest-mock
98
98
  requirement: !ruby/object:Gem::Requirement
99
99
  requirements:
100
100
  - - ">="
@@ -108,7 +108,7 @@ dependencies:
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  - !ruby/object:Gem::Dependency
111
- name: minitest-focus
111
+ name: minitest-flash
112
112
  requirement: !ruby/object:Gem::Requirement
113
113
  requirements:
114
114
  - - ">="
@@ -223,7 +223,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
223
223
  - !ruby/object:Gem::Version
224
224
  version: '0'
225
225
  requirements: []
226
- rubygems_version: 3.6.9
226
+ rubygems_version: 4.0.3
227
227
  specification_version: 4
228
228
  summary: Radical filters to block denial-of-service (DoS) requests.
229
229
  test_files: []