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
+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");
});
});