Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.
Ruby agplai-assistantdockerdsgvogdprhotwireissue-trackerjira-alternativeknowledge-basellmmcp-servermeeting-notesopen-sourceproject-managementredmine-alternativeruby-on-railsself-hostedticketingtime-tracking
Ignore the Forwarded header when resolving the client address
Guido Boykec73e50276140
diff --git a/config/initializers/forwarded_headers.rb b/config/initializers/forwarded_headers.rbnew file mode 100644index 0000000..8e99339--- /dev/null+++ b/config/initializers/forwarded_headers.rb@@ -0,0 +1,11 @@+# frozen_string_literal: true++# Which header names the client behind a proxy: X-Forwarded-For only.+#+# Rack reads the standard `Forwarded` header first and X-Forwarded-For second, for the+# client's address (req.ip, the key of every Rack::Attack throttle) and for host and+# scheme. The proxies an instance runs behind set X-Forwarded-For (Caddy replaces it,+# nginx appends to it) and pass a `Forwarded` header from the client through untouched.+# With Rack's order a client picked its own address: a new `for=` per request was a new+# throttle bucket per request. Pinned by spec/requests/auth_throttle_spec.rb.+Rack::Request.forwarded_priority = [:x_forwarded]diff --git a/spec/requests/auth_throttle_spec.rb b/spec/requests/auth_throttle_spec.rbindex 3ec2643..8cd879d 100644--- a/spec/requests/auth_throttle_spec.rb+++ b/spec/requests/auth_throttle_spec.rb@@ -100,6 +100,32 @@ RSpec.describe 'Auth endpoint throttling', type: :request doexpect(response).to have_http_status(:too_many_requests)end++ # Behind a proxy (REMOTE_ADDR is the proxy, a trusted address) the client's+ # address comes from a header. Caddy and nginx set X-Forwarded-For and pass+ # the client's own `Forwarded` header through untouched, and Rack read that+ # one first: a new `for=` per request was a new address per request, for+ # every throttle keyed on req.ip. A new address per attempt as well, so only+ # the per-IP brake can answer 429. Without X-Forwarded-For too: `Forwarded`+ # must not count as the fallback either.+ {+ 'behind a proxy' => { 'X-Forwarded-For' => '203.0.113.5' },+ 'without X-Forwarded-For' => {}+ }.each do |setting, proxy_headers|+ it "cannot be bypassed with a Forwarded header of the client (#{setting})" do+ Prosopite.pause++ 6.times do |i|+ attempt = { user: { email: "nobody#{i}@example.com", password: 'wrong-password' } }+ post user_session_path, params: attempt, headers: proxy_headers.merge(+ 'REMOTE_ADDR' => '172.18.0.5',+ 'Forwarded' => "for=198.51.100.#{i + 1}"+ )+ end++ expect(response).to have_http_status(:too_many_requests)+ end+ endend# The most privileged login in the app got the same rewrite as the others,