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

Rack::Cors modifies headers to be case-sensitive, producing duplicated headers #218

Closed
dgmora opened this issue Jan 26, 2021 · 1 comment

Comments

@dgmora
Copy link

dgmora commented Jan 26, 2021

Similarly to #162, I was getting duplicated headers with Rack::Cors. Not only with Access-Control-Allow-Origin, but also with other headers. The issue is that here Rack::Cors merges the existing headers into add_headers here:

rack-cors/lib/rack/cors.rb

Lines 103 to 106 in 908ea29

status, headers, body = @app.call env
if add_headers
headers = add_headers.merge(headers)

This is a problem because headers is usually a Rack::Utils::HeaderHash, which is a case-insensitive hash. By merging it into a regular hash, the headers are now case-sensitive, allowing duplicates if they have different cases.

I think this could be solved by making this hash a HeaderHash instead of a normal hash:

def to_headers(env)
h = {
'Access-Control-Allow-Origin' => origin_for_response_header(env[Rack::Cors::HTTP_ORIGIN]),
'Access-Control-Allow-Methods' => methods.collect { |m| m.to_s.upcase }.join(', '),
'Access-Control-Expose-Headers' => expose.nil? ? '' : expose.join(', '),
'Access-Control-Max-Age' => max_age.to_s
}
h['Access-Control-Allow-Credentials'] = 'true' if credentials
h
end

@cyu
Copy link
Owner

cyu commented Sep 11, 2022

Fixed in [862a776]

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