crystal icon indicating copy to clipboard operation
crystal copied to clipboard

Cross Origin Requests

Open tim-field opened this issue 10 years ago • 34 comments

Thank you for writing this.

I'm wondering if postgraphql could enable cross origin requests by default. As it's not currently possible to talk to it from a Relay application without setting up a proxy or the like.

I see this ( accidental ) pull request. https://github.com/calebmer/postgraphql/pull/79/commits/4211c773da0d83e244a4e55804f94e93e9630362

Wondering if this would be a good approach to take

tim-field avatar Jul 14 '16 09:07 tim-field

I think it would be a useful feature to have cross origin support built in so that you dont also need to have a proxy for it. Rather than being enabled by default perhaps it would be better to have an option/cli flag to enable it so that people aren't exposing that without knowing it?

pvdvreede avatar Jul 14 '16 23:07 pvdvreede

Any plan when this will be merged and released?

d-winter avatar Jul 27 '16 09:07 d-winter

@d-winter I was waiting for a LGTM, I’ll merge :wink:

calebmer avatar Jul 27 '16 11:07 calebmer

Released in 1.8.0 👍

calebmer avatar Jul 27 '16 11:07 calebmer

@calebmer awesome timing 👍 Thanks a lot!

d-winter avatar Jul 27 '16 11:07 d-winter

Just looked into this for my needs so sharing the results ...

While this feature seems like a good idea at first (no proxy needed), you will add a network overhead on each request. The first overhead is that you will need those headers on each request (not so bad). The second overhead is that you will have preflight request for EVERY request.
Caching preflight requests does not really work http://stackoverflow.com/questions/12013216/how-to-apply-cors-preflight-cache-to-an-entire-domain

Do a bit more work and add the proxy (a real one like nginx, not a nodejs script) and you will make your app faster.

ruslantalpa avatar Aug 29 '16 07:08 ruslantalpa

@ruslantalpa does this change with HTTP2 and/or hosting on the same domain?

Unless preflight requests seriously slow down request performance, I’m comfortable with leaving CORS on by default to simplify the beginner experience. But that’s a good optimization to be aware of, thanks so much 👍

calebmer avatar Aug 29 '16 13:08 calebmer

I don't know about the http2 Same domain means no need for CORS ... so no preflight (OPTIONS) requests, which means the headers are not needed at all and they just add (unused) bytes to traffic. About slowdown .. yes they do, it's all nice when it's on localhost and the OPTIONS takes 5-10ms, but move this to production over https and suddenly a requests that would take 100ms in normal conditions, turns into 2 sequential requests, each taking 100ms so in total 200ms. Mix in Relay with it's preference for splitting requests into multiple requests to node endpoint and suddenly you have a flood.

ruslantalpa avatar Aug 29 '16 15:08 ruslantalpa

For anyone else who struggles to understand... the CORS headers are now disabled by default, and enabled on the command line by --cors.

estaub avatar Mar 10 '17 02:03 estaub

Hi @estaub can you please tell me the command, for how to enable cors

DevPrasan1 avatar Jul 21 '17 12:07 DevPrasan1

https://github.com/postgraphql/postgraphql/blob/master/docs/cli.md

postgraphql --cors -c postgres://...

benjie avatar Jul 21 '17 13:07 benjie

Since I use expressjs as my custom server, so I just add npm install cors --save as a middleware to expressjs and done.

Yup it is for more control such as whitelist domains.

pencilcheck avatar Aug 05 '17 00:08 pencilcheck

You can just use "enableCors" from the library settings (it's what "--cors" maps to) but using the cors middleware will give you more control 👍

benjie avatar Aug 05 '17 08:08 benjie

I'm trying to use it in Docker, and behind traefik in order to setup a domain name and manage ssl, but it can only be accessed though its IP, I'm using --cors and -n 0.0.0.0 and I can't access it though a reverse proxy :(

celvin avatar Jun 13 '18 01:06 celvin

Could you post more details? What command are you running, what’s the error you’re getting, how is your reverse proxy configured, etc etc?

benjie avatar Jun 13 '18 05:06 benjie

Thanks a lot for your time, I have uploaded the testing environment to github, if you can check it out, I would be really grateful.

https://github.com/celvin/postgraphile-docker

I'm using traefik as a reverse proxy in order to use domain names to access different containers mapping their ports

Basically you just need to run docker-compose up --build and then you can navigate to the traefik dashboard to see the deployment details:

http://localdev.zlivio.com

and the postgraphile service (it's already routed to /graphiql) is using:

http://pgpgraph-local.zlivio.com/

and to enter to the graphile container:

docker exec -it postgraphile-nexxtel bash

celvin avatar Jun 14 '18 03:06 celvin

I can't spin your env up right now, but what's the exact error you're getting?

benjie avatar Jun 14 '18 07:06 benjie

There is no error in the log, and when I navigate directly to the container's IP, it works

but when I try to get it through a domain name managed by traefik (reverse proxy) it fails under timeout message.

If I post another node app in the same container it works well, so I think must be something related with allowing Cross Origin Requests or not

celvin avatar Jun 14 '18 07:06 celvin

So a timeout implies it's not a CORS issue (those errors are explicit and typically immediate); sounds like it's a connectivity issue in your setup.

benjie avatar Jun 14 '18 08:06 benjie

Mmm everything is possible, the thing is the same settings even the same container running another app works well through traefik but postgraphile doesn't...

celvin avatar Jun 14 '18 08:06 celvin