v0.34.2
This commit is contained in:
1 parent
34fb534e02
commit
b2b5b45d8a
22 files changed
+2146
-107
No files matched your search
@@ -21,7 +21,8 @@ use tokio::io::{AsyncReadExt, AsyncSeekExt};
|
||||
use tokio_util::io::ReaderStream;
|
||||
|
||||
pub(super) fn status_response(status: StatusCode, detail: &'static str) -> Response {
|
||||
// Keep internal diagnostics in server logs, not in public HTTP bodies.
|
||||
// Keep implementation diagnostics in logs; use the existing embedded
|
||||
// application-style pages for every Web Server-generated error.
|
||||
if status.is_server_error() {
|
||||
tracing::warn!(
|
||||
status = status.as_u16(),
|
||||
@@ -29,46 +30,46 @@ pub(super) fn status_response(status: StatusCode, detail: &'static str) -> Respo
|
||||
"Web Server error"
|
||||
);
|
||||
}
|
||||
let message = format!(
|
||||
"{} {}\n",
|
||||
status.as_u16(),
|
||||
status.canonical_reason().unwrap_or("Error")
|
||||
);
|
||||
let mut response = Response::new(Body::from(message.clone()));
|
||||
*response.status_mut() = status;
|
||||
response.headers_mut().insert(
|
||||
CONTENT_TYPE,
|
||||
HeaderValue::from_static("text/plain; charset=utf-8"),
|
||||
);
|
||||
response.headers_mut().insert(
|
||||
CONTENT_LENGTH,
|
||||
HeaderValue::from_str(&message.len().to_string()).unwrap(),
|
||||
);
|
||||
response
|
||||
.headers_mut()
|
||||
.insert(CACHE_CONTROL, HeaderValue::from_static("no-store"));
|
||||
response.headers_mut().insert(
|
||||
"x-content-type-options",
|
||||
HeaderValue::from_static("nosniff"),
|
||||
);
|
||||
response
|
||||
// The public serve() facade strips bodies from HEAD responses while
|
||||
// retaining the same Content-Length as the corresponding GET.
|
||||
builtin_error_response(status, &Method::GET)
|
||||
}
|
||||
|
||||
pub(super) fn builtin_error_response(status: StatusCode, method: &Method) -> Response {
|
||||
let html = crate::pages::builtin_gateway_error(status)
|
||||
.unwrap_or("<!doctype html><html><body><h1>Error</h1></body></html>");
|
||||
let mut response = Response::new(if *method == Method::HEAD {
|
||||
Body::empty()
|
||||
} else {
|
||||
Body::from(html)
|
||||
});
|
||||
// Reuse the bundled pages wherever one exists. For statuses such as 405,
|
||||
// use the shared styled application fallback instead of a plain-text or
|
||||
// unstyled generic error. Do not mark these as PageServed so normal Vhost
|
||||
// error-page policy can still replace them.
|
||||
let (body, length) = match crate::pages::builtin_gateway_error(status) {
|
||||
Some(html) => (
|
||||
if *method == Method::HEAD {
|
||||
Body::empty()
|
||||
} else {
|
||||
Body::from(html)
|
||||
},
|
||||
html.len(),
|
||||
),
|
||||
None => {
|
||||
let html = crate::pages::builtin_application_page(status);
|
||||
let length = html.len();
|
||||
(
|
||||
if *method == Method::HEAD {
|
||||
Body::empty()
|
||||
} else {
|
||||
Body::from(html)
|
||||
},
|
||||
length,
|
||||
)
|
||||
}
|
||||
};
|
||||
let mut response = Response::new(body);
|
||||
*response.status_mut() = status;
|
||||
response.headers_mut().insert(
|
||||
CONTENT_TYPE,
|
||||
HeaderValue::from_static("text/html; charset=utf-8"),
|
||||
);
|
||||
if let Ok(length) = HeaderValue::from_str(&html.len().to_string()) {
|
||||
response.headers_mut().insert(CONTENT_LENGTH, length);
|
||||
if let Ok(value) = HeaderValue::from_str(&length.to_string()) {
|
||||
response.headers_mut().insert(CONTENT_LENGTH, value);
|
||||
}
|
||||
response
|
||||
.headers_mut()
|
||||
|
||||
+99
-26
@@ -119,10 +119,13 @@ async fn missing_files_and_disabled_directory_listing_return_normal_errors() {
|
||||
.await;
|
||||
assert_eq!(missing.status(), StatusCode::NOT_FOUND);
|
||||
assert_eq!(missing.headers()[CACHE_CONTROL], "no-store");
|
||||
assert_eq!(missing.headers()[CONTENT_TYPE], "text/plain; charset=utf-8");
|
||||
assert_eq!(missing.headers()[CONTENT_TYPE], "text/html; charset=utf-8");
|
||||
assert_eq!(missing.headers()["x-content-type-options"], "nosniff");
|
||||
assert_eq!(
|
||||
missing.into_body().collect().await.unwrap().to_bytes(),
|
||||
"404 Not Found\n"
|
||||
missing.into_body().collect().await.unwrap().to_bytes().as_ref(),
|
||||
crate::pages::builtin_gateway_error(StatusCode::NOT_FOUND)
|
||||
.unwrap()
|
||||
.as_bytes()
|
||||
);
|
||||
|
||||
let missing_head = serve_request(
|
||||
@@ -132,7 +135,14 @@ async fn missing_files_and_disabled_directory_listing_return_normal_errors() {
|
||||
)
|
||||
.await;
|
||||
assert_eq!(missing_head.status(), StatusCode::NOT_FOUND);
|
||||
assert_eq!(missing_head.headers()[CONTENT_LENGTH], "14");
|
||||
let missing_length = crate::pages::builtin_gateway_error(StatusCode::NOT_FOUND)
|
||||
.unwrap()
|
||||
.len()
|
||||
.to_string();
|
||||
assert_eq!(
|
||||
missing_head.headers()[CONTENT_LENGTH],
|
||||
HeaderValue::from_str(&missing_length).unwrap()
|
||||
);
|
||||
assert!(missing_head
|
||||
.into_body()
|
||||
.collect()
|
||||
@@ -171,42 +181,105 @@ async fn missing_files_and_disabled_directory_listing_return_normal_errors() {
|
||||
.await;
|
||||
assert_eq!(method.status(), StatusCode::METHOD_NOT_ALLOWED);
|
||||
assert_eq!(method.headers()[ALLOW], "GET, HEAD");
|
||||
assert_eq!(
|
||||
method.into_body().collect().await.unwrap().to_bytes(),
|
||||
"405 Method Not Allowed\n"
|
||||
);
|
||||
assert_eq!(method.headers()[CONTENT_TYPE], "text/html; charset=utf-8");
|
||||
assert_eq!(method.headers()[CACHE_CONTROL], "no-store");
|
||||
let method_body = method.into_body().collect().await.unwrap().to_bytes();
|
||||
let method_html = std::str::from_utf8(&method_body).unwrap();
|
||||
assert!(method_html.contains("<h1>405 - Method Not Allowed</h1>"));
|
||||
assert!(method_html.contains("background: #131615"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn internal_filesystem_diagnostics_never_appear_in_public_error_responses() {
|
||||
use std::io::ErrorKind;
|
||||
for (kind, expected_status, expected_text) in [
|
||||
(
|
||||
ErrorKind::NotFound,
|
||||
StatusCode::NOT_FOUND,
|
||||
"404 Not Found\n",
|
||||
),
|
||||
(
|
||||
ErrorKind::PermissionDenied,
|
||||
StatusCode::FORBIDDEN,
|
||||
"403 Forbidden\n",
|
||||
),
|
||||
(
|
||||
ErrorKind::Other,
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"500 Internal Server Error\n",
|
||||
),
|
||||
for (kind, expected_status) in [
|
||||
(ErrorKind::NotFound, StatusCode::NOT_FOUND),
|
||||
(ErrorKind::PermissionDenied, StatusCode::FORBIDDEN),
|
||||
(ErrorKind::Other, StatusCode::INTERNAL_SERVER_ERROR),
|
||||
] {
|
||||
let error = std::io::Error::from(kind);
|
||||
let response = super::response::io_response(&error);
|
||||
assert_eq!(response.status(), expected_status);
|
||||
assert_eq!(response.headers()[CACHE_CONTROL], "no-store");
|
||||
assert_eq!(response.headers()[CONTENT_TYPE], "text/html; charset=utf-8");
|
||||
assert_eq!(response.headers()["x-content-type-options"], "nosniff");
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
assert_eq!(body.as_ref(), expected_text.as_bytes());
|
||||
assert!(!body.starts_with(b"RustProxy:"));
|
||||
assert_eq!(
|
||||
body.as_ref(),
|
||||
crate::pages::builtin_gateway_error(expected_status)
|
||||
.unwrap()
|
||||
.as_bytes()
|
||||
);
|
||||
assert!(!body.windows(b"RustProxy:".len()).any(|w| w == b"RustProxy:"));
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn web_server_validation_and_unmapped_error_statuses_are_styled() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let service = service(tmp.path(), false);
|
||||
// Path validation follows the same existing built-in page as other 400s.
|
||||
let invalid = serve_request(&service, &request(Method::GET, "/"), "/../secret").await;
|
||||
assert_eq!(invalid.status(), StatusCode::BAD_REQUEST);
|
||||
assert_eq!(invalid.headers()[CONTENT_TYPE], "text/html; charset=utf-8");
|
||||
assert_eq!(
|
||||
invalid.into_body().collect().await.unwrap().to_bytes().as_ref(),
|
||||
crate::pages::builtin_gateway_error(StatusCode::BAD_REQUEST)
|
||||
.unwrap()
|
||||
.as_bytes()
|
||||
);
|
||||
|
||||
// 405 has no dedicated packaged HTML file: reuse the embedded styled
|
||||
// fallback, while preserving Allow and the actual HTTP status.
|
||||
let method = serve_request(&service, &request(Method::PUT, "/"), "/").await;
|
||||
assert_eq!(method.status(), StatusCode::METHOD_NOT_ALLOWED);
|
||||
assert_eq!(method.headers()[ALLOW], "GET, HEAD");
|
||||
assert_eq!(method.headers()[CONTENT_TYPE], "text/html; charset=utf-8");
|
||||
let body = method.into_body().collect().await.unwrap().to_bytes();
|
||||
assert!(std::str::from_utf8(&body)
|
||||
.unwrap()
|
||||
.contains("405 - Method Not Allowed"));
|
||||
|
||||
// An operator-configured status without a dedicated HTML template still
|
||||
// uses the common styled fallback, including for HEAD requests.
|
||||
let fallback_service = Service {
|
||||
id: "teapot".into(),
|
||||
name: "Teapot".into(),
|
||||
document_root: tmp.path().to_path_buf(),
|
||||
try_files: vec!["=418".into()],
|
||||
..Default::default()
|
||||
}
|
||||
.compile()
|
||||
.unwrap();
|
||||
let fallback = serve_request(
|
||||
&fallback_service,
|
||||
&request(Method::GET, "/missing"),
|
||||
"/missing",
|
||||
)
|
||||
.await;
|
||||
assert_eq!(fallback.status(), StatusCode::IM_A_TEAPOT);
|
||||
assert_eq!(fallback.headers()[CONTENT_TYPE], "text/html; charset=utf-8");
|
||||
assert_eq!(fallback.headers()[CACHE_CONTROL], "no-store");
|
||||
assert!(fallback.extensions().get::<crate::pages::PageServed>().is_none());
|
||||
let fallback_html = fallback.into_body().collect().await.unwrap().to_bytes();
|
||||
let fallback_html = std::str::from_utf8(&fallback_html).unwrap();
|
||||
assert!(fallback_html.contains("<h1>418 -"));
|
||||
assert!(fallback_html.contains("background: #131615"));
|
||||
|
||||
let head = serve_request(
|
||||
&fallback_service,
|
||||
&request(Method::HEAD, "/missing"),
|
||||
"/missing",
|
||||
)
|
||||
.await;
|
||||
assert_eq!(head.status(), StatusCode::IM_A_TEAPOT);
|
||||
assert_eq!(
|
||||
head.headers()[CONTENT_LENGTH],
|
||||
HeaderValue::from_str(&fallback_html.len().to_string()).unwrap()
|
||||
);
|
||||
assert!(head.into_body().collect().await.unwrap().to_bytes().is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn last_modified_condition_returns_304() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
|
||||
Reference in new issue
Block a user