-
Notifications
You must be signed in to change notification settings - Fork 171
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
Add Cors
for setting CORS headers
#112
Conversation
I've had a look and one thing I'm missing is a little more flexibility for origins. .allowed_origin_fn(|origin, _req_head| {
origin.as_bytes().ends_with(b".rust-lang.org")
}) and I've previously done a very similar thing with that crate, with an additional |
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've had a look and one thing I'm missing is a little more flexibility for origins.
actix_cors
allows you to simply provide a closure to check origins which aren't in the allow list. In the crate docs this is part of their example:.allowed_origin_fn(|origin, _req_head| { origin.as_bytes().ends_with(b".rust-lang.org") })and I've previously done a very similar thing with that crate, with an additional
origin.starts_with(b"https://")
check.
Hmm, yeah, I wonder if what we want is a trait for allowed origin predicates that can be implemented by closures and by T: Into<AnyOr<Origin>>
?
I wonder if it would make sense to implement the CORS logic as an OTTOH, I'm not sure how much value there is from this — most of the meat is in the |
I like that idea! I'll experiment. |
Another thing to consider is that since parts of this code is copied from tide, we have to include their license somewhere. |
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.
Adding my comments from Discord so I can add follow-ups given the time
Using Axum for a project at the moment and in need of a CORS layer, any idea when this will be ready? |
I'm gonna look into this once axum 0.2 is released, which will happen this week. I can't give an ETA on merging this however. Until then you can apply the CORS headers manually and add a catch-all route for OPTIONS requests. |
Ended up quickly forking this branch as I needed the ability to allow multiple origins which was difficult to do with manually injected headers. With these changes it seems to be working nicely. |
a93fee4
to
050d055
Compare
Believe I've addresses all the comments now. Thanks for the feedback! Allow-Origin can be now determined via a closure, similar to actix-cors: use tower_http::cors::{CorsLayer, Any};
use http::{HeaderValue, request::Parts};
let layer = CorsLayer::new().allow_origin(|origin: &HeaderValue, _request_head: &Parts| {
origin.as_bytes().ends_with(b".rust-lang.org")
}); @jplatte @hawkw @fourbytes @nkconnor Wanna give this another look? |
lg2m - thanks! |
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.
Doesn't seem to be working for me yet due to an issue with the valid method check. Other than that though, API looks great.
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.
One thing this still doesn't allow which actix-cors allows is
Cors::ctor()
.allowed_origin("http://127.0.0.1")
.allowed_origin("foo.bar.org")
.allowed_origin_fn(|origin, _| {
let origin = origin.as_bytes();
origin.starts_with(b"https://") && origin.ends_with(b".rust-lang.org")
})
which also means that it's impossible with the current code structure for clients to discover some of the origins that would be allowed to use a route, at least when using a closure (it will look like no origins are allowed when doing the request from a disallowed origin). I think what actix-cors does is send all origins that are specified explicitly regardless of whether they match, and additionally send the request origin back in the header if the function marks it as allowed.
I don't think it would be a problem for me, but I thought I'd still bring it up.
Hm thats a good point. I was wondering why actix that it setup the way they did. Although I do think its unfortunate to have to store both a list a allowed origins, and a closure to call if nothing else matches 🤔
Would be an issue for me either. I'm leaning towards keeping it as is 🤷 |
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.
We're interested in having cors for our ongoing migration to axum.
I've put some minor comments
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.
Small suggestions to improve efficiency and performance
Hey guys, is there any plan when it will be merged? |
I basically just have to clean up the docs and then it should be good to go. Should happen somewhat soon but dunno exactly when. I recommend going with a git dependency on tower-http for now. |
Cors
for settings CORS headersCors
for setting CORS headers
- New middleware: Add `Cors` for setting [CORS] headers ([#112]) - New middleware: Add `AsyncRequireAuthorization` ([#118]) - `Compression`: Don't recompress HTTP responses ([#140]) - `Compression` and `Decompression`: Pass configuration from layer into middleware ([#132]) - `ServeDir` and `ServeFile`: Improve performance ([#137]) - `Compression`: Remove needless `ResBody::Error: Into<BoxError>` bounds ([#117]) - `ServeDir`: Percent decode path segments ([#129]) - `ServeDir`: Use correct redirection status ([#130]) - `ServeDir`: Return `404 Not Found` on requests to directories if `append_index_html_on_directories` is set to `false` ([#122]) [#112]: #112 [#118]: #118 [#140]: #140 [#132]: #132 [#137]: #137 [#117]: #117 [#129]: #129 [#130]: #130 [#122]: #122
- New middleware: Add `Cors` for setting [CORS] headers ([#112]) - New middleware: Add `AsyncRequireAuthorization` ([#118]) - `Compression`: Don't recompress HTTP responses ([#140]) - `Compression` and `Decompression`: Pass configuration from layer into middleware ([#132]) - `ServeDir` and `ServeFile`: Improve performance ([#137]) - `Compression`: Remove needless `ResBody::Error: Into<BoxError>` bounds ([#117]) - `ServeDir`: Percent decode path segments ([#129]) - `ServeDir`: Use correct redirection status ([#130]) - `ServeDir`: Return `404 Not Found` on requests to directories if `append_index_html_on_directories` is set to `false` ([#122]) [#112]: #112 [#118]: #118 [#140]: #140 [#132]: #132 [#137]: #137 [#117]: #117 [#129]: #129 [#130]: #130 [#122]: #122
- New middleware: Add `Cors` for setting [CORS] headers ([#112]) - New middleware: Add `AsyncRequireAuthorization` ([#118]) - `Compression`: Don't recompress HTTP responses ([#140]) - `Compression` and `Decompression`: Pass configuration from layer into middleware ([#132]) - `ServeDir` and `ServeFile`: Improve performance ([#137]) - `Compression`: Remove needless `ResBody::Error: Into<BoxError>` bounds ([#117]) - `ServeDir`: Percent decode path segments ([#129]) - `ServeDir`: Use correct redirection status ([#130]) - `ServeDir`: Return `404 Not Found` on requests to directories if `append_index_html_on_directories` is set to `false` ([#122]) [#112]: #112 [#118]: #118 [#140]: #140 [#132]: #132 [#137]: #137 [#117]: #117 [#129]: #129 [#130]: #130 [#122]: #122
- New middleware: Add `Cors` for setting [CORS] headers ([#112]) - New middleware: Add `AsyncRequireAuthorization` ([#118]) - `Compression`: Don't recompress HTTP responses ([#140]) - `Compression` and `Decompression`: Pass configuration from layer into middleware ([#132]) - `ServeDir` and `ServeFile`: Improve performance ([#137]) - `Compression`: Remove needless `ResBody::Error: Into<BoxError>` bounds ([#117]) - `ServeDir`: Percent decode path segments ([#129]) - `ServeDir`: Use correct redirection status ([#130]) - `ServeDir`: Return `404 Not Found` on requests to directories if `append_index_html_on_directories` is set to `false` ([#122]) [#112]: #112 [#118]: #118 [#140]: #140 [#132]: #132 [#137]: #137 [#117]: #117 [#129]: #129 [#130]: #130 [#122]: #122
Example usage:
Implementation is based on
CorsMiddleware
from tide.