|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../spec_helper" |
| 4 | + |
| 5 | +describe "error handling" do |
| 6 | + |
| 7 | + let(:store) do |
| 8 | + ActiveSupport::Cache::MemoryStore.new |
| 9 | + end |
| 10 | + |
| 11 | + before do |
| 12 | + Rack::Attack.cache.store = store |
| 13 | + |
| 14 | + Rack::Attack.blocklist("fail2ban pentesters") do |request| |
| 15 | + Rack::Attack::Fail2Ban.filter(request.ip, maxretry: 0, bantime: 600, findtime: 30) { true } |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + describe '.ignored_errors' do |
| 20 | + before do |
| 21 | + allow(store).to receive(:read).and_raise(RuntimeError) |
| 22 | + end |
| 23 | + |
| 24 | + it 'has default value' do |
| 25 | + assert_equal Rack::Attack.ignored_errors, %w[Dalli::DalliError Redis::BaseError] |
| 26 | + end |
| 27 | + |
| 28 | + it 'can get and set value' do |
| 29 | + Rack::Attack.ignored_errors = %w[Foobar] |
| 30 | + assert_equal Rack::Attack.ignored_errors, %w[Foobar] |
| 31 | + end |
| 32 | + |
| 33 | + it 'can ignore error as Class' do |
| 34 | + Rack::Attack.ignored_errors = [RuntimeError] |
| 35 | + |
| 36 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 37 | + |
| 38 | + assert_equal 200, last_response.status |
| 39 | + end |
| 40 | + |
| 41 | + it 'can ignore error ancestor as Class' do |
| 42 | + Rack::Attack.ignored_errors = [StandardError] |
| 43 | + |
| 44 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 45 | + |
| 46 | + assert_equal 200, last_response.status |
| 47 | + end |
| 48 | + |
| 49 | + it 'can ignore error as String' do |
| 50 | + Rack::Attack.ignored_errors = %w[RuntimeError] |
| 51 | + |
| 52 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 53 | + |
| 54 | + assert_equal 200, last_response.status |
| 55 | + end |
| 56 | + |
| 57 | + it 'can ignore error error ancestor as String' do |
| 58 | + Rack::Attack.ignored_errors = %w[StandardError] |
| 59 | + |
| 60 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 61 | + |
| 62 | + assert_equal 200, last_response.status |
| 63 | + end |
| 64 | + |
| 65 | + it 'raises error if not ignored' do |
| 66 | + assert_raises(RuntimeError) do |
| 67 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + describe '.ignored_errors?' do |
| 73 | + |
| 74 | + it 'can match String or Class' do |
| 75 | + Rack::Attack.ignored_errors = ['ArgumentError', RuntimeError] |
| 76 | + assert Rack::Attack.ignored_error?(ArgumentError.new) |
| 77 | + assert Rack::Attack.ignored_error?(RuntimeError.new) |
| 78 | + refute Rack::Attack.ignored_error?(StandardError.new) |
| 79 | + end |
| 80 | + |
| 81 | + it 'can match Class ancestors' do |
| 82 | + Rack::Attack.ignored_errors = [StandardError] |
| 83 | + assert Rack::Attack.ignored_error?(ArgumentError.new) |
| 84 | + refute Rack::Attack.ignored_error?(Exception.new) |
| 85 | + end |
| 86 | + |
| 87 | + it 'can match String ancestors' do |
| 88 | + Rack::Attack.ignored_errors = ['StandardError'] |
| 89 | + assert Rack::Attack.ignored_error?(ArgumentError.new) |
| 90 | + refute Rack::Attack.ignored_error?(Exception.new) |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + describe '.error_handler' do |
| 95 | + before do |
| 96 | + Rack::Attack.error_handler = error_handler if defined?(error_handler) |
| 97 | + allow(store).to receive(:read).and_raise(ArgumentError) |
| 98 | + end |
| 99 | + |
| 100 | + it 'can get and set value' do |
| 101 | + Rack::Attack.error_handler = :test |
| 102 | + assert_equal Rack::Attack.error_handler, :test |
| 103 | + end |
| 104 | + |
| 105 | + describe 'Proc which returns :block' do |
| 106 | + let(:error_handler) { ->(_error) { :block } } |
| 107 | + |
| 108 | + it 'blocks the request' do |
| 109 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 110 | + |
| 111 | + assert_equal 403, last_response.status |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + describe 'Proc which returns :throttle' do |
| 116 | + let(:error_handler) { ->(_error) { :throttle } } |
| 117 | + |
| 118 | + it 'throttles the request' do |
| 119 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 120 | + |
| 121 | + assert_equal 429, last_response.status |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + describe 'Proc which returns :allow' do |
| 126 | + let(:error_handler) { ->(_error) { :allow } } |
| 127 | + |
| 128 | + it 'allows the request' do |
| 129 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 130 | + |
| 131 | + assert_equal 200, last_response.status |
| 132 | + end |
| 133 | + end |
| 134 | + |
| 135 | + describe 'Proc which returns nil' do |
| 136 | + let(:error_handler) { ->(_error) { nil } } |
| 137 | + |
| 138 | + it 'allows the request' do |
| 139 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 140 | + |
| 141 | + assert_equal 200, last_response.status |
| 142 | + end |
| 143 | + end |
| 144 | + |
| 145 | + describe 'Proc which re-raises the error' do |
| 146 | + let(:error_handler) { ->(error) { raise error } } |
| 147 | + |
| 148 | + it 'raises the error' do |
| 149 | + assert_raises(ArgumentError) do |
| 150 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 151 | + end |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + describe ':block' do |
| 156 | + let(:error_handler) { :block } |
| 157 | + |
| 158 | + it 'blocks the request' do |
| 159 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 160 | + |
| 161 | + assert_equal 403, last_response.status |
| 162 | + end |
| 163 | + end |
| 164 | + |
| 165 | + describe ':throttle' do |
| 166 | + let(:error_handler) { :throttle } |
| 167 | + |
| 168 | + it 'throttles the request' do |
| 169 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 170 | + |
| 171 | + assert_equal 429, last_response.status |
| 172 | + end |
| 173 | + end |
| 174 | + |
| 175 | + describe ':allow' do |
| 176 | + let(:error_handler) { :allow } |
| 177 | + |
| 178 | + it 'allows the request' do |
| 179 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 180 | + |
| 181 | + assert_equal 200, last_response.status |
| 182 | + end |
| 183 | + end |
| 184 | + |
| 185 | + describe 'non-nil value' do |
| 186 | + let(:error_handler) { true } |
| 187 | + |
| 188 | + it 'allows the request' do |
| 189 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 190 | + |
| 191 | + assert_equal 200, last_response.status |
| 192 | + end |
| 193 | + end |
| 194 | + |
| 195 | + describe 'nil' do |
| 196 | + let(:error_handler) { nil } |
| 197 | + |
| 198 | + it 'raises the error' do |
| 199 | + assert_raises(ArgumentError) do |
| 200 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 201 | + end |
| 202 | + end |
| 203 | + end |
| 204 | + |
| 205 | + describe 'when error ignored' do |
| 206 | + let(:error_handler) { :throttle } |
| 207 | + |
| 208 | + before do |
| 209 | + Rack::Attack.ignored_errors = [ArgumentError] |
| 210 | + end |
| 211 | + |
| 212 | + it 'calls handler despite ignored error' do |
| 213 | + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
| 214 | + |
| 215 | + assert_equal 429, last_response.status |
| 216 | + end |
| 217 | + end |
| 218 | + end |
| 219 | +end |
0 commit comments