Skip to content

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

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

Merged
merged 2 commits into from
Dec 9, 2024

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 follows headers are added to each response:

  • Access-Control-Allow-Origin: *
  • Access-Control-Allow-Methods: *
  • Access-Control-Allow-Header: *

image

image

Is there a way to add automated tests for that?

Thanks!

#SymfonyHackday

@tucksaun
Copy link
Member

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
Member

@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
Member

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

Copy link
Member

@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
Member

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
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it fine like this? I don't really know Go :D

	if s.AllowCORS {
		if w.Header().Get("Access-Control-Allow-Origin") != "" {
			w.Header().Add("Access-Control-Allow-Origin", "*")
		}
	}

Copy link
Member

Choose a reason for hiding this comment

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

no this would not work because at this stage the header will never be already defined because the upstream server has not been contacted yet (contacting the upstream server is done in s.Callback)

@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
Member

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.

@broncha
Copy link

broncha commented Sep 16, 2024

I just had a need for this as I am serving a static json file from public folder and its not handled by nelmio/cors-bundle

@Kocal
Copy link
Contributor Author

Kocal commented Sep 18, 2024

PR rebased and conflicts fixed, I will see to add a test if I can

@Kocal
Copy link
Contributor Author

Kocal commented Sep 22, 2024

I think I will need some help to finalize this PR (fix the "add header" logic and tests).
If someone wants to help me (or re-open a new open if it's easier).. thanks :)

@tucksaun
Copy link
Member

I think I will need some help to finalize this PR (fix the "add header" logic and tests). If someone wants to help me (or re-open a new open if it's easier).. thanks :)

If you happen to be at the Forum PHP in 15 days we can catch-up there and have a look :)

@Kocal
Copy link
Contributor Author

Kocal commented Sep 25, 2024

I think I will need some help to finalize this PR (fix the "add header" logic and tests). If someone wants to help me (or re-open a new open if it's easier).. thanks :)

If you happen to be at the Forum PHP in 15 days we can catch-up there and have a look :)

@tucksaun I will! :D

@@ -52,6 +52,7 @@ import (

var localWebServerProdWarningMsg = "The local web server is optimized for local development and MUST never be used in a production setup."
var localWebServerTlsKeyLogWarningMsg = "Logging TLS master key is enabled. It means TLS connections between the client and this server will be INSECURE. This is NOT recommended unless you are debugging the connections."
var localWebServerAllowCorsLogWarningMsg = "Cross-origin resource sharing (CORS) is enabled for every requests.\nYou may want to use https://github.com/nelmio/NelmioCorsBundle to have more control on HTTP headers."
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe add another sentence informing user that if they rely on this behavior to make their application work, it won't work in production server oob.

@Kocal Kocal marked this pull request as draft October 11, 2024 09:37
@tucksaun
Copy link
Member

@Kocal after a week thinking about our peer-programing session, I stand by what I was saying before leaving the Forum PHP: I believe we can accept your PR where the CLI prepends the headers and ignore the case where the application also add some headers. If this happens then I think this is the user’s responsibility to decide between disabling CORS headers addition in their app or in the CLI.

what we might want to do, just to be clear for the user, is add a warning if we detect such scenario but that’s it.

@Kocal
Copy link
Contributor Author

Kocal commented Oct 25, 2024

Hey @tucksaun, sorry for the late reply, I've been quite busy those last weeks :)

That's fine to me, a warning should be enough, I'll check it out as soon as I can!

PS: it was a real pleasure to meet you at the ForumPHP :D

@tucksaun
Copy link
Member

Nice! Let me know if you need some help to finalize it.

PS: shared pleasure :)

@rustymcfly
Copy link

Please, Merge this and create a release! This is the Feature i am waiting for :)

@Kocal
Copy link
Contributor Author

Kocal commented Dec 5, 2024

Ah! My bad, I didn't find the time to complete this PR, I just needed to commit and push :)

It's should be ok now, cc @tucksaun :)

@Kocal Kocal marked this pull request as ready for review December 5, 2024 14:17
Copy link
Contributor Author

@Kocal Kocal left a comment

Choose a reason for hiding this comment

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

Thanks @tucksaun for the last modifications 😍

@tucksaun tucksaun requested a review from fabpot December 7, 2024 11:18
@tucksaun tucksaun requested a review from fabpot December 9, 2024 11:53
@fabpot fabpot merged commit c91767a into symfony-cli:main Dec 9, 2024
1 check passed
@Kocal
Copy link
Contributor Author

Kocal commented Dec 9, 2024

We dit it 🙌🏻

@tucksaun
Copy link
Member

tucksaun commented Dec 9, 2024

Thank you @Kocal for keeping up the effort for a year and half ! 🥳

@Kocal Kocal deleted the feat/229-cors branch December 9, 2024 20:23
@Kocal
Copy link
Contributor Author

Kocal commented Dec 9, 2024

Thank you for our pair-programming sessions and your patience 🙏🏻

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.

6 participants