Files
vikingowl 95adabf435
ci/someci/push/web Pipeline was successful
fix(web): honor X-Forwarded-Proto so event.url.origin is https
@deno/svelte-adapter 0.1.1 passes req straight to SvelteKit without any
proxy-aware rewriting (the Node adapter does this via ORIGIN env or
PROTOCOL_HEADER). Behind itsh.dev's nginx-gateway TLS terminates and the
pod receives plain http, so:

  req.url           = http://marktvogt.de/<path>
  event.url.origin  = http://marktvogt.de
  event.fetch() sets Origin: http://marktvogt.de on outbound calls

Backend CORS allowlists only https://marktvogt.de — every /auth/refresh
POST from the web pod was 403'd, breaking session refresh on prod.

Rewrap baseHandler in serve.ts: rewrite the request URL's scheme to
match X-Forwarded-Proto before forwarding. Comma-split handles the
'proto1,proto2' form some proxies emit.
2026-05-16 04:33:30 +02:00

32 lines
1.3 KiB
TypeScript

import rawDeployConfig from './.deno-deploy/deploy.json' with { type: 'json' };
import rawSvelteData from './.deno-deploy/svelte.json' with { type: 'json' };
import { prepareServer } from './.deno-deploy/handler.ts';
const port = Number(Deno.env.get('PORT') ?? 8000);
const hostname = Deno.env.get('HOST') ?? '0.0.0.0';
const baseHandler = prepareServer(rawSvelteData, rawDeployConfig, Deno.cwd());
// Behind a TLS-terminating gateway the inbound request to the pod is plain
// HTTP, so req.url has scheme http and event.url.origin = http://marktvogt.de.
// SvelteKit's event.fetch then sets Origin: http://... on outbound calls,
// which the backend's CORS allowlist rejects.
//
// @deno/svelte-adapter 0.1.1 doesn't honor ORIGIN env or X-Forwarded-Proto on
// its own (unlike adapter-node), so we rewrite the request scheme here based
// on the trusted X-Forwarded-Proto header from the cluster ingress.
const handler: Deno.ServeHandler = (req, info) => {
const forwardedProto = req.headers.get('x-forwarded-proto');
if (forwardedProto) {
const url = new URL(req.url);
const scheme = forwardedProto.split(',')[0].trim();
if (scheme && `${scheme}:` !== url.protocol) {
url.protocol = `${scheme}:`;
req = new Request(url.toString(), req);
}
}
return baseHandler(req, info);
};
Deno.serve({ port, hostname }, handler);