svelte-adapter-bun: "Cross-site POST form submissions are forbidden"

Just encountered this error while testing my website in production.

Setup: I’m using docker with nginx proxy manager as a reverse proxy.

So i was building my multi-container through this docker-compose.yml

version: '3.9'
services:
  db:
    restart: 'always'
    build: './pocketbase'
    expose:
      - 80
    ports:
      - ${PORTDB:-8081}:80
    environment:
      FRONTEND_URL: http://web:3001
    volumes:
      - dbdata:/pb/pb_data
      - ./pocketbase/pb_migrations:/pb/pb_migrations
      - ./pocketbase/pb_hooks:/pb/pb_hooks
    networks:
      - nginx-network
  web:
    restart: 'always'
    build: 
      context: './web'
      dockerfile: 'Dockerfile'
      args:
        PUBLIC_DATABASE: ${PUBLIC_DATABASE:-https://api.domain.tld/sub}
    ports:
      - ${PORTWEB:-8080}:3001
    expose:
      - 3001
    environment:
      PORT: 3001
      PUBLIC_DATABASE: ${PUBLIC_DATABASE:-https://api.domain.tld/sub}
      ORIGIN: https://sub.domain.tld
    networks:
      - nginx-network

volumes:
  dbdata:

networks:
  nginx-network:
    name: nginx-proxy-manager_default
    external: true

With my web Dockerfile looking like this:

FROM oven/bun as builder

ARG PUBLIC_DATABASE
ENV PUBLIC_DATABASE=${PUBLIC_DATABASE}

WORKDIR /app

COPY . .
ENV ORIGIN=https://sub.domain.tld

RUN bun i
RUN bun run build

FROM oven/bun
COPY --from=builder /app/build .

ENV ORIGIN=https://sub.domain.tld

EXPOSE 3001

CMD [ "bun", "index.js" ]

I know that the ORIGIN env is set multiple times just wanted to check that it wasn’t on the wrong spot.

I was also printing the url.origin in the load function in +layout.server.ts which resulted in the following output: http://sub.domain.tld

Running bun --print process.env also tried with (Bun.env) resulted in:

{
  ...,
  ORIGIN: "https://sub.domain.tld",
  ...,
}

What i discovered was that when i didnt enforce https with my reverse proxy and use the website with “http://” it just works fine but that isnt the way i think

Hope somebody can help fixing this monster that cost me more then 2 hours of trying and failing If you need some more infos just ask for it

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 5
  • Comments: 17

Most upvoted comments

bump this issue still persists, might be related or fixable py this pr: #61

Setting both PROTOCOL_HEADER=x-forwarded-proto and HOST_HEADER=x-forwarded-host env vars is what fixed it for me

Please fix this issue, it’s still blocking our production release.

My setup is a bit more complex but I observed something so maybe it can help solve this issue.

I’m deploying behind Traefik to a subdomain + path (e.g. https://subdomain.example.com/project). On the /project route there is a login SvelteKit action. This is where the CSRF issue arises.

I’m also using paraglide for i18n which runs in a hook or rather sequence of hooks sequence(originalHandle, authGuard, handleParaglide). Maybe the sequence causes the issue in my case. But maybe not.

Couldn’t fix it with adapter-bun - neither ORIGIN, nor PROTOCOL_HEADER + HOST_HEADER (and optionally PORT_HEADER=x-forwarded-port) worked. I see in the client request that origin (lowercase) is set correctly, but none of the other headers exist.

Switched back to adapter-node and it works out of the box now (I’m not even setting ORIGIN).

Now I see following x-headers when logging the reguest in the action (=server-side) using adapter-node:

    'x-forwarded-for': '94.xxx.xxx.xxx',
    'x-forwarded-host': 'subdomain.example.com',
    'x-forwarded-port': '443',
    'x-forwarded-proto': 'https',
    'x-forwarded-server': 'a-12-characters-hash',
    'x-real-ip': '94.xxx.xxx.xxx',
    'x-sveltekit-action': 'true'

What is it with x-forwarded-for and x-forwarded-server? Maybe these should be set as ENV vars as well?

I have the same issue, currently not smart enough to fix this with a pr 😓 Has #61 fixed this issue? I see its merged, But if so can a new release be build?