-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a way to setup CORS origins? Nginx config overwritten by postgrest #2441
Comments
Sure, this shouldn't be too hard to customize. The cors logic is on this file: postgrest/src/PostgREST/Cors.hs Lines 19 to 42 in a41380b
|
Ok thanks, I would need this before going to production, as having open cors in an api with authentication might be a high security risk. I didn't found a way to tell nginx proxy to ignore postgrest cors headers yet, and only use those defined in nginx. I might look into this when I have some time, but maybe other can implement this easily. |
To do this at the proxy level with nginx, you could just use |
Amazing! I will try this thanks. |
I tried: nginx.org/proxy-hide-headers: "Access-Control-Allow-Origin,Access-Control-Allow-Credentials,Access-Control-Allow-Methods,Access-Control-Allow-Headers,Access-Control-Max-Age" But it doesn't seem to be working, maybe I'm missing something. |
Just to give an example to hide the server {
# ...
location /api/ {
default_type application/json;
proxy_hide_header Content-Location;
add_header Content-Location /api/$upstream_http_content_location;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_pass http://postgrest/;
# Hides the header
proxy_hide_header 'Access-Control-Allow-Origin';
# Sets the new value
add_header Access-Control-Allow-Origin https://www.mydomain.com;
}
} |
Yes sorry, it indeed worked adding proxy hide headers thanks all! Closing this. |
In case it's useful to my fellow Gophers... How to do the same thing as above, but using Go rather than Nginx as your reverse proxy: postgrestURL, _ := url.Parse("http://localhost:3000")
postgrestProxy := httputil.NewSingleHostReverseProxy(postgrestURL)
postgrestProxy.ModifyResponse = func(resp *http.Response) error {
resp.Header.Del("Access-Control-Allow-Origin") // Delete too-permissive header coming back from PostgREST
resp.Header.Set("Access-Control-Allow-Origin", "https://www.mydomain.com")
return nil
}
handlePostgrest := http.StripPrefix("/postgrest", postgrestProxy)
// Using gorilla/mux:
r := mux.NewRouter()
r.PathPrefix("/postgrest").Handler(handlePostgrest)
http.Handle("/", r)
// Or without gorilla/mux:
http.Handle("/postgrest", handlePostgrest) |
Hm, since we do modify CORS (doc) I think making it configurable is reasonable. It should be simple to implement too.
By default it's:
Alternatively, this should already be possible with |
@steve-chavez How should we handle the case when an |
@taimoorzaeem According to MDN, you'll need to:
It only returns the value specified by |
@taimoorzaeem Sorry for the late reply here. So as per my understanding, if I also recommend checking the MDN docs that Laurence linked. |
I was trying to setup nginx CORS rules for my project in nginx ingress in kubernetes, until I realized a weird behaviour, and it is that postgrest is always adding the "access-control-allow-origin: *" header. Is there a way to completely disable this? or modify this header? As nginx is returning this header when it shoudn't. It would be nice being able to disable this behaviour, or just being able to modify the origins returned by the access-control-allow-origin header.
The text was updated successfully, but these errors were encountered: