Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Cloudflare Setting: Optionally fetch CF IPs on container boot. #2878

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions config/defaults/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,21 @@ throttling:
# - '1.2.3.4'
# - '2.3.4.5'

## Cloudflare Proxy
#
# If you are using Cloudflare as a proxy, you will need to set the following
# value to true. This will cause the application to fetch the list of Cloudflare
# proxy IP addresses and add them to the list of trusted proxies.
#
# Note that on application boot, this will trigger two HTTPs requests to fetch
# the list of Cloudflare IP addresses. The requests each have a timeout of 15 seconds
# and may delay on container boot.
#
# Environment Variable Override:
# PWP__CLOUDFLARE_PROXY='false'
#
cloudflare_proxy: false

### Mail Server Configuration
#
# When logins are enabled, an SMTP server is required to send emails to users
Expand Down
40 changes: 40 additions & 0 deletions config/initializers/cloudflare_proxy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require "net/http"

if Settings.cloudflare_proxy
def fetch_with_timeout(url, timeout: 15)
uri = URI(url)
Net::HTTP.start(uri.host, uri.port, use_ssl: true, open_timeout: timeout, read_timeout: timeout) do |http|
request = Net::HTTP::Get.new(uri)
http.request(request).body
end
rescue => e
Rails.logger.warn "Failed to fetch #{url}: #{e.message}"
""
end

Rails.logger.info "Fetching latest Cloudflare IPs..."

cf_ipv4_url = "https://www.cloudflare.com/ips-v4"
cf_ipv6_url = "https://www.cloudflare.com/ips-v6"

begin
# Fetch Cloudflare IP ranges with timeout
ipv4 = fetch_with_timeout(cf_ipv4_url).split("\n")
ipv6 = fetch_with_timeout(cf_ipv6_url).split("\n")
cloudflare_ips = ipv4 + ipv6
rescue => e
Rails.logger.warn "Failed to fetch Cloudflare IPs: #{e.message}"
cloudflare_ips = [] # Fallback to no Cloudflare IPs
end

# Add Cloudflare IPs to existing trusted proxies
Rails.application.config.action_dispatch.trusted_proxies ||= []
Rails.application.config.action_dispatch.trusted_proxies += cloudflare_ips.filter_map do |ip|
IPAddr.new(ip)
rescue ArgumentError => e
Rails.logger.warn "Invalid IP format skipped: #{ip} (#{e.message})"
nil
end

Rails.logger.info "Cloudflare IPs added to trusted proxies."
end
15 changes: 15 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,21 @@ throttling:
# - '1.2.3.4'
# - '2.3.4.5'

## Cloudflare Proxy
#
# If you are using Cloudflare as a proxy, you will need to set the following
# value to true. This will cause the application to fetch the list of Cloudflare
# proxy IP addresses and add them to the list of trusted proxies.
#
# Note that on application boot, this will trigger two HTTPs requests to fetch
# the list of Cloudflare IP addresses. The requests each have a timeout of 15 seconds
# and may delay on container boot.
#
# Environment Variable Override:
# PWP__CLOUDFLARE_PROXY='false'
#
cloudflare_proxy: false

### Mail Server Configuration
#
# When logins are enabled, an SMTP server is required to send emails to users
Expand Down
Loading