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

Commit 748247d

Browse files
committed
Merge pull request #17 from jehiah/encoded_slashes_17
Encoded slashes are expanded by the proxy
2 parents 85e025d + 71ae708 commit 748247d

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

oauthproxy.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,18 @@ func setProxyUpstreamHostHeader(proxy *httputil.ReverseProxy, target *url.URL) {
5555
director := proxy.Director
5656
proxy.Director = func(req *http.Request) {
5757
director(req)
58-
req.Host = target.Host
58+
// use RequestURI so that we aren't unescaping encoded slashes in the request path
59+
req.URL.Opaque = fmt.Sprintf("//%s%s", target.Host, req.RequestURI)
60+
req.URL.RawQuery = ""
61+
}
62+
}
63+
func setProxyDirector(proxy *httputil.ReverseProxy) {
64+
director := proxy.Director
65+
proxy.Director = func(req *http.Request) {
66+
director(req)
67+
// use RequestURI so that we aren't unescaping encoded slashes in the request path
68+
req.URL.Opaque = fmt.Sprintf("//%s%s", req.URL.Host, req.RequestURI)
69+
req.URL.RawQuery = ""
5970
}
6071
}
6172

@@ -70,6 +81,8 @@ func NewOauthProxy(opts *Options, validator func(string) bool) *OauthProxy {
7081
proxy := NewReverseProxy(u)
7182
if !opts.PassHostHeader {
7283
setProxyUpstreamHostHeader(proxy, u)
84+
} else {
85+
setProxyDirector(proxy)
7386
}
7487
serveMux.Handle(path, proxy)
7588
}

oauthproxy_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,30 @@ func TestNewReverseProxy(t *testing.T) {
3535
t.Errorf("got body %q; expected %q", g, e)
3636
}
3737
}
38+
39+
func TestEncodedSlashes(t *testing.T) {
40+
var seen string
41+
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
42+
w.WriteHeader(200)
43+
seen = r.RequestURI
44+
}))
45+
defer backend.Close()
46+
47+
b, _ := url.Parse(backend.URL)
48+
proxyHandler := NewReverseProxy(b)
49+
setProxyDirector(proxyHandler)
50+
frontend := httptest.NewServer(proxyHandler)
51+
defer frontend.Close()
52+
53+
f, _ := url.Parse(frontend.URL)
54+
encodedPath := "/a%2Fb/"
55+
getReq := &http.Request{URL: &url.URL{Scheme: "http", Host: f.Host, Opaque: encodedPath}}
56+
_, err := http.DefaultClient.Do(getReq)
57+
if err != nil {
58+
t.Fatalf("err %s", err)
59+
}
60+
expected := backend.URL + encodedPath
61+
if seen != expected {
62+
t.Errorf("got bad request %q expected %q", seen, expected)
63+
}
64+
}

0 commit comments

Comments
 (0)