Skip to content

Commit

Permalink
fix: Build HTTPS redirect URI when behind LB
Browse files Browse the repository at this point in the history
  • Loading branch information
iskandar committed Feb 24, 2021
1 parent 53de747 commit a437992
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/s3-proxy/server/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ func TemplateExecution(tplPath, tplString string, logger log.Logger, rw http.Res

func GetRequestURI(r *http.Request) string {
scheme := "http"
if r.TLS != nil {
fwdScheme := r.Header.Get("X-Forwarded-Proto")

if r.TLS != nil || fwdScheme == "https" {
scheme = "https"
}

Expand Down
15 changes: 15 additions & 0 deletions pkg/s3-proxy/server/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,21 @@ func TestGetRequestURI(t *testing.T) {
}
}

func TestProxiedGetRequestURI(t *testing.T) {
req, err := http.NewRequest("GET", "http://localhost:989/fake/path", nil)
if err != nil {
t.Fatal(err)
}
// Add the same header a Load Balancer should set
req.Header.Set("X-Forwarded-Proto", "https")

want := "https://localhost:989/fake/path"
got := GetRequestURI(req)
if got != want {
t.Errorf("GetRequestURI() = %v, want %v", got, want)
}
}

func Test_RequestHost(t *testing.T) {
hXForwardedHost1 := http.Header{
"X-Forwarded-Host": []string{"fake.host"},
Expand Down

0 comments on commit a437992

Please # to comment.