@@ -15,7 +15,6 @@ import (
1515 "time"
1616
1717 "github.com/coreos/go-oidc/v3/oidc"
18− middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
1918 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
2019 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
2120 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/authentication/hmacauth"
@@ -54,7 +53,7 @@ func TestRobotsTxt(t *testing.T) {
5453 t.Fatal(err)
5554 }
5655 rw := httptest.NewRecorder()
57− req, _ := http.NewRequest(http.MethodGet, "/robots.txt", nil)
56+ req, _ := http.NewRequest("GET", "/robots.txt", nil)
5857 proxy.ServeHTTP(rw, req)
5958 assert.Equal(t, 200, rw.Code)
6059 assert.Equal(t, "User-agent: *\nDisallow: /\n", rw.Body.String())
@@ -241,7 +240,7 @@ func TestBasicAuthPassword(t *testing.T) {
241240
242241 // Save the required session
243242 rw := httptest.NewRecorder()
244− req, _ := http.NewRequest(http.MethodGet, "/", nil)
243+ req, _ := http.NewRequest("GET", "/", nil)
245244 err = proxy.sessionStore.Save(rw, req, &sessions.SessionState{
246245 Email: emailAddress,
247246 })
@@ -250,7 +249,7 @@ func TestBasicAuthPassword(t *testing.T) {
250249 // Extract the cookie value to inject into the test request
251250 cookie := rw.Header().Values("Set-Cookie")[0]
252251
253− req, _ = http.NewRequest(http.MethodGet, "/", nil)
252+ req, _ = http.NewRequest("GET", "/", nil)
254253 req.Header.Set("Cookie", cookie)
255254 rw = httptest.NewRecorder()
256255 proxy.ServeHTTP(rw, req)
@@ -300,14 +299,14 @@ func TestPassGroupsHeadersWithGroups(t *testing.T) {
300299
301300 // Save the required session
302301 rw := httptest.NewRecorder()
303− req, _ := http.NewRequest(http.MethodGet, "/", nil)
302+ req, _ := http.NewRequest("GET", "/", nil)
304303 err = proxy.sessionStore.Save(rw, req, session)
305304 assert.NoError(t, err)
306305
307306 // Extract the cookie value to inject into the test request
308307 cookie := rw.Header().Values("Set-Cookie")[0]
309308
310− req, _ = http.NewRequest(http.MethodGet, "/", nil)
309+ req, _ = http.NewRequest("GET", "/", nil)
311310 req.Header.Set("Cookie", cookie)
312311 rw = httptest.NewRecorder()
313312 proxy.ServeHTTP(rw, req)
@@ -457,7 +456,7 @@ func (patTest *PassAccessTokenTest) getEndpointWithCookie(cookie string, endpoin
457456 return 0, ""
458457 }
459458
460− req, err := http.NewRequest(http.MethodGet, endpoint, strings.NewReader(""))
459+ req, err := http.NewRequest("GET", endpoint, strings.NewReader(""))
461460 if err != nil {
462461 return 0, ""
463462 }
@@ -608,7 +607,7 @@ func NewSignInPageTest(skipProvider bool) (*SignInPageTest, error) {
608607
609608func (sipTest *SignInPageTest) GetEndpoint(endpoint string) (int, string) {
610609 rw := httptest.NewRecorder()
611− req, _ := http.NewRequest(http.MethodGet, endpoint, strings.NewReader(""))
610+ req, _ := http.NewRequest("GET", endpoint, strings.NewReader(""))
612611 sipTest.proxy.ServeHTTP(rw, req)
613612 return rw.Code, rw.Body.String()
614613}
@@ -713,50 +712,6 @@ func TestManualSignInCorrectCredentials(t *testing.T) {
713712 assert.Equal(t, http.StatusFound, statusCode)
714713}
715714
716−func TestSignInPageClearsExistingSessionCookie(t *testing.T) {
717− opts := baseTestOptions()
718− err := validation.Validate(opts)
719− require.NoError(t, err)
720−
721− proxy, err := NewOAuthProxy(opts, func(string) bool {
722− return true
723− })
724− require.NoError(t, err)
725−
726− // Create a real session cookie using the actual session store.
727− saveRW := httptest.NewRecorder()
728− saveReq := httptest.NewRequest(http.MethodGet, "/", nil)
729− err = proxy.sessionStore.Save(saveRW, saveReq, &sessions.SessionState{
730− Email: "john.doe@example.com",
731− })
732− require.NoError(t, err)
733−
734− cookies := saveRW.Result().Cookies()
735− require.NotEmpty(t, cookies)
736−
737− // Send that cookie to the sign-in page.
738− req := httptest.NewRequest(http.MethodGet, "/oauth2/sign_in", nil)
739− for _, c := range cookies {
740− req.AddCookie(c)
741− }
742−
743− rw := httptest.NewRecorder()
744− proxy.ServeHTTP(rw, req)
745−
746− assert.Equal(t, http.StatusOK, rw.Code)
747−
748− cleared := false
749− for _, c := range rw.Result().Cookies() {
750− if c.Name == proxy.CookieOptions.Name {
751− cleared = true
752− assert.Equal(t, "", c.Value)
753− assert.Less(t, c.MaxAge, 0)
754− }
755− }
756−
757− assert.True(t, cleared, "expected sign-in page to clear existing session cookie")
758−}
759−
760715func TestSignInPageIncludesTargetRedirect(t *testing.T) {
761716 sipTest, err := NewSignInPageTest(false)
762717 if err != nil {
@@ -879,8 +834,20 @@ func NewProcessCookieTest(opts ProcessCookieTestOpts, modifiers ...OptionsModifi
879834 return nil, err
880835 }
881836 testProvider := &TestProvider{
882− ProviderData: &providers.ProviderData{},
883− ValidToken: opts.providerValidateCookieResponse,
837+ ProviderData: &providers.ProviderData{
838+ ProviderName: "Test Provider",
839+ LoginURL: &url.URL{
840+ Scheme: "http",
841+ Host: "localhost",
842+ Path: "/oauth/authorize",
843+ },
844+ RedeemURL: &url.URL{
845+ Scheme: "http",
846+ Host: "localhost",
847+ Path: "/oauth/token",
848+ },
849+ },
850+ ValidToken: opts.providerValidateCookieResponse,
884851 }
885852
886853 groups := pcTest.opts.Providers[0].AllowedGroups
@@ -894,7 +861,7 @@ func NewProcessCookieTest(opts ProcessCookieTestOpts, modifiers ...OptionsModifi
894861 // access_token validation.
895862 pcTest.proxy.CookieOptions.Refresh = time.Duration(0)
896863 pcTest.rw = httptest.NewRecorder()
897− pcTest.req, _ = http.NewRequest(http.MethodGet, "/", strings.NewReader(""))
864+ pcTest.req, _ = http.NewRequest("GET", "/", strings.NewReader(""))
898865 pcTest.validateUser = true
899866 return &pcTest, nil
900867}
@@ -1027,7 +994,7 @@ func NewUserInfoEndpointTest() (*ProcessCookieTest, error) {
1027994 if err != nil {
1028995 return nil, err
1029996 }
1030− pcTest.req, _ = http.NewRequest(http.MethodGet,
997+ pcTest.req, _ = http.NewRequest("GET",
1031998 pcTest.opts.ProxyPrefix+"/userinfo", nil)
1032999 return pcTest, nil
10331000}
@@ -1077,20 +1044,6 @@ func TestUserInfoEndpointAccepted(t *testing.T) {
10771044 },
10781045 expectedResponse: "{\"user\":\"john.doe\",\"email\":\"john.doe@example.com\",\"groups\":[\"example\",\"groups\"],\"preferredUsername\":\"john\"}\n",
10791046 },
1080− {
1081− name: "With Additional Claim",
1082− session: &sessions.SessionState{
1083− User: "john.doe",
1084− PreferredUsername: "john",
1085− Email: "john.doe@example.com",
1086− Groups: []string{"example", "groups"},
1087− AccessToken: "my_access_token",
1088− AdditionalClaims: map[string]interface{}{
1089− "foo": "bar",
1090− },
1091− },
1092− expectedResponse: "{\"user\":\"john.doe\",\"email\":\"john.doe@example.com\",\"groups\":[\"example\",\"groups\"],\"preferredUsername\":\"john\",\"additionalClaims\":{\"foo\":\"bar\"}}\n",
1093− },
10941047 }
10951048
10961049 for _, tc := range testCases {
@@ -1135,7 +1088,7 @@ func NewAuthOnlyEndpointTest(querystring string, modifiers ...OptionsModifier) (
11351088 return nil, err
11361089 }
11371090 pcTest.req, _ = http.NewRequest(
1138− http.MethodGet,
1091+ "GET",
11391092 fmt.Sprintf("%s/auth%s", pcTest.opts.ProxyPrefix, querystring),
11401093 nil)
11411094 return pcTest, nil
@@ -1171,6 +1124,35 @@ func TestAuthOnlyEndpointUnauthorizedOnNoCookieSetError(t *testing.T) {
11711124 assert.Equal(t, "Unauthorized\n", string(bodyBytes))
11721125}
11731126
1127+// TestAuthOnlyEndpointRedirectWithSkipProviderButton tests that when SkipProviderButton
1128+// is true and no session exists, the /auth endpoint should return a 302 redirect
1129+// to the OAuth provider instead of 401. This is important for nginx auth_request
1130+// architecture where 401 triggers error_page handling which can break the redirect flow.
1131+// See: https://github.com/oauth2-proxy/oauth2-proxy/issues/334
1132+func TestAuthOnlyEndpointRedirectWithSkipProviderButton(t *testing.T) {
1133+ test, err := NewAuthOnlyEndpointTest("", func(opts *options.Options) {
1134+ opts.SkipProviderButton = true
1135+ })
1136+ if err != nil {
1137+ t.Fatal(err)
1138+ }
1139+
1140+ test.proxy.ServeHTTP(test.rw, test.req)
1141+
1142+ // With SkipProviderButton=true and no session, should return 302 redirect
1143+ // to OAuth provider, NOT 401 Unauthorized
1144+ assert.Equal(t, http.StatusFound, test.rw.Code)
1145+ location := test.rw.Header().Get("Location")
1146+ assert.NotEmpty(t, location, "Expected Location header for redirect")
1147+
1148+ // Verify the redirect points to the OAuth provider's authorize endpoint
1149+ // and contains key OAuth parameters
1150+ assert.Contains(t, location, "/oauth/authorize", "Expected redirect to OAuth authorize endpoint")
1151+ assert.Contains(t, location, "client_id=", "Expected client_id in redirect URL")
1152+ assert.Contains(t, location, "redirect_uri=", "Expected redirect_uri in redirect URL")
1153+ assert.Contains(t, location, "state=", "Expected state parameter in redirect URL")
1154+}
1155+
11741156func TestAuthOnlyEndpointUnauthorizedOnExpiration(t *testing.T) {
11751157 test, err := NewAuthOnlyEndpointTest("", func(opts *options.Options) {
11761158 opts.Cookie.Expire = time.Duration(24) * time.Hour
@@ -1274,7 +1256,7 @@ func TestAuthOnlyEndpointSetXAuthRequestHeaders(t *testing.T) {
12741256 pcTest.validateUser = true
12751257
12761258 pcTest.rw = httptest.NewRecorder()
1277− pcTest.req, _ = http.NewRequest(http.MethodGet,
1259+ pcTest.req, _ = http.NewRequest("GET",
12781260 pcTest.opts.ProxyPrefix+authOnlyPath, nil)
12791261
12801262 created := time.Now()
@@ -1367,7 +1349,7 @@ func TestAuthOnlyEndpointSetBasicAuthTrueRequestHeaders(t *testing.T) {
13671349 pcTest.validateUser = true
13681350
13691351 pcTest.rw = httptest.NewRecorder()
1370− pcTest.req, _ = http.NewRequest(http.MethodGet,
1352+ pcTest.req, _ = http.NewRequest("GET",
13711353 pcTest.opts.ProxyPrefix+authOnlyPath, nil)
13721354
13731355 created := time.Now()
@@ -1447,7 +1429,7 @@ func TestAuthOnlyEndpointSetBasicAuthFalseRequestHeaders(t *testing.T) {
14471429 pcTest.validateUser = true
14481430
14491431 pcTest.rw = httptest.NewRecorder()
1450− pcTest.req, _ = http.NewRequest(http.MethodGet,
1432+ pcTest.req, _ = http.NewRequest("GET",
14511433 pcTest.opts.ProxyPrefix+authOnlyPath, nil)
14521434
14531435 created := time.Now()
@@ -1495,7 +1477,7 @@ func TestAuthSkippedForPreflightRequests(t *testing.T) {
14951477 }
14961478 proxy.provider = NewTestProvider(upstreamURL, "")
14971479 rw := httptest.NewRecorder()
1498− req, _ := http.NewRequest(http.MethodOptions, "/preflight-request", nil)
1480+ req, _ := http.NewRequest("OPTIONS", "/preflight-request", nil)
14991481 proxy.ServeHTTP(rw, req)
15001482
15011483 assert.Equal(t, 200, rw.Code)
@@ -1652,19 +1634,19 @@ func TestRequestSignature(t *testing.T) {
16521634 resp string
16531635 }{
16541636 "No request signature": {
1655− method: http.MethodGet,
1637+ method: "GET",
16561638 body: "",
16571639 key: "",
16581640 resp: "no signature received",
16591641 },
16601642 "Get request": {
1661− method: http.MethodGet,
1643+ method: "GET",
16621644 body: "",
16631645 key: "7d9e1aa87a5954e6f9fc59266b3af9d7c35fda2d",
16641646 resp: "signatures match",
16651647 },
16661648 "Post request": {
1667− method: http.MethodPost,
1649+ method: "POST",
16681650 body: `{ "hello": "world!" }`,
16691651 key: "d90df39e2d19282840252612dd7c81421a372f61",
16701652 resp: "signatures match",
@@ -2087,44 +2069,6 @@ func Test_noCacheHeaders(t *testing.T) {
20872069 })
20882070}
20892071
2090−func TestSignOutCallsBackendLogoutURL(t *testing.T) {
2091− const testIDToken = "test-id-token-12345"
2092− var receivedURL string
2093− backendLogoutServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
2094− receivedURL = req.URL.String()
2095− rw.WriteHeader(http.StatusOK)
2096− }))
2097− defer backendLogoutServer.Close()
2098−
2099− opts := baseTestOptions()
2100− opts.Providers[0].BackendLogoutURL = backendLogoutServer.URL + "/logout?id_token_hint={id_token}"
2101− err := validation.Validate(opts)
2102− require.NoError(t, err)
2103−
2104− proxy, err := NewOAuthProxy(opts, func(string) bool { return true })
2105− require.NoError(t, err)
2106−
2107− // Save a session with IDToken so backend logout can use it
2108− rw := httptest.NewRecorder()
2109− req := httptest.NewRequest(http.MethodGet, "/", nil)
2110− err = proxy.sessionStore.Save(rw, req, &sessions.SessionState{
2111− Email: "user@example.com",
2112− IDToken: testIDToken,
2113− })
2114− require.NoError(t, err)
2115− cookie := rw.Header().Values("Set-Cookie")[0]
2116−
2117− // Hit sign_out with the session cookie; backend logout should be called before session is cleared
2118− signOutReq := httptest.NewRequest(http.MethodGet, "/oauth2/sign_out", nil)
2119− signOutReq.Header.Set("Cookie", cookie)
2120− rec := httptest.NewRecorder()
2121− proxy.ServeHTTP(rec, signOutReq)
2122−
2123− assert.Equal(t, http.StatusFound, rec.Code, "sign_out should redirect")
2124− assert.Contains(t, receivedURL, "id_token_hint="+testIDToken,
2125− "backend logout URL should have been called with id_token from session")
2126−}
2127−
21282072func baseTestOptions() *options.Options {
21292073 opts := options.NewOptions()
21302074 opts.Cookie.Secret = rawCookieSecret
@@ -2189,33 +2133,7 @@ func TestTrustedIPs(t *testing.T) {
21892133 reverseProxy: false,
21902134 realClientIPHeader: "X-Real-IP", // Default value
21912135 req: func() *http.Request {
2192− req, _ := http.NewRequest(http.MethodGet, "/", nil)
2193− return req
2194− }(),
2195− expectTrusted: false,
2196− },
2197− // Check Unix socket with no trusted IPs configured does not error.
2198− {
2199− name: "UnixSocketWithoutTrustedIPs",
2200− trustedIPs: nil,
2201− reverseProxy: false,
2202− realClientIPHeader: "X-Real-IP",
2203− req: func() *http.Request {
2204− req, _ := http.NewRequest(http.MethodGet, "/", nil)
2205− req.RemoteAddr = "@"
2206− return req
2207− }(),
2208− expectTrusted: false,
2209− },
2210− // Check Unix socket with trusted IPs configured returns false (no IP to match).
2211− {
2212− name: "UnixSocketWithTrustedIPs",
2213− trustedIPs: []string{"127.0.0.1"},
2214− reverseProxy: false,
2215− realClientIPHeader: "X-Real-IP",
2216− req: func() *http.Request {
2217− req, _ := http.NewRequest(http.MethodGet, "/", nil)
2218− req.RemoteAddr = "@"
2136+ req, _ := http.NewRequest("GET", "/", nil)
22192137 return req
22202138 }(),
22212139 expectTrusted: false,
@@ -2227,7 +2145,7 @@ func TestTrustedIPs(t *testing.T) {
22272145 reverseProxy: false,
22282146 realClientIPHeader: "X-Real-IP", // Default value
22292147 req: func() *http.Request {
2230− req, _ := http.NewRequest(http.MethodGet, "/", nil)
2148+ req, _ := http.NewRequest("GET", "/", nil)
22312149 req.RemoteAddr = "127.0.0.1:43670"
22322150 return req
22332151 }(),
@@ -2240,7 +2158,7 @@ func TestTrustedIPs(t *testing.T) {
22402158 reverseProxy: true,
22412159 realClientIPHeader: "X-Real-IP", // Default value
22422160 req: func() *http.Request {
2243− req, _ := http.NewRequest(http.MethodGet, "/", nil)
2161+ req, _ := http.NewRequest("GET", "/", nil)
22442162 req.RemoteAddr = "127.0.0.1:44324"
22452163 return req
22462164 }(),
@@ -2253,7 +2171,7 @@ func TestTrustedIPs(t *testing.T) {
22532171 reverseProxy: true,
22542172 realClientIPHeader: "X-Forwarded-For",
22552173 req: func() *http.Request {
2256− req, _ := http.NewRequest(http.MethodGet, "/", nil)
2174+ req, _ := http.NewRequest("GET", "/", nil)
22572175 req.Header.Add("X-Forwarded-For", "127.0.0.1")
22582176 return req
22592177 }(),
@@ -2266,7 +2184,7 @@ func TestTrustedIPs(t *testing.T) {
22662184 reverseProxy: true,
22672185 realClientIPHeader: "X-Forwarded-For",
22682186 req: func() *http.Request {
2269− req, _ := http.NewRequest(http.MethodGet, "/", nil)
2187+ req, _ := http.NewRequest("GET", "/", nil)
22702188 req.Header.Add("X-Forwarded-For", "::1")
22712189 return req
22722190 }(),
@@ -2279,7 +2197,7 @@ func TestTrustedIPs(t *testing.T) {
22792197 reverseProxy: true,
22802198 realClientIPHeader: "X-Forwarded-For",
22812199 req: func() *http.Request {
2282− req, _ := http.NewRequest(http.MethodGet, "/", nil)
2200+ req, _ := http.NewRequest("GET", "/", nil)
22832201 req.Header.Add("X-Forwarded-For", "12.34.56.78")
22842202 return req
22852203 }(),
@@ -2292,7 +2210,7 @@ func TestTrustedIPs(t *testing.T) {
22922210 reverseProxy: true,
22932211 realClientIPHeader: "X-Forwarded-For",
22942212 req: func() *http.Request {
2295− req, _ := http.NewRequest(http.MethodGet, "/", nil)
2213+ req, _ := http.NewRequest("GET", "/", nil)
22962214 req.Header.Add("X-Forwarded-For", "::2")
22972215 return req
22982216 }(),
@@ -2305,7 +2223,7 @@ func TestTrustedIPs(t *testing.T) {
23052223 reverseProxy: true,
23062224 realClientIPHeader: "X-Forwarded-For",
23072225 req: func() *http.Request {
2308− req, _ := http.NewRequest(http.MethodGet, "/", nil)
2226+ req, _ := http.NewRequest("GET", "/", nil)
23092227 req.Header.Add("X-Real-IP", "::1")
23102228 return req
23112229 }(),
@@ -2318,7 +2236,7 @@ func TestTrustedIPs(t *testing.T) {
23182236 reverseProxy: true,
23192237 realClientIPHeader: "X-Forwarded-For",
23202238 req: func() *http.Request {
2321− req, _ := http.NewRequest(http.MethodGet, "/", nil)
2239+ req, _ := http.NewRequest("GET", "/", nil)
23222240 req.Header.Add("X-Forwarded-For", "adsfljk29242as!!")
23232241 return req
23242242 }(),
@@ -2331,7 +2249,7 @@ func TestTrustedIPs(t *testing.T) {
23312249 reverseProxy: false,
23322250 realClientIPHeader: "X-Real-IP",
23332251 req: func() *http.Request {
2334− req, _ := http.NewRequest(http.MethodGet, "/", nil)
2252+ req, _ := http.NewRequest("GET", "/", nil)
23352253 req.RemoteAddr = "adsfljk29242as!!"
23362254 return req
23372255 }(),
@@ -2427,12 +2345,12 @@ func Test_buildRoutesAllowlist(t *testing.T) {
24272345 },
24282346 expectedRoutes: []expectedAllowedRoute{
24292347 {
2430− method: http.MethodGet,
2348+ method: "GET",
24312349 negate: false,
24322350 regexString: "^/foo/bar",
24332351 },
24342352 {
2435− method: http.MethodPost,
2353+ method: "POST",
24362354 negate: false,
24372355 regexString: "^/baz/[0-9]+/thing",
24382356 },
@@ -2485,11 +2403,11 @@ func Test_buildRoutesAllowlist(t *testing.T) {
24852403 regexString: "^/baz/[0-9]+/thing/regex",
24862404 },
24872405 {
2488− method: http.MethodGet,
2406+ method: "GET",
24892407 regexString: "^/foo/bar",
24902408 },
24912409 {
2492− method: http.MethodPost,
2410+ method: "POST",
24932411 regexString: "^/baz/[0-9]+/thing",
24942412 },
24952413 {
@@ -2641,7 +2559,7 @@ func TestApiRoutes(t *testing.T) {
26412559
26422560 for _, tc := range testCases {
26432561 t.Run(tc.name, func(t *testing.T) {
2644− req, err := http.NewRequest(http.MethodGet, tc.url, nil)
2562+ req, err := http.NewRequest("GET", tc.url, nil)
26452563 req.Header.Set("Accept", tc.contentType)
26462564 assert.NoError(t, err)
26472565
@@ -2679,11 +2597,9 @@ func TestAllowedRequest(t *testing.T) {
26792597 }
26802598 opts.SkipAuthRegex = []string{
26812599 "^/skip/auth/regex$",
2682− "^/public/.*/endpoint$",
26832600 }
26842601 opts.SkipAuthRoutes = []string{
26852602 "GET=^/skip/auth/routes/get",
2686− "^/foo/.*/bar$",
26872603 }
26882604 err := validation.Validate(opts)
26892605 assert.NoError(t, err)
@@ -2700,37 +2616,25 @@ func TestAllowedRequest(t *testing.T) {
27002616 }{
27012617 {
27022618 name: "Regex GET allowed",
2703− method: http.MethodGet,
2619+ method: "GET",
27042620 url: "/skip/auth/regex",
27052621 allowed: true,
27062622 },
27072623 {
27082624 name: "Regex POST allowed ",
2709− method: http.MethodPost,
2625+ method: "POST",
27102626 url: "/skip/auth/regex",
27112627 allowed: true,
27122628 },
27132629 {
27142630 name: "Regex denied",
2715− method: http.MethodGet,
2631+ method: "GET",
27162632 url: "/wrong/denied",
27172633 allowed: false,
27182634 },
2719− {
2720− name: "Regex allowed with fragment-free path",
2721− method: http.MethodGet,
2722− url: "/public/legit/endpoint",
2723− allowed: true,
2724− },
2725− {
2726− name: "Regex denied when path contains encoded fragment suffix",
2727− method: http.MethodGet,
2728− url: "/public/secret%23/endpoint",
2729− allowed: false,
2730− },
27312635 {
27322636 name: "Route allowed",
2733− method: http.MethodGet,
2637+ method: "GET",
27342638 url: "/skip/auth/routes/get",
27352639 allowed: true,
27362640 },
@@ -2742,28 +2646,16 @@ func TestAllowedRequest(t *testing.T) {
27422646 },
27432647 {
27442648 name: "Route denied with wrong path",
2745− method: http.MethodGet,
2649+ method: "GET",
27462650 url: "/skip/auth/routes/wrong/path",
27472651 allowed: false,
27482652 },
27492653 {
27502654 name: "Route denied with wrong path and method",
2751− method: http.MethodPost,
2655+ method: "POST",
27522656 url: "/skip/auth/routes/wrong/path",
27532657 allowed: false,
27542658 },
2755− {
2756− name: "Route allowed with fragment-free path",
2757− method: http.MethodGet,
2758− url: "/foo/public/bar",
2759− allowed: true,
2760− },
2761− {
2762− name: "Route denied when path contains encoded fragment suffix",
2763− method: http.MethodGet,
2764− url: "/foo/secret%23/bar",
2765− allowed: false,
2766− },
27672659 }
27682660
27692661 for _, tc := range testCases {
@@ -2804,11 +2696,9 @@ func TestAllowedRequestWithForwardedUriHeader(t *testing.T) {
28042696 }
28052697 opts.SkipAuthRegex = []string{
28062698 "^/skip/auth/regex$",
2807− "^/public/.*/endpoint$",
28082699 }
28092700 opts.SkipAuthRoutes = []string{
28102701 "GET=^/skip/auth/routes/get",
2811− "^/foo/.*/bar$",
28122702 }
28132703 err := validation.Validate(opts)
28142704 assert.NoError(t, err)
@@ -2825,37 +2715,25 @@ func TestAllowedRequestWithForwardedUriHeader(t *testing.T) {
28252715 }{
28262716 {
28272717 name: "Regex GET allowed",
2828− method: http.MethodGet,
2718+ method: "GET",
28292719 url: "/skip/auth/regex",
28302720 allowed: true,
28312721 },
28322722 {
28332723 name: "Regex POST allowed ",
2834− method: http.MethodPost,
2724+ method: "POST",
28352725 url: "/skip/auth/regex",
28362726 allowed: true,
28372727 },
28382728 {
28392729 name: "Regex denied",
2840− method: http.MethodGet,
2730+ method: "GET",
28412731 url: "/wrong/denied",
28422732 allowed: false,
28432733 },
2844− {
2845− name: "Regex allowed with fragment-free path",
2846− method: http.MethodGet,
2847− url: "/public/legit/endpoint",
2848− allowed: true,
2849− },
2850− {
2851− name: "Regex denied when X-Forwarded-Uri contains an encoded fragment suffix",
2852− method: http.MethodGet,
2853− url: "/public/secret%23/endpoint",
2854− allowed: false,
2855− },
28562734 {
28572735 name: "Route allowed",
2858− method: http.MethodGet,
2736+ method: "GET",
28592737 url: "/skip/auth/routes/get",
28602738 allowed: true,
28612739 },
@@ -2867,35 +2745,22 @@ func TestAllowedRequestWithForwardedUriHeader(t *testing.T) {
28672745 },
28682746 {
28692747 name: "Route denied with wrong path",
2870− method: http.MethodGet,
2748+ method: "GET",
28712749 url: "/skip/auth/routes/wrong/path",
28722750 allowed: false,
28732751 },
28742752 {
28752753 name: "Route denied with wrong path and method",
2876− method: http.MethodPost,
2754+ method: "POST",
28772755 url: "/skip/auth/routes/wrong/path",
28782756 allowed: false,
28792757 },
2880− {
2881− name: "Route allowed with fragment-free path",
2882− method: http.MethodGet,
2883− url: "/foo/public/bar",
2884− allowed: true,
2885− },
2886− {
2887− name: "Route denied when X-Forwarded-Uri contains an encoded fragment suffix",
2888− method: http.MethodGet,
2889− url: "/foo/secret%23/bar",
2890− allowed: false,
2891− },
28922758 }
28932759
28942760 for _, tc := range testCases {
28952761 t.Run(tc.name, func(t *testing.T) {
28962762 req, err := http.NewRequest(tc.method, opts.ProxyPrefix+authOnlyPath, nil)
28972763 req.Header.Set("X-Forwarded-Uri", tc.url)
2898− req.RemoteAddr = "127.0.0.1:4180"
28992764 assert.NoError(t, err)
29002765
29012766 rw := httptest.NewRecorder()
@@ -2910,43 +2775,6 @@ func TestAllowedRequestWithForwardedUriHeader(t *testing.T) {
29102775 }
29112776}
29122777
2913−func TestAllowedRequestWithForwardedUriHeaderRequiresTrustedProxy(t *testing.T) {
2914− upstreamServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2915− w.WriteHeader(200)
2916− }))
2917− t.Cleanup(upstreamServer.Close)
2918−
2919− opts := baseTestOptions()
2920− opts.ReverseProxy = true
2921− opts.TrustedProxyIPs = []string{"127.0.0.1/32"}
2922− opts.UpstreamServers = options.UpstreamConfig{
2923− Upstreams: []options.Upstream{
2924− {
2925− ID: upstreamServer.URL,
2926− Path: "/",
2927− URI: upstreamServer.URL,
2928− },
2929− },
2930− }
2931− opts.SkipAuthRegex = []string{"^/skip/auth/regex$"}
2932−
2933− err := validation.Validate(opts)
2934− assert.NoError(t, err)
2935−
2936− proxy, err := NewOAuthProxy(opts, func(_ string) bool { return true })
2937− assert.NoError(t, err)
2938−
2939− req, err := http.NewRequest(http.MethodGet, opts.ProxyPrefix+authOnlyPath, nil)
2940− assert.NoError(t, err)
2941− req.RemoteAddr = "192.0.2.10:4180"
2942− req.Header.Set("X-Forwarded-Uri", "/skip/auth/regex")
2943−
2944− rw := httptest.NewRecorder()
2945− proxy.ServeHTTP(rw, req)
2946−
2947− assert.Equal(t, 401, rw.Code)
2948−}
2949−
29502778func TestAllowedRequestNegateWithoutMethod(t *testing.T) {
29512779 upstreamServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
29522780 w.WriteHeader(200)
@@ -2986,37 +2814,37 @@ func TestAllowedRequestNegateWithoutMethod(t *testing.T) {
29862814 }{
29872815 {
29882816 name: "Some static file allowed",
2989− method: http.MethodGet,
2817+ method: "GET",
29902818 url: "/static/file.txt",
29912819 allowed: true,
29922820 },
29932821 {
29942822 name: "POST to contact form allowed",
2995− method: http.MethodPost,
2823+ method: "POST",
29962824 url: "/contact",
29972825 allowed: true,
29982826 },
29992827 {
30002828 name: "Regex POST allowed",
3001− method: http.MethodPost,
2829+ method: "POST",
30022830 url: "/api/public-entity",
30032831 allowed: true,
30042832 },
30052833 {
30062834 name: "Regex POST with trailing slash allowed",
3007− method: http.MethodPost,
2835+ method: "POST",
30082836 url: "/api/public-entity/",
30092837 allowed: true,
30102838 },
30112839 {
30122840 name: "Regex GET api route denied",
3013− method: http.MethodGet,
2841+ method: "GET",
30142842 url: "/api/users",
30152843 allowed: false,
30162844 },
30172845 {
30182846 name: "Regex POST api route denied",
3019− method: http.MethodPost,
2847+ method: "POST",
30202848 url: "/api/users",
30212849 allowed: false,
30222850 },
@@ -3086,37 +2914,37 @@ func TestAllowedRequestNegateWithMethod(t *testing.T) {
30862914 }{
30872915 {
30882916 name: "Some static file allowed",
3089− method: http.MethodGet,
2917+ method: "GET",
30902918 url: "/static/file.txt",
30912919 allowed: true,
30922920 },
30932921 {
30942922 name: "POST to contact form not allowed",
3095− method: http.MethodPost,
2923+ method: "POST",
30962924 url: "/contact",
30972925 allowed: false,
30982926 },
30992927 {
31002928 name: "Regex POST allowed",
3101− method: http.MethodPost,
2929+ method: "POST",
31022930 url: "/api/public-entity",
31032931 allowed: true,
31042932 },
31052933 {
31062934 name: "Regex POST with trailing slash allowed",
3107− method: http.MethodPost,
2935+ method: "POST",
31082936 url: "/api/public-entity/",
31092937 allowed: true,
31102938 },
31112939 {
31122940 name: "Regex GET api route denied",
3113− method: http.MethodGet,
2941+ method: "GET",
31142942 url: "/api/users",
31152943 allowed: false,
31162944 },
31172945 {
31182946 name: "Regex POST api route denied",
3119− method: http.MethodPost,
2947+ method: "POST",
31202948 url: "/api/users",
31212949 allowed: false,
31222950 },
@@ -3256,7 +3084,7 @@ func TestProxyAllowedGroups(t *testing.T) {
32563084 t.Fatal(err)
32573085 }
32583086
3259− test.req, _ = http.NewRequest(http.MethodGet, fmt.Sprintf("/%s", tt.querystring), nil)
3087+ test.req, _ = http.NewRequest("GET", fmt.Sprintf("/%s", tt.querystring), nil)
32603088
32613089 test.req.Header.Add("accept", applicationJSON)
32623090 err = test.SaveSession(session)
@@ -3400,7 +3228,7 @@ func TestAuthOnlyAllowedGroupsWithSkipMethods(t *testing.T) {
34003228 {
34013229 name: "UserWithGroupSkipAuthPreflight",
34023230 groups: []string{"a", "c"},
3403− method: http.MethodOptions,
3231+ method: "OPTIONS",
34043232 ip: "1.2.3.5:43670",
34053233 withSession: true,
34063234 expectedStatusCode: http.StatusAccepted,
@@ -3408,7 +3236,7 @@ func TestAuthOnlyAllowedGroupsWithSkipMethods(t *testing.T) {
34083236 {
34093237 name: "UserWithGroupTrustedIp",
34103238 groups: []string{"a", "c"},
3411− method: http.MethodGet,
3239+ method: "GET",
34123240 ip: "1.2.3.4:43670",
34133241 withSession: true,
34143242 expectedStatusCode: http.StatusAccepted,
@@ -3416,7 +3244,7 @@ func TestAuthOnlyAllowedGroupsWithSkipMethods(t *testing.T) {
34163244 {
34173245 name: "UserWithoutGroupSkipAuthPreflight",
34183246 groups: []string{"c"},
3419− method: http.MethodOptions,
3247+ method: "OPTIONS",
34203248 ip: "1.2.3.5:43670",
34213249 withSession: true,
34223250 expectedStatusCode: http.StatusForbidden,
@@ -3424,21 +3252,21 @@ func TestAuthOnlyAllowedGroupsWithSkipMethods(t *testing.T) {
34243252 {
34253253 name: "UserWithoutGroupTrustedIp",
34263254 groups: []string{"c"},
3427− method: http.MethodGet,
3255+ method: "GET",
34283256 ip: "1.2.3.4:43670",
34293257 withSession: true,
34303258 expectedStatusCode: http.StatusForbidden,
34313259 },
34323260 {
34333261 name: "UserWithoutSessionSkipAuthPreflight",
3434− method: http.MethodOptions,
3262+ method: "OPTIONS",
34353263 ip: "1.2.3.5:43670",
34363264 withSession: false,
34373265 expectedStatusCode: http.StatusAccepted,
34383266 },
34393267 {
34403268 name: "UserWithoutSessionTrustedIp",
3441− method: http.MethodGet,
3269+ method: "GET",
34423270 ip: "1.2.3.4:43670",
34433271 withSession: false,
34443272 expectedStatusCode: http.StatusAccepted,
@@ -3538,24 +3366,6 @@ func TestAuthOnlyAllowedEmailDomains(t *testing.T) {
35383366 querystring: "?allowed_email_domains=a.b.c.example.com,*.c.example.com",
35393367 expectedStatusCode: http.StatusAccepted,
35403368 },
3541− {
3542− name: "UserWithMultipleAtSignsExactDomain",
3543− email: "attacker@evil.com@example.com",
3544− querystring: "?allowed_email_domains=example.com",
3545− expectedStatusCode: http.StatusForbidden,
3546− },
3547− {
3548− name: "UserWithMultipleAtSignsWildcardDomain",
3549− email: "attacker@evil.com@foo.example.com",
3550− querystring: "?allowed_email_domains=*.example.com",
3551− expectedStatusCode: http.StatusForbidden,
3552− },
3553− {
3554− name: "UserWithMultipleAtSignsDotPrefixedDomain",
3555− email: "attacker@evil.com@foo.example.com",
3556− querystring: "?allowed_email_domains=.example.com",
3557− expectedStatusCode: http.StatusForbidden,
3558− },
35593369 }
35603370
35613371 for _, tc := range testCases {
@@ -3762,49 +3572,3 @@ func TestGetOAuthRedirectURI(t *testing.T) {
37623572 })
37633573 }
37643574}
3765−
3766−func TestIdTokenPlaceholderInSignOut(t *testing.T) {
3767− opts := baseTestOptions()
3768− opts.WhitelistDomains = []string{"my-oidc-provider.example.com"}
3769−
3770− err := validation.Validate(opts)
3771− assert.NoError(t, err)
3772−
3773− const emailAddress = "john.doe@example.com"
3774− const userName = "9fcab5c9b889a557"
3775− created := time.Now()
3776−
3777− session := &sessions.SessionState{
3778− User: userName,
3779− Groups: []string{"a", "b"},
3780− Email: emailAddress,
3781− IDToken: "eYjjjjjj.vvvv.ddd",
3782− AccessToken: "oauth_token",
3783− CreatedAt: &created,
3784− }
3785−
3786− proxy, err := NewOAuthProxy(opts, func(email string) bool {
3787− return true
3788− })
3789− assert.NoError(t, err)
3790−
3791− // Save the required session
3792− rw := httptest.NewRecorder()
3793− req, _ := http.NewRequest(http.MethodGet, "/", nil)
3794− err = proxy.sessionStore.Save(rw, req, session)
3795− assert.NoError(t, err)
3796−
3797− rw = httptest.NewRecorder()
3798−
3799− rdUrl := url.QueryEscape("https://my-oidc-provider.example.com/sign_out_page?id_token_hint={id_token}&post_logout_redirect_uri=https://my-app.example.com/")
3800− req, _ = http.NewRequest(http.MethodGet, "/oauth2/sign_out?rd="+rdUrl, nil)
3801− req = middlewareapi.AddRequestScope(req, &middlewareapi.RequestScope{
3802− RequestID: "11111111-2222-4333-8444-555555555555",
3803− Session: session,
3804− })
3805−
3806− proxy.SignOut(rw, req)
3807− newLocation := rw.Header().Values("Location")[0]
3808−
3809− assert.Equal(t, "https://my-oidc-provider.example.com/sign_out_page?id_token_hint=eYjjjjjj.vvvv.ddd&post_logout_redirect_uri=https://my-app.example.com/", newLocation)
3810−}