-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Capture user's actual IP when rate limiting in QA and Live where we u…
…se CloudFlare (#1920) When behind cloudflare, Rack::Attack receive's cloudflare's IPs instead of the user's actual IP address. Here, we are changing the configuration to consume HTTP_CF_CONNECTING_IP if it is available.
- Loading branch information
Showing
2 changed files
with
47 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,32 @@ class ThrottlingTest < ActionDispatch::IntegrationTest | |
assert_response :forbidden | ||
end | ||
end | ||
|
||
test "should handle requests via Cloudflare correctly in production" do | ||
original_env = Rails.env | ||
Rails.env = 'production' | ||
|
||
stub_configs({ 'api_rate_limit' => 3, 'login_block_limit' => 2 }) do | ||
# Test throttling for /api/graphql via Cloudflare | ||
3.times do | ||
post api_graphql_path, headers: { 'CF-Connecting-IP' => '1.2.3.4' } | ||
assert_response :unauthorized | ||
end | ||
|
||
post api_graphql_path, headers: { 'CF-Connecting-IP' => '1.2.3.4' } | ||
assert_response :too_many_requests | ||
|
||
# Test blocking for /api/users/sign_in via Cloudflare | ||
user_params = { api_user: { email: '[email protected]', password: random_complex_password } } | ||
|
||
2.times do | ||
post api_user_session_path, params: user_params, as: :json, headers: { 'CF-Connecting-IP' => '1.2.3.4' } | ||
end | ||
|
||
post api_user_session_path, params: user_params, as: :json, headers: { 'CF-Connecting-IP' => '1.2.3.4' } | ||
assert_response :forbidden | ||
end | ||
|
||
Rails.env = original_env | ||
end | ||
end |