feat(login): redirect configuration options in multi-domain setups (#11527)

# Which Problems Are Solved

This PR introduces robust redirect configuration options and enhances
security for Server Actions in multi-domain setups.

# How the Problems Are Solved

- Redirect Fallback Logic: Implemented a unified `resolveRedirectUri`
utility that prioritizes redirect targets as follows:
  - Environment override (`DEFAULT_REDIRECT_URI`)
  - Organization settings (`defaultRedirectUri`).
  - Relative fallback (`/signedin?...`).
- Host Reflection: `DEFAULT_REDIRECT_URI` supports absolute URLs or
relative paths via host reflection.
- Server Action Security: Configured `serverActions.allowedOrigins` to
be dynamically manageable via the `SERVER_ACTION_ALLOWED_ORIGINS`
environment variable (comma-separated list). It defaults to an empty
list for maximum security.
- UI Integration: Updated the /signedin page to use the unified redirect
logic, ensuring consistent behavior for the "Continue" button.
- Documentation: Added type definitions and detailed descriptions for
new environment variables in
`next-env-vars.d.ts`

---------

Co-authored-by: Copilot <[email protected]>
This commit is contained in:
Max Peintner
2026-02-24 11:02:29 +03:00
committed by GitHub
co-authored by Copilot
parent 87aae1b5fc
commit d27833e1bc
22 changed files with 267 additions and 53 deletions
+44
View File
@@ -0,0 +1,44 @@
import { describe, expect, test, vi, beforeEach } from "vitest";
import { isSafeRedirectUri } from "./client-utils";
describe("isSafeRedirectUri", () => {
beforeEach(() => {
vi.clearAllMocks();
});
test("should reject empty uri", async () => {
expect(await isSafeRedirectUri("")).toBe(false);
expect(await isSafeRedirectUri(undefined as unknown as string)).toBe(false);
});
test("should allow relative paths", async () => {
expect(await isSafeRedirectUri("/dashboard")).toBe(true);
expect(await isSafeRedirectUri("/ui/console")).toBe(true);
expect(await isSafeRedirectUri("/")).toBe(true);
});
test("should reject protocol-relative paths", async () => {
expect(await isSafeRedirectUri("//evil.com")).toBe(false);
expect(await isSafeRedirectUri("\\\\evil.com")).toBe(false);
});
test("should allow absolute URLs matching the current host", async () => {
expect(await isSafeRedirectUri("https://my-zitadel.com/console")).toBe(true);
});
test("should allow absolute URLs to external domains", async () => {
expect(await isSafeRedirectUri("https://evil.com")).toBe(true);
expect(await isSafeRedirectUri("https://settings.com/dashboard")).toBe(true);
});
test("should reject non-http(s) protocols", async () => {
expect(await isSafeRedirectUri("javascript:alert(1)")).toBe(false);
expect(await isSafeRedirectUri("data:text/html;base64,PHNjcmlwdD5hbGVydCgxKTwvc2NyaXB0Pg==")).toBe(false);
expect(await isSafeRedirectUri("file:///etc/passwd")).toBe(false);
});
test("should gracefully reject invalid, unparsable URLs that aren't relative", async () => {
expect(await isSafeRedirectUri("not-a-valid-url")).toBe(false);
expect(await isSafeRedirectUri("http://%")).toBe(false);
});
});
+64
View File
@@ -0,0 +1,64 @@
export type ServerActionResponse =
| { redirect: string }
| { error: string }
| { samlData: { url: string; fields: Record<string, string> } }
| undefined
| null;
export function handleServerActionResponse(
response: ServerActionResponse,
router: { push: (url: string) => void },
setSamlData: (data: { url: string; fields: Record<string, string> }) => void,
setError: (error: string) => void,
): boolean {
if (!response) {
return false;
}
if ("redirect" in response && response.redirect) {
router.push(response.redirect);
return true;
}
if ("samlData" in response && response.samlData) {
setSamlData(response.samlData);
return true;
}
if ("error" in response && response.error) {
setError(response.error);
return true;
}
return false;
}
/**
* Validates whether a given redirect URI is safe.
* Safe URIs are either relative paths or absolute URLs matching the current host.
* This prevents open redirect vulnerabilities and XSS via javascript:/data: URIs.
*/
export function isSafeRedirectUri(uri: string): boolean {
if (!uri) return false;
// 1. Relative paths are generally safe
if (uri.startsWith("/") && !uri.startsWith("//")) {
return true;
}
// 2. Check absolute URLs for safe protocols (http/https)
// We allow external domains, but strictly forbid javascript:/data: etc.
try {
const parsedUri = new URL(uri);
// Only allow http(s) protocols
if (parsedUri.protocol !== "http:" && parsedUri.protocol !== "https:") {
return false;
}
return true;
} catch {
// If it can't be parsed as a URL and didn't start with /, it's unsafe
return false;
}
}
+56
View File
@@ -0,0 +1,56 @@
import { describe, expect, test, vi, beforeEach } from "vitest";
import { getNextUrl } from "./client";
// Mock next/headers
vi.mock("next/headers", () => ({
headers: vi.fn(),
}));
// Mock host helper
vi.mock("./server/host", () => ({
getPublicHostWithProtocol: vi.fn(),
}));
describe("getNextUrl", () => {
const command = { loginName: "test-user" };
beforeEach(() => {
vi.clearAllMocks();
delete (process.env as any).DEFAULT_REDIRECT_URI;
delete (process.env as any).NEXT_PUBLIC_BASE_PATH;
});
test("should use DEFAULT_REDIRECT_URI if set", async () => {
process.env.DEFAULT_REDIRECT_URI = "https://env-override.com";
const result = await getNextUrl(command);
expect(result).toBe("https://env-override.com");
});
test("should use host-based redirect if DEFAULT_REDIRECT_URI is set to a path (starting with '/')", async () => {
const { headers } = await import("next/headers");
const { getPublicHostWithProtocol } = await import("./server/host");
process.env.DEFAULT_REDIRECT_URI = "/dashboard";
vi.mocked(headers).mockResolvedValue({} as any);
vi.mocked(getPublicHostWithProtocol).mockReturnValue("https://my-host.com");
process.env.NEXT_PUBLIC_BASE_PATH = "/ui/v2/login";
const result = await getNextUrl(command);
expect(result).toBe("https://my-host.com/dashboard");
});
test("should use defaultRedirectUri if env is NOT set", async () => {
const result = await getNextUrl(command, "https://settings.com");
expect(result).toBe("https://settings.com");
});
test("should fallback to relative signedin page if everything else fails (the new default)", async () => {
const { headers } = await import("next/headers");
vi.mocked(headers).mockRejectedValue(new Error("No headers"));
const result = await getNextUrl(command);
expect(result).toBe("/signedin?loginName=test-user");
});
});
+46 -36
View File
@@ -1,4 +1,9 @@
"use server";
import { headers } from "next/headers";
import { completeAuthFlow } from "./server/auth-flow";
import { getPublicHostWithProtocol } from "./server/host";
import { isSafeRedirectUri } from "./client-utils";
type FinishFlowCommand =
| {
@@ -100,47 +105,52 @@ export async function getNextUrl(
// OIDC/SAML flows are now handled by completeAuthFlowAction() server action
// This function only handles device flows and fallback navigation
if (defaultRedirectUri) {
console.log("getNextUrl: Using defaultRedirectUri:", defaultRedirectUri);
return defaultRedirectUri;
}
const result = goToSignedInPage(command);
console.log("getNextUrl: got goToSignedInPage result");
const result = await resolveRedirectUri(command, defaultRedirectUri);
console.log("getNextUrl: Resolved redirect URI:", result);
return result;
}
export type ServerActionResponse =
| { redirect: string }
| { error: string }
| { samlData: { url: string; fields: Record<string, string> } }
| undefined
| null;
export function handleServerActionResponse(
response: ServerActionResponse,
router: { push: (url: string) => void },
setSamlData: (data: { url: string; fields: Record<string, string> }) => void,
setError: (error: string) => void,
): boolean {
if (!response) {
return false;
/**
* Resolves the redirect URI based on the following priority:
* 1. DEFAULT_REDIRECT_URI environment variable
* 2. defaultRedirectUri from organization settings
* 3. Relative signed-in page fallback
* 4. Reserved for future extensions
*/
export async function resolveRedirectUri(command: FinishFlowCommand, defaultRedirectUri?: string): Promise<string> {
// 1. Environment variable override
const envOverride = process.env.DEFAULT_REDIRECT_URI;
if (envOverride) {
if (envOverride.startsWith("/")) {
// Special state: trigger absolute host-based redirect with provided path
try {
const _headers = await headers();
const host = getPublicHostWithProtocol(_headers);
const result = `${host}${envOverride}`;
console.log("resolveRedirectUri: Using host-based redirect from override:", result);
return result;
} catch (error) {
console.warn("resolveRedirectUri: Could not determine host for override, falling back", error);
}
} else {
console.log("resolveRedirectUri: Using DEFAULT_REDIRECT_URI override:", envOverride);
return envOverride;
}
}
if ("redirect" in response && response.redirect) {
router.push(response.redirect);
return true;
// 2. Default redirect URI from settings
if (defaultRedirectUri) {
if (isSafeRedirectUri(defaultRedirectUri)) {
console.log("resolveRedirectUri: Using defaultRedirectUri from settings:", defaultRedirectUri);
return defaultRedirectUri;
} else {
console.warn("resolveRedirectUri: Unsafe defaultRedirectUri prevented:", defaultRedirectUri);
}
}
if ("samlData" in response && response.samlData) {
setSamlData(response.samlData);
return true;
}
if ("error" in response && response.error) {
setError(response.error);
return true;
}
return false;
// 3. Default signed-in page (relative)
const result = goToSignedInPage(command);
console.log("resolveRedirectUri: Using relative goToSignedInPage result:", result);
return result;
}
+6
View File
@@ -29,6 +29,12 @@ export function getServiceConfig(headers: ReadonlyHeaders): { serviceConfig: Ser
const instanceHost = getInstanceHost(headers);
const publicHost = getPublicHost(headers);
console.log("serviceConfig", {
baseUrl: process.env.ZITADEL_API_URL,
...(instanceHost && { instanceHost: stripProtocol(instanceHost) }),
...(publicHost && { publicHost: stripProtocol(publicHost) }),
});
return {
serviceConfig: {
baseUrl: process.env.ZITADEL_API_URL,