@@ -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 }
@@ -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",
@@ -2130,7 +2189,33 @@ func TestTrustedIPs(t *testing.T) {
21302189 reverseProxy: false,
21312190 realClientIPHeader: "X-Real-IP", // Default value
21322191 req: func() *http.Request {
2133− 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 = "@"
21342219 return req
21352220 }(),
21362221 expectTrusted: false,
@@ -2142,7 +2227,7 @@ func TestTrustedIPs(t *testing.T) {
21422227 reverseProxy: false,
21432228 realClientIPHeader: "X-Real-IP", // Default value
21442229 req: func() *http.Request {
2145− req, _ := http.NewRequest("GET", "/", nil)
2230+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
21462231 req.RemoteAddr = "127.0.0.1:43670"
21472232 return req
21482233 }(),
@@ -2155,7 +2240,7 @@ func TestTrustedIPs(t *testing.T) {
21552240 reverseProxy: true,
21562241 realClientIPHeader: "X-Real-IP", // Default value
21572242 req: func() *http.Request {
2158− req, _ := http.NewRequest("GET", "/", nil)
2243+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
21592244 req.RemoteAddr = "127.0.0.1:44324"
21602245 return req
21612246 }(),
@@ -2168,7 +2253,7 @@ func TestTrustedIPs(t *testing.T) {
21682253 reverseProxy: true,
21692254 realClientIPHeader: "X-Forwarded-For",
21702255 req: func() *http.Request {
2171− req, _ := http.NewRequest("GET", "/", nil)
2256+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
21722257 req.Header.Add("X-Forwarded-For", "127.0.0.1")
21732258 return req
21742259 }(),
@@ -2181,7 +2266,7 @@ func TestTrustedIPs(t *testing.T) {
21812266 reverseProxy: true,
21822267 realClientIPHeader: "X-Forwarded-For",
21832268 req: func() *http.Request {
2184− req, _ := http.NewRequest("GET", "/", nil)
2269+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
21852270 req.Header.Add("X-Forwarded-For", "::1")
21862271 return req
21872272 }(),
@@ -2194,7 +2279,7 @@ func TestTrustedIPs(t *testing.T) {
21942279 reverseProxy: true,
21952280 realClientIPHeader: "X-Forwarded-For",
21962281 req: func() *http.Request {
2197− req, _ := http.NewRequest("GET", "/", nil)
2282+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
21982283 req.Header.Add("X-Forwarded-For", "12.34.56.78")
21992284 return req
22002285 }(),
@@ -2207,7 +2292,7 @@ func TestTrustedIPs(t *testing.T) {
22072292 reverseProxy: true,
22082293 realClientIPHeader: "X-Forwarded-For",
22092294 req: func() *http.Request {
2210− req, _ := http.NewRequest("GET", "/", nil)
2295+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
22112296 req.Header.Add("X-Forwarded-For", "::2")
22122297 return req
22132298 }(),
@@ -2220,7 +2305,7 @@ func TestTrustedIPs(t *testing.T) {
22202305 reverseProxy: true,
22212306 realClientIPHeader: "X-Forwarded-For",
22222307 req: func() *http.Request {
2223− req, _ := http.NewRequest("GET", "/", nil)
2308+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
22242309 req.Header.Add("X-Real-IP", "::1")
22252310 return req
22262311 }(),
@@ -2233,7 +2318,7 @@ func TestTrustedIPs(t *testing.T) {
22332318 reverseProxy: true,
22342319 realClientIPHeader: "X-Forwarded-For",
22352320 req: func() *http.Request {
2236− req, _ := http.NewRequest("GET", "/", nil)
2321+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
22372322 req.Header.Add("X-Forwarded-For", "adsfljk29242as!!")
22382323 return req
22392324 }(),
@@ -2246,7 +2331,7 @@ func TestTrustedIPs(t *testing.T) {
22462331 reverseProxy: false,
22472332 realClientIPHeader: "X-Real-IP",
22482333 req: func() *http.Request {
2249− req, _ := http.NewRequest("GET", "/", nil)
2334+ req, _ := http.NewRequest(http.MethodGet, "/", nil)
22502335 req.RemoteAddr = "adsfljk29242as!!"
22512336 return req
22522337 }(),
@@ -2342,12 +2427,12 @@ func Test_buildRoutesAllowlist(t *testing.T) {
23422427 },
23432428 expectedRoutes: []expectedAllowedRoute{
23442429 {
2345− method: "GET",
2430+ method: http.MethodGet,
23462431 negate: false,
23472432 regexString: "^/foo/bar",
23482433 },
23492434 {
2350− method: "POST",
2435+ method: http.MethodPost,
23512436 negate: false,
23522437 regexString: "^/baz/[0-9]+/thing",
23532438 },
@@ -2400,11 +2485,11 @@ func Test_buildRoutesAllowlist(t *testing.T) {
24002485 regexString: "^/baz/[0-9]+/thing/regex",
24012486 },
24022487 {
2403− method: "GET",
2488+ method: http.MethodGet,
24042489 regexString: "^/foo/bar",
24052490 },
24062491 {
2407− method: "POST",
2492+ method: http.MethodPost,
24082493 regexString: "^/baz/[0-9]+/thing",
24092494 },
24102495 {
@@ -2556,7 +2641,7 @@ func TestApiRoutes(t *testing.T) {
25562641
25572642 for _, tc := range testCases {
25582643 t.Run(tc.name, func(t *testing.T) {
2559− req, err := http.NewRequest("GET", tc.url, nil)
2644+ req, err := http.NewRequest(http.MethodGet, tc.url, nil)
25602645 req.Header.Set("Accept", tc.contentType)
25612646 assert.NoError(t, err)
25622647
@@ -2594,9 +2679,11 @@ func TestAllowedRequest(t *testing.T) {
25942679 }
25952680 opts.SkipAuthRegex = []string{
25962681 "^/skip/auth/regex$",
2682+ "^/public/.*/endpoint$",
25972683 }
25982684 opts.SkipAuthRoutes = []string{
25992685 "GET=^/skip/auth/routes/get",
2686+ "^/foo/.*/bar$",
26002687 }
26012688 err := validation.Validate(opts)
26022689 assert.NoError(t, err)
@@ -2613,25 +2700,37 @@ func TestAllowedRequest(t *testing.T) {
26132700 }{
26142701 {
26152702 name: "Regex GET allowed",
2616− method: "GET",
2703+ method: http.MethodGet,
26172704 url: "/skip/auth/regex",
26182705 allowed: true,
26192706 },
26202707 {
26212708 name: "Regex POST allowed ",
2622− method: "POST",
2709+ method: http.MethodPost,
26232710 url: "/skip/auth/regex",
26242711 allowed: true,
26252712 },
26262713 {
26272714 name: "Regex denied",
2628− method: "GET",
2715+ method: http.MethodGet,
26292716 url: "/wrong/denied",
26302717 allowed: false,
26312718 },
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+ },
26322731 {
26332732 name: "Route allowed",
2634− method: "GET",
2733+ method: http.MethodGet,
26352734 url: "/skip/auth/routes/get",
26362735 allowed: true,
26372736 },
@@ -2643,16 +2742,28 @@ func TestAllowedRequest(t *testing.T) {
26432742 },
26442743 {
26452744 name: "Route denied with wrong path",
2646− method: "GET",
2745+ method: http.MethodGet,
26472746 url: "/skip/auth/routes/wrong/path",
26482747 allowed: false,
26492748 },
26502749 {
26512750 name: "Route denied with wrong path and method",
2652− method: "POST",
2751+ method: http.MethodPost,
26532752 url: "/skip/auth/routes/wrong/path",
26542753 allowed: false,
26552754 },
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+ },
26562767 }
26572768
26582769 for _, tc := range testCases {
@@ -2693,9 +2804,11 @@ func TestAllowedRequestWithForwardedUriHeader(t *testing.T) {
26932804 }
26942805 opts.SkipAuthRegex = []string{
26952806 "^/skip/auth/regex$",
2807+ "^/public/.*/endpoint$",
26962808 }
26972809 opts.SkipAuthRoutes = []string{
26982810 "GET=^/skip/auth/routes/get",
2811+ "^/foo/.*/bar$",
26992812 }
27002813 err := validation.Validate(opts)
27012814 assert.NoError(t, err)
@@ -2712,25 +2825,37 @@ func TestAllowedRequestWithForwardedUriHeader(t *testing.T) {
27122825 }{
27132826 {
27142827 name: "Regex GET allowed",
2715− method: "GET",
2828+ method: http.MethodGet,
27162829 url: "/skip/auth/regex",
27172830 allowed: true,
27182831 },
27192832 {
27202833 name: "Regex POST allowed ",
2721− method: "POST",
2834+ method: http.MethodPost,
27222835 url: "/skip/auth/regex",
27232836 allowed: true,
27242837 },
27252838 {
27262839 name: "Regex denied",
2727− method: "GET",
2840+ method: http.MethodGet,
27282841 url: "/wrong/denied",
27292842 allowed: false,
27302843 },
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+ },
27312856 {
27322857 name: "Route allowed",
2733− method: "GET",
2858+ method: http.MethodGet,
27342859 url: "/skip/auth/routes/get",
27352860 allowed: true,
27362861 },
@@ -2742,22 +2867,35 @@ func TestAllowedRequestWithForwardedUriHeader(t *testing.T) {
27422867 },
27432868 {
27442869 name: "Route denied with wrong path",
2745− method: "GET",
2870+ method: http.MethodGet,
27462871 url: "/skip/auth/routes/wrong/path",
27472872 allowed: false,
27482873 },
27492874 {
27502875 name: "Route denied with wrong path and method",
2751− method: "POST",
2876+ method: http.MethodPost,
27522877 url: "/skip/auth/routes/wrong/path",
27532878 allowed: false,
27542879 },
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+ },
27552892 }
27562893
27572894 for _, tc := range testCases {
27582895 t.Run(tc.name, func(t *testing.T) {
27592896 req, err := http.NewRequest(tc.method, opts.ProxyPrefix+authOnlyPath, nil)
27602897 req.Header.Set("X-Forwarded-Uri", tc.url)
2898+ req.RemoteAddr = "127.0.0.1:4180"
27612899 assert.NoError(t, err)
27622900
27632901 rw := httptest.NewRecorder()
@@ -2772,6 +2910,43 @@ func TestAllowedRequestWithForwardedUriHeader(t *testing.T) {
27722910 }
27732911}
27742912
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+
27752950func TestAllowedRequestNegateWithoutMethod(t *testing.T) {
27762951 upstreamServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
27772952 w.WriteHeader(200)
@@ -2811,37 +2986,37 @@ func TestAllowedRequestNegateWithoutMethod(t *testing.T) {
28112986 }{
28122987 {
28132988 name: "Some static file allowed",
2814− method: "GET",
2989+ method: http.MethodGet,
28152990 url: "/static/file.txt",
28162991 allowed: true,
28172992 },
28182993 {
28192994 name: "POST to contact form allowed",
2820− method: "POST",
2995+ method: http.MethodPost,
28212996 url: "/contact",
28222997 allowed: true,
28232998 },
28242999 {
28253000 name: "Regex POST allowed",
2826− method: "POST",
3001+ method: http.MethodPost,
28273002 url: "/api/public-entity",
28283003 allowed: true,
28293004 },
28303005 {
28313006 name: "Regex POST with trailing slash allowed",
2832− method: "POST",
3007+ method: http.MethodPost,
28333008 url: "/api/public-entity/",
28343009 allowed: true,
28353010 },
28363011 {
28373012 name: "Regex GET api route denied",
2838− method: "GET",
3013+ method: http.MethodGet,
28393014 url: "/api/users",
28403015 allowed: false,
28413016 },
28423017 {
28433018 name: "Regex POST api route denied",
2844− method: "POST",
3019+ method: http.MethodPost,
28453020 url: "/api/users",
28463021 allowed: false,
28473022 },
@@ -2911,37 +3086,37 @@ func TestAllowedRequestNegateWithMethod(t *testing.T) {
29113086 }{
29123087 {
29133088 name: "Some static file allowed",
2914− method: "GET",
3089+ method: http.MethodGet,
29153090 url: "/static/file.txt",
29163091 allowed: true,
29173092 },
29183093 {
29193094 name: "POST to contact form not allowed",
2920− method: "POST",
3095+ method: http.MethodPost,
29213096 url: "/contact",
29223097 allowed: false,
29233098 },
29243099 {
29253100 name: "Regex POST allowed",
2926− method: "POST",
3101+ method: http.MethodPost,
29273102 url: "/api/public-entity",
29283103 allowed: true,
29293104 },
29303105 {
29313106 name: "Regex POST with trailing slash allowed",
2932− method: "POST",
3107+ method: http.MethodPost,
29333108 url: "/api/public-entity/",
29343109 allowed: true,
29353110 },
29363111 {
29373112 name: "Regex GET api route denied",
2938− method: "GET",
3113+ method: http.MethodGet,
29393114 url: "/api/users",
29403115 allowed: false,
29413116 },
29423117 {
29433118 name: "Regex POST api route denied",
2944− method: "POST",
3119+ method: http.MethodPost,
29453120 url: "/api/users",
29463121 allowed: false,
29473122 },
@@ -3081,7 +3256,7 @@ func TestProxyAllowedGroups(t *testing.T) {
30813256 t.Fatal(err)
30823257 }
30833258
3084− test.req, _ = http.NewRequest("GET", fmt.Sprintf("/%s", tt.querystring), nil)
3259+ test.req, _ = http.NewRequest(http.MethodGet, fmt.Sprintf("/%s", tt.querystring), nil)
30853260
30863261 test.req.Header.Add("accept", applicationJSON)
30873262 err = test.SaveSession(session)
@@ -3225,7 +3400,7 @@ func TestAuthOnlyAllowedGroupsWithSkipMethods(t *testing.T) {
32253400 {
32263401 name: "UserWithGroupSkipAuthPreflight",
32273402 groups: []string{"a", "c"},
3228− method: "OPTIONS",
3403+ method: http.MethodOptions,
32293404 ip: "1.2.3.5:43670",
32303405 withSession: true,
32313406 expectedStatusCode: http.StatusAccepted,
@@ -3233,7 +3408,7 @@ func TestAuthOnlyAllowedGroupsWithSkipMethods(t *testing.T) {
32333408 {
32343409 name: "UserWithGroupTrustedIp",
32353410 groups: []string{"a", "c"},
3236− method: "GET",
3411+ method: http.MethodGet,
32373412 ip: "1.2.3.4:43670",
32383413 withSession: true,
32393414 expectedStatusCode: http.StatusAccepted,
@@ -3241,7 +3416,7 @@ func TestAuthOnlyAllowedGroupsWithSkipMethods(t *testing.T) {
32413416 {
32423417 name: "UserWithoutGroupSkipAuthPreflight",
32433418 groups: []string{"c"},
3244− method: "OPTIONS",
3419+ method: http.MethodOptions,
32453420 ip: "1.2.3.5:43670",
32463421 withSession: true,
32473422 expectedStatusCode: http.StatusForbidden,
@@ -3249,21 +3424,21 @@ func TestAuthOnlyAllowedGroupsWithSkipMethods(t *testing.T) {
32493424 {
32503425 name: "UserWithoutGroupTrustedIp",
32513426 groups: []string{"c"},
3252− method: "GET",
3427+ method: http.MethodGet,
32533428 ip: "1.2.3.4:43670",
32543429 withSession: true,
32553430 expectedStatusCode: http.StatusForbidden,
32563431 },
32573432 {
32583433 name: "UserWithoutSessionSkipAuthPreflight",
3259− method: "OPTIONS",
3434+ method: http.MethodOptions,
32603435 ip: "1.2.3.5:43670",
32613436 withSession: false,
32623437 expectedStatusCode: http.StatusAccepted,
32633438 },
32643439 {
32653440 name: "UserWithoutSessionTrustedIp",
3266− method: "GET",
3441+ method: http.MethodGet,
32673442 ip: "1.2.3.4:43670",
32683443 withSession: false,
32693444 expectedStatusCode: http.StatusAccepted,
@@ -3363,6 +3538,24 @@ func TestAuthOnlyAllowedEmailDomains(t *testing.T) {
33633538 querystring: "?allowed_email_domains=a.b.c.example.com,*.c.example.com",
33643539 expectedStatusCode: http.StatusAccepted,
33653540 },
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+ },
33663559 }
33673560
33683561 for _, tc := range testCases {
@@ -3569,3 +3762,49 @@ func TestGetOAuthRedirectURI(t *testing.T) {
35693762 })
35703763 }
35713764}
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+}