2017-09-05 06:59:51 +00:00
|
|
|
//! This is an example of how you can mix and match the "Truly manual" mode with "Guard".
|
|
|
|
//!
|
|
|
|
//! In this example, you typically have an application wide `Cors` struct except for one specific
|
|
|
|
//! `ping` route that you want to allow all Origins to access.
|
2018-10-31 02:25:10 +00:00
|
|
|
#![feature(proc_macro_hygiene, decl_macro)]
|
2018-11-21 04:17:40 +00:00
|
|
|
use rocket_cors;
|
2017-09-05 06:59:51 +00:00
|
|
|
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
2020-08-11 07:43:51 +00:00
|
|
|
use rocket::http::hyper;
|
2018-02-14 05:22:43 +00:00
|
|
|
use rocket::http::{Header, Method, Status};
|
2017-09-05 06:59:51 +00:00
|
|
|
use rocket::local::Client;
|
2019-03-12 07:05:40 +00:00
|
|
|
use rocket::response::Body;
|
2017-09-05 06:59:51 +00:00
|
|
|
use rocket::response::Responder;
|
2019-03-12 07:05:40 +00:00
|
|
|
use rocket::{get, options, routes};
|
2017-09-05 06:59:51 +00:00
|
|
|
|
2018-12-19 00:29:26 +00:00
|
|
|
use rocket_cors::{AllowedHeaders, AllowedOrigins, CorsOptions, Guard};
|
2017-09-05 06:59:51 +00:00
|
|
|
|
|
|
|
/// The "usual" app route
|
|
|
|
#[get("/")]
|
2018-11-21 04:17:40 +00:00
|
|
|
fn app(cors: Guard<'_>) -> rocket_cors::Responder<'_, &str> {
|
2017-09-05 06:59:51 +00:00
|
|
|
cors.responder("Hello CORS!")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The special "ping" route
|
|
|
|
#[get("/ping")]
|
|
|
|
fn ping<'r>() -> impl Responder<'r> {
|
2018-12-19 00:29:26 +00:00
|
|
|
let cors = cors_options_all().to_cors()?;
|
|
|
|
cors.respond_owned(|guard| guard.responder("Pong!"))
|
2017-09-05 06:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// You need to define an OPTIONS route for preflight checks if you want to use `Cors` struct
|
|
|
|
/// that is not in Rocket's managed state.
|
|
|
|
/// These routes can just return the unit type `()`
|
|
|
|
#[options("/ping")]
|
|
|
|
fn ping_options<'r>() -> impl Responder<'r> {
|
2018-12-19 00:29:26 +00:00
|
|
|
let cors = cors_options_all().to_cors()?;
|
|
|
|
cors.respond_owned(|guard| guard.responder(()))
|
2017-09-05 06:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the "application wide" Cors struct
|
2018-12-19 00:29:26 +00:00
|
|
|
fn cors_options() -> CorsOptions {
|
2019-03-12 07:05:40 +00:00
|
|
|
let allowed_origins = AllowedOrigins::some_exact(&["https://www.acme.com"]);
|
2017-09-05 06:59:51 +00:00
|
|
|
|
|
|
|
// You can also deserialize this
|
2018-12-19 00:29:26 +00:00
|
|
|
rocket_cors::CorsOptions {
|
2019-03-12 07:05:40 +00:00
|
|
|
allowed_origins,
|
2017-09-05 06:59:51 +00:00
|
|
|
allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(),
|
|
|
|
allowed_headers: AllowedHeaders::some(&["Authorization", "Accept"]),
|
|
|
|
allow_credentials: true,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A special struct that allows all origins
|
|
|
|
///
|
|
|
|
/// Note: In your real application, you might want to use something like `lazy_static` to generate
|
|
|
|
/// a `&'static` reference to this instead of creating a new struct on every request.
|
2018-12-19 00:29:26 +00:00
|
|
|
fn cors_options_all() -> CorsOptions {
|
2017-09-05 06:59:51 +00:00
|
|
|
// You can also deserialize this
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rocket() -> rocket::Rocket {
|
|
|
|
rocket::ignite()
|
2018-10-31 02:30:10 +00:00
|
|
|
.mount("/", routes![app, ping, ping_options,])
|
2017-09-05 06:59:51 +00:00
|
|
|
.mount("/", rocket_cors::catch_all_options_routes()) // mount the catch all routes
|
2018-12-19 00:29:26 +00:00
|
|
|
.manage(cors_options().to_cors().expect("Not to fail"))
|
2017-09-05 06:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn smoke_test() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
|
|
|
// `Options` pre-flight checks
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.acme.com").unwrap());
|
2017-09-05 06:59:51 +00:00
|
|
|
let method_header = Header::from(hyper::header::AccessControlRequestMethod(
|
2020-08-11 07:43:51 +00:00
|
|
|
hyper::Method::Get,
|
2017-09-05 06:59:51 +00:00
|
|
|
));
|
2018-10-31 02:30:10 +00:00
|
|
|
let request_headers =
|
|
|
|
hyper::header::AccessControlRequestHeaders(vec![
|
|
|
|
FromStr::from_str("Authorization").unwrap()
|
|
|
|
]);
|
2017-09-05 06:59:51 +00:00
|
|
|
let request_headers = Header::from(request_headers);
|
|
|
|
let req = client
|
|
|
|
.options("/")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
|
|
|
|
|
|
|
let response = req.dispatch();
|
|
|
|
assert!(response.status().class().is_success());
|
|
|
|
|
|
|
|
// "Actual" request
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.acme.com").unwrap());
|
2017-09-05 06:59:51 +00:00
|
|
|
let authorization = Header::new("Authorization", "let me in");
|
|
|
|
let req = client.get("/").header(origin_header).header(authorization);
|
|
|
|
|
|
|
|
let mut response = req.dispatch();
|
|
|
|
assert!(response.status().class().is_success());
|
2019-03-12 07:05:40 +00:00
|
|
|
let body_str = response.body().and_then(Body::into_string);
|
2017-09-05 06:59:51 +00:00
|
|
|
assert_eq!(body_str, Some("Hello CORS!".to_string()));
|
|
|
|
|
|
|
|
let origin_header = response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.expect("to exist");
|
|
|
|
assert_eq!("https://www.acme.com", origin_header);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cors_options_check() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.acme.com").unwrap());
|
2017-09-05 06:59:51 +00:00
|
|
|
let method_header = Header::from(hyper::header::AccessControlRequestMethod(
|
2020-08-11 07:43:51 +00:00
|
|
|
hyper::Method::Get,
|
2017-09-05 06:59:51 +00:00
|
|
|
));
|
2018-10-31 02:30:10 +00:00
|
|
|
let request_headers =
|
|
|
|
hyper::header::AccessControlRequestHeaders(vec![
|
|
|
|
FromStr::from_str("Authorization").unwrap()
|
|
|
|
]);
|
2017-09-05 06:59:51 +00:00
|
|
|
let request_headers = Header::from(request_headers);
|
|
|
|
let req = client
|
|
|
|
.options("/")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
|
|
|
|
|
|
|
let response = req.dispatch();
|
|
|
|
assert!(response.status().class().is_success());
|
|
|
|
|
|
|
|
let origin_header = response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.expect("to exist");
|
|
|
|
assert_eq!("https://www.acme.com", origin_header);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cors_get_check() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.acme.com").unwrap());
|
2017-09-05 06:59:51 +00:00
|
|
|
let authorization = Header::new("Authorization", "let me in");
|
|
|
|
let req = client.get("/").header(origin_header).header(authorization);
|
|
|
|
|
|
|
|
let mut response = req.dispatch();
|
|
|
|
assert!(response.status().class().is_success());
|
2019-03-12 07:05:40 +00:00
|
|
|
let body_str = response.body().and_then(Body::into_string);
|
2017-09-05 06:59:51 +00:00
|
|
|
assert_eq!(body_str, Some("Hello CORS!".to_string()));
|
|
|
|
|
|
|
|
let origin_header = response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.expect("to exist");
|
|
|
|
assert_eq!("https://www.acme.com", origin_header);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This test is to check that non CORS compliant requests to GET should still work. (i.e. curl)
|
|
|
|
#[test]
|
|
|
|
fn cors_get_no_origin() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
|
|
|
let authorization = Header::new("Authorization", "let me in");
|
|
|
|
let req = client.get("/").header(authorization);
|
|
|
|
|
|
|
|
let mut response = req.dispatch();
|
|
|
|
assert!(response.status().class().is_success());
|
2019-03-12 07:05:40 +00:00
|
|
|
let body_str = response.body().and_then(Body::into_string);
|
2017-09-05 06:59:51 +00:00
|
|
|
assert_eq!(body_str, Some("Hello CORS!".to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cors_options_bad_origin() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.bad-origin.com").unwrap());
|
2017-09-05 06:59:51 +00:00
|
|
|
let method_header = Header::from(hyper::header::AccessControlRequestMethod(
|
2020-08-11 07:43:51 +00:00
|
|
|
hyper::Method::Get,
|
2017-09-05 06:59:51 +00:00
|
|
|
));
|
2018-10-31 02:30:10 +00:00
|
|
|
let request_headers =
|
|
|
|
hyper::header::AccessControlRequestHeaders(vec![
|
|
|
|
FromStr::from_str("Authorization").unwrap()
|
|
|
|
]);
|
2017-09-05 06:59:51 +00:00
|
|
|
let request_headers = Header::from(request_headers);
|
|
|
|
let req = client
|
|
|
|
.options("/")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
|
|
|
|
|
|
|
let response = req.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Forbidden);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cors_options_missing_origin() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
|
|
|
let method_header = Header::from(hyper::header::AccessControlRequestMethod(
|
2020-08-11 07:43:51 +00:00
|
|
|
hyper::Method::Get,
|
2017-09-05 06:59:51 +00:00
|
|
|
));
|
2018-10-31 02:30:10 +00:00
|
|
|
let request_headers =
|
|
|
|
hyper::header::AccessControlRequestHeaders(vec![
|
|
|
|
FromStr::from_str("Authorization").unwrap()
|
|
|
|
]);
|
2017-09-05 06:59:51 +00:00
|
|
|
let request_headers = Header::from(request_headers);
|
2018-02-14 05:22:43 +00:00
|
|
|
let req = client
|
|
|
|
.options("/")
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
2017-09-05 06:59:51 +00:00
|
|
|
|
|
|
|
let response = req.dispatch();
|
|
|
|
assert!(response.status().class().is_success());
|
2018-10-31 02:30:10 +00:00
|
|
|
assert!(response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.is_none());
|
2017-09-05 06:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cors_options_bad_request_method() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.acme.com").unwrap());
|
2017-09-05 06:59:51 +00:00
|
|
|
let method_header = Header::from(hyper::header::AccessControlRequestMethod(
|
2020-08-11 07:43:51 +00:00
|
|
|
hyper::Method::Post,
|
2017-09-05 06:59:51 +00:00
|
|
|
));
|
2018-10-31 02:30:10 +00:00
|
|
|
let request_headers =
|
|
|
|
hyper::header::AccessControlRequestHeaders(vec![
|
|
|
|
FromStr::from_str("Authorization").unwrap()
|
|
|
|
]);
|
2017-09-05 06:59:51 +00:00
|
|
|
let request_headers = Header::from(request_headers);
|
|
|
|
let req = client
|
|
|
|
.options("/")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
|
|
|
|
|
|
|
let response = req.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Forbidden);
|
2018-10-31 02:30:10 +00:00
|
|
|
assert!(response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.is_none());
|
2017-09-05 06:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cors_options_bad_request_header() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.acme.com").unwrap());
|
2017-09-05 06:59:51 +00:00
|
|
|
let method_header = Header::from(hyper::header::AccessControlRequestMethod(
|
2020-08-11 07:43:51 +00:00
|
|
|
hyper::Method::Get,
|
2017-09-05 06:59:51 +00:00
|
|
|
));
|
|
|
|
let request_headers =
|
|
|
|
hyper::header::AccessControlRequestHeaders(vec![FromStr::from_str("Foobar").unwrap()]);
|
|
|
|
let request_headers = Header::from(request_headers);
|
|
|
|
let req = client
|
|
|
|
.options("/")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
|
|
|
|
|
|
|
let response = req.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Forbidden);
|
2018-10-31 02:30:10 +00:00
|
|
|
assert!(response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.is_none());
|
2017-09-05 06:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cors_get_bad_origin() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.bad-origin.com").unwrap());
|
2017-09-05 06:59:51 +00:00
|
|
|
let authorization = Header::new("Authorization", "let me in");
|
|
|
|
let req = client.get("/").header(origin_header).header(authorization);
|
|
|
|
|
|
|
|
let response = req.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Forbidden);
|
2018-10-31 02:30:10 +00:00
|
|
|
assert!(response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.is_none());
|
2017-09-05 06:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Tests that the `ping` route accepts other Origins
|
|
|
|
#[test]
|
|
|
|
fn cors_options_ping_check() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.example.com").unwrap());
|
2017-09-05 06:59:51 +00:00
|
|
|
let method_header = Header::from(hyper::header::AccessControlRequestMethod(
|
2020-08-11 07:43:51 +00:00
|
|
|
hyper::Method::Get,
|
2017-09-05 06:59:51 +00:00
|
|
|
));
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let req = client
|
|
|
|
.options("/ping")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(method_header);
|
2017-09-05 06:59:51 +00:00
|
|
|
|
|
|
|
let response = req.dispatch();
|
|
|
|
assert!(response.status().class().is_success());
|
|
|
|
|
|
|
|
let origin_header = response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.expect("to exist");
|
|
|
|
assert_eq!("https://www.example.com", origin_header);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tests that the `ping` route accepts other Origins
|
|
|
|
#[test]
|
|
|
|
fn cors_get_ping_check() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.example.com").unwrap());
|
2017-09-05 06:59:51 +00:00
|
|
|
|
|
|
|
let req = client.get("/ping").header(origin_header);
|
|
|
|
|
|
|
|
let mut response = req.dispatch();
|
|
|
|
assert!(response.status().class().is_success());
|
2019-03-12 07:05:40 +00:00
|
|
|
let body_str = response.body().and_then(Body::into_string);
|
2017-09-05 06:59:51 +00:00
|
|
|
assert_eq!(body_str, Some("Pong!".to_string()));
|
|
|
|
|
|
|
|
let origin_header = response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.expect("to exist");
|
|
|
|
assert_eq!("https://www.example.com", origin_header);
|
|
|
|
}
|