2018-11-14 23:22:02 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Middleware
|
|
|
|
class EnforceHostname
|
|
|
|
def initialize(app, settings = nil)
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
# enforces hostname to match the hostname of our connection
|
|
|
|
# this middleware lives after rails multisite so at this point
|
|
|
|
# Discourse.current_hostname MUST be canonical, enforce it so
|
2021-05-20 21:43:47 -04:00
|
|
|
# all Rails helpers are guaranteed to use it unconditionally and
|
2018-11-14 23:22:02 -05:00
|
|
|
# never generate incorrect links
|
|
|
|
env[Rack::Request::HTTP_X_FORWARDED_HOST] = nil
|
2020-03-19 15:54:42 -04:00
|
|
|
|
|
|
|
allowed_hostnames = RailsMultisite::ConnectionManagement.current_db_hostnames
|
|
|
|
requested_hostname = env[Rack::HTTP_HOST]
|
|
|
|
|
2021-01-28 21:14:49 -05:00
|
|
|
env[Discourse::REQUESTED_HOSTNAME] = requested_hostname
|
2022-12-22 08:13:43 -05:00
|
|
|
env[Rack::HTTP_HOST] = allowed_hostnames.find { |h| h == requested_hostname } ||
|
|
|
|
Discourse.current_hostname_with_port
|
2020-03-19 15:54:42 -04:00
|
|
|
|
2018-11-14 23:22:02 -05:00
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|