Skip to content

Commit 5ac38d3

Browse files
authored
Merge pull request #3261 from DMPRoadmap/bug-3214-vulnerability_no_rate_limit_on_reset_password_link
Fix for bug #3214 which had noted there was no request rate limit to - WIP
2 parents 385e884 + 668b67d commit 5ac38d3

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ With the removal of the webpacker gem, the DartSass package has been installed t
4343
- Sass variables are no longer declared globally and have to be included in files where they are used.
4444
For more detailed explanation, please refer to this video : https://www.youtube.com/watch?v=CR-a8upNjJ0
4545

46+
### Introduction of RackAttack
47+
[Rack Attack](https://github.com/rack/rack-attack) is middleware that can be used to help protect the application from malicious activity. You can establish white/black lists for specific IP addresses and also define rate limits.
48+
49+
- Using Rack-attack address vulnerabilities pointed out in password reset and login: there was no request rate limit.[#3214](https://github.com/DMPRoadmap/roadmap/issues/3214)
50+
4651
### Cleanup of Capybara configuration
4752
- Cleaned up Gemfile by:
4853
- removing gems that were already commented out
@@ -71,8 +76,9 @@ For more detailed explanation, please refer to this video : https://www.youtube.
7176
- Added validation with custom error message in research_output.rb to ensure a user does not enter a very large value as 'Anticipated file size'. [#3161](https://github.com/DMPRoadmap/roadmap/issues/3161)
7277
- Added popover for org profile page and added explanation for public plan
7378

74-
### Fixed
79+
- Added rack-attack version 6.6.1 gem. https://rubygems.org/gems/rack-attack/versions/6.6.1
7580

81+
### Fixed
7682
- Fixed an issue that was preventing uses from leaving the research output byte_size field blank
7783
- Patched issue that was causing template visibility to default to organizationally visible after saving
7884
- Froze mail gem version [#3254](https://github.com/DMPRoadmap/roadmap/issues/3254)

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ gem 'jwt'
108108
# OO authorization for Rails (https://github.com/elabs/pundit)
109109
gem 'pundit'
110110

111+
# Gem for throttling malicious attacks
112+
gem 'rack-attack', '~> 6.6', '>= 6.6.1'
113+
111114
# ========== #
112115
# UI / VIEWS #
113116
# ========== #

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ GEM
350350
rspec-rails (>= 3.0.0)
351351
racc (1.6.2)
352352
rack (2.2.6.4)
353+
rack-attack (6.6.1)
354+
rack (>= 1.0, < 3)
353355
rack-mini-profiler (3.0.0)
354356
rack (>= 1.2.0)
355357
rack-protection (3.0.5)
@@ -574,6 +576,7 @@ DEPENDENCIES
574576
puma
575577
pundit
576578
pundit-matchers
579+
rack-attack (~> 6.6, >= 6.6.1)
577580
rack-mini-profiler
578581
rails (~> 6.1)
579582
rails-controller-testing

config/initializers/rack_attack.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
# NB: `req` is a Rack::Request object (basically an env hash with friendly accessor methods)
4+
5+
# Enable/disable Rack::Attack
6+
Rack::Attack.enabled = true
7+
8+
# Cache store required to work.
9+
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new # defaults to Rails.cache
10+
11+
# Throttle should send a 429 Error responsec code and display public/429.html
12+
Rack::Attack.throttled_responder = lambda do |_env|
13+
html = ActionView::Base.empty.render(file: 'public/429.html')
14+
[429, { 'Content-Type' => 'text/html' }, [html]]
15+
end
16+
17+
# Throttle attempts to a particular path. 2 POSTs to /users/password every 30 seconds
18+
Rack::Attack.throttle "password_resets/ip", limit: 2, period: 30.seconds do |req|
19+
req.post? && req.path == "/users/password" && req.ip
20+
end
21+
22+
# Throttle attempts to a particular path. 4 POSTs to /users/sign_in every 30 seconds
23+
Rack::Attack.throttle "logins/ip", limit: 4, period: 30.seconds do |req|
24+
req.post? && req.path == "/users/sign_in" && req.ip
25+
end

public/429.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>We're sorry, but something went wrong (500)</title>
5+
<style type="text/css">
6+
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7+
div.dialog {
8+
width: 25em;
9+
padding: 0 4em;
10+
margin: 4em auto 0 auto;
11+
border: 1px solid #ccc;
12+
border-right-color: #999;
13+
border-bottom-color: #999;
14+
}
15+
h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16+
</style>
17+
</head>
18+
19+
<body>
20+
<!-- This file lives in public/429.html -->
21+
<div class="dialog">
22+
<h1>Too Many Requests</h1>
23+
24+
<p>You have exceeded the number of requests for this resource. For security reasons access is limited to a fixed number in a given period. Retry later.</p>
25+
26+
27+
</div>
28+
</body>
29+
</html>

0 commit comments

Comments
 (0)