FIX: Use asset_hostnames in rails_multisite

FIX: Use asset_hostnames in rails_multisite

We want to respond to the x-forwarded-for host from the CDN too.

diff --git a/Gemfile.lock b/Gemfile.lock
index 82a4164..aaf2b86 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -281,7 +281,7 @@ GEM
     rails_failover (0.5.7)
       activerecord (~> 6.0)
       railties (~> 6.0)
-    rails_multisite (2.4.0)
+    rails_multisite (2.5.0)
       activerecord (> 5.0, < 7)
       railties (> 5.0, < 7)
     railties (6.0.3.3)
diff --git a/app/models/global_setting.rb b/app/models/global_setting.rb
index d6b076e..abd555a 100644
--- a/app/models/global_setting.rb
+++ b/app/models/global_setting.rb
@@ -112,6 +112,13 @@ class GlobalSetting
     @use_s3 = nil
   end
 
+  def self.cdn_hostnames
+    hostnames = []
+    hostnames << URI.parse(cdn_url).host if cdn_url.present?
+    hostnames << cdn_origin_hostname if cdn_origin_hostname.present?
+    hostnames
+  end
+
   def self.database_config
     hash = { "adapter" => "postgresql" }
 
diff --git a/config/initializers/200-first_middlewares.rb b/config/initializers/200-first_middlewares.rb
index 4732938..bcb1b6d 100644
--- a/config/initializers/200-first_middlewares.rb
+++ b/config/initializers/200-first_middlewares.rb
@@ -21,9 +21,14 @@ if Rails.env != 'development' || ENV['TRACK_REQUESTS']
 end
 
 if Rails.configuration.multisite
-  RailsMultisite::ConnectionManagement.asset_hostname =
-    GlobalSetting.cdn_origin_hostname ||
-    Discourse::Application.config.database_configuration[Rails.env]["host_names"].first
+  assets_hostnames = GlobalSetting.cdn_hostnames
+
+  if assets_hostnames.empty?
+    assets_hostnames =
+      Discourse::Application.config.database_configuration[Rails.env]["host_names"]
+  end
+
+  RailsMultisite::ConnectionManagement.asset_hostnames = assets_hostnames
 
   # Multisite needs to be first, because the request tracker and message bus rely on it
   Rails.configuration.middleware.unshift RailsMultisite::Middleware, RailsMultisite::DiscoursePatches.config

GitHub sha: 6e69c251

2 Likes