#nextjs server actions allowed origins
1 messages · Page 1 of 1 (latest)
In your case, where you have a SaaS application that works with many custom domains, one custom domain per customer, you might face challenges with the allowedOrigins configuration for Server Actions in Next.js. This configuration is used to specify a list of safe origin domains from which Server Actions can be invoked, as a measure against CSRF attacks.
However, as you've noted, it's not feasible to keep adding domains to the config and rebuild the project whenever a new domain is assigned by a user.
Unfortunately, based on the provided knowledge sources, there isn't a direct solution to handle this scenario. The allowedOrigins configuration does support wildcards (e.g., *.mydomain.com), but this only works for subdomains, not for entirely different domains.
One potential workaround could be to use a reverse proxy, as suggested in one of the GitHub comments. This would involve setting the x-forwarded-host to your main domain, which is included in the allowedOrigins configuration. However, this might not be a suitable solution depending on your application's architecture and security requirements.
Here's an example of how you might set this up in an nginx configuration:
server {
server_name <DOMAIN_HERE>;
location / {
proxy_set_header x-forwarded-host <DOMAIN_HERE>;
proxy_pass <http://localhost:<PORT_HOSTED_AT>;>
}
}
In this configuration, <DOMAIN_HERE> would be your main domain (abc.main.com), and <PORT_HOSTED_AT> would be the port your application is hosted at.
Please note that this is a workaround and might not cover all use cases or meet all security requirements. It's recommended to thoroughly test this setup and consult with a security expert.
Sources:
- GitHub Issue Comment (https://github.com/vercel/next.js/issues/58019)
- Next.js Server Actions Documentation (https://nextjs.org/docs/app/api-reference/next-config-js/serverActions)
- Vercel Custom Domains Documentation (https://vercel.com/docs/projects/overview#custom-domains)
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@fresh compass kapa.ai is still learning and improving, please let us know how it did by reacting below
@forest kraken i already use
proxy_set_header X-Forwarded-Host abc.main.com
but get the following
x-forwarded-host` header with value `lisa-ai-git-add-pwainstallprompt-ankit-deploy-lu-lisa.vercel.app` does not match `origin` header with value `letsupgrade.io` from a forwarded Server Actions request. Aborting the action.
Let me check the knowledge base for you. One moment! :books:
The error message you're seeing indicates that the x-forwarded-host header value (lisa-ai-git-add-pwainstallprompt-ankit-deploy-lu-lisa.vercel.app) does not match the origin header value (letsupgrade.io). This is a security feature in Next.js to prevent Cross-Site Request Forgery (CSRF) attacks.
If you're using the proxy_set_header X-Forwarded-Host abc.main.com directive in your nginx configuration, it should set the x-forwarded-host header to abc.main.com for all requests. However, the error message indicates that the x-forwarded-host header is set to a different value (lisa-ai-git-add-pwainstallprompt-ankit-deploy-lu-lisa.vercel.app).
This discrepancy could be due to several reasons:
- There might be another proxy or middleware that's modifying the
x-forwarded-hostheader after nginx sets it. - The nginx configuration might not be correctly applied or reloaded after changes.
- There might be a specific route or location block in your nginx configuration that's not correctly setting the
x-forwarded-hostheader.
Without more information about your setup, it's hard to pinpoint the exact issue. I would recommend checking your nginx configuration and any other proxies or middleware in your setup to ensure they're correctly setting the x-forwarded-host header.
Also, make sure that your allowedOrigins configuration in Next.js includes the correct domains. In your case, it should include abc.main.com.
Sources:
- GitHub Issue Comment (https://github.com/vercel/next.js/issues/58295)
- Next.js Server Actions Documentation (https://nextjs.org/docs/app/api-reference/next-config-js/serverActions)
- Nginx Proxy Module Documentation (http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header)