From 641eddc9f1b1266f5205995d3e7bae0b9d9264b4 Mon Sep 17 00:00:00 2001
From: Travis Ralston <travpc@gmail.com>
Date: Fri, 27 Dec 2019 13:59:33 -0700
Subject: [PATCH] Flip forwarded host config option to be positive

---
 api/webserver/route_handler.go |  2 +-
 common/config/config.go        | 20 ++++++++++----------
 config.sample.yaml             |  5 +++++
 3 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/api/webserver/route_handler.go b/api/webserver/route_handler.go
index 5d08e912..218905f7 100644
--- a/api/webserver/route_handler.go
+++ b/api/webserver/route_handler.go
@@ -32,7 +32,7 @@ type handler struct {
 
 func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 	isUsingForwardedHost := false
-	if r.Header.Get("X-Forwarded-Host") != "" && !config.Get().General.IgnoreForwardedHost  {
+	if r.Header.Get("X-Forwarded-Host") != "" && config.Get().General.UseForwardedHost {
 		r.Host = r.Header.Get("X-Forwarded-Host")
 		isUsingForwardedHost = true
 	}
diff --git a/common/config/config.go b/common/config/config.go
index de873f1b..234eb527 100644
--- a/common/config/config.go
+++ b/common/config/config.go
@@ -24,11 +24,11 @@ type HomeserverConfig struct {
 }
 
 type GeneralConfig struct {
-	BindAddress         string `yaml:"bindAddress"`
-	Port                int    `yaml:"port"`
-	LogDirectory        string `yaml:"logDirectory"`
-	TrustAnyForward     bool   `yaml:"trustAnyForwardedAddress"`
-	IgnoreForwardedHost bool   `yaml:"ignoreForwardedHost"`
+	BindAddress      string `yaml:"bindAddress"`
+	Port             int    `yaml:"port"`
+	LogDirectory     string `yaml:"logDirectory"`
+	TrustAnyForward  bool   `yaml:"trustAnyForwardedAddress"`
+	UseForwardedHost bool   `yaml:"useForwardedHost"`
 }
 
 type DbPoolConfig struct {
@@ -228,11 +228,11 @@ func Get() *MediaRepoConfig {
 func NewDefaultConfig() *MediaRepoConfig {
 	return &MediaRepoConfig{
 		General: &GeneralConfig{
-			BindAddress:         "127.0.0.1",
-			Port:                8000,
-			LogDirectory:        "logs",
-			TrustAnyForward:     false,
-			IgnoreForwardedHost: false,
+			BindAddress:      "127.0.0.1",
+			Port:             8000,
+			LogDirectory:     "logs",
+			TrustAnyForward:  false,
+			UseForwardedHost: true,
 		},
 		Database: &DatabaseConfig{
 			Postgres: "postgres://your_username:your_password@localhost/database_name?sslmode=disable",
diff --git a/config.sample.yaml b/config.sample.yaml
index 02a58f4a..31550cf3 100644
--- a/config.sample.yaml
+++ b/config.sample.yaml
@@ -12,6 +12,11 @@ repo:
   # header, but validates it to ensure the IP being given makes sense.
   trustAnyForwardedAddress: false
 
+  # If false, the media repo will not use the X-Forwarded-Host header commonly added by reverse proxies.
+  # Typically this should remain as true, though in some circumstances it may need to be disabled.
+  # See https://github.com/turt2live/matrix-media-repo/issues/202 for more information.
+  useForwardedHost: true
+
 # The database configuration for the media repository
 database:
   # Currently only "postgres" is supported.
-- 
GitLab