Skip to content
This repository was archived by the owner on Jan 24, 2019. It is now read-only.

Redirect Insecure Requests with the X-Forwarded-Proto header #556

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion oauthproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import (
"github.com/mbland/hmacauth"
)

const SignatureHeader = "GAP-Signature"
const (
SignatureHeader = "GAP-Signature"
XForwardedProtoHeader = "X-Forwarded-Proto"
)

var SignatureHeaders []string = []string{
"Content-Length",
Expand Down Expand Up @@ -457,6 +460,7 @@ func getRemoteAddr(req *http.Request) (s string) {
}

func (p *OAuthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
p.redirectInsecureRequest(rw, req)
switch path := req.URL.Path; {
case path == p.RobotsPath:
p.RobotsTxt(rw)
Expand Down Expand Up @@ -607,6 +611,12 @@ func (p *OAuthProxy) Proxy(rw http.ResponseWriter, req *http.Request) {
}
}

func (p *OAuthProxy) redirectInsecureRequest(rw http.ResponseWriter, req *http.Request) {
if req.Header.Get(XForwardedProtoHeader) == "http" {
http.Redirect(rw, req, fmt.Sprintf("https://%v%v", req.Host, req.URL), http.StatusMovedPermanently)
}
}

func (p *OAuthProxy) Authenticate(rw http.ResponseWriter, req *http.Request) int {
var saveSession, clearSession, revalidated bool
remoteAddr := getRemoteAddr(req)
Expand Down
15 changes: 15 additions & 0 deletions oauthproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,3 +836,18 @@ func TestRequestSignaturePostRequest(t *testing.T) {
assert.Equal(t, 200, st.rw.Code)
assert.Equal(t, st.rw.Body.String(), "signatures match")
}

func TestRedirectInsecureRequest(t *testing.T) {
opts := NewOptions()
opts.Validate()

proxy := NewOAuthProxy(opts, func(string) bool { return true })
rw := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
req.Host = "example.com"
req.Header.Set("X-Forwarded-Proto", "http")
proxy.ServeHTTP(rw, req)

assert.Equal(t, http.StatusMovedPermanently, rw.Code)
assert.Equal(t, "https://example.com/", rw.Header().Get("Location"))
}