fix(login): ClassifiedConnectError breaking ConnectError instanceof checks (#12022)

# Which Problems Are Solved

1. SSL/TLS handshake failures on Cloud Run (middleware self-loopback)
PR #11903 changed the middleware (proxy.ts) to fetch security settings
via fetch(origin + "/security") on every request, including non-proxy
routes like /login. On Cloud Run, this self-loopback goes through the
Google Front End (GFE) load balancer, causing intermittent SSL
routines::record layer failure errors that were tenant-consistent.

2. The ClassifiedConnectError introduced in #11926 set this.name =
"ClassifiedConnectError", which broke ConnectError's custom
Symbol.hasInstance duck-typing check (v.name === "ConnectError"). This
caused ConnectError.from() inside the connectRPC transport's abort
handler to re-wrap classified errors as new ConnectError instances with
Code.Unknown — losing the original gRPC error code and all
classification metadata.

# How the Problems Are Solved

Middleware: eliminate self-loopback fetch:

- Replaced the fetch(origin + "/security") self-loopback with a direct
fetch to ZITADEL_API_URL using the Connect protocol (POST + JSON),
bypassing the load balancer entirely
- Security settings are cached in-memory with a 1-hour TTL per instance
host
- Extracted the fetching/caching logic into
src/lib/server/security-settings.ts
Removed the now-unused /security API route (src/app/security/route.ts)
- CSP headers with iframe origins are now applied to all routes without
any loopback

Error classification interceptor: fixed
- Keep this.name = "ConnectError" in ClassifiedConnectError so the
duck-typing Symbol.hasInstance check passes
- The branded Symbol.for check via isClassifiedError() still correctly
distinguishes the subclass
- Remove redundant Object.setPrototypeOf call (the super constructor
already handles it via new.target)
- Replace remaining instanceof ConnectError checks with
isClassifiedError() in setUserPassword and checkSessionAndSetPassword
This commit is contained in:
Max Peintner
2026-04-23 09:11:13 +02:00
committed by GitHub
parent dd9dcf9f72
commit de98c6edb8
12 changed files with 196 additions and 129 deletions
@@ -38,7 +38,15 @@ describe("ClassifiedConnectError", () => {
expect(classified.message).toContain("not found");
expect(classified.code).toBe(Code.NotFound);
expect(classified.name).toBe("ClassifiedConnectError");
// name must remain "ConnectError" so ConnectError's custom Symbol.hasInstance passes
expect(classified.name).toBe("ConnectError");
});
it("is instanceof ConnectError (required for ConnectError.from compatibility)", () => {
const source = new ConnectError("test", Code.FailedPrecondition);
const classified = new ClassifiedConnectError(source);
expect(classified instanceof ConnectError).toBe(true);
});
it("sets httpStatus from gRPC code", () => {
@@ -70,10 +70,10 @@ export class ClassifiedConnectError extends ConnectError {
constructor(source: ConnectError) {
super(source.rawMessage, source.code, source.metadata, undefined, source.cause);
// ConnectError's constructor resets the prototype chain via Object.setPrototypeOf.
// We must restore it so instanceof ClassifiedConnectError works correctly.
Object.setPrototypeOf(this, ClassifiedConnectError.prototype);
this.name = "ClassifiedConnectError";
// Keep name as "ConnectError" so that ConnectError's custom Symbol.hasInstance
// duck-typing check (v.name === "ConnectError") passes. The branded Symbol.for
// check via isClassifiedError() is used to distinguish this subclass instead.
this.name = "ConnectError";
// Preserve the original stack trace so debugging/alert triage can see the RPC call site.
if (source.stack) {
this.stack = source.stack;
@@ -67,7 +67,7 @@ describe("otelGrpcInterceptor", () => {
});
});
it("records error spans with gRPC status codes", async () => {
it("records error spans with gRPC status codes and classification", async () => {
const mockTransport = createRouterTransport(
({ service }) => {
service(SessionService, {