Production URLs: recommend URL_PORT=:443, strip default ports

A missing URL_PORT defaults to :8080, and deploy panels often can't store
an empty value, so production kept ORIGIN=https://admin.<domain>:8080 and
the admin login still failed with "Cross-site POST form submissions are
forbidden" (reproduced against production). URL_PORT=:443 is non-empty
and adapter-node normalises it away (new URL().origin).

The PUBLIC_* URLs are used verbatim in saved image URLs, email links and
the admin's View link, so they now go through baseUrl(), which drops
default ports and trailing slashes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KgF3Kf47LjPGHKQHszpUAv
This commit is contained in:
agent 2026-10-07 14:56:53 +00:00
parent 7b6c8c2511
commit bf4c619074
10 changed files with 34 additions and 13 deletions

View file

@ -1,8 +1,9 @@
import PocketBase from 'pocketbase';
import { env } from '$env/dynamic/public';
import { browser } from '$app/environment';
import { baseUrl } from './url';
export const pbUrl = env.PUBLIC_POCKETBASE_URL;
export const pbUrl = baseUrl(env.PUBLIC_POCKETBASE_URL);
export function createPocketBase(authCookie?: string, url = pbUrl) {
const pb = new PocketBase(url);

View file

@ -13,6 +13,7 @@ import {
import { normalizeEmailTemplate } from '../../email/defaults';
import { renderEmail, sampleEmailContext, type EmailContext, type EmailLine } from '../../email/render';
import { buildIcs } from './ics';
import { baseUrl } from '../../url';
import { sendEmail, type SendResult } from './transport';
export { sendEmail, isEmailConfigured, emailSender, type SendResult, type OutgoingEmail } from './transport';
@ -58,7 +59,7 @@ async function getGlobalData<T extends object>(pb: PocketBase, key: string, fall
/** Public site URL — the admin app's ORIGIN is the admin subdomain, so prefer PUBLIC_SITE_URL. */
export function emailSiteUrl(): string {
return (publicEnv.PUBLIC_SITE_URL || env.ORIGIN || '').replace(/\/$/, '');
return baseUrl(publicEnv.PUBLIC_SITE_URL || env.ORIGIN);
}
export async function loadEmailSettings(pb: PocketBase): Promise<EmailSettings> {

View file

@ -0,0 +1,15 @@
/**
* A configured base URL without a trailing slash or a default port, e.g.
* "https://db.example.com:443/" → "https://db.example.com". docker-compose
* builds these from URL_SCHEME/URL_PORT, and URL_PORT=:443 is the usual
* production value; the URLs end up in saved page data and emails.
*/
export function baseUrl(value: string | undefined | null): string {
const raw = String(value ?? '').trim();
try {
const url = new URL(raw);
return (url.origin + url.pathname).replace(/\/+$/, '');
} catch {
return raw.replace(/\/+$/, '');
}
}