Skip to content

Commit a5ed2b9

Browse files
authored
Merge pull request #4583 from Growstuff/dev
Release 84
2 parents 7ca689f + bd637c3 commit a5ed2b9

5 files changed

Lines changed: 47 additions & 3 deletions

File tree

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ gem 'xmlrpc' # fixes rake error - can be removed if not needed later
116116

117117
gem 'puma'
118118

119+
gem 'rack-attack'
120+
119121
gem 'loofah', '>= 2.19.1'
120122
gem 'rack-protection', '>= 2.0.1'
121123

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,8 @@ GEM
503503
query_diet (0.7.3)
504504
racc (1.8.1)
505505
rack (2.2.23)
506+
rack-attack (6.8.0)
507+
rack (>= 1.0, < 4)
506508
rack-cors (2.0.2)
507509
rack (>= 2.0.0)
508510
rack-protection (3.2.0)
@@ -841,6 +843,7 @@ DEPENDENCIES
841843
pry
842844
puma
843845
query_diet
846+
rack-attack
844847
rack-cors
845848
rack-protection (>= 2.0.1)
846849
rails (~> 7.2.0)

app/controllers/charts/crops_controller.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ def planted_from
1414

1515
def harvested_for
1616
@crop = Crop.find_by!(slug: params[:crop_slug])
17-
render json: Harvest.joins(:plant_part)
18-
.where(crop: @crop)
19-
.group("plant_parts.name").count(:id)
17+
data = Rails.cache.fetch("#{@crop.cache_key_with_version}/harvested_for", expires_in: 1.day) do
18+
Harvest.joins(:plant_part)
19+
.where(crop: @crop)
20+
.group("plant_parts.name").count(:id)
21+
end
22+
render json: data
2023
end
2124

2225
private

config/application.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class Application < Rails::Application
7373
config.newsletter_list_id = ENV.fetch('GROWSTUFF_MAILCHIMP_NEWSLETTER_ID', nil)
7474

7575
# config.active_record.raise_in_transactional_callbacks = true
76+
config.middleware.insert_before 0, Rack::Attack
77+
7678
config.middleware.insert_before 0, Rack::Cors do
7779
allow do
7880
origins '*'

config/initializers/rack_attack.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
class Rack::Attack
4+
### Throttle Config ###
5+
6+
if Rails.env.production?
7+
# Throttle requests to /plantings, /harvests, and /members to 10 per minute per IP
8+
# Includes API routes
9+
throttle('req/ip/restricted_routes', limit: 20, period: 1.minute) do |req|
10+
if req.path =~ %r{^/(plantings|harvests|members)(/|$)} || req.path =~ %r{^/api/v1/(plantings|harvests|members)(/|$)}
11+
req.ip
12+
end
13+
end
14+
15+
### Fail2Ban Config ###
16+
17+
# Block IPs that make too many requests to suspicious paths
18+
# After 5 "bad" requests in 10 minutes, block the IP for 1 hour
19+
blocklist('fail2ban/pentesters') do |req|
20+
Fail2Ban.filter("pentesters-#{req.ip}", maxretry: 5, findtime: 10.minutes, bantime: 1.hour) do
21+
# The count for the IP is incremented if the return value is truthy.
22+
req.path.include?('wp-admin') ||
23+
req.path.include?('wp-login') ||
24+
req.path.include?('cgi-bin') ||
25+
req.path.end_with?('.php', '.asp', '.aspx', '.jsp', '.exe', '.env', '.git')
26+
end
27+
end
28+
end
29+
30+
### Custom Response Headers ###
31+
32+
# Add Retry-After header to throttled responses
33+
self.throttled_response_retry_after_header = true
34+
end

0 commit comments

Comments
 (0)