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

@ -32,10 +32,11 @@ SITE_TIMEZONE=Europe/Oslo
# Public URLs the browser uses: <URL_SCHEME>://admin.<DOMAIN><URL_PORT>. # Public URLs the browser uses: <URL_SCHEME>://admin.<DOMAIN><URL_PORT>.
# They must match the address bar exactly (SvelteKit's form CSRF check). # They must match the address bar exactly (SvelteKit's form CSRF check).
# Local default: http + :8080. Behind a reverse proxy / TLS on 443, e.g.: # Local default: http + :8080. Behind a reverse proxy / TLS on 443:
# URL_SCHEME=https # URL_SCHEME=https
# URL_PORT= # URL_PORT=:443
# (empty URL_PORT = no port in the URL; leave the line out to get :8080) # (:443 is the https default, so URLs come out without a port. An empty
# value works too; a MISSING URL_PORT means :8080.)
DOMAIN=localhost DOMAIN=localhost
URL_SCHEME=http URL_SCHEME=http
URL_PORT=:8080 URL_PORT=:8080

View file

@ -173,7 +173,7 @@ cd app-public && npm run build
## Environment ## Environment
- `DOMAIN` — base domain for subdomain routing. Defaults to `localhost` for local dev. Set in `docker-compose.yml` or `.env`. - `DOMAIN` — base domain for subdomain routing. Defaults to `localhost` for local dev. Set in `docker-compose.yml` or `.env`.
- `URL_SCHEME` (default `http`), `URL_PORT` (default `:8080`; set it empty for no port) — build every browser-facing URL below as `<scheme>://<sub>.<DOMAIN><port>`. They must match the address bar exactly: a mismatched `ORIGIN` makes SvelteKit reject form POSTs (admin login) with "Cross-site POST form submissions are forbidden". Behind a TLS reverse proxy: `URL_SCHEME=https`, `URL_PORT=`. (podman-compose 1.5 can't do nested `${A:-${B}}` defaults, hence two flat variables.) - `URL_SCHEME` (default `http`), `URL_PORT` (default `:8080` when unset; `:443` or empty for no port) — build every browser-facing URL below as `<scheme>://<sub>.<DOMAIN><port>`. They must match the address bar exactly: a mismatched `ORIGIN` makes SvelteKit reject form POSTs (admin login) with "Cross-site POST form submissions are forbidden". Behind a TLS reverse proxy: `URL_SCHEME=https`, `URL_PORT=:443` (normalised away by `new URL().origin`; deploy panels often can't store an empty value, and a missing one means `:8080`). (podman-compose 1.5 can't do nested `${A:-${B}}` defaults, hence two flat variables.)
- `PUBLIC_POCKETBASE_URL` — public-facing PocketBase URL (`<scheme>://db.${DOMAIN}<port>`). Set in `docker-compose.yml`. - `PUBLIC_POCKETBASE_URL` — public-facing PocketBase URL (`<scheme>://db.${DOMAIN}<port>`). Set in `docker-compose.yml`.
- `POCKETBASE_INTERNAL_URL` — internal Docker network URL (`http://pocketbase:80`). Set in `docker-compose.yml`. - `POCKETBASE_INTERNAL_URL` — internal Docker network URL (`http://pocketbase:80`). Set in `docker-compose.yml`.
- `ORIGIN` — SvelteKit origin for SSR. Differs per app: `<scheme>://admin.${DOMAIN}<port>` (admin), `<scheme>://www.${DOMAIN}<port>` (public). - `ORIGIN` — SvelteKit origin for SSR. Differs per app: `<scheme>://admin.${DOMAIN}<port>` (admin), `<scheme>://www.${DOMAIN}<port>` (public).

View file

@ -43,7 +43,7 @@ Caddy's `.localhost` handling works without DNS or hosts-file changes. Source ch
```sh ```sh
DOMAIN=example.com # → admin.example.com, www.example.com, db.example.com DOMAIN=example.com # → admin.example.com, www.example.com, db.example.com
URL_SCHEME=https URL_SCHEME=https
URL_PORT= # empty: no port in the URL URL_PORT=:443 # default https port → no port in the URLs (don't leave it out: missing = :8080)
CADDY_SCHEME=http # this stack's Caddy stays plain HTTP behind the TLS proxy CADDY_SCHEME=http # this stack's Caddy stays plain HTTP behind the TLS proxy
``` ```

View file

@ -1,6 +1,6 @@
import { env } from '$env/dynamic/public';
import { error, json } from '@sveltejs/kit'; import { error, json } from '@sveltejs/kit';
import type { RequestHandler } from './$types'; import type { RequestHandler } from './$types';
import { pbUrl } from '$lib/pb';
import { import {
dataUriToFile, dataUriToFile,
restorePlaceholders, restorePlaceholders,
@ -41,7 +41,7 @@ export const POST: RequestHandler = async ({ request, locals }) => {
formData.append('alt', filename.replace(/\.[^/.]+$/, '').replace(/[-_]/g, ' ')); formData.append('alt', filename.replace(/\.[^/.]+$/, '').replace(/[-_]/g, ' '));
const record = await locals.pb.collection('media').create(formData); const record = await locals.pb.collection('media').create(formData);
const publicUrl = env.PUBLIC_POCKETBASE_URL; const publicUrl = pbUrl;
const newUrl = `${publicUrl}/api/files/${record.collectionId}/${record.id}/${record.file}`; const newUrl = `${publicUrl}/api/files/${record.collectionId}/${record.id}/${record.file}`;
keyToUrl.set(key, newUrl); keyToUrl.set(key, newUrl);

View file

@ -1,6 +1,6 @@
import { env } from '$env/dynamic/public';
import { error, json } from '@sveltejs/kit'; import { error, json } from '@sveltejs/kit';
import type { RequestHandler } from './$types'; import type { RequestHandler } from './$types';
import { pbUrl } from '$lib/pb';
export const POST: RequestHandler = async ({ request, locals }) => { export const POST: RequestHandler = async ({ request, locals }) => {
const formData = await request.formData(); const formData = await request.formData();
@ -17,7 +17,7 @@ export const POST: RequestHandler = async ({ request, locals }) => {
try { try {
const record = await locals.pb.collection('media').create(uploadForm); const record = await locals.pb.collection('media').create(uploadForm);
const publicUrl = env.PUBLIC_POCKETBASE_URL; const publicUrl = pbUrl;
const url = `${publicUrl}/api/files/${record.collectionId}/${record.id}/${record.file}`; const url = `${publicUrl}/api/files/${record.collectionId}/${record.id}/${record.file}`;
return json({ record, url }); return json({ record, url });
} catch (e) { } catch (e) {

View file

@ -2,11 +2,12 @@
import { env } from '$env/dynamic/public'; import { env } from '$env/dynamic/public';
import { confirmSubmit } from '$lib/admin/confirm'; import { confirmSubmit } from '$lib/admin/confirm';
import Modal from '$lib/admin/Modal.svelte'; import Modal from '$lib/admin/Modal.svelte';
import { baseUrl } from '$shared/url';
let { data, form } = $props(); let { data, form } = $props();
let showCreate = $state(false); let showCreate = $state(false);
const siteUrl = (env.PUBLIC_SITE_URL || '').replace(/\/$/, ''); const siteUrl = baseUrl(env.PUBLIC_SITE_URL);
const publicUrl = (slug: string) => `${siteUrl}/${slug === 'home' ? '' : slug}`; const publicUrl = (slug: string) => `${siteUrl}/${slug === 'home' ? '' : slug}`;
</script> </script>

View file

@ -36,7 +36,9 @@ services:
POCKETBASE_INTERNAL_URL: http://pocketbase:80 POCKETBASE_INTERNAL_URL: http://pocketbase:80
# Browser-facing URLs. They must match the address bar exactly, or # Browser-facing URLs. They must match the address bar exactly, or
# SvelteKit rejects logins/forms ("Cross-site POST form submissions are # SvelteKit rejects logins/forms ("Cross-site POST form submissions are
# forbidden"). Behind a TLS proxy: URL_SCHEME=https and URL_PORT= (empty). # forbidden"). Behind a TLS proxy: URL_SCHEME=https and URL_PORT=:443
# (the default port is dropped, giving https://admin.<domain>). If
# URL_PORT is not set at all it defaults to :8080 (local dev).
PUBLIC_POCKETBASE_URL: ${URL_SCHEME:-http}://db.${DOMAIN:-localhost}${URL_PORT-:8080} PUBLIC_POCKETBASE_URL: ${URL_SCHEME:-http}://db.${DOMAIN:-localhost}${URL_PORT-:8080}
ORIGIN: ${URL_SCHEME:-http}://admin.${DOMAIN:-localhost}${URL_PORT-:8080} ORIGIN: ${URL_SCHEME:-http}://admin.${DOMAIN:-localhost}${URL_PORT-:8080}
PUBLIC_SITE_URL: ${URL_SCHEME:-http}://www.${DOMAIN:-localhost}${URL_PORT-:8080} PUBLIC_SITE_URL: ${URL_SCHEME:-http}://www.${DOMAIN:-localhost}${URL_PORT-:8080}

View file

@ -1,8 +1,9 @@
import PocketBase from 'pocketbase'; import PocketBase from 'pocketbase';
import { env } from '$env/dynamic/public'; import { env } from '$env/dynamic/public';
import { browser } from '$app/environment'; 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) { export function createPocketBase(authCookie?: string, url = pbUrl) {
const pb = new PocketBase(url); const pb = new PocketBase(url);

View file

@ -13,6 +13,7 @@ import {
import { normalizeEmailTemplate } from '../../email/defaults'; import { normalizeEmailTemplate } from '../../email/defaults';
import { renderEmail, sampleEmailContext, type EmailContext, type EmailLine } from '../../email/render'; import { renderEmail, sampleEmailContext, type EmailContext, type EmailLine } from '../../email/render';
import { buildIcs } from './ics'; import { buildIcs } from './ics';
import { baseUrl } from '../../url';
import { sendEmail, type SendResult } from './transport'; import { sendEmail, type SendResult } from './transport';
export { sendEmail, isEmailConfigured, emailSender, type SendResult, type OutgoingEmail } 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. */ /** Public site URL — the admin app's ORIGIN is the admin subdomain, so prefer PUBLIC_SITE_URL. */
export function emailSiteUrl(): string { 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> { 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(/\/+$/, '');
}
}