@@ -15,6 +15,7 @@ import (
1515 "time"
1616
1717 "github.com/coreos/go-oidc/v3/oidc"
18+ middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
1819 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
1920 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
2021 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/authentication/hmacauth"
@@ -53,7 +54,7 @@ func TestRobotsTxt(t *testing.T) {
5354 t.Fatal(err)
5455 }
5556 rw := httptest.NewRecorder()
56− req, _ := http.NewRequest("GET", "/robots.txt", nil)
57+ req, _ := http.NewRequest(http.MethodGet, "/robots.txt", nil)
5758 proxy.ServeHTTP(rw, req)
5859 assert.Equal(t, 200, rw.Code)
5960 assert.Equal(t, "User-agent: *\nDisallow: /\n", rw.Body.String())
@@ -240,7 +241,7 @@ func TestBasicAuthPassword(t *testing.T) {
240241
241242 // Save the required session
242243 rw := httptest.NewRecorder()
243− req, _ := http.NewRequest("GET", "/", nil)
244+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
244245 err = proxy.sessionStore.Save(rw, req, &sessions.SessionState{
245246 Email: emailAddress,
246247 })
@@ -249,7 +250,7 @@ func TestBasicAuthPassword(t *testing.T) {
249250 // Extract the cookie value to inject into the test request
250251 cookie := rw.Header().Values("Set-Cookie")[0]
251252
252− req, _ = http.NewRequest("GET", "/", nil)
253+ req, _ = http.NewRequest(http.MethodGet, "/", nil)
253254 req.Header.Set("Cookie", cookie)
254255 rw = httptest.NewRecorder()
255256 proxy.ServeHTTP(rw, req)
@@ -299,14 +300,14 @@ func TestPassGroupsHeadersWithGroups(t *testing.T) {
299300
300301 // Save the required session
301302 rw := httptest.NewRecorder()
302− req, _ := http.NewRequest("GET", "/", nil)
303+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
303304 err = proxy.sessionStore.Save(rw, req, session)
304305 assert.NoError(t, err)
305306
306307 // Extract the cookie value to inject into the test request
307308 cookie := rw.Header().Values("Set-Cookie")[0]
308309
309− req, _ = http.NewRequest("GET", "/", nil)
310+ req, _ = http.NewRequest(http.MethodGet, "/", nil)
310311 req.Header.Set("Cookie", cookie)
311312 rw = httptest.NewRecorder()
312313 proxy.ServeHTTP(rw, req)
@@ -456,7 +457,7 @@ func (patTest *PassAccessTokenTest) getEndpointWithCookie(cookie string, endpoin
456457 return 0, ""
457458 }
458459
459− req, err := http.NewRequest("GET", endpoint, strings.NewReader(""))
460+ req, err := http.NewRequest(http.MethodGet, endpoint, strings.NewReader(""))
460461 if err != nil {
461462 return 0, ""
462463 }
@@ -507,7 +508,7 @@ func TestStaticProxyUpstream(t *testing.T) {
507508 ProxyUpstream: options.Upstream{
508509 ID: "static-proxy",
509510 Path: "/static-proxy",
510− Static: ptr.Ptr(true),
511+ Static: ptr.To(true),
511512 },
512513 })
513514 if err != nil {
@@ -607,7 +608,7 @@ func NewSignInPageTest(skipProvider bool) (*SignInPageTest, error) {
607608
608609func (sipTest *SignInPageTest) GetEndpoint(endpoint string) (int, string) {
609610 rw := httptest.NewRecorder()
610− req, _ := http.NewRequest("GET", endpoint, strings.NewReader(""))
611+ req, _ := http.NewRequest(http.MethodGet, endpoint, strings.NewReader(""))
611612 sipTest.proxy.ServeHTTP(rw, req)
612613 return rw.Code, rw.Body.String()
613614}
@@ -712,6 +713,50 @@ func TestManualSignInCorrectCredentials(t *testing.T) {
712713 assert.Equal(t, http.StatusFound, statusCode)
713714}
714715
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+
715760func TestSignInPageIncludesTargetRedirect(t *testing.T) {
716761 sipTest, err := NewSignInPageTest(false)
717762 if err != nil {
@@ -849,7 +894,7 @@ func NewProcessCookieTest(opts ProcessCookieTestOpts, modifiers ...OptionsModifi
849894 // access_token validation.
850895 pcTest.proxy.CookieOptions.Refresh = time.Duration(0)
851896 pcTest.rw = httptest.NewRecorder()
852− pcTest.req, _ = http.NewRequest("GET", "/", strings.NewReader(""))
897+ pcTest.req, _ = http.NewRequest(http.MethodGet, "/", strings.NewReader(""))
853898 pcTest.validateUser = true
854899 return &pcTest, nil
855900}
@@ -982,7 +1027,7 @@ func NewUserInfoEndpointTest() (*ProcessCookieTest, error) {
9821027 if err != nil {
9831028 return nil, err
9841029 }
985− pcTest.req, _ = http.NewRequest("GET",
1030+ pcTest.req, _ = http.NewRequest(http.MethodGet,
9861031 pcTest.opts.ProxyPrefix+"/userinfo", nil)
9871032 return pcTest, nil
9881033}
@@ -1032,6 +1077,20 @@ func TestUserInfoEndpointAccepted(t *testing.T) {
10321077 },
10331078 expectedResponse: "{\"user\":\"john.doe\",\"email\":\"john.doe@example.com\",\"groups\":[\"example\",\"groups\"],\"preferredUsername\":\"john\"}\n",
10341079 },
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+ },
10351094 }
10361095
10371096 for _, tc := range testCases {
@@ -1076,7 +1135,7 @@ func NewAuthOnlyEndpointTest(querystring string, modifiers ...OptionsModifier) (
10761135 return nil, err
10771136 }
10781137 pcTest.req, _ = http.NewRequest(
1079− "GET",
1138+ http.MethodGet,
10801139 fmt.Sprintf("%s/auth%s", pcTest.opts.ProxyPrefix, querystring),
10811140 nil)
10821141 return pcTest, nil
@@ -1215,7 +1274,7 @@ func TestAuthOnlyEndpointSetXAuthRequestHeaders(t *testing.T) {
12151274 pcTest.validateUser = true
12161275
12171276 pcTest.rw = httptest.NewRecorder()
1218− pcTest.req, _ = http.NewRequest("GET",
1277+ pcTest.req, _ = http.NewRequest(http.MethodGet,
12191278 pcTest.opts.ProxyPrefix+authOnlyPath, nil)
12201279
12211280 created := time.Now()
@@ -1308,7 +1367,7 @@ func TestAuthOnlyEndpointSetBasicAuthTrueRequestHeaders(t *testing.T) {
13081367 pcTest.validateUser = true
13091368
13101369 pcTest.rw = httptest.NewRecorder()
1311− pcTest.req, _ = http.NewRequest("GET",
1370+ pcTest.req, _ = http.NewRequest(http.MethodGet,
13121371 pcTest.opts.ProxyPrefix+authOnlyPath, nil)
13131372
13141373 created := time.Now()
@@ -1388,7 +1447,7 @@ func TestAuthOnlyEndpointSetBasicAuthFalseRequestHeaders(t *testing.T) {
13881447 pcTest.validateUser = true
13891448
13901449 pcTest.rw = httptest.NewRecorder()
1391− pcTest.req, _ = http.NewRequest("GET",
1450+ pcTest.req, _ = http.NewRequest(http.MethodGet,
13921451 pcTest.opts.ProxyPrefix+authOnlyPath, nil)
13931452
13941453 created := time.Now()
@@ -1436,7 +1495,7 @@ func TestAuthSkippedForPreflightRequests(t *testing.T) {
14361495 }
14371496 proxy.provider = NewTestProvider(upstreamURL, "")
14381497 rw := httptest.NewRecorder()
1439− req, _ := http.NewRequest("OPTIONS", "/preflight-request", nil)
1498+ req, _ := http.NewRequest(http.MethodOptions, "/preflight-request", nil)
14401499 proxy.ServeHTTP(rw, req)
14411500
14421501 assert.Equal(t, 200, rw.Code)
@@ -1593,19 +1652,19 @@ func TestRequestSignature(t *testing.T) {
15931652 resp string
15941653 }{
15951654 "No request signature": {
1596− method: "GET",
1655+ method: http.MethodGet,
15971656 body: "",
15981657 key: "",
15991658 resp: "no signature received",
16001659 },
16011660 "Get request": {
1602− method: "GET",
1661+ method: http.MethodGet,
16031662 body: "",
16041663 key: "7d9e1aa87a5954e6f9fc59266b3af9d7c35fda2d",
16051664 resp: "signatures match",
16061665 },
16071666 "Post request": {
1608− method: "POST",
1667+ method: http.MethodPost,
16091668 body: `{ "hello": "world!" }`,
16101669 key: "d90df39e2d19282840252612dd7c81421a372f61",
16111670 resp: "signatures match",
@@ -2028,6 +2087,44 @@ func Test_noCacheHeaders(t *testing.T) {
20282087 })
20292088}
20302089
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+
20312128func baseTestOptions() *options.Options {
20322129 opts := options.NewOptions()
20332130 opts.Cookie.Secret = rawCookieSecret
@@ -2092,7 +2189,33 @@ func TestTrustedIPs(t *testing.T) {
20922189 reverseProxy: false,
20932190 realClientIPHeader: "X-Real-IP", // Default value
20942191 req: func() *http.Request {
2095− req, _ := http.NewRequest("GET", "/", nil)
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 = "@"
20962219 return req
20972220 }(),
20982221 expectTrusted: false,
@@ -2104,7 +2227,7 @@ func TestTrustedIPs(t *testing.T) {
21042227 reverseProxy: false,
21052228 realClientIPHeader: "X-Real-IP", // Default value
21062229 req: func() *http.Request {
2107− req, _ := http.NewRequest("GET", "/", nil)
2230+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
21082231 req.RemoteAddr = "127.0.0.1:43670"
21092232 return req
21102233 }(),
@@ -2117,7 +2240,7 @@ func TestTrustedIPs(t *testing.T) {
21172240 reverseProxy: true,
21182241 realClientIPHeader: "X-Real-IP", // Default value
21192242 req: func() *http.Request {
2120− req, _ := http.NewRequest("GET", "/", nil)
2243+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
21212244 req.RemoteAddr = "127.0.0.1:44324"
21222245 return req
21232246 }(),
@@ -2130,7 +2253,7 @@ func TestTrustedIPs(t *testing.T) {
21302253 reverseProxy: true,
21312254 realClientIPHeader: "X-Forwarded-For",
21322255 req: func() *http.Request {
2133− req, _ := http.NewRequest("GET", "/", nil)
2256+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
21342257 req.Header.Add("X-Forwarded-For", "127.0.0.1")
21352258 return req
21362259 }(),
@@ -2143,7 +2266,7 @@ func TestTrustedIPs(t *testing.T) {
21432266 reverseProxy: true,
21442267 realClientIPHeader: "X-Forwarded-For",
21452268 req: func() *http.Request {
2146− req, _ := http.NewRequest("GET", "/", nil)
2269+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
21472270 req.Header.Add("X-Forwarded-For", "::1")
21482271 return req
21492272 }(),
@@ -2156,7 +2279,7 @@ func TestTrustedIPs(t *testing.T) {
21562279 reverseProxy: true,
21572280 realClientIPHeader: "X-Forwarded-For",
21582281 req: func() *http.Request {
2159− req, _ := http.NewRequest("GET", "/", nil)
2282+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
21602283 req.Header.Add("X-Forwarded-For", "12.34.56.78")
21612284 return req
21622285 }(),
@@ -2169,7 +2292,7 @@ func TestTrustedIPs(t *testing.T) {
21692292 reverseProxy: true,
21702293 realClientIPHeader: "X-Forwarded-For",
21712294 req: func() *http.Request {
2172− req, _ := http.NewRequest("GET", "/", nil)
2295+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
21732296 req.Header.Add("X-Forwarded-For", "::2")
21742297 return req
21752298 }(),
@@ -2182,7 +2305,7 @@ func TestTrustedIPs(t *testing.T) {
21822305 reverseProxy: true,
21832306 realClientIPHeader: "X-Forwarded-For",
21842307 req: func() *http.Request {
2185− req, _ := http.NewRequest("GET", "/", nil)
2308+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
21862309 req.Header.Add("X-Real-IP", "::1")
21872310 return req
21882311 }(),
@@ -2195,7 +2318,7 @@ func TestTrustedIPs(t *testing.T) {
21952318 reverseProxy: true,
21962319 realClientIPHeader: "X-Forwarded-For",
21972320 req: func() *http.Request {
2198− req, _ := http.NewRequest("GET", "/", nil)
2321+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
21992322 req.Header.Add("X-Forwarded-For", "adsfljk29242as!!")
22002323 return req
22012324 }(),
@@ -2208,7 +2331,7 @@ func TestTrustedIPs(t *testing.T) {
22082331 reverseProxy: false,
22092332 realClientIPHeader: "X-Real-IP",
22102333 req: func() *http.Request {
2211− req, _ := http.NewRequest("GET", "/", nil)
2334+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
22122335 req.RemoteAddr = "adsfljk29242as!!"
22132336 return req
22142337 }(),
@@ -2224,7 +2347,7 @@ func TestTrustedIPs(t *testing.T) {
22242347 {
22252348 ID: "static",
22262349 Path: "/",
2227− Static: ptr.Ptr(true),
2350+ Static: ptr.To(true),
22282351 },
22292352 },
22302353 }
@@ -2304,12 +2427,12 @@ func Test_buildRoutesAllowlist(t *testing.T) {
23042427 },
23052428 expectedRoutes: []expectedAllowedRoute{
23062429 {
2307− method: "GET",
2430+ method: http.MethodGet,
23082431 negate: false,
23092432 regexString: "^/foo/bar",
23102433 },
23112434 {
2312− method: "POST",
2435+ method: http.MethodPost,
23132436 negate: false,
23142437 regexString: "^/baz/[0-9]+/thing",
23152438 },
@@ -2362,11 +2485,11 @@ func Test_buildRoutesAllowlist(t *testing.T) {
23622485 regexString: "^/baz/[0-9]+/thing/regex",
23632486 },
23642487 {
2365− method: "GET",
2488+ method: http.MethodGet,
23662489 regexString: "^/foo/bar",
23672490 },
23682491 {
2369− method: "POST",
2492+ method: http.MethodPost,
23702493 regexString: "^/baz/[0-9]+/thing",
23712494 },
23722495 {
@@ -2518,7 +2641,7 @@ func TestApiRoutes(t *testing.T) {
25182641
25192642 for _, tc := range testCases {
25202643 t.Run(tc.name, func(t *testing.T) {
2521− req, err := http.NewRequest("GET", tc.url, nil)
2644+ req, err := http.NewRequest(http.MethodGet, tc.url, nil)
25222645 req.Header.Set("Accept", tc.contentType)
25232646 assert.NoError(t, err)
25242647
@@ -2556,9 +2679,11 @@ func TestAllowedRequest(t *testing.T) {
25562679 }
25572680 opts.SkipAuthRegex = []string{
25582681 "^/skip/auth/regex$",
2682+ "^/public/.*/endpoint$",
25592683 }
25602684 opts.SkipAuthRoutes = []string{
25612685 "GET=^/skip/auth/routes/get",
2686+ "^/foo/.*/bar$",
25622687 }
25632688 err := validation.Validate(opts)
25642689 assert.NoError(t, err)
@@ -2575,25 +2700,37 @@ func TestAllowedRequest(t *testing.T) {
25752700 }{
25762701 {
25772702 name: "Regex GET allowed",
2578− method: "GET",
2703+ method: http.MethodGet,
25792704 url: "/skip/auth/regex",
25802705 allowed: true,
25812706 },
25822707 {
25832708 name: "Regex POST allowed ",
2584− method: "POST",
2709+ method: http.MethodPost,
25852710 url: "/skip/auth/regex",
25862711 allowed: true,
25872712 },
25882713 {
25892714 name: "Regex denied",
2590− method: "GET",
2715+ method: http.MethodGet,
25912716 url: "/wrong/denied",
25922717 allowed: false,
25932718 },
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+ },
25942731 {
25952732 name: "Route allowed",
2596− method: "GET",
2733+ method: http.MethodGet,
25972734 url: "/skip/auth/routes/get",
25982735 allowed: true,
25992736 },
@@ -2605,16 +2742,28 @@ func TestAllowedRequest(t *testing.T) {
26052742 },
26062743 {
26072744 name: "Route denied with wrong path",
2608− method: "GET",
2745+ method: http.MethodGet,
26092746 url: "/skip/auth/routes/wrong/path",
26102747 allowed: false,
26112748 },
26122749 {
26132750 name: "Route denied with wrong path and method",
2614− method: "POST",
2751+ method: http.MethodPost,
26152752 url: "/skip/auth/routes/wrong/path",
26162753 allowed: false,
26172754 },
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+ },
26182767 }
26192768
26202769 for _, tc := range testCases {
@@ -2655,9 +2804,11 @@ func TestAllowedRequestWithForwardedUriHeader(t *testing.T) {
26552804 }
26562805 opts.SkipAuthRegex = []string{
26572806 "^/skip/auth/regex$",
2807+ "^/public/.*/endpoint$",
26582808 }
26592809 opts.SkipAuthRoutes = []string{
26602810 "GET=^/skip/auth/routes/get",
2811+ "^/foo/.*/bar$",
26612812 }
26622813 err := validation.Validate(opts)
26632814 assert.NoError(t, err)
@@ -2674,25 +2825,37 @@ func TestAllowedRequestWithForwardedUriHeader(t *testing.T) {
26742825 }{
26752826 {
26762827 name: "Regex GET allowed",
2677− method: "GET",
2828+ method: http.MethodGet,
26782829 url: "/skip/auth/regex",
26792830 allowed: true,
26802831 },
26812832 {
26822833 name: "Regex POST allowed ",
2683− method: "POST",
2834+ method: http.MethodPost,
26842835 url: "/skip/auth/regex",
26852836 allowed: true,
26862837 },
26872838 {
26882839 name: "Regex denied",
2689− method: "GET",
2840+ method: http.MethodGet,
26902841 url: "/wrong/denied",
26912842 allowed: false,
26922843 },
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+ },
26932856 {
26942857 name: "Route allowed",
2695− method: "GET",
2858+ method: http.MethodGet,
26962859 url: "/skip/auth/routes/get",
26972860 allowed: true,
26982861 },
@@ -2704,22 +2867,35 @@ func TestAllowedRequestWithForwardedUriHeader(t *testing.T) {
27042867 },
27052868 {
27062869 name: "Route denied with wrong path",
2707− method: "GET",
2870+ method: http.MethodGet,
27082871 url: "/skip/auth/routes/wrong/path",
27092872 allowed: false,
27102873 },
27112874 {
27122875 name: "Route denied with wrong path and method",
2713− method: "POST",
2876+ method: http.MethodPost,
27142877 url: "/skip/auth/routes/wrong/path",
27152878 allowed: false,
27162879 },
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+ },
27172892 }
27182893
27192894 for _, tc := range testCases {
27202895 t.Run(tc.name, func(t *testing.T) {
27212896 req, err := http.NewRequest(tc.method, opts.ProxyPrefix+authOnlyPath, nil)
27222897 req.Header.Set("X-Forwarded-Uri", tc.url)
2898+ req.RemoteAddr = "127.0.0.1:4180"
27232899 assert.NoError(t, err)
27242900
27252901 rw := httptest.NewRecorder()
@@ -2734,6 +2910,43 @@ func TestAllowedRequestWithForwardedUriHeader(t *testing.T) {
27342910 }
27352911}
27362912
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+
27372950func TestAllowedRequestNegateWithoutMethod(t *testing.T) {
27382951 upstreamServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
27392952 w.WriteHeader(200)
@@ -2773,37 +2986,37 @@ func TestAllowedRequestNegateWithoutMethod(t *testing.T) {
27732986 }{
27742987 {
27752988 name: "Some static file allowed",
2776− method: "GET",
2989+ method: http.MethodGet,
27772990 url: "/static/file.txt",
27782991 allowed: true,
27792992 },
27802993 {
27812994 name: "POST to contact form allowed",
2782− method: "POST",
2995+ method: http.MethodPost,
27832996 url: "/contact",
27842997 allowed: true,
27852998 },
27862999 {
27873000 name: "Regex POST allowed",
2788− method: "POST",
3001+ method: http.MethodPost,
27893002 url: "/api/public-entity",
27903003 allowed: true,
27913004 },
27923005 {
27933006 name: "Regex POST with trailing slash allowed",
2794− method: "POST",
3007+ method: http.MethodPost,
27953008 url: "/api/public-entity/",
27963009 allowed: true,
27973010 },
27983011 {
27993012 name: "Regex GET api route denied",
2800− method: "GET",
3013+ method: http.MethodGet,
28013014 url: "/api/users",
28023015 allowed: false,
28033016 },
28043017 {
28053018 name: "Regex POST api route denied",
2806− method: "POST",
3019+ method: http.MethodPost,
28073020 url: "/api/users",
28083021 allowed: false,
28093022 },
@@ -2873,37 +3086,37 @@ func TestAllowedRequestNegateWithMethod(t *testing.T) {
28733086 }{
28743087 {
28753088 name: "Some static file allowed",
2876− method: "GET",
3089+ method: http.MethodGet,
28773090 url: "/static/file.txt",
28783091 allowed: true,
28793092 },
28803093 {
28813094 name: "POST to contact form not allowed",
2882− method: "POST",
3095+ method: http.MethodPost,
28833096 url: "/contact",
28843097 allowed: false,
28853098 },
28863099 {
28873100 name: "Regex POST allowed",
2888− method: "POST",
3101+ method: http.MethodPost,
28893102 url: "/api/public-entity",
28903103 allowed: true,
28913104 },
28923105 {
28933106 name: "Regex POST with trailing slash allowed",
2894− method: "POST",
3107+ method: http.MethodPost,
28953108 url: "/api/public-entity/",
28963109 allowed: true,
28973110 },
28983111 {
28993112 name: "Regex GET api route denied",
2900− method: "GET",
3113+ method: http.MethodGet,
29013114 url: "/api/users",
29023115 allowed: false,
29033116 },
29043117 {
29053118 name: "Regex POST api route denied",
2906− method: "POST",
3119+ method: http.MethodPost,
29073120 url: "/api/users",
29083121 allowed: false,
29093122 },
@@ -3043,7 +3256,7 @@ func TestProxyAllowedGroups(t *testing.T) {
30433256 t.Fatal(err)
30443257 }
30453258
3046− test.req, _ = http.NewRequest("GET", fmt.Sprintf("/%s", tt.querystring), nil)
3259+ test.req, _ = http.NewRequest(http.MethodGet, fmt.Sprintf("/%s", tt.querystring), nil)
30473260
30483261 test.req.Header.Add("accept", applicationJSON)
30493262 err = test.SaveSession(session)
@@ -3187,7 +3400,7 @@ func TestAuthOnlyAllowedGroupsWithSkipMethods(t *testing.T) {
31873400 {
31883401 name: "UserWithGroupSkipAuthPreflight",
31893402 groups: []string{"a", "c"},
3190− method: "OPTIONS",
3403+ method: http.MethodOptions,
31913404 ip: "1.2.3.5:43670",
31923405 withSession: true,
31933406 expectedStatusCode: http.StatusAccepted,
@@ -3195,7 +3408,7 @@ func TestAuthOnlyAllowedGroupsWithSkipMethods(t *testing.T) {
31953408 {
31963409 name: "UserWithGroupTrustedIp",
31973410 groups: []string{"a", "c"},
3198− method: "GET",
3411+ method: http.MethodGet,
31993412 ip: "1.2.3.4:43670",
32003413 withSession: true,
32013414 expectedStatusCode: http.StatusAccepted,
@@ -3203,7 +3416,7 @@ func TestAuthOnlyAllowedGroupsWithSkipMethods(t *testing.T) {
32033416 {
32043417 name: "UserWithoutGroupSkipAuthPreflight",
32053418 groups: []string{"c"},
3206− method: "OPTIONS",
3419+ method: http.MethodOptions,
32073420 ip: "1.2.3.5:43670",
32083421 withSession: true,
32093422 expectedStatusCode: http.StatusForbidden,
@@ -3211,21 +3424,21 @@ func TestAuthOnlyAllowedGroupsWithSkipMethods(t *testing.T) {
32113424 {
32123425 name: "UserWithoutGroupTrustedIp",
32133426 groups: []string{"c"},
3214− method: "GET",
3427+ method: http.MethodGet,
32153428 ip: "1.2.3.4:43670",
32163429 withSession: true,
32173430 expectedStatusCode: http.StatusForbidden,
32183431 },
32193432 {
32203433 name: "UserWithoutSessionSkipAuthPreflight",
3221− method: "OPTIONS",
3434+ method: http.MethodOptions,
32223435 ip: "1.2.3.5:43670",
32233436 withSession: false,
32243437 expectedStatusCode: http.StatusAccepted,
32253438 },
32263439 {
32273440 name: "UserWithoutSessionTrustedIp",
3228− method: "GET",
3441+ method: http.MethodGet,
32293442 ip: "1.2.3.4:43670",
32303443 withSession: false,
32313444 expectedStatusCode: http.StatusAccepted,
@@ -3325,6 +3538,24 @@ func TestAuthOnlyAllowedEmailDomains(t *testing.T) {
33253538 querystring: "?allowed_email_domains=a.b.c.example.com,*.c.example.com",
33263539 expectedStatusCode: http.StatusAccepted,
33273540 },
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+ },
33283559 }
33293560
33303561 for _, tc := range testCases {
@@ -3531,3 +3762,49 @@ func TestGetOAuthRedirectURI(t *testing.T) {
35313762 })
35323763 }
35333764}
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+}