Skip to content

fix: dispatch SSR self-calls against the listen address - #3188

Merged
tada5hi merged 2 commits into
masterfrom
fix/ssr-internal-http-client
Jul 6, 2026
Merged

tada5hi merged 2 commits into
masterfrom
fix/ssr-internal-http-client

Conversation

@tada5hi

@tada5hi tada5hi commented Jul 6, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

The SSR'd auth pages (/authorize, /register, ...) build their API client from publicUrl, so every server-side render round-trips through the reverse proxy. With a self-signed local certificate (e.g. https://hub.local), Node's fetch fails with UNABLE_TO_GET_ISSUER_CERT_LOCALLY — and because the identity-provider list load fires as an unawaited promise, the rejection escaped the request pipeline and crashed the whole server process. The setup also silently required the container to be able to resolve and reach its own public hostname.

Changes

server-core — internal loopback client for SSR self-calls

  • HTTPModule.setup now registers a production default under HTTPInjectionKey.UIHttpClient (previously test-injection only; a registration made before setup still wins).
  • The client (createInternalUIHttpClient, adapters/http/ui/internal-http-client.ts) keeps baseURL = publicUrl — rendered hrefs (e.g. identity-provider authorize links) derive from it and hydration does not patch attribute mismatches — while its hapic FetchTransport rewrites every request targeting the public origin + sub-path prefix onto the server's own listen address (server.url, wildcard binds normalized to loopback, resolved lazily per request).
  • Registration is lifetime: 'transient': a fresh client per render, no shared per-user state.

client-web-kit — SSR crash hardening

  • The entity-collection manager's load() no longer rejects: errors are caught and emitted as a new failed event. A failed SSR fetch renders the page degraded (login form without the provider list) instead of killing the process via an unhandled rejection.

core-http-kit

  • ClientOptions widened from RequestBaseOptions to hapic's ClientOptionsInput (strictly additive), exposing the transport seam.

Tests

  • Unit spec for the URL rewriter: prefixed public URLs (https://hub.local/auth), trailing slash, bare prefix, foreign origins, segment-boundary safety (/authorize vs /auth/...), wildcard-host normalization.
  • New integration suite ui-pages-internal-client.spec.ts: exercises the default client (no fake registered) against a live test server, once with default config and once with publicUrl = 'https://hub.local/auth/' (the test factory gained a config-override hook) — loopback dispatch returns real data, getBaseURL()/getAuthorizeUri() stay public, per-request resolves yield distinct clients, and the rendered /authorize page never leaks the listen address (and carries /auth/public/ asset URLs in sub-path mode).
  • Kit spec: a throwing GET /identity-providers handler → login form still renders and failed is emitted.

Full server-core suite (105 files / 1057 tests), client-web-kit and core-http-kit suites pass. .agents/architecture.md / .agents/testing.md updated.

Summary by CodeRabbit

  • New Features

    • SSR-auth pages now use a more reliable internal request path, improving page rendering behind proxies and in local/test environments.
    • URL handling now keeps user-facing links and assets aligned with the configured public address, even when the app is served from a sub-path.
  • Bug Fixes

    • SSR page loads degrade gracefully when internal requests fail, reducing the chance of render failures.
    • Login and identity-provider flows now handle fetch errors more safely without breaking the page.

The SSR'd auth pages built their API client from publicUrl, so every
render round-tripped through the reverse proxy — a self-signed local
certificate (e.g. https://hub.local) failed Node's fetch with
UNABLE_TO_GET_ISSUER_CERT_LOCALLY, and the unhandled rejection from the
fire-and-forget identity-provider load killed the server process.

- server-core: register a default UIHttpClient (transient) whose hapic
  transport rewrites requests targeting publicUrl (origin + sub-path
  prefix, wildcard listen hosts normalized to loopback) onto the
  server's own listen address; baseURL stays publicUrl so rendered
  hrefs remain user-facing
- client-web-kit: entity-collection loads no longer reject — errors are
  caught and emitted as 'failed', so a failed SSR fetch renders the
  page degraded instead of crashing the server
- core-http-kit: widen ClientOptions to hapic's ClientOptionsInput,
  exposing the transport seam
Copilot AI review requested due to automatic review settings July 6, 2026 11:04
@coderabbitai

coderabbitai Bot commented Jul 6, 2026 •

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

This PR introduces an internal HTTP client for SSR UI page self-calls, rewriting requests from the public URL to the server's internal listen address while preserving public-facing rendered hrefs. It's wired into the HTTP module's DI lifecycle, documented, and tested. Separately, entity-collection components gain a "failed" emit event triggered on load errors instead of throwing.

Changes

Internal UI HTTP client for SSR self-calls

Layer / File(s) Summary
Client context type and options contract
apps/server-core/src/adapters/http/ui/types.ts, packages/core-http-kit/src/client/type.ts
Adds InternalUIHttpClientContext type (publicURL/internalURL) and changes ClientOptions to alias ClientOptionsInput instead of RequestBaseOptions.
URL rewriter and internal client factory
apps/server-core/src/adapters/http/ui/internal-http-client.ts, apps/server-core/src/adapters/http/ui/index.ts
Implements createPublicToInternalURLRewriter (public-to-internal URL rewriting with wildcard host normalization) and createInternalUIHttpClient (Client with public baseURL but rewritten fetch transport); re-exports from the ui index.
HTTP module registration and teardown
apps/server-core/src/app/modules/http/module.ts, apps/server-core/src/app/modules/http/constants.ts, apps/server-core/src/adapters/http/middleware/built-in/ui-http-client.ts
Registers UIHttpClient in DI during setup (internal or public-fallback), skips if pre-registered, tracks registration state, and unregisters in teardown; updates related comments.
Rewriter, client, and SSR page tests
apps/server-core/test/unit/adapters/http/ui/internal-http-client.spec.ts, apps/server-core/test/unit/http/controllers/workflows/ui-pages-internal-client.spec.ts, apps/server-core/test/app/factory.ts
Adds unit tests for URL rewriting/client base URL and SSR /authorize page tests (including sub-path publicUrl); extends test factory with a config alter callback.
Architecture and testing documentation
.agents/architecture.md, .agents/testing.md
Documents the internal client's transport-level rewriting, degraded-render error handling, and updated test wiring/caveats.

Estimated code review effort: 3 (Moderate) | ~30 minutes

Entity-collection failed emit event

Layer / File(s) Summary
Failed event contract and load error handling
packages/client-web-kit/src/components/utility/entity/collection/types.ts, .../utils/component.ts, .../module.ts
Adds failed(error: Error) to EntityCollectionVEmitOptions and its default implementation; load() now catches errors, normalizes them into an Error, emits failed, and returns without throwing.
Login form failure regression test
packages/client-web-kit/test/unit/components/workflows/login-form.spec.ts
Adds a test mocking a failing GET /identity-providers request and asserting the login form renders and AIdentityProviders emits failed.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant SSRPage as SSR Page Render
  participant UIHttpClient as UIHttpClient
  participant Rewriter as URL Rewriter
  participant Server as Server (internal listen address)

  SSRPage->>UIHttpClient: request(publicUrl-based path)
  UIHttpClient->>Rewriter: rewrite(url)
  Rewriter-->>UIHttpClient: internal-address URL
  UIHttpClient->>Server: fetch(internal URL)
  Server-->>UIHttpClient: response
  UIHttpClient-->>SSRPage: response (rendered hrefs stay public)
Loading

Possibly related PRs

  • authup/authup#3104: Extends the same adapters/http/ui SSR rendering surface that this PR adds the internal HTTP client to.
  • authup/authup#3114: Touches the same HTTPInjectionKey.UIHttpClient injection pipeline that this PR implements and registers.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: routing SSR self-calls to the listen address.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/ssr-internal-http-client

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Jul 6, 2026

Copy link
Copy Markdown

Open in StackBlitz

authup

npm i https://pkg.pr.new/authup@3188

@authup/client-web

npm i https://pkg.pr.new/@authup/client-web@3188

@authup/server-core

npm i https://pkg.pr.new/@authup/server-core@3188

@authup/access

npm i https://pkg.pr.new/@authup/access@3188

@authup/client-web-kit

npm i https://pkg.pr.new/@authup/client-web-kit@3188

@authup/client-web-kit-theme

npm i https://pkg.pr.new/@authup/client-web-kit-theme@3188

@authup/client-web-nuxt

npm i https://pkg.pr.new/@authup/client-web-nuxt@3188

@authup/client-web-theme

npm i https://pkg.pr.new/@authup/client-web-theme@3188

@authup/core-http-kit

npm i https://pkg.pr.new/@authup/core-http-kit@3188

@authup/core-kit

npm i https://pkg.pr.new/@authup/core-kit@3188

@authup/core-realtime-kit

npm i https://pkg.pr.new/@authup/core-realtime-kit@3188

@authup/errors

npm i https://pkg.pr.new/@authup/errors@3188

@authup/i18n

npm i https://pkg.pr.new/@authup/i18n@3188

@authup/kit

npm i https://pkg.pr.new/@authup/kit@3188

@authup/server-adapter-kit

npm i https://pkg.pr.new/@authup/server-adapter-kit@3188

@authup/server-adapter-node

npm i https://pkg.pr.new/@authup/server-adapter-node@3188

@authup/server-adapter-socket-io

npm i https://pkg.pr.new/@authup/server-adapter-socket-io@3188

@authup/server-adapter-web

npm i https://pkg.pr.new/@authup/server-adapter-web@3188

@authup/server-kit

npm i https://pkg.pr.new/@authup/server-kit@3188

@authup/server-test-kit

npm i https://pkg.pr.new/@authup/server-test-kit@3188

@authup/specs

npm i https://pkg.pr.new/@authup/specs@3188

commit: f5bcc05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves SSR reliability for Authup’s UI workflow pages by ensuring server-side “self-calls” dispatch to the server’s listen address (instead of round-tripping through publicUrl and a reverse proxy), and by hardening client-web-kit SSR collection loads against unhandled promise rejections.

Changes:

  • Register a default transient UIHttpClient in server-core that keeps baseURL = publicUrl for rendered links but rewrites transport dispatch to the server listen address.
  • Make entity-collection load() in client-web-kit never reject; emit a new failed event instead, preventing SSR process crashes from unhandled rejections.
  • Widen core-http-kit ClientOptions to hapic’s ClientOptionsInput to expose the transport seam.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
packages/core-http-kit/src/client/type.ts Widen ClientOptions to support custom transports.
packages/client-web-kit/test/unit/components/workflows/login-form.spec.ts Add regression test ensuring login form renders when IDP load fails.
packages/client-web-kit/src/components/utility/entity/collection/utils/component.ts Add failed to collection component emit validators.
packages/client-web-kit/src/components/utility/entity/collection/types.ts Extend collection emits typing with failed(error).
packages/client-web-kit/src/components/utility/entity/collection/module.ts Catch load errors and emit failed instead of rejecting.
apps/server-core/test/unit/http/controllers/workflows/ui-pages-internal-client.spec.ts Integration coverage for default internal-loopback UI client behavior.
apps/server-core/test/unit/adapters/http/ui/internal-http-client.spec.ts Unit coverage for public→internal URL rewriting rules.
apps/server-core/test/app/factory.ts Add config override hook to test application factory.
apps/server-core/src/app/modules/http/module.ts Register transient default UI HTTP client during HTTP module setup; unregister on teardown.
apps/server-core/src/app/modules/http/constants.ts Update UI HTTP client token docs to reflect production default registration.
apps/server-core/src/adapters/http/ui/types.ts Add context type for internal UI HTTP client creation.
apps/server-core/src/adapters/http/ui/internal-http-client.ts Implement internal UI HTTP client + URL rewriter and transport-level dispatch rewrite.
apps/server-core/src/adapters/http/ui/index.ts Export internal UI HTTP client utilities.
apps/server-core/src/adapters/http/middleware/built-in/ui-http-client.ts Update middleware docs to match new default client wiring.
.agents/testing.md Document default UI client wiring and new internal-client integration suite.
.agents/architecture.md Document SSR self-call internal client behavior and SSR crash hardening.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +24 to +28
// a wildcard listen address (0.0.0.0 / [::]) is not reliably dialable —
// loop back explicitly
if (internalParsed.hostname === '0.0.0.0' || internalParsed.hostname === '[::]') {
internalParsed.hostname = '127.0.0.1';
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified against Node's WHATWG URL implementation: new URL('http://[::]:64331/').hostname returns '[::]' with brackets (the URL Standard serializes IPv6 hosts bracketed in the hostname getter), so the check does match. Legacy url.parse() is where hostname comes back bracket-less. Added an IPv6 wildcard case to the rewriter spec (e33d1df) to pin this behavior.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
apps/server-core/src/app/modules/http/module.ts (1)

90-119: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Untested fallback branch when server.url is unavailable.

The new Client({ baseURL: config.publicUrl }) fallback (Lines 103-114) reproduces exactly the pre-fix behavior this PR is meant to eliminate (round-tripping through the reverse proxy). None of the provided specs exercise this path (e.g. by resolving UIHttpClient before Server is registered). Worth a small unit test asserting the fallback still works when Server isn't yet resolvable, since a regression here would silently reintroduce the original TLS/self-call bug.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/server-core/src/app/modules/http/module.ts` around lines 90 - 119, The
fallback path in registerUIHttpClient is untested: when HTTPInjectionKey.Server
is not yet resolvable and createInternalUIHttpClient is skipped, the code
returns a Client using config.publicUrl. Add a unit test around
registerUIHttpClient / UIHttpClient resolution that forces the Server lookup to
be unavailable, then assert the fallback Client is created and used correctly so
this branch cannot regress.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@apps/server-core/src/app/modules/http/module.ts`:
- Around line 90-119: The fallback path in registerUIHttpClient is untested:
when HTTPInjectionKey.Server is not yet resolvable and
createInternalUIHttpClient is skipped, the code returns a Client using
config.publicUrl. Add a unit test around registerUIHttpClient / UIHttpClient
resolution that forces the Server lookup to be unavailable, then assert the
fallback Client is created and used correctly so this branch cannot regress.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e693e100-2f2c-4893-b967-1bbdec3c0964

📥 Commits

Reviewing files that changed from the base of the PR and between b26a429 and f5bcc05.

📒 Files selected for processing (16)
  • .agents/architecture.md
  • .agents/testing.md
  • apps/server-core/src/adapters/http/middleware/built-in/ui-http-client.ts
  • apps/server-core/src/adapters/http/ui/index.ts
  • apps/server-core/src/adapters/http/ui/internal-http-client.ts
  • apps/server-core/src/adapters/http/ui/types.ts
  • apps/server-core/src/app/modules/http/constants.ts
  • apps/server-core/src/app/modules/http/module.ts
  • apps/server-core/test/app/factory.ts
  • apps/server-core/test/unit/adapters/http/ui/internal-http-client.spec.ts
  • apps/server-core/test/unit/http/controllers/workflows/ui-pages-internal-client.spec.ts
  • packages/client-web-kit/src/components/utility/entity/collection/module.ts
  • packages/client-web-kit/src/components/utility/entity/collection/types.ts
  • packages/client-web-kit/src/components/utility/entity/collection/utils/component.ts
  • packages/client-web-kit/test/unit/components/workflows/login-form.spec.ts
  • packages/core-http-kit/src/client/type.ts

@tada5hi

tada5hi commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator Author

@coderabbitai pause

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown
✅ Action performed

Reviews paused.

…ract

Review feedback (PR #3188):
- add an IPv6 wildcard listen-address case to the rewriter spec — WHATWG
  URL serializes IPv6 hostnames WITH brackets, so the '[::]' check holds
- reword the collection load() comment: some call sites await the promise
  but none reliably catch, which is the actual property the catch protects
- use the isError duck guard from @authup/errors for the caught value
@tada5hi
tada5hi merged commit e57ebc5 into master Jul 6, 2026
7 checks passed
@tada5hi
tada5hi deleted the fix/ssr-internal-http-client branch July 6, 2026 11:23
@github-actions github-actions Bot mentioned this pull request Jul 6, 2026
tada5hi added a commit that referenced this pull request Jul 6, 2026
Integrate the merged rotation feature (#3186, squash) + SSR internal-http-client
(#3188) from master. Conflict resolutions:
- issuers / refresh grant / session-token repository / grant spec: take master's
  version (inlined row-write + review-fix hardening), dropping the now-removed
  persistSessionTokenRow helper
- architecture.md: keep master's issuer-row wording + this branch's Session
  Management API section
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants