2017-07-17 09:36:41 +00:00
|
|
|
//! Fairing implementation
|
2017-07-26 06:37:02 +00:00
|
|
|
|
2018-10-31 02:59:01 +00:00
|
|
|
use log::{error, info, log};
|
2018-11-01 06:44:28 +00:00
|
|
|
use rocket::http::{self, uri::Origin, Status};
|
2018-10-31 02:59:01 +00:00
|
|
|
use rocket::{self, error_, info_, log_, Outcome, Request};
|
2017-07-17 09:36:41 +00:00
|
|
|
|
2018-10-31 02:30:58 +00:00
|
|
|
use crate::{
|
|
|
|
actual_request_response, origin, preflight_response, request_headers, validate, Cors, Error,
|
|
|
|
};
|
2017-07-17 09:36:41 +00:00
|
|
|
|
2018-11-01 06:44:28 +00:00
|
|
|
/// Request Local State to store CORS validation results
|
|
|
|
enum CorsValidation {
|
2017-07-19 01:51:31 +00:00
|
|
|
Success,
|
|
|
|
Failure,
|
|
|
|
}
|
|
|
|
|
2017-07-17 09:36:41 +00:00
|
|
|
/// Route for Fairing error handling
|
|
|
|
pub(crate) fn fairing_error_route<'r>(
|
|
|
|
request: &'r Request,
|
|
|
|
_: rocket::Data,
|
|
|
|
) -> rocket::handler::Outcome<'r> {
|
2018-10-31 02:30:10 +00:00
|
|
|
let status = request
|
|
|
|
.get_param::<u16>(0)
|
|
|
|
.unwrap_or(Ok(0))
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
error_!("Fairing Error Handling Route error: {:?}", e);
|
|
|
|
500
|
|
|
|
});
|
2017-07-17 09:36:41 +00:00
|
|
|
let status = Status::from_code(status).unwrap_or_else(|| Status::InternalServerError);
|
|
|
|
Outcome::Failure(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a new `Route` for Fairing handling
|
2018-02-14 06:21:50 +00:00
|
|
|
fn fairing_route(rank: isize) -> rocket::Route {
|
|
|
|
rocket::Route::ranked(rank, http::Method::Get, "/<status>", fairing_error_route)
|
2017-07-17 09:36:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Modifies a `Request` to route to Fairing error handler
|
|
|
|
fn route_to_fairing_error_handler(options: &Cors, status: u16, request: &mut Request) {
|
2018-10-31 02:25:10 +00:00
|
|
|
let origin = Origin::parse_owned(format!("{}/{}", options.fairing_route_base, status)).unwrap();
|
|
|
|
|
2017-07-17 09:36:41 +00:00
|
|
|
request.set_method(http::Method::Get);
|
2018-10-31 02:25:10 +00:00
|
|
|
request.set_uri(origin);
|
2017-07-17 09:36:41 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 11:19:51 +00:00
|
|
|
fn on_response_wrapper(
|
|
|
|
options: &Cors,
|
|
|
|
request: &Request,
|
|
|
|
response: &mut rocket::Response,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let origin = match origin(request)? {
|
|
|
|
None => {
|
|
|
|
// Not a CORS request
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
Some(origin) => origin,
|
|
|
|
};
|
|
|
|
|
2018-11-01 06:44:28 +00:00
|
|
|
let result = request.local_cache(|| unreachable!("This should not be executed so late"));
|
2017-07-19 01:51:31 +00:00
|
|
|
|
2018-11-01 06:44:28 +00:00
|
|
|
if let &CorsValidation::Failure = result {
|
2017-07-19 01:51:31 +00:00
|
|
|
// Nothing else for us to do
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2017-07-17 11:19:51 +00:00
|
|
|
let cors_response = if request.method() == http::Method::Options {
|
|
|
|
let headers = request_headers(request)?;
|
2017-07-26 06:37:02 +00:00
|
|
|
preflight_response(options, &origin, headers.as_ref())
|
2017-07-17 11:19:51 +00:00
|
|
|
} else {
|
2017-07-26 06:37:02 +00:00
|
|
|
actual_request_response(options, &origin)
|
2017-07-17 11:19:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
cors_response.merge(response);
|
|
|
|
|
|
|
|
// If this was an OPTIONS request and no route can be found, we should turn this
|
|
|
|
// into a HTTP 204 with no content body.
|
|
|
|
// This allows the user to not have to specify an OPTIONS route for everything.
|
|
|
|
//
|
|
|
|
// TODO: Is there anyway we can make this smarter? Only modify status codes for
|
|
|
|
// requests where an actual route exist?
|
2018-02-14 05:22:43 +00:00
|
|
|
if request.method() == http::Method::Options && request.route().is_none() {
|
2017-07-24 05:55:35 +00:00
|
|
|
info_!(
|
|
|
|
"CORS Fairing: Turned missing route {} into an OPTIONS pre-flight request",
|
|
|
|
request
|
|
|
|
);
|
2017-07-17 11:19:51 +00:00
|
|
|
response.set_status(Status::NoContent);
|
|
|
|
let _ = response.take_body();
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-07-17 09:36:41 +00:00
|
|
|
impl rocket::fairing::Fairing for Cors {
|
|
|
|
fn info(&self) -> rocket::fairing::Info {
|
|
|
|
rocket::fairing::Info {
|
|
|
|
name: "CORS",
|
2018-07-18 05:26:33 +00:00
|
|
|
kind: rocket::fairing::Kind::Attach
|
|
|
|
| rocket::fairing::Kind::Request
|
2018-02-14 05:22:43 +00:00
|
|
|
| rocket::fairing::Kind::Response,
|
2017-07-17 09:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_attach(&self, rocket: rocket::Rocket) -> Result<rocket::Rocket, rocket::Rocket> {
|
|
|
|
match self.validate() {
|
2018-02-14 06:21:50 +00:00
|
|
|
Ok(()) => Ok(rocket.mount(
|
|
|
|
&self.fairing_route_base,
|
|
|
|
vec![fairing_route(self.fairing_route_rank)],
|
|
|
|
)),
|
2017-07-17 09:36:41 +00:00
|
|
|
Err(e) => {
|
|
|
|
error_!("Error attaching CORS fairing: {}", e);
|
|
|
|
Err(rocket)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_request(&self, request: &mut Request, _: &rocket::Data) {
|
2018-11-01 06:44:28 +00:00
|
|
|
let result = match validate(self, request) {
|
|
|
|
Ok(_) => CorsValidation::Success,
|
2017-07-19 01:51:31 +00:00
|
|
|
Err(err) => {
|
|
|
|
error_!("CORS Error: {}", err);
|
|
|
|
let status = err.status();
|
|
|
|
route_to_fairing_error_handler(self, status.code, request);
|
2018-11-01 06:44:28 +00:00
|
|
|
CorsValidation::Failure
|
2017-07-19 01:51:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-01 06:44:28 +00:00
|
|
|
let _ = request.local_cache(|| result);
|
2017-07-17 09:36:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn on_response(&self, request: &Request, response: &mut rocket::Response) {
|
2017-07-17 11:19:51 +00:00
|
|
|
if let Err(err) = on_response_wrapper(self, request, response) {
|
|
|
|
error_!("Fairings on_response error: {}\nMost likely a bug", err);
|
|
|
|
response.set_status(Status::InternalServerError);
|
|
|
|
let _ = response.take_body();
|
2017-07-17 09:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-18 05:11:30 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use rocket::http::{Method, Status};
|
|
|
|
use rocket::local::Client;
|
2018-07-18 05:26:33 +00:00
|
|
|
use rocket::Rocket;
|
2017-07-18 05:11:30 +00:00
|
|
|
|
2018-10-31 02:30:58 +00:00
|
|
|
use crate::{AllOrSome, AllowedHeaders, AllowedOrigins, Cors};
|
2017-07-18 05:11:30 +00:00
|
|
|
|
|
|
|
const CORS_ROOT: &'static str = "/my_cors";
|
|
|
|
|
|
|
|
fn make_cors_options() -> Cors {
|
2017-07-19 04:25:56 +00:00
|
|
|
let (allowed_origins, failed_origins) = AllowedOrigins::some(&["https://www.acme.com"]);
|
2017-07-18 05:11:30 +00:00
|
|
|
assert!(failed_origins.is_empty());
|
|
|
|
|
|
|
|
Cors {
|
|
|
|
allowed_origins: allowed_origins,
|
|
|
|
allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(),
|
2017-07-19 04:25:56 +00:00
|
|
|
allowed_headers: AllowedHeaders::some(&["Authorization", "Accept"]),
|
2017-07-18 05:11:30 +00:00
|
|
|
allow_credentials: true,
|
|
|
|
fairing_route_base: CORS_ROOT.to_string(),
|
|
|
|
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rocket(fairing: Cors) -> Rocket {
|
|
|
|
Rocket::ignite().attach(fairing)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fairing_error_route_returns_passed_in_status() {
|
|
|
|
let client = Client::new(rocket(make_cors_options())).expect("to not fail");
|
|
|
|
let request = client.get(format!("{}/403", CORS_ROOT));
|
|
|
|
let response = request.dispatch();
|
|
|
|
assert_eq!(Status::Forbidden, response.status());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fairing_error_route_returns_500_for_unknown_status() {
|
|
|
|
let client = Client::new(rocket(make_cors_options())).expect("to not fail");
|
|
|
|
let request = client.get(format!("{}/999", CORS_ROOT));
|
|
|
|
let response = request.dispatch();
|
|
|
|
assert_eq!(Status::InternalServerError, response.status());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn error_route_is_mounted_on_attach() {
|
|
|
|
let rocket = rocket(make_cors_options());
|
|
|
|
|
|
|
|
let expected_uri = format!("{}/<status>", CORS_ROOT);
|
2018-02-14 05:22:43 +00:00
|
|
|
let error_route = rocket
|
|
|
|
.routes()
|
2018-10-31 02:25:10 +00:00
|
|
|
.find(|r| r.method == Method::Get && r.uri.to_string() == expected_uri);
|
2017-07-18 05:11:30 +00:00
|
|
|
assert!(error_route.is_some());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic(expected = "launch fairing failure")]
|
|
|
|
fn options_are_validated_on_attach() {
|
|
|
|
let mut options = make_cors_options();
|
|
|
|
options.allowed_origins = AllOrSome::All;
|
|
|
|
options.send_wildcard = true;
|
|
|
|
|
|
|
|
let _ = rocket(options).launch();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rest of the things can only be tested in integration tests
|
|
|
|
}
|