Merge commit from fork

* fix: Improve host header handling and deployment mode detection

* ZITADEL_CLOUD env

* refactor service config

* service config

* cleanup

* cleanup

* util for original and public host

* instanceHost, publicHost

* cleanup constructUrl

* cleanup middleware

* add x-zitadel-forward-host as fallback for current cloud setup

* fix build

* fix: lint

* fix: update test mocks

* fix: provide basepath to unit tests

* host handling

* unit tests

* imports

* fix: getPublicHost instead of getInstanceHost

* unit tests

* update nextjs, implement jwt verify for logout_token

* pass logout information in jwt to login v2

* handle hosts in jwks url

* point idp callbacks to the api and not the login UI

* logout_hint out of logout token, typecheck

* rm unused imports

* fix oidc integration test

---------

Co-authored-by: Livio Spring <[email protected]>
This commit is contained in:
Max Peintner
2025-12-08 10:01:49 +01:00
committed by GitHub
co-authored by Livio Spring
parent f9e3871bb3
commit df75be96ff
75 changed files with 2075 additions and 1817 deletions
+28 -39
View File
@@ -1,58 +1,47 @@
import { ReadonlyHeaders } from "next/dist/server/web/spec-extension/adapters/headers";
import { NextRequest } from "next/server";
import { ServiceConfig } from "./zitadel";
import { getInstanceHost, getPublicHost } from "./server/host";
/**
* Extracts the service url and region from the headers if used in a multitenant context (host, x-zitadel-forward-host header)
* or falls back to the ZITADEL_API_URL for a self hosting deployment
* or falls back to the host header for a self hosting deployment using custom domains
* @param headers
* @returns the service url and region from the headers
* @throws if the service url could not be determined
* Extracts the service URL based on deployment mode and configuration.
*
* Priority:
* 1. ZITADEL_API_URL (required) - Used by both self-hosted and multi-tenant
* 2. x-zitadel-forward-host (multi-tenant only) - Set by Zitadel proxy
* 3. host header (multi-tenant fallback) - For dynamic host resolution
*
* @param headers - Request headers
* @returns Object containing the service Configuration
* @throws Error if the service Configuration could not be determined
*/
export function getServiceUrlFromHeaders(headers: ReadonlyHeaders): {
serviceUrl: string;
} {
let instanceUrl;
const forwardedHost = headers.get("x-zitadel-forward-host");
// use the forwarded host if available (multitenant), otherwise fall back to the host of the deployment itself
if (forwardedHost) {
instanceUrl = forwardedHost;
instanceUrl = instanceUrl.startsWith("http://")
? instanceUrl
: `https://${instanceUrl}`;
} else if (process.env.ZITADEL_API_URL) {
instanceUrl = process.env.ZITADEL_API_URL;
} else {
const host = headers.get("host");
function stripProtocol(url: string): string {
return url.replace(/^https?:\/\//, "");
}
if (host) {
const [hostname] = host.split(":");
if (hostname !== "localhost") {
instanceUrl = host.startsWith("http") ? host : `https://${host}`;
}
}
export function getServiceConfig(headers: ReadonlyHeaders): { serviceConfig: ServiceConfig } {
if (!process.env.ZITADEL_API_URL) {
throw new Error("ZITADEL_API_URL is not set");
}
if (!instanceUrl) {
throw new Error("Service URL could not be determined");
}
// use forwarded host from proxy - headers are forwarded to the APIs.
const instanceHost = getInstanceHost(headers);
const publicHost = getPublicHost(headers);
return {
serviceUrl: instanceUrl,
serviceConfig: {
baseUrl: process.env.ZITADEL_API_URL,
...(instanceHost && { instanceHost: stripProtocol(instanceHost) }),
...(publicHost && { publicHost: stripProtocol(publicHost) }),
},
};
}
export function constructUrl(request: NextRequest, path: string) {
const forwardedProto = request.headers.get("x-forwarded-proto")
? `${request.headers.get("x-forwarded-proto")}:`
: request.nextUrl.protocol;
const protocol = request.nextUrl.protocol;
const forwardedHost =
request.headers.get("x-zitadel-forward-host") ??
request.headers.get("x-forwarded-host") ??
request.headers.get("host");
const forwardedHost = getPublicHost(request.headers);
const basePath = process.env.NEXT_PUBLIC_BASE_PATH || "";
return new URL(`${basePath}${path}`, `${forwardedProto}//${forwardedHost}`);
return new URL(`${basePath}${path}`, `${protocol}//${forwardedHost}`);
}