From 90c05547f6864b59d039b1ab706d6736f653c770 Mon Sep 17 00:00:00 2001 From: Donovan Daniels Date: Fri, 3 May 2024 17:51:05 -0500 Subject: [PATCH] Move dev host rewriting into middleware This makes matching against routes later much more reliable and less complicated --- config/application.rb | 4 +- config/default_config.rb | 2 + config/routes/domain_constraint.rb | 44 +++++-------------- .../custom_static_middleware.rb | 0 lib/middleware/dev_host_middleware.rb | 14 ++++++ 5 files changed, 30 insertions(+), 34 deletions(-) rename lib/{ => middleware}/custom_static_middleware.rb (100%) create mode 100644 lib/middleware/dev_host_middleware.rb diff --git a/config/application.rb b/config/application.rb index da3a66b..451c741 100644 --- a/config/application.rb +++ b/config/application.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true require_relative "boot" -require_relative "../lib/custom_static_middleware" +require_relative "../lib/middleware/custom_static_middleware" +require_relative "../lib/middleware/dev_host_middleware" require "rails/all" @@ -35,6 +36,7 @@ module Websites config.action_controller.action_on_unpermitted_parameters = :raise config.action_dispatch.default_headers.clear + config.middleware.insert_before(0, DevHostMiddleware) if Rails.env.development? config.middleware.insert_before(ActionDispatch::Static, CustomStaticMiddleware, { /^i\.furry\.cool/ => "/furry.cool/images", /^i\.maidboye\.cafe/ => "/maidboye.cafe/images", diff --git a/config/default_config.rb b/config/default_config.rb index 1ebd1a3..9037ccf 100644 --- a/config/default_config.rb +++ b/config/default_config.rb @@ -135,6 +135,8 @@ module Websites end def common_headers(domain) + # We don't want to set headers like HSTS in development + return {} if Rails.env.development? { "Report-To": { group: "default", diff --git a/config/routes/domain_constraint.rb b/config/routes/domain_constraint.rb index 8f8ad0a..7e1b6f5 100644 --- a/config/routes/domain_constraint.rb +++ b/config/routes/domain_constraint.rb @@ -3,47 +3,25 @@ class DomainConstraint attr_reader :domain, :subdomain - DEFAULT_DOMAIN = nil # "yiff.rest".freeze - USE_DEV_HOST = Rails.env.development? - - def dev_host - "#{domain.tr('.', '-')}.websites.containers.local" - end + DEFAULT_DOMAIN = nil # "yiff.rest" def initialize(domain, subdomain = nil) @domain = domain @subdomain = subdomain end - def resolve_domains(current_host, level = 1) - current_subdomains = nil - if current_host.scan(".").length > level - parts = current_host.split(".") - current_host = parts.pop(level + 1).join(".") - current_subdomains = parts.join(".") if parts.length >= 1 - end - - [current_host, current_subdomains] + def matches?(request) + Rails.logger.info("Domain: #{@domain}; Subdomain: #{@subdomain}; Current Host: #{request.domain}; Current Subdomains: #{request.subdomain}; Matches (Domain): #{domain_matches?(request)}; Matches (Subdomain): #{subdomain_matches?(request)}") if Rails.env.development? + subdomain_matches?(request) && domain_matches?(request) end - def matches?(request) - host = domain - parse = request.domain - parse = "#{request.subdomains.join('.')}.#{request.domain}" unless request.subdomains.empty? - current_host, current_subdomains = resolve_domains(parse) - if Rails.env.development? && USE_DEV_HOST - if DEFAULT_DOMAIN.present? - current_host, current_subdomains = resolve_domains(DEFAULT_DOMAIN) - elsif request.query_parameters[:domain].present? - current_host, current_subdomains = resolve_domains(request.query_parameters[:domain]) - elsif current_host.scan(".").length > 3 - current_host, current_subdomains = resolve_domains(current_host, 3) - end + private - current_host = dev_host if current_host == domain - host = dev_host - end - Rails.logger.info("Host: #{host}; Subdomain: #{subdomain}; Current Host: #{current_host}; Current Subdomain: #{current_subdomains}; Matches (Domain): #{current_host == host}; Matches (Subdomain): #{current_subdomains == subdomain || subdomain&.to_sym == :any}") if Rails.env.development? - current_host == host && (current_subdomains == subdomain || subdomain&.to_sym == :any) + def domain_matches?(request) + @domain.nil? || request.domain == @domain + end + + def subdomain_matches?(request) + @subdomain.nil? || request.subdomain == @subdomain end end diff --git a/lib/custom_static_middleware.rb b/lib/middleware/custom_static_middleware.rb similarity index 100% rename from lib/custom_static_middleware.rb rename to lib/middleware/custom_static_middleware.rb diff --git a/lib/middleware/dev_host_middleware.rb b/lib/middleware/dev_host_middleware.rb new file mode 100644 index 0000000..f9b12e9 --- /dev/null +++ b/lib/middleware/dev_host_middleware.rb @@ -0,0 +1,14 @@ +class DevHostMiddleware + def initialize(app) + @app = app + end + + def call(env) + request = Rack::Request.new(env) + domain = request.params["domain"] + + env["HTTP_HOST"] = domain if Rails.env.development? && domain + + @app.call(env) + end +end