Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions frontend/src/lib/hooks/screen-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type ScreenParams = {
oidc_scope?: string;
oidc_name?: string;
oidc_prompt?: "none" | "login";
skip_auto_redirect?: "true";
};

const zodScreenParams = z.object({
Expand All @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/pages/login-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
30 changes: 29 additions & 1 deletion frontend/src/pages/unauthorized-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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}`);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return;
}

navigate("/login");
};

Expand Down
9 changes: 5 additions & 4 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions internal/controller/proxy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions internal/controller/proxy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down Expand Up @@ -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")
},
},
{
Expand Down Expand Up @@ -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/"))
},
},
{
Expand Down Expand Up @@ -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/"))
},
},
{
Expand Down