Skip to content

Commit f1088db

Browse files
authored
Make Throttling Configurable (#361)
* Make Throttling Configurable * Newlines at end of file
1 parent 3de3611 commit f1088db

File tree

5 files changed

+110
-12
lines changed

5 files changed

+110
-12
lines changed

Gemfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ gem 'net-smtp'
9595
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
9696

9797
group :production do
98-
gem 'rack-timeout'
99-
gem 'rack-throttle'
10098
gem 'pg'
10199
gem 'sentry-ruby'
102100
gem 'sentry-rails', '>= 5.0.2'
@@ -105,3 +103,8 @@ end
105103
group :private do
106104
gem 'sqlite3'
107105
end
106+
107+
group :production, :private do
108+
gem 'rack-timeout'
109+
gem 'rack-throttle'
110+
end

config/environments/private.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
protocol: Settings.host_protocol
1919
}
2020

21+
config.middleware.use Rack::Throttle::Daily, max: Settings.throttling.daily
22+
config.middleware.use Rack::Throttle::Hourly, max: Settings.throttling.hourly
23+
config.middleware.use Rack::Throttle::Minute, max: Settings.throttling.minute
24+
config.middleware.use Rack::Throttle::Second, max: Settings.throttling.second
25+
2126
config.action_mailer.smtp_settings = {
2227
address: Settings.mail.smtp_address,
2328
port: Settings.mail.smtp_port,

config/environments/production.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
# Apache or NGINX already handles this.
2323
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
2424

25-
config.middleware.use Rack::Throttle::Daily, max: 1000 # requests
26-
config.middleware.use Rack::Throttle::Hourly, max: 100 # requests
27-
config.middleware.use Rack::Throttle::Minute, max: 30 # requests
28-
config.middleware.use Rack::Throttle::Second, max: 2 # requests
25+
config.middleware.use Rack::Throttle::Daily, max: Settings.throttling.daily
26+
config.middleware.use Rack::Throttle::Hourly, max: Settings.throttling.hourly
27+
config.middleware.use Rack::Throttle::Minute, max: Settings.throttling.minute
28+
config.middleware.use Rack::Throttle::Second, max: Settings.throttling.second
2929

3030
# Compress JavaScripts and CSS.
3131
config.assets.js_compressor = :uglifier

config/settings.yml

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
# Global Application Defaults
1+
# Global Application Configuration
22

3-
# This is initially for internal settings. If all goes well using
4-
# this new config gem, then we'll migrate entirely to the new Settings
5-
# format for private instances too.
3+
# See also https://github.com/pglombardo/PasswordPusher/blob/master/Configuration.md
4+
# for a further explanation of the larger settings available here.
65

76
# Logins are disabled by default since they require an MTA (email) server
87
# available to send emails through.
8+
#
99
# For instructions on how to enable logins, see this page:
1010
# https://github.com/pglombardo/PasswordPusher/discussions/276
11+
#
12+
# Environment variable override:
13+
# PWP__ENABLE_LOGINS='false'
14+
#
1115
enable_logins: false
1216

1317
# By default, Password Pusher can be used by anonymous users to push
@@ -24,10 +28,18 @@ allow_anonymous: true
2428

2529
# The domain (without protocol) where this instance is hosted
2630
# Used in generating fully qualified URLs.
31+
#
32+
# Environment variable override:
33+
# PWP__HOST_DOMAIN='pwpush.com'
34+
#
2735
# host_domain: 'pwpush.com'
2836

2937
# The protocol to reach the domain above
3038
# Used in generating fully qualified URLs.
39+
#
40+
# Environment variable override:
41+
# PWP__HOST_PROTOCOL='https'
42+
#
3143
host_protocol: 'https'
3244

3345
# Set the following value to force the base URL of generated links.
@@ -42,23 +54,96 @@ host_protocol: 'https'
4254
#
4355
# override_base_url: 'https://pwpush.mydomain.com'
4456

57+
58+
# Configure the application throttling limits.
59+
#
60+
# Throttling enforces a minimum time interval
61+
# between subsequent HTTP requests from a particular client, as
62+
# well as by defining a maximum number of allowed HTTP requests
63+
# per a given time period (per minute, hourly, or daily).
64+
#
65+
# See https://github.com/dryruby/rack-throttle#throttling-strategies
66+
# for a description of function.
67+
#
68+
throttling:
69+
# ..maximum number of allowed HTTP requests per day
70+
#
71+
# Default: 1000
72+
#
73+
# Environment Variable Override: PWP__THROTTLING__DAILY='1000'
74+
daily: 1000
75+
76+
# ..maximum number of allowed HTTP requests per hour
77+
#
78+
# Default: 100
79+
#
80+
# Environment Variable Override: PWP__THROTTLING__HOURLY='100'
81+
hourly: 100
82+
83+
# ..maximum number of allowed HTTP requests per minute
84+
#
85+
# Default: 30
86+
#
87+
# Environment Variable Override: PWP__THROTTLING__MINUTE='30'
88+
minute: 30
89+
90+
# ..maximum number of allowed HTTP requests per second
91+
#
92+
# Default: 2
93+
#
94+
# Environment Variable Override: PWP__THROTTLING__SECOND='2'
95+
second: 2
96+
97+
4598
# When logins are enabled, an SMTP server is required to send emails to users
4699
# for things such as forgot password, unlock account, confirm account etc.
47100
# If `enable_logins` is set to true above, the following _are required_ to be
48101
# filled out with valid values.
49102
mail:
103+
# Email delivery errors will be shown in the application
104+
# Environment Variable Override: PWP__MAIL__RAISE_DELIVERY_ERRORS='false'
50105
raise_delivery_errors: false
106+
107+
# Allows you to use a remote mail server. Just change it from its default "localhost" setting.
108+
# Environment Variable Override: PWP__MAIL__SMTP_ADDRESS='smtp.example.com'
51109
# smtp_address: smtp.example.com
110+
111+
# If your mail server requires authentication, set the username in this setting.
112+
# Environment Variable Override: PWP__MAIL__SMTP_USER_NAME='apikey'
52113
# smtp_user_name: 'apikey'
114+
115+
# If your mail server requires authentication, set the password in this setting.
116+
# Environment Variable Override: PWP__MAIL__SMTP_PASSWORD='something@&#$'
53117
# smtp_password: ''
118+
119+
# Port of the SMTP server
120+
# Environment Variable Override: PWP__MAIL__SMTP_PORT='587'
54121
smtp_port: 587
122+
123+
# If your mail server requires authentication, you need to specify the
124+
# authentication type here. This is a string and one of :plain (will send
125+
# the password in the clear), :login (will send password Base64 encoded)
126+
# or :cram_md5 (combines a Challenge/Response mechanism to exchange
127+
# information and a cryptographic Message Digest 5 algorithm to hash
128+
# important information)
129+
# Environment Variable Override: PWP__MAIL__SMTP_AUTHENTICATION='plain'
55130
smtp_authentication: 'plain'
131+
132+
# Use STARTTLS when connecting to your SMTP server and fail if unsupported.
133+
# Environment Variable Override: PWP__MAIL__SMTP_STARTTLS='true'
56134
smtp_starttls: true
135+
136+
# Number of seconds to wait while attempting to open a connection.
137+
# Environment Variable Override: PWP__MAIL__SMTP_OPEN_TIMEOUT='10'
57138
smtp_open_timeout: 10
139+
140+
# Number of seconds to wait until timing-out a read(2) call.
141+
# Environment Variable Override: PWP__MAIL__SMTP_READ_TIMEOUT='10'
58142
smtp_read_timeout: 10
59143

60144
# Configure the e-mail address which will be shown as 'From' in emails
61145
# See config/initializers/devise.rb where this is used
146+
# Environment Variable Override: PWP__MAIL__MAILER_SENDER='"Password Pusher" <[email protected]>'
62147
# mailer_sender: '"Password Pusher" <[email protected]>'
63148

64149
# List of supported languages indexed by language code. This is used
@@ -85,7 +170,9 @@ language_codes:
85170

86171
# The default language for the application. This must be one of the
87172
# valid/supported language codes from the list above.
173+
#
88174
# Example: default_locale: :es
175+
#
89176
# Environment Variable Override: PWP__DEFAULT_LOCALE='es'
90177
default_locale: :en
91178

gemfiles/Gemfile-mysql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ gem 'net-smtp'
9696
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
9797

9898
group :production do
99-
gem 'rack-timeout'
100-
gem 'rack-throttle'
10199
gem 'sentry-ruby'
102100
gem 'sentry-rails'
103101
gem 'mysql2'
@@ -106,3 +104,8 @@ end
106104
group :private do
107105
gem 'sqlite3'
108106
end
107+
108+
group :production, :private do
109+
gem 'rack-timeout'
110+
gem 'rack-throttle'
111+
end

0 commit comments

Comments
 (0)