Cross-site POST form submissions are forbidden? #7600
Replies: 24 comments 16 replies
|
I absolutely have the same problem! On Postman all GET requests work fine. Trying POST requests and I also got Someone please help us ! :) |
|
Yeah, this issue is also biting me up while am on remote platform gitpod.io , this is actually due to cross-site origin policy , I have also not got any permanent soluton for now, but I guess you can keep origin check false for now while you are in dev environment and turn on while building the app , This seem untidy but I have been doing this :( ! |
|
I just had the same issue, running SvelteKit behind Traefik reverse proxy, that does the TLS/SSL termination. Not recommended (but easy) solution from StackOverflow: For reference, another issue about this topic: #6589 |
|
For reference: #8314. This happens workspaces like Gitpod or StackBlitz where you access your preview on a non-localhost URL. |
|
It can happen when your urls point to the wrong port. It's the external port you need. |
|
After new update, get constantly |
|
as far as I can tell the solution is to disable csrf which seems like a non-solution. any ideas from dev team on an actual solution? I run a lot of sveltekit apps on a caddy reverse_proxy setup and this is kinda silly there's not a better solution.... |
|
@Rich-Harris hi could you help us? Thanks for the best framework |
|
It works for node deployments etcetra, to get arround this will using the dev server you need to modify the headers somewhat. here is a simple solution to that. Anyone have improvements I could make to this? //vite.config.ts
export default defineConfig({
plugins: [
{
name: "configure-response-headers",
configureServer: (server) => {
server.middlewares.use((_req, res, next) => {
const ori = _req.headers.origin
const referer = _req.headers.referer
if((ori == "https://example.com"|| !ori)){
_req.headers.origin = "http://localhost:5173"
}
next();
// _req.headers.origin = originalOrigin
});
},
},
sveltekit(),
]
}); |
|
same problem happened to me. turns out it was for some headers i've set in the hooks.server.ts // response.headers.set('Referrer-Policy', 'no-referrer'); // this one breaks form submission
// response.headers.set('Referrer-Policy', 'origin-when-cross-origin');
response.headers.set('Referrer-Policy', 'strict-origin-when-cross-origin'); // default, works |
|
Hope this helps |
|
HI got this svelte.config.js: That works absolutely fine |
|
I overwrote the checkorigin code by basically copying it from the svelte
kit source code and adding my own parts to it...
…On Mon, 22 Apr 2024 at 22:26, Maximilian Kürschner ***@***.***> wrote:
I think you should not set checkOrigin to false. Especially not for your
production app, as it is a protection against CSRF attacks. Did you try
setting the ORIGIN environment variable before starting your server like
this: ORIGIN=http://localhost:3000 node build/index.js
You can also check my blog post on the topic here
<https://www.programonaut.com/cross-site-post-form-submissions-are-forbidden-in-sveltekit/>
—
Reply to this email directly, view it on GitHub
<#7600 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AE7H37OSN26BZ4ATAL4W4SDY6VXARAVCNFSM6AAAAAAR466XCGVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TCOJTGQZTE>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
|
ORIGIN env variable only works while building
…On Thu, 25 Apr 2024 at 09:52, Julian Hammer ***@***.***> wrote:
I know, and I tried to set it to the ORIGIN but it still didn't work.
I put the ORIGIN into my .env do I need to do something else with it?
And how do I set the ORIGIN if the domain is https://realgolf.games and I
have a costume node server at server/app.js?
—
Reply to this email directly, view it on GitHub
<#7600 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AE7H37OQU5VOBFJHL5LLYZDY7CY3TAVCNFSM6AAAAAAR466XCGVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4TEMRRHE2DO>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
|
I don't think it's a good design to pre-set the ORIGIN in the environment variables. The value of ORIGIN is determined by the deployment environment and can be obtained for each request, as the request contains the current protocol, domain name, and port number. When a browser sends a request to the server, it also carries the ORIGIN in the header. Comparing these two values can determine whether they are same-origin. Having worked in Java development for many years, I find it odd that the ORIGIN needs to be set in environment variables in advance in a SvelteKit project... |
|
I have had the same issue, I've solved it with the following steps, keeping csfr protection enabled: Setting the correct origin address was definitely required (ORIGIN has to be the same address I use in my browser to access the server via nginx): Enable csfr protection (nice to have for security): Add the following nginx header to my nginx.conf (I don't know which of the headers, if any, are actually required): |
|
I'm having same issue when trying to implement sign in with apple which redirects me to their login form and sends a form post back to my redirect URL but the page just shows "Cross-site POST form submissions are forbidden" I mean idk that I'll even use the apple id sign in or not but this did come up as hurdle for me in trying |
|
Still struggling with the same issue. I use I added this to my handler: console.log(`ORIGIN: ${process.env.ORIGIN}`)
console.log(`URL: ${request.url}`)
console.log(request.headers)If I access my endpoint (I redacted a few sensitive headers) But as soon as I switch the method to POST we are back to: The behaviour is the same when I manually set my What gives? EDIT: |
|
I have added ORIGIN env var in docker environment, added all of those to nginx reverse proxy: But still getting "Cross-site POST form submissions are forbidden" when simply submitting a simple form via use:enhance to an action. I am using adapter bun btw. |
|
Since this remains unanswered, I had to disable this feature and write a custom solution: // CSRF protection
if (request.method !== "GET" && request.method !== "HEAD") {
const origin = request.headers.get("Origin");
const publicUrl = process.env.PUBLIC_URL
if (publicUrl === null || publicUrl === undefined || publicUrl === "" || origin !== publicUrl) {
return json({ error: "Origin not allowed. Was PUBLIC_URL set correctly in the frontend?" }, { status: 403 });
}
} |
|
I've had the same issue and after some investigation and the comments above thank God the following works:
Note the For Report back with what you find. |
|
I've tried all of the previous suggestions and none of them work. I'm using |
|
I've run into the same problem but was able to solve it. Both in dev and production. This is my configuration: env.ts .env in my nginx config |
|
Hi folks. I'm wondering if the new |

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hey. I've got a pretty fresh SvelteKit installation. I'm trying to set up a login form action. When I submit the form I get the error:
Cross-site POST form submissions are forbiddenReading up on it it seems like it could be that the ORIGIN env var isn't set (but I have set it, and it matches the origin that my requests are coming from).
If I turn of origin checking in the config then it works, but this doesn't seem like a good idea. I'd like to get it working securely if possible.
Here's my
+page.svelete(I've simplified it by removing the components that I'm using for labels and errors etc.):And here's my
+page.server.jsfile:I'm hoping these are pretty standard? Nothing odd about them? (I'm new to SvelteKit).
In my attempts to debug I added some console debugging to the
respondfunction in SvelteKit'ssrc/runtime/server/index.jsfile. For a single form submission (my browser shows only one network request)respondis called twice. The first timerequest.headers.get('origin')is set to the origin I expect. The second time it isnull. Does that help?Thanks all!
EDIT: Silly mistakes, not importing things, etc.
All reactions