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

cors not adding multiple Access-Control-Allow-Origin entries #172

Closed
udf2457 opened this issue Apr 24, 2024 · 2 comments
Closed

cors not adding multiple Access-Control-Allow-Origin entries #172

udf2457 opened this issue Apr 24, 2024 · 2 comments

Comments

@udf2457
Copy link

udf2457 commented Apr 24, 2024

I am loading cors with the following:

corsAllowedHeaders := "Hx-Current-Url,Hx-Request"
corsAllowedOrgins := "https://one.example.com,https://two.example.com"
cors.New(cors.Options{
		AllowedHeaders: strings.Split(corsAllowedHeaders, ","),
		AllowedOrigins: strings.Split(corsAllowedOrgins, ","),
		AllowedMethods: []string{"GET", "POST", "HEAD", "OPTIONS"},
		Debug:          true,
	})

But the debug output only shows the first URL being added:

Actual response added headers: map[Access-Control-Allow-Origin:[https://one.example.com] Vary:[Origin]]

Am I doing something wrong, or is this a bug ?

@jub0bs
Copy link
Contributor

jub0bs commented Apr 28, 2024

The middleware is working as expected in that respect: according to the Fetch standard, a response to a CORS request should indeed contain at most one Access-Control-Allow-Origin header.

If more than one such header is present in a response, browsers joins their values with the character sequence , ; browsers would essentially interpret

Access-Control-Allow-Origin: https://one.example.com
Access-Control-Allow-Origin: https://two.example.com

as

Access-Control-Allow-Origin: https://one.example.com, https://two.example.com

If that happens, the CORS check is bound to fail, because no valid origin value can contain , ; here is just one example.

@jub0bs
Copy link
Contributor

jub0bs commented May 5, 2024

@udf2457 Has my last comment answer your question? Can we close this issue?

@udf2457 udf2457 closed this as completed May 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants