Skip to content
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

feat(http): add flag/config to allow CORS requests, close #229 #293

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Kocal
Copy link
Contributor

@Kocal Kocal commented Mar 15, 2023

Hi everyone, this PR is a proposal for #229.

A new flag --allow-cors has been added to server:start command, and to config http.allow_cors in .symfony.local.yaml.

When allowing CORS, the header Access-Control-Allow-Origin: * is added to each response.

image

image

Is there a way to add automated tests for that?

Thanks!

@tucksaun
Copy link
Contributor

Is there a way to add automated tests for that?

you should be able to write a functional test as you would in Go but I believe we don't have a test case available to get you inspired

@tucksaun
Copy link
Contributor

@Kocal did you get a change to have a look for a test?

@Kocal
Copy link
Contributor Author

Kocal commented Apr 19, 2023

@tucksaun not at all, I can try to take a look in the next days

@tucksaun
Copy link
Contributor

@Kocal I guess we are good, it's not worst than the actual situation :)

Copy link
Contributor

@tucksaun tucksaun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check for preflight requests as well?

@@ -212,6 +213,10 @@ func (s *Server) ProxyHandler(w http.ResponseWriter, r *http.Request) {

// Handler handles HTTP requests
func (s *Server) Handler(w http.ResponseWriter, r *http.Request) {
if s.AllowCORS {
w.Header().Add("Access-Control-Allow-Origin", "*")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we should not add default CORS values (see what Gorillw used to have: https://github.com/gorilla/handlers/blob/546854cf1d61a016260b224e2526a29ff5b5c5ae/cors.go#L28-L32)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also be careful not to override header sent by the upstream app

@sarim
Copy link
Contributor

sarim commented Jan 4, 2024

I use nelmio/cors-bundle for this and it works perfectly for api-platform for a long time now. composer require nelmio/cors-bundle and CORS_ALLOW_ORIGIN=* would accomplish the same with better control.

What is the use case for this feature? For all application request I'd rather let the application handle the headers. This would be useful only for static files? Though we normally don't load static files over XHR/fetch. I think this should only be added to headers if symfony-cli is serving static file. If servering a request via php, let php handle the headers.

@tucksaun
Copy link
Contributor

Serving static files is one use case indeed.
Usually you don't load them via XHR except when this is a JS framework loading some templates (or similar use cases).

But you can also imagine running a JS development server alongside the CLI and both can be on different domain/port and this causes issues. But only in dev, so you might not want to (or not allowed to) introduce nelmio cors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants