mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-23 15:51:06 +01:00
allow OPTIONS requests without request-method header (#226)
This commit is contained in:
parent
0ba1073cb2
commit
6fbe2eab94
@ -1,6 +1,9 @@
|
||||
# Changes
|
||||
|
||||
## Unreleased - 2021-xx-xx
|
||||
- Do not consider requests without a `Access-Control-Request-Method` as preflight. [#226]
|
||||
|
||||
[#226]: https://github.com/actix/actix-extras/pull/226
|
||||
|
||||
|
||||
## 0.6.0 - 2022-02-25
|
||||
|
@ -17,7 +17,6 @@ name = "actix_cors"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
actix-service = "2"
|
||||
actix-utils = "3"
|
||||
actix-web = { version = "4", default-features = false }
|
||||
|
||||
|
@ -33,7 +33,7 @@ impl fmt::Debug for OriginFn {
|
||||
}
|
||||
|
||||
/// Try to parse header value as HTTP method.
|
||||
fn header_value_try_into_method(hdr: &HeaderValue) -> Option<Method> {
|
||||
pub(crate) fn header_value_try_into_method(hdr: &HeaderValue) -> Option<Method> {
|
||||
hdr.to_str()
|
||||
.ok()
|
||||
.and_then(|meth| Method::try_from(meth).ok())
|
||||
@ -141,7 +141,7 @@ impl Inner {
|
||||
// method invalid
|
||||
Some(_) => Err(CorsError::BadRequestMethod),
|
||||
|
||||
// method missing
|
||||
// method missing so this is not a preflight request
|
||||
None => Err(CorsError::MissingRequestMethod),
|
||||
}
|
||||
}
|
||||
@ -277,7 +277,7 @@ mod test {
|
||||
assert!(cors.inner.validate_allowed_method(req.head()).is_err());
|
||||
assert!(cors.inner.validate_allowed_headers(req.head()).is_err());
|
||||
let resp = test::call_service(&cors, req).await;
|
||||
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
let req = TestRequest::default()
|
||||
.method(Method::OPTIONS)
|
||||
|
@ -3,7 +3,7 @@ use std::{collections::HashSet, rc::Rc};
|
||||
use actix_utils::future::ok;
|
||||
use actix_web::{
|
||||
body::{EitherBody, MessageBody},
|
||||
dev::{Service, ServiceRequest, ServiceResponse},
|
||||
dev::{forward_ready, Service, ServiceRequest, ServiceResponse},
|
||||
http::{
|
||||
header::{self, HeaderValue},
|
||||
Method,
|
||||
@ -13,7 +13,11 @@ use actix_web::{
|
||||
use futures_util::future::{FutureExt as _, LocalBoxFuture};
|
||||
use log::debug;
|
||||
|
||||
use crate::{builder::intersperse_header_values, inner::add_vary_header, AllOrSome, Inner};
|
||||
use crate::{
|
||||
builder::intersperse_header_values,
|
||||
inner::{add_vary_header, header_value_try_into_method},
|
||||
AllOrSome, Inner,
|
||||
};
|
||||
|
||||
/// Service wrapper for Cross-Origin Resource Sharing support.
|
||||
///
|
||||
@ -27,6 +31,25 @@ pub struct CorsMiddleware<S> {
|
||||
}
|
||||
|
||||
impl<S> CorsMiddleware<S> {
|
||||
fn is_request_preflight(req: &ServiceRequest) -> bool {
|
||||
// check request method is OPTIONS
|
||||
if req.method() != Method::OPTIONS {
|
||||
return false;
|
||||
}
|
||||
|
||||
// check follow-up request method is present and valid
|
||||
if req
|
||||
.headers()
|
||||
.get(header::ACCESS_CONTROL_REQUEST_METHOD)
|
||||
.and_then(header_value_try_into_method)
|
||||
.is_none()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
fn handle_preflight(inner: &Inner, req: ServiceRequest) -> ServiceResponse {
|
||||
if let Err(err) = inner
|
||||
.validate_origin(req.head())
|
||||
@ -136,14 +159,15 @@ where
|
||||
type Error = Error;
|
||||
type Future = LocalBoxFuture<'static, Result<ServiceResponse<EitherBody<B>>, Error>>;
|
||||
|
||||
actix_service::forward_ready!(service);
|
||||
forward_ready!(service);
|
||||
|
||||
fn call(&self, req: ServiceRequest) -> Self::Future {
|
||||
if self.inner.preflight && req.method() == Method::OPTIONS {
|
||||
if self.inner.preflight && Self::is_request_preflight(&req) {
|
||||
let inner = Rc::clone(&self.inner);
|
||||
let res = Self::handle_preflight(&inner, req);
|
||||
ok(res.map_into_right_body()).boxed_local()
|
||||
} else {
|
||||
return ok(res.map_into_right_body()).boxed_local();
|
||||
}
|
||||
|
||||
let origin = req.headers().get(header::ORIGIN).cloned();
|
||||
|
||||
if origin.is_some() {
|
||||
@ -164,7 +188,6 @@ where
|
||||
}
|
||||
.boxed_local()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -1,5 +1,5 @@
|
||||
use actix_service::fn_service;
|
||||
use actix_utils::future::ok;
|
||||
use actix_web::dev::fn_service;
|
||||
use actix_web::{
|
||||
dev::{ServiceRequest, Transform},
|
||||
http::{
|
||||
|
Loading…
Reference in New Issue
Block a user