I have a SvelteKit app running at http://localhost:5173
(when doing local development) with a Nginx in front of it running at http://localhost:8057
, serving some static files and proxying requests that must be handled by SvelteKit.
I have a login form at http://localhost:8057/login
and when I submit it I get a web page with just "Cross-site POST form submissions are forbidden".
I tried using the ORIGIN
environment variable as per Cross-site POST form submissions are forbidden and https://kit.svelte.dev/docs/adapter-node#environment-variables. Here is my .env
file:
ORIGIN=http://localhost:8057
I can see this environment variable in my environment if I log env
from import { env } from '$env/dynamic/private';
in the server side. But I still get "Cross-site POST form submissions are forbidden".
I also tried passing the environement variable when starting SvelteKit (with ORIGIN=http://localhost:8057 npm run dev
), but same result.
Because SvelteKit does not log much when it sends this error I can just try to guess what's going on inside SvelteKit, but if in Nginx I add proxy_set_header Origin http://localhost:5173;
then it works, so it's pretty clear what origin SvelteKit expects here. I'd rather not do this however because it is pretty much equivalent to disabling CSRF protection.
I then tried to use the other method suggested in https://kit.svelte.dev/docs/adapter-node#environment-variables, that is, to use x-forwarded-proto
and x-forwarded-host
, so now here is my .env
file (and again I can definitely see these values if I log them in my app):
PROTOCOL_HEADER=x-forwarded-proto
HOST_HEADER=x-forwarded-host
ORIGIN=http://localhost:8057
and in my Nginx configuration (the same place where replacing the Origin header would fix everything):
location @sveltekit {
proxy_pass http://localhost:5173;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
}
And I still get "Cross-site POST form submissions are forbidden". Any idea what else I could try?
In case people want to see or run the code:
- https://gitlab.com/cedricvanrompay/mazette/-/commit/bfa59261b8e5e5c95ac0bec37524a878cd815e91
docker-compose up --build
to launch Nginxcd web-app; npm run dev
to run the SvelteKit app (may require anpm install
first)- on Mac you will have to revert this commit due to docker host network mode not working on Mac: https://gitlab.com/cedricvanrompay/mazette/-/commit/fee6fcd753b2844de307feb8acefd2898bb6efcd