From bfb22506ff5f2c3db37eb7db7aa4089ac7ebbd7b Mon Sep 17 00:00:00 2001 From: Kamal Nasser Date: Fri, 11 Oct 2019 15:39:57 +0300 Subject: [PATCH 1/5] allow redirects to whitelisted hosts with ports --- oauthproxy.go | 2 +- oauthproxy_test.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/oauthproxy.go b/oauthproxy.go index 01c18c39..dbcb42b7 100644 --- a/oauthproxy.go +++ b/oauthproxy.go @@ -505,7 +505,7 @@ func (p *OAuthProxy) IsValidRedirect(redirect string) bool { return false } for _, domain := range p.whitelistDomains { - if (redirectURL.Host == domain) || (strings.HasPrefix(domain, ".") && strings.HasSuffix(redirectURL.Host, domain)) { + if (redirectURL.Hostname() == domain) || (strings.HasPrefix(domain, ".") && strings.HasSuffix(redirectURL.Hostname(), domain)) { return true } } diff --git a/oauthproxy_test.go b/oauthproxy_test.go index 8dd3adfb..d7774cc1 100644 --- a/oauthproxy_test.go +++ b/oauthproxy_test.go @@ -225,6 +225,12 @@ func TestIsValidRedirect(t *testing.T) { invalidHTTPS2 := proxy.IsValidRedirect("https://evil.corp/redirect?rd=foo.bar") assert.Equal(t, false, invalidHTTPS2) + + validPort := proxy.IsValidRedirect("http://foo.bar:3838/redirect") + assert.Equal(t, true, validPort) + + validPortSubdomain := proxy.IsValidRedirect("http://baz.bar.foo:3838/redirect") + assert.Equal(t, true, validPortSubdomain) } type TestProvider struct { From ae4e9155d27a4079670b909484e50696455e6925 Mon Sep 17 00:00:00 2001 From: Kamal Nasser Date: Sat, 12 Oct 2019 23:47:23 +0300 Subject: [PATCH 2/5] implicit/explicit redirect port matching --- oauthproxy.go | 20 ++++++++++++++++++-- oauthproxy_test.go | 25 ++++++++++++++++++++----- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/oauthproxy.go b/oauthproxy.go index dbcb42b7..5278445c 100644 --- a/oauthproxy.go +++ b/oauthproxy.go @@ -504,11 +504,27 @@ func (p *OAuthProxy) IsValidRedirect(redirect string) bool { if err != nil { return false } + redirectHostname := redirectURL.Hostname() + for _, domain := range p.whitelistDomains { - if (redirectURL.Hostname() == domain) || (strings.HasPrefix(domain, ".") && strings.HasSuffix(redirectURL.Hostname(), domain)) { - return true + domainURL := url.URL{ + Host: strings.TrimLeft(domain, "."), + } + domainHostname := domainURL.Hostname() + if domainHostname == "" { + continue + } + + if (redirectHostname == domainHostname) || (strings.HasPrefix(domain, ".") && strings.HasSuffix(redirectHostname, domainHostname)) { + // if the domain has a port, only allow that port + // otherwise allow any port + domainPort := domainURL.Port() + if (domainPort == "") || (domainPort == redirectURL.Port()) { + return true + } } } + return false default: return false diff --git a/oauthproxy_test.go b/oauthproxy_test.go index d7774cc1..2745c1b7 100644 --- a/oauthproxy_test.go +++ b/oauthproxy_test.go @@ -182,7 +182,7 @@ func TestIsValidRedirect(t *testing.T) { opts.ClientSecret = "fgkdsgj" opts.CookieSecret = "ljgiogbj" // Should match domains that are exactly foo.bar and any subdomain of bar.foo - opts.WhitelistDomains = []string{"foo.bar", ".bar.foo"} + opts.WhitelistDomains = []string{"foo.bar", ".bar.foo", "port.bar:8080", ".sub.port.bar:8080"} opts.Validate() proxy := NewOAuthProxy(opts, func(string) bool { return true }) @@ -226,11 +226,26 @@ func TestIsValidRedirect(t *testing.T) { invalidHTTPS2 := proxy.IsValidRedirect("https://evil.corp/redirect?rd=foo.bar") assert.Equal(t, false, invalidHTTPS2) - validPort := proxy.IsValidRedirect("http://foo.bar:3838/redirect") - assert.Equal(t, true, validPort) + invalidPort := proxy.IsValidRedirect("https://evil.corp:3838/redirect") + assert.Equal(t, false, invalidPort) - validPortSubdomain := proxy.IsValidRedirect("http://baz.bar.foo:3838/redirect") - assert.Equal(t, true, validPortSubdomain) + validAnyPort := proxy.IsValidRedirect("http://foo.bar:3838/redirect") + assert.Equal(t, true, validAnyPort) + + validAnyPortSubdomain := proxy.IsValidRedirect("http://baz.bar.foo:3838/redirect") + assert.Equal(t, true, validAnyPortSubdomain) + + validSpecificPort := proxy.IsValidRedirect("http://port.bar:8080/redirect") + assert.Equal(t, true, validSpecificPort) + + invalidSpecificPort := proxy.IsValidRedirect("http://port.bar:3838/redirect") + assert.Equal(t, false, invalidSpecificPort) + + validSpecificPortSubdomain := proxy.IsValidRedirect("http://foo.sub.port.bar:8080/redirect") + assert.Equal(t, true, validSpecificPortSubdomain) + + invalidSpecificPortSubdomain := proxy.IsValidRedirect("http://foo.sub.port.bar:3838/redirect") + assert.Equal(t, false, invalidSpecificPortSubdomain) } type TestProvider struct { From a12bae35ca0a19e6571ca4ca4ea5497d6f1c31d5 Mon Sep 17 00:00:00 2001 From: Kamal Nasser Date: Wed, 23 Oct 2019 16:38:44 +0300 Subject: [PATCH 3/5] update port whitelisting rules, refactor IsValidRedirect tests --- oauthproxy.go | 56 +++++++++++-- oauthproxy_test.go | 204 ++++++++++++++++++++++++++++++++------------- 2 files changed, 193 insertions(+), 67 deletions(-) diff --git a/oauthproxy.go b/oauthproxy.go index 5278445c..8d781770 100644 --- a/oauthproxy.go +++ b/oauthproxy.go @@ -494,6 +494,43 @@ func (p *OAuthProxy) GetRedirect(req *http.Request) (redirect string, err error) return } +// splitHostPort separates host and port. If the port is not valid, it returns +// the entire input as host, and it doesn't check the validity of the host. +// Unlike net.SplitHostPort, but per RFC 3986, it requires ports to be numeric. +// *** taken from net/url, modified validOptionalPort() to accept ":*" +func splitHostPort(hostport string) (host, port string) { + host = hostport + + colon := strings.LastIndexByte(host, ':') + if colon != -1 && validOptionalPort(host[colon:]) { + host, port = host[:colon], host[colon+1:] + } + + if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") { + host = host[1 : len(host)-1] + } + + return +} + +// validOptionalPort reports whether port is either an empty string +// or matches /^:\d*$/ +// *** taken from net/url, modified to accept ":*" +func validOptionalPort(port string) bool { + if port == "" || port == ":*" { + return true + } + if port[0] != ':' { + return false + } + for _, b := range port[1:] { + if b < '0' || b > '9' { + return false + } + } + return true +} + // IsValidRedirect checks whether the redirect URL is whitelisted func (p *OAuthProxy) IsValidRedirect(redirect string) bool { switch { @@ -507,19 +544,20 @@ func (p *OAuthProxy) IsValidRedirect(redirect string) bool { redirectHostname := redirectURL.Hostname() for _, domain := range p.whitelistDomains { - domainURL := url.URL{ - Host: strings.TrimLeft(domain, "."), - } - domainHostname := domainURL.Hostname() - if domainHostname == "" { + domainHostname, domainPort := splitHostPort(strings.TrimLeft(domain, ".")) + if err != nil || domainHostname == "" { continue } if (redirectHostname == domainHostname) || (strings.HasPrefix(domain, ".") && strings.HasSuffix(redirectHostname, domainHostname)) { - // if the domain has a port, only allow that port - // otherwise allow any port - domainPort := domainURL.Port() - if (domainPort == "") || (domainPort == redirectURL.Port()) { + // the domain names match, now validate the ports + // if the whitelisted domain's port is '*', allow all ports + // if the whitelisted domain contains a specific port, only allow that port + // if the whitelisted domain doesn't contain a port at all, only allow empty redirect ports ie http and https + redirectPort := redirectURL.Port() + if (domainPort == "*") || + (domainPort == redirectPort) || + (domainPort == "" && redirectPort == "") { return true } } diff --git a/oauthproxy_test.go b/oauthproxy_test.go index 2745c1b7..5bd6523c 100644 --- a/oauthproxy_test.go +++ b/oauthproxy_test.go @@ -182,70 +182,158 @@ func TestIsValidRedirect(t *testing.T) { opts.ClientSecret = "fgkdsgj" opts.CookieSecret = "ljgiogbj" // Should match domains that are exactly foo.bar and any subdomain of bar.foo - opts.WhitelistDomains = []string{"foo.bar", ".bar.foo", "port.bar:8080", ".sub.port.bar:8080"} + opts.WhitelistDomains = []string{ + "foo.bar", + ".bar.foo", + "port.bar:8080", + ".sub.port.bar:8080", + "anyport.bar:*", + ".sub.anyport.bar:*", + } opts.Validate() proxy := NewOAuthProxy(opts, func(string) bool { return true }) - noRD := proxy.IsValidRedirect("") - assert.Equal(t, false, noRD) + testCases := []struct { + Desc, Redirect string + ExpectedResult bool + }{ + { + Desc: "noRD", + Redirect: "", + ExpectedResult: false, + }, + { + Desc: "singleSlash", + Redirect: "/redirect", + ExpectedResult: true, + }, + { + Desc: "doubleSlash", + Redirect: "//redirect", + ExpectedResult: false, + }, + { + Desc: "validHTTP", + Redirect: "http://foo.bar/redirect", + ExpectedResult: true, + }, + { + Desc: "validHTTPS", + Redirect: "https://foo.bar/redirect", + ExpectedResult: true, + }, + { + Desc: "invalidHTTPSubdomain", + Redirect: "http://baz.foo.bar/redirect", + ExpectedResult: false, + }, + { + Desc: "invalidHTTPSSubdomain", + Redirect: "https://baz.foo.bar/redirect", + ExpectedResult: false, + }, + { + Desc: "validHTTPSubdomain", + Redirect: "http://baz.bar.foo/redirect", + ExpectedResult: true, + }, + { + Desc: "validHTTPSSubdomain", + Redirect: "https://baz.bar.foo/redirect", + ExpectedResult: true, + }, + { + Desc: "validHTTPDomain", + Redirect: "http://bar.foo/redirect", + ExpectedResult: true, + }, + { + Desc: "invalidHTTP1", + Redirect: "http://foo.bar.evil.corp/redirect", + ExpectedResult: false, + }, + { + Desc: "invalidHTTPS1", + Redirect: "https://foo.bar.evil.corp/redirect", + ExpectedResult: false, + }, + { + Desc: "invalidHTTP2", + Redirect: "http://evil.corp/redirect?rd=foo.bar", + ExpectedResult: false, + }, + { + Desc: "invalidHTTPS2", + Redirect: "https://evil.corp/redirect?rd=foo.bar", + ExpectedResult: false, + }, + { + Desc: "invalidPort", + Redirect: "https://evil.corp:3838/redirect", + ExpectedResult: false, + }, + { + Desc: "invalidEmptyPort", + Redirect: "http://foo.bar:3838/redirect", + ExpectedResult: false, + }, + { + Desc: "invalidEmptyPortSubdomain", + Redirect: "http://baz.bar.foo:3838/redirect", + ExpectedResult: false, + }, + { + Desc: "validSpecificPort", + Redirect: "http://port.bar:8080/redirect", + ExpectedResult: true, + }, + { + Desc: "invalidSpecificPort", + Redirect: "http://port.bar:3838/redirect", + ExpectedResult: false, + }, + { + Desc: "validSpecificPortSubdomain", + Redirect: "http://foo.sub.port.bar:8080/redirect", + ExpectedResult: true, + }, + { + Desc: "invalidSpecificPortSubdomain", + Redirect: "http://foo.sub.port.bar:3838/redirect", + ExpectedResult: false, + }, + { + Desc: "validAnyPort1", + Redirect: "http://anyport.bar:8080/redirect", + ExpectedResult: true, + }, + { + Desc: "validAnyPort2", + Redirect: "http://anyport.bar:8081/redirect", + ExpectedResult: true, + }, + { + Desc: "validAnyPortSubdomain1", + Redirect: "http://a.sub.anyport.bar:8080/redirect", + ExpectedResult: true, + }, + { + Desc: "validAnyPortSubdomain2", + Redirect: "http://a.sub.anyport.bar:8081/redirect", + ExpectedResult: true, + }, + } - singleSlash := proxy.IsValidRedirect("/redirect") - assert.Equal(t, true, singleSlash) + for _, tc := range testCases { + t.Run(tc.Desc, func(t *testing.T) { + result := proxy.IsValidRedirect(tc.Redirect) - doubleSlash := proxy.IsValidRedirect("//redirect") - assert.Equal(t, false, doubleSlash) - - validHTTP := proxy.IsValidRedirect("http://foo.bar/redirect") - assert.Equal(t, true, validHTTP) - - validHTTPS := proxy.IsValidRedirect("https://foo.bar/redirect") - assert.Equal(t, true, validHTTPS) - - invalidHTTPSubdomain := proxy.IsValidRedirect("http://baz.foo.bar/redirect") - assert.Equal(t, false, invalidHTTPSubdomain) - - invalidHTTPSSubdomain := proxy.IsValidRedirect("https://baz.foo.bar/redirect") - assert.Equal(t, false, invalidHTTPSSubdomain) - - validHTTPSubdomain := proxy.IsValidRedirect("http://baz.bar.foo/redirect") - assert.Equal(t, true, validHTTPSubdomain) - - validHTTPSSubdomain := proxy.IsValidRedirect("https://baz.bar.foo/redirect") - assert.Equal(t, true, validHTTPSSubdomain) - - invalidHTTP1 := proxy.IsValidRedirect("http://foo.bar.evil.corp/redirect") - assert.Equal(t, false, invalidHTTP1) - - invalidHTTPS1 := proxy.IsValidRedirect("https://foo.bar.evil.corp/redirect") - assert.Equal(t, false, invalidHTTPS1) - - invalidHTTP2 := proxy.IsValidRedirect("http://evil.corp/redirect?rd=foo.bar") - assert.Equal(t, false, invalidHTTP2) - - invalidHTTPS2 := proxy.IsValidRedirect("https://evil.corp/redirect?rd=foo.bar") - assert.Equal(t, false, invalidHTTPS2) - - invalidPort := proxy.IsValidRedirect("https://evil.corp:3838/redirect") - assert.Equal(t, false, invalidPort) - - validAnyPort := proxy.IsValidRedirect("http://foo.bar:3838/redirect") - assert.Equal(t, true, validAnyPort) - - validAnyPortSubdomain := proxy.IsValidRedirect("http://baz.bar.foo:3838/redirect") - assert.Equal(t, true, validAnyPortSubdomain) - - validSpecificPort := proxy.IsValidRedirect("http://port.bar:8080/redirect") - assert.Equal(t, true, validSpecificPort) - - invalidSpecificPort := proxy.IsValidRedirect("http://port.bar:3838/redirect") - assert.Equal(t, false, invalidSpecificPort) - - validSpecificPortSubdomain := proxy.IsValidRedirect("http://foo.sub.port.bar:8080/redirect") - assert.Equal(t, true, validSpecificPortSubdomain) - - invalidSpecificPortSubdomain := proxy.IsValidRedirect("http://foo.sub.port.bar:3838/redirect") - assert.Equal(t, false, invalidSpecificPortSubdomain) + if result != tc.ExpectedResult { + t.Errorf("expected %t got %t", tc.ExpectedResult, result) + } + }) + } } type TestProvider struct { From 1af7c208eeab793f648dca2ca42c92cb104b069b Mon Sep 17 00:00:00 2001 From: Kamal Nasser Date: Wed, 23 Oct 2019 16:48:16 +0300 Subject: [PATCH 4/5] Update documentation and changelog --- CHANGELOG.md | 1 + docs/configuration/configuration.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55548857..e659781e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - [#275](https://github.com/pusher/oauth2_proxy/pull/275) docker: build from debian buster (@syscll) - [#258](https://github.com/pusher/oauth2_proxy/pull/258) Add IDToken for Azure provider - This PR adds the IDToken into the session for the Azure provider allowing requests to a backend to be identified as a specific user. As a consequence, if you are using a cookie to store the session the cookie will now exceed the 4kb size limit and be split into multiple cookies. This can cause problems when using nginx as a proxy, resulting in no cookie being passed at all. Either increase the proxy_buffer_size in nginx or implement the redis session storage (see https://pusher.github.io/oauth2_proxy/configuration#redis-storage) + - [#280](https://github.com/pusher/oauth2_proxy/pull/280) Add support for whitelisting specific ports or allowing wildcard ports in whitelisted redirect domains # v4.0.0 diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md index 332c2238..0caf942a 100644 --- a/docs/configuration/configuration.md +++ b/docs/configuration/configuration.md @@ -111,7 +111,7 @@ An example [oauth2_proxy.cfg]({{ site.gitweb }}/contrib/oauth2_proxy.cfg.example | `-version` | n/a | print version string | | | `-whitelist-domain` | string \| list | allowed domains for redirection after authentication. Prefix domain with a `.` to allow subdomains (eg `.example.com`) | | -Note, when using the `whitelist-domain` option, any domain prefixed with a `.` will allow any subdomain of the specified domain as a valid redirect URL. +Note: when using the `whitelist-domain` option, any domain prefixed with a `.` will allow any subdomain of the specified domain as a valid redirect URL. By default, only empty ports are allowed. This translates to allowing the default port of the URL's protocol (80 for HTTP, 443 for HTTPS, etc.) since browsers omit them. To allow only a specific port, add it to the whitelisted domain: `example.com:8080`. To allow any port, use `*`: `example.com:*`. See below for provider specific options From 898b6b81c9b7812ca4c197b19420fa07eb7ac323 Mon Sep 17 00:00:00 2001 From: Kamal Nasser Date: Thu, 14 Nov 2019 17:17:12 +0200 Subject: [PATCH 5/5] remove unnecessary if conditional --- oauthproxy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oauthproxy.go b/oauthproxy.go index 8d781770..11860a95 100644 --- a/oauthproxy.go +++ b/oauthproxy.go @@ -545,7 +545,7 @@ func (p *OAuthProxy) IsValidRedirect(redirect string) bool { for _, domain := range p.whitelistDomains { domainHostname, domainPort := splitHostPort(strings.TrimLeft(domain, ".")) - if err != nil || domainHostname == "" { + if domainHostname == "" { continue }