fix(login): host utility to provide correct host behind proxies (#10770)

<!--
Please inform yourself about the contribution guidelines on submitting a
PR here:
https://github.com/zitadel/zitadel/blob/main/CONTRIBUTING.md#submit-a-pull-request-pr.
Take note of how PR/commit titles should be written and replace the
template texts in the sections below. Don't remove any of the sections.
It is important that the commit history clearly shows what is changed
and why.
Important: By submitting a contribution you agree to the terms from our
Licensing Policy as described here:
https://github.com/zitadel/zitadel/blob/main/LICENSING.md#community-contributions.
-->

# Which Problems Are Solved

When deploying the login application behind proxies or using Vercel
rewrites (e.g., `zitadel.com/login` → `login-zitadel-qa.vercel.app`),
the application was using the internal rewritten host instead of the
original user-facing host. This caused several issues:

1. **Broken Password Reset Emails**: Email links contained internal
hosts like `login-zitadel-qa.vercel.app` instead of `zitadel.com`
2. **Inconsistent User Experience**: Users would see different domains
in various parts of the flow
3. **Security Concerns**: Internal infrastructure details were exposed
to end users
4. **Scattered Logic**: Host detection logic was duplicated across
multiple files with inconsistent error handling

# How the Problems Are Solved

Created comprehensive host detection utilities in `/lib/server/host.ts`
and `/lib/client/host.ts`:

**Server-side utilities:**
- `getOriginalHost()` - Returns the original user-facing host
- `getOriginalHostWithProtocol()` - Returns host with proper protocol
(http/https)
This commit is contained in:
Max Peintner
2025-09-23 16:21:01 +00:00
committed by GitHub
parent 16906d2c2c
commit 09d09ab337
24 changed files with 1324 additions and 840 deletions
+18 -10
View File
@@ -1,8 +1,8 @@
"use client";
import { createNewSessionFromIdpIntent } from "@/lib/server/idp";
import { CreateNewSessionCommand, createNewSessionFromIdpIntent } from "@/lib/server/idp";
import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import { useEffect, useRef, useState } from "react";
import { Alert } from "./alert";
import { Spinner } from "./spinner";
@@ -16,25 +16,33 @@ type Props = {
requestId?: string;
};
export function IdpSignin({
userId,
idpIntent: { idpIntentId, idpIntentToken },
requestId,
}: Props) {
export function IdpSignin({ userId, idpIntent: { idpIntentId, idpIntentToken }, requestId }: Props) {
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const executedRef = useRef(false);
const router = useRouter();
useEffect(() => {
createNewSessionFromIdpIntent({
// Prevent double execution in React Strict Mode
if (executedRef.current) {
return;
}
executedRef.current = true;
let request: CreateNewSessionCommand = {
userId,
idpIntent: {
idpIntentId,
idpIntentToken,
},
requestId,
})
};
if (requestId) {
request = { ...request, requestId: requestId };
}
createNewSessionFromIdpIntent(request)
.then((response) => {
if (response && "error" in response && response?.error) {
setError(response?.error);
+20 -22
View File
@@ -1,7 +1,7 @@
"use client";
import { sendLoginname } from "@/lib/server/loginname";
import { clearSession, continueWithSession } from "@/lib/server/session";
import { clearSession, continueWithSession, ContinueWithSessionCommand } from "@/lib/server/session";
import { XCircleIcon } from "@heroicons/react/24/outline";
import * as Tooltip from "@radix-ui/react-tooltip";
import { Timestamp, timestampDate } from "@zitadel/client";
@@ -21,9 +21,7 @@ export function isSessionValid(session: Partial<Session>): {
const validPasskey = session?.factors?.webAuthN?.verifiedAt;
const validIDP = session?.factors?.intent?.verifiedAt;
const stillValid = session.expirationDate
? timestampDate(session.expirationDate) > new Date()
: true;
const stillValid = session.expirationDate ? timestampDate(session.expirationDate) > new Date() : true;
const verifiedAt = validPassword || validPasskey || validIDP;
const valid = !!((validPassword || validPasskey || validIDP) && stillValid);
@@ -31,15 +29,7 @@ export function isSessionValid(session: Partial<Session>): {
return { valid, verifiedAt };
}
export function SessionItem({
session,
reload,
requestId,
}: {
session: Session;
reload: () => void;
requestId?: string;
}) {
export function SessionItem({ session, reload, requestId }: { session: Session; reload: () => void; requestId?: string }) {
const currentLocale = useLocale();
moment.locale(currentLocale === "zh" ? "zh-cn" : currentLocale);
@@ -73,10 +63,21 @@ export function SessionItem({
<button
onClick={async () => {
if (valid && session?.factors?.user) {
await continueWithSession({
...session,
requestId: requestId,
});
const sessionPayload: ContinueWithSessionCommand = session;
if (requestId) {
sessionPayload.requestId = requestId;
}
const callbackResponse = await continueWithSession(sessionPayload);
if (callbackResponse && "error" in callbackResponse) {
setError(callbackResponse.error);
return;
}
if (callbackResponse && "redirect" in callbackResponse) {
return router.push(callbackResponse.redirect);
}
} else if (session.factors?.user) {
setLoading(true);
const res = await sendLoginname({
@@ -114,9 +115,7 @@ export function SessionItem({
<div className="flex flex-col items-start overflow-hidden">
<span className="">{session.factors?.user?.displayName}</span>
<span className="text-ellipsis text-xs opacity-80">
{session.factors?.user?.loginName}
</span>
<span className="text-ellipsis text-xs opacity-80">{session.factors?.user?.loginName}</span>
{valid ? (
<span className="text-ellipsis text-xs opacity-80">
<Translated i18nKey="verified" namespace="accounts" />{" "}
@@ -126,8 +125,7 @@ export function SessionItem({
verifiedAt && (
<span className="text-ellipsis text-xs opacity-80">
<Translated i18nKey="expired" namespace="accounts" />{" "}
{session.expirationDate &&
moment(timestampDate(session.expirationDate)).fromNow()}
{session.expirationDate && moment(timestampDate(session.expirationDate)).fromNow()}
</span>
)
)}