Skip to content
Snippets Groups Projects
Commit e925fc1a authored by Travis Ralston's avatar Travis Ralston
Browse files

Support a config option for accepting the XFF header as-is

Useful in a limited number of scenarios.

Fixes https://github.com/turt2live/matrix-media-repo/issues/112
parent 1d3c3c4e
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,7 @@ import ( ...@@ -17,6 +17,7 @@ import (
"github.com/turt2live/matrix-media-repo/api" "github.com/turt2live/matrix-media-repo/api"
"github.com/turt2live/matrix-media-repo/api/r0" "github.com/turt2live/matrix-media-repo/api/r0"
"github.com/turt2live/matrix-media-repo/common" "github.com/turt2live/matrix-media-repo/common"
"github.com/turt2live/matrix-media-repo/common/config"
"github.com/turt2live/matrix-media-repo/metrics" "github.com/turt2live/matrix-media-repo/metrics"
"github.com/turt2live/matrix-media-repo/util" "github.com/turt2live/matrix-media-repo/util"
) )
...@@ -36,7 +37,16 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -36,7 +37,16 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
r.Host = strings.Split(r.Host, ":")[0] r.Host = strings.Split(r.Host, ":")[0]
raddr := xff.GetRemoteAddr(r) var raddr string
if config.Get().General.TrustAnyForward {
raddr = r.Header.Get("X-Forwarded-For")
} else {
raddr = xff.GetRemoteAddr(r)
}
if raddr == "" {
raddr = r.RemoteAddr
}
host, _, err := net.SplitHostPort(raddr) host, _, err := net.SplitHostPort(raddr)
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
......
...@@ -23,9 +23,10 @@ type HomeserverConfig struct { ...@@ -23,9 +23,10 @@ type HomeserverConfig struct {
} }
type GeneralConfig struct { type GeneralConfig struct {
BindAddress string `yaml:"bindAddress"` BindAddress string `yaml:"bindAddress"`
Port int `yaml:"port"` Port int `yaml:"port"`
LogDirectory string `yaml:"logDirectory"` LogDirectory string `yaml:"logDirectory"`
TrustAnyForward bool `yaml:"trustAnyForwardedAddress"`
} }
type DbPoolConfig struct { type DbPoolConfig struct {
...@@ -218,9 +219,10 @@ func Get() *MediaRepoConfig { ...@@ -218,9 +219,10 @@ func Get() *MediaRepoConfig {
func NewDefaultConfig() *MediaRepoConfig { func NewDefaultConfig() *MediaRepoConfig {
return &MediaRepoConfig{ return &MediaRepoConfig{
General: &GeneralConfig{ General: &GeneralConfig{
BindAddress: "127.0.0.1", BindAddress: "127.0.0.1",
Port: 8000, Port: 8000,
LogDirectory: "logs", LogDirectory: "logs",
TrustAnyForward: false,
}, },
Database: &DatabaseConfig{ Database: &DatabaseConfig{
Postgres: "postgres://your_username:your_password@localhost/database_name?sslmode=disable", Postgres: "postgres://your_username:your_password@localhost/database_name?sslmode=disable",
......
...@@ -7,6 +7,11 @@ repo: ...@@ -7,6 +7,11 @@ repo:
# rotated every day and held for 14 days. To disable the repo logging to files, set this to "-". # rotated every day and held for 14 days. To disable the repo logging to files, set this to "-".
logDirectory: logs logDirectory: logs
# If true, the media repo will accept any X-Forwarded-For header without validation. In most cases
# this option should be left as "false". Note that the media repo already expects an X-Forwarded-For
# header, but validates it to ensure the IP being given makes sense.
trustAnyForwardedAddress: false
# The database configuration for the media repository # The database configuration for the media repository
database: database:
# Currently only "postgres" is supported. # Currently only "postgres" is supported.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment