From cc3fabd0be9cee637b9b6ef561675c27a3242617 Mon Sep 17 00:00:00 2001 From: aaronskiba Date: Tue, 24 Dec 2024 15:10:22 -0700 Subject: [PATCH 1/2] Refactor logic for allowing multiple hosts This code was originally added as a hotfix for release `4.1.1+portage-4.2.1`. This commit refactors that code to adhere to DRY principles. --- config/environments/production.rb | 8 +------- config/environments/sandbox.rb | 6 ------ config/environments/staging.rb | 5 ----- config/initializers/allowed_hosts.rb | 16 ++++++++++++++++ 4 files changed, 17 insertions(+), 18 deletions(-) create mode 100644 config/initializers/allowed_hosts.rb diff --git a/config/environments/production.rb b/config/environments/production.rb index ef4ef84249..6c6ec595a3 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -2,7 +2,6 @@ require 'syslog/logger' -# rubocop:disable Metrics/BlockLength Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. @@ -97,12 +96,7 @@ # This allows us to define the hostname and add it to the whitelist. If you attempt # to access the site and receive a 'Blocked host' error then you will need to # set this environment variable - # Convert comma-separated string to array - dmproadmap_hosts = Rails.application.secrets.dmproadmap_host.to_s.split(',').map(&:strip) - dmproadmap_hosts.each do |host| - config.hosts << host - end + # Handled by config/initializers/allowed_hosts.rb end -# rubocop:enable Metrics/BlockLength # Used by Rails' routes url_helpers (typically when including a link in an email) Rails.application.routes.default_url_options[:host] = Rails.application.secrets.dmproadmap_host diff --git a/config/environments/sandbox.rb b/config/environments/sandbox.rb index ae2366aa2e..c019a7e670 100644 --- a/config/environments/sandbox.rb +++ b/config/environments/sandbox.rb @@ -89,12 +89,6 @@ authentication: Rails.application.secrets.smtp_authentication || 'plain', enable_starttls_auto: true } - - # Convert comma-separated string to array - dmproadmap_hosts = Rails.application.secrets.dmproadmap_host.to_s.split(',').map(&:strip) - dmproadmap_hosts.each do |host| - config.hosts << host - end end # Fix JSON Download Error Rails.application.routes.default_url_options[:host] = Rails.application.secrets.dmproadmap_host diff --git a/config/environments/staging.rb b/config/environments/staging.rb index 71d2034836..17cd7ef3b4 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -88,10 +88,5 @@ authentication: Rails.application.secrets.smtp_authentication || 'plain', enable_starttls_auto: true } - # Convert comma-separated string to array - dmproadmap_hosts = Rails.application.secrets.dmproadmap_host.to_s.split(',').map(&:strip) - dmproadmap_hosts.each do |host| - config.hosts << host - end end Rails.application.routes.default_url_options[:host] = Rails.application.secrets.dmproadmap_host diff --git a/config/initializers/allowed_hosts.rb b/config/initializers/allowed_hosts.rb new file mode 100644 index 0000000000..6d64c985d2 --- /dev/null +++ b/config/initializers/allowed_hosts.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +# This initializer sets the allowed hosts for the app configuration. +# https://guides.rubyonrails.org/configuring.html#actiondispatch-hostauthorization +module AllowedHosts + def self.add_allowed_hosts(config) + # Convert comma-separated string to array + hosts = Rails.application.secrets.dmproadmap_host.to_s.split(',').map(&:strip) + hosts.each do |host| + config.hosts << host + end + end +end + +# Add the allowed hosts (unless in development or test environment) +AllowedHosts.add_allowed_hosts(Rails.application.config) unless Rails.env.in?(%w[development test]) From 75ac5032f430814bc9a8e7308165b1c91f3b1a36 Mon Sep 17 00:00:00 2001 From: aaronskiba Date: Thu, 2 Jan 2025 10:56:13 -0700 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d8c5d93ad..9bede3f85c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [Unreleased] + +### Changed + + - Refactor Logic For Allowing Multiple Hosts [#972](https://github.com/portagenetwork/roadmap/pull/972) + ## [4.1.1+portage-4.2.3] - 2024-11-20 ### Added