diff --git a/frontend/src/lib/hooks/screen-params.ts b/frontend/src/lib/hooks/screen-params.ts index 6a459733..44291ea6 100644 --- a/frontend/src/lib/hooks/screen-params.ts +++ b/frontend/src/lib/hooks/screen-params.ts @@ -7,6 +7,7 @@ type ScreenParams = { oidc_scope?: string; oidc_name?: string; oidc_prompt?: "none" | "login"; + skip_auto_redirect?: "true"; }; const zodScreenParams = z.object({ @@ -16,6 +17,7 @@ const zodScreenParams = z.object({ oidc_scope: z.string().optional(), oidc_name: z.string().optional(), oidc_prompt: z.enum(["none", "login"]).optional(), + skip_auto_redirect: z.enum(["true"]).optional(), }); export function useScreenParams(params: URLSearchParams): ScreenParams { diff --git a/frontend/src/pages/login-page.tsx b/frontend/src/pages/login-page.tsx index 91478e6c..4fa4e761 100644 --- a/frontend/src/pages/login-page.tsx +++ b/frontend/src/pages/login-page.tsx @@ -72,12 +72,18 @@ export const LoginPage = () => { })(); const loginForUrl = useLoginFor({ login_for: screenParams.login_for, - params: searchParamsFromObject({ ...screenParams, oidc_prompt: undefined}), + params: searchParamsFromObject({ + ...screenParams, + oidc_prompt: undefined, + skip_auto_redirect: undefined, + }), }); const [isOauthAutoRedirect, setIsOauthAutoRedirect] = useState( providers.find((provider) => provider.id === oauth.autoRedirect) !== - undefined && (screenParams.redirect_uri || screenParams.oidc_ticket), + undefined && + screenParams.skip_auto_redirect !== "true" && + (screenParams.redirect_uri || screenParams.oidc_ticket), ); const oauthProviders = providers.filter( diff --git a/frontend/src/pages/unauthorized-page.tsx b/frontend/src/pages/unauthorized-page.tsx index e5bce8b5..6e57f244 100644 --- a/frontend/src/pages/unauthorized-page.tsx +++ b/frontend/src/pages/unauthorized-page.tsx @@ -6,9 +6,12 @@ import { CardHeader, CardTitle, } from "@/components/ui/card"; +import { searchParamsFromObject } from "@/lib/hooks/screen-params"; +import axios from "axios"; import { useState } from "react"; import { Trans, useTranslation } from "react-i18next"; import { Navigate, useLocation, useNavigate } from "react-router"; +import { toast } from "sonner"; export const UnauthorizedPage = () => { const { search } = useLocation(); @@ -22,9 +25,34 @@ export const UnauthorizedPage = () => { const resource = searchParams.get("resource"); const groupErr = searchParams.get("groupErr"); const ip = searchParams.get("ip"); + const redirectUri = searchParams.get("redirect_uri"); - const handleRedirect = () => { + const handleRedirect = async () => { setLoading(true); + + // The user is signed in but not allowed to access the app. Sign out first + // so that another user can sign in, then return to the app after login. + // Skip the automatic OAuth redirect, which could sign the same user in + // again, so that the user can choose how to sign in. + if (username && redirectUri) { + try { + await axios.post("/api/user/logout"); + } catch { + setLoading(false); + toast.error(t("logoutFailTitle"), { + description: t("logoutFailSubtitle"), + }); + return; + } + const params = searchParamsFromObject({ + redirect_uri: redirectUri, + login_for: "app", + skip_auto_redirect: "true", + }); + window.location.replace(`/login?${params}`); + return; + } + navigate("/login"); }; diff --git a/internal/controller/controller.go b/internal/controller/controller.go index d711efdd..3f6fe7d5 100644 --- a/internal/controller/controller.go +++ b/internal/controller/controller.go @@ -8,10 +8,11 @@ const ( ) type UnauthorizedQuery struct { - Username string `url:"username"` - Resource string `url:"resource"` - GroupErr bool `url:"groupErr"` - IP string `url:"ip"` + Username string `url:"username"` + Resource string `url:"resource"` + GroupErr bool `url:"groupErr"` + IP string `url:"ip"` + RedirectURI string `url:"redirect_uri,omitempty"` } type RedirectQuery struct { diff --git a/internal/controller/proxy_controller.go b/internal/controller/proxy_controller.go index 4be21258..1f34f3a4 100644 --- a/internal/controller/proxy_controller.go +++ b/internal/controller/proxy_controller.go @@ -190,7 +190,8 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) { controller.log.App.Warn().Str("user", userContext.GetUsername()).Str("resource", strings.Split(proxyCtx.Host, ".")[0]).Msg("User is not allowed to access resource") queries, err := query.Values(UnauthorizedQuery{ - Resource: strings.Split(proxyCtx.Host, ".")[0], + Resource: strings.Split(proxyCtx.Host, ".")[0], + RedirectURI: fmt.Sprintf("%s://%s%s", proxyCtx.Proto, proxyCtx.Host, proxyCtx.PathRaw), }) if err != nil { @@ -233,8 +234,9 @@ func (controller *ProxyController) proxyHandler(c *gin.Context) { controller.log.App.Warn().Str("user", userContext.GetUsername()).Str("resource", strings.Split(proxyCtx.Host, ".")[0]).Msg("User is not in the required group to access resource") queries, err := query.Values(UnauthorizedQuery{ - Resource: strings.Split(proxyCtx.Host, ".")[0], - GroupErr: true, + Resource: strings.Split(proxyCtx.Host, ".")[0], + GroupErr: true, + RedirectURI: fmt.Sprintf("%s://%s%s", proxyCtx.Proto, proxyCtx.Host, proxyCtx.PathRaw), }) if err != nil { diff --git a/internal/controller/proxy_controller_test.go b/internal/controller/proxy_controller_test.go index df606e2d..acef4e23 100644 --- a/internal/controller/proxy_controller_test.go +++ b/internal/controller/proxy_controller_test.go @@ -666,6 +666,25 @@ func TestProxyController(t *testing.T) { assert.Equal(t, "", recorder.Header().Get("remote-email")) }, }, + { + description: "Ensure user allow ACL redirects incorrect user with browser to unauthorized page with redirect uri", + middlewares: []gin.HandlerFunc{ + simpleCtxTotp, + }, + run: func(t *testing.T, router *gin.Engine, recorder *httptest.ResponseRecorder) { + req := httptest.NewRequest("GET", "/api/auth/traefik", nil) + req.Header.Set("x-forwarded-host", "user-allow.example.com") + req.Header.Set("x-forwarded-proto", "https") + req.Header.Set("x-forwarded-uri", "/some/path") + req.Header.Set("user-agent", browserUserAgent) + router.ServeHTTP(recorder, req) + assert.Equal(t, http.StatusFound, recorder.Code) + location := recorder.Header().Get("Location") + assert.Contains(t, location, runtime.AppURL+"/unauthorized?") + assert.Contains(t, location, "username=totpuser") + assert.Contains(t, location, "redirect_uri="+url.QueryEscape("https://user-allow.example.com/some/path")) + }, + }, { description: "Test IP block rule, with non browser user agent", middlewares: []gin.HandlerFunc{}, @@ -699,6 +718,7 @@ func TestProxyController(t *testing.T) { assert.Contains(t, location, url.QueryEscape("10.10.10.10")) assert.Contains(t, location, url.QueryEscape("ip-block")) assert.Contains(t, location, runtime.AppURL) + assert.NotContains(t, location, "redirect_uri") }, }, { @@ -799,6 +819,7 @@ func TestProxyController(t *testing.T) { assert.Contains(t, location, "groupErr=true") assert.Contains(t, location, "oauth-group") assert.Contains(t, location, runtime.AppURL) + assert.Contains(t, location, "redirect_uri="+url.QueryEscape("https://oauth-group.example.com/")) }, }, { @@ -899,6 +920,7 @@ func TestProxyController(t *testing.T) { assert.Contains(t, location, "groupErr=true") assert.Contains(t, location, "ldap-group") assert.Contains(t, location, runtime.AppURL) + assert.Contains(t, location, "redirect_uri="+url.QueryEscape("https://ldap-group.example.com/")) }, }, {