fix(login): refactor SAML Post Flow to bypass cookie limits, fix SAML SP post (#11170)

Closes #10816
Closes #10759

# Which Problems Are Solved

This PR refactors the SAML Post binding flow to address issues with
payloads exceeding browser cookie size limits (typically ~4KB).
Previously, the SAML form data was serialized into a cookie, and the
user was redirected to an intermediate /saml-post page to render the
form. This approach failed for large SAML assertions.

# How the Problems Are Solved

The new implementation bypasses cookies entirely for the payload
transfer. Server actions now return the form data directly to the
client, which immediately renders and submits the form.

- Direct Data Transfer: Updated `startIdentityProviderFlow` to return an
object { url, fields } instead of a redirect URL when the next step is
formData.
- Server Actions: Updated redirectToIdp, sendLoginname, and
completeAuthFlow to propagate this samlData object to the client.
- Client-Side Auto-Submission: Added a new AutoSubmitForm component that
renders a hidden form with the provided fields and automatically submits
it to the IdP on mount.

---------

Co-authored-by: conblem <[email protected]>
This commit is contained in:
Max Peintner
2026-02-09 13:46:30 +01:00
committed by GitHub
co-authored by conblem
parent 6f4f54708e
commit 9492c12c09
36 changed files with 891 additions and 1454 deletions
+21 -56
View File
@@ -4,8 +4,6 @@ import { createResponse, getLoginSettings, ServiceConfig } from "@/lib/zitadel";
import { create } from "@zitadel/client";
import { CreateResponseRequestSchema } from "@zitadel/proto/zitadel/saml/v2/saml_service_pb";
import { Session } from "@zitadel/proto/zitadel/session/v2/session_pb";
import { cookies } from "next/headers";
import { v4 as uuidv4 } from "uuid";
import { isSessionValid } from "./session";
type LoginWithSAMLAndSession = {
@@ -16,64 +14,17 @@ type LoginWithSAMLAndSession = {
sessionCookies: Cookie[];
};
export async function getSAMLFormUID() {
return uuidv4();
}
export async function setSAMLFormCookie(value: string): Promise<string> {
const cookiesList = await cookies();
const uid = await getSAMLFormUID();
try {
// Log the attempt
await cookiesList.set({
name: uid,
value: value,
httpOnly: true,
secure: process.env.NODE_ENV === "production", // Required for HTTPS in production
sameSite: "lax", // Allows cookies with top-level navigation (needed for SAML redirects)
path: "/",
maxAge: 5 * 60, // 5 minutes
});
// Note: We can't reliably verify immediately due to Next.js cookies API behavior
// Instead, we'll rely on the getSAMLFormCookie function to detect failures
console.log(`Successfully set SAML form cookie with uid: ${uid}`);
return uid;
} catch (error) {
throw new Error(`Failed to set SAML form cookie: ${error instanceof Error ? error.message : String(error)}`);
}
}
export async function getSAMLFormCookie(uid: string): Promise<string | null> {
const cookiesList = await cookies();
try {
const cookie = cookiesList.get(uid);
if (!cookie) {
return null;
}
if (!cookie.value) {
return null;
}
return cookie.value;
} catch {
return null;
}
}
export async function loginWithSAMLAndSession({
serviceConfig,
samlRequest,
sessionId,
sessions,
sessionCookies,
}: LoginWithSAMLAndSession): Promise<{ error: string } | { redirect: string }> {
}: LoginWithSAMLAndSession): Promise<
{ error: string } | { redirect: string } | { samlData: { url: string; fields: Record<string, string> } }
> {
console.log(`Login with session: ${sessionId} and samlRequest: ${samlRequest}`);
const selectedSession = sessions.find((s) => s.id === sessionId);
if (selectedSession && selectedSession.id) {
@@ -93,6 +44,10 @@ export async function loginWithSAMLAndSession({
if (res && "redirect" in res && res?.redirect) {
return { redirect: res.redirect };
}
if (res && "samlData" in res && res?.samlData) {
return { samlData: res.samlData };
}
}
const cookie = sessionCookies.find((cookie) => cookie.id === selectedSession?.id);
@@ -105,7 +60,7 @@ export async function loginWithSAMLAndSession({
// works not with _rsc request
try {
const { url } = await createResponse({
const { url, binding } = await createResponse({
serviceConfig,
req: create(CreateResponseRequestSchema, {
samlRequestId: samlRequest,
@@ -115,8 +70,18 @@ export async function loginWithSAMLAndSession({
},
}),
});
if (url) {
if (url && binding.case === "redirect") {
return { redirect: url };
} else if (url && binding.case === "post") {
return {
samlData: {
url,
fields: {
RelayState: binding.value.relayState,
SAMLResponse: binding.value.samlResponse,
},
},
};
} else {
return { error: "An error occurred!" };
}