-
Notifications
You must be signed in to change notification settings - Fork 22.1k
Add basic support for access control headers to ActionDispatch::Static #19135
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although this implementation works, I'm not sure about this interface since Cache-Control is also part of the HTTP headers and the third argument seems redundant. I guess this interface could be justdefault_headers.
|
Ultimately I think it looks good. Let's change some naming so that it's a little more generic. Instead of You can reduce the initialization logic to something like this: I would also change I agree we should deprecate some interfaces here and clean some things up. Unfortunately as I understand, the deadline for getting deprecations in to Rails 5 was Rails 4.2.0 |
ab1bc3f to
68e35a0
Compare
|
Updated the commit as suggested. Thanks @schneems for reviewing! |
|
forgot to add a CHANGELOG entry btw. I'll add it tonight. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merge and reverse_merge are really expensive. Even though this only gets called once on initialization the code in this file is very performance sensitive and I don't want people copying existing style. Can you also factor out the merge?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @schneems, removed the merge call,
68e35a0 to
ef5c266
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this is the right place to define public_file_server. I'm open to suggestions.
ef5c266 to
57e68ad
Compare
|
What are we waiting on for this to go in? Anything a blocker here? |
57e68ad to
582cca3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I needed to use :not_set explicitly due to this change: #20017. I think we should make the index arg a keyword argument so we don't have to care about the order of the args here. Alternatively, we can just merge the static_index config into the public_file_server namespace, which I actually prefer, but I'm open to suggestions.
cc/ @eliotsykes @jonatack
582cca3 to
8914567
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
index can be kwarg too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely. See my comment above about it: #19135 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just sent a separate pull request for this: #20520. Now we need to wait on it to be merged before merging this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment states optional HTTP headers but the headers keyword is required. Perhaps headers: needs a default value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Headers are totally optional. I changed it to use a {} by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great.
This PR will break the interface for other services/gems that hook into it, for instance the Heroku-deflator gem here but seems unavoidable for adding this feature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, I didn't know that ActionDispatch::FileHandler is a public class. I don't think it's ok to break it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did a little investigation but I'm not sure if we should treat ActionDispatch::FileHandler as a public class. It's basically an inner class. Does it still have to be public?
|
@schneems updated the commit. I'm a bit concerned about the arguments of the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to have this as-
config.public_file_server.headers= {'Cache-Control' => #{value}}This would suggest the options being passed to headers have been expanded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated.
|
@yuki24 Thanks a ton for this, I was looking to implement something similar due to this - https://twitter.com/vipulnsward/status/603866969201373184 |
f235974 to
3223d80
Compare
Now ActionDispatch::Static can accept HTTP headers so that developers
will have control of returning arbitrary headers like
'Access-Control-Allow-Origin' when a response is delivered. They can
be configured through `#config.public_file_server.headers`:
config.public_file_server.headers = {
"Cache-Control" => "public, max-age=60",
"Access-Control-Allow-Origin" => "http://rubyonrails.org"
}
Also deprecate `config.static_cache_control` in favor of
`config.public_file_server.headers`.
3223d80 to
5226058
Compare
|
I believe everything should be good and we are ready to merge. |
|
I spoke to @jeremy, I think it just fell off his radar. Can you rebase, this is good to merge. |
|
👍 Looks like a changelog conflict only, no need to rebase. |
Add basic support for access control headers to ActionDispatch::Static
|
@yuki24 nice! Have you sent a pull request to enable |
|
@kaspth Not yet. I'm not sure if I can work on it anytime soon, so feel free to send a PR if you could. |
|
What was the idea? |
As discussed in rails#19135 (comment). Replaces `serve_static_files` to unify the static options under the `public_file_server` wing. Deprecates `serve_static_files` accessors, but make them use the newer config internally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels like this should be in the Railties CHANGELOG file. I will change it at #22173.
It was deprecated in #19135. We're now favoring `public_file_server.headers`.
…ions. Deprecation of static_cache_control: rails/rails#19135 Deprecation of serve_static_files: rails/rails#22173
Now ActionDispatch::Static can accept access control headers such as
Access-Control-Allow-Origin. These headers will be set as well asCache-Controlheader when a response is delivered. Access control headers can be configured with#configinconfig/application.rborconfig/environments/***.rb:cc/ @schneems