Skip to content

Commit

Permalink
Adjust warp::fs rejections depending on File::open error kind
Browse files Browse the repository at this point in the history
- Return 404 Not Found for NotFound
- Return 403 Forbidden for PermissionDenied
- Return 500 Internal Server Error for other errors
  • Loading branch information
seanmonstar committed Jan 27, 2020
1 parent 39b63a1 commit d6ac489
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
16 changes: 15 additions & 1 deletion src/filters/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,17 @@ fn file_reply(
log::debug!("file not found: {:?}", path.as_ref().display());
reject::not_found()
}
io::ErrorKind::PermissionDenied => {
log::warn!("file permission denied: {:?}", path.as_ref().display());
reject::known(FilePermissionError { _p: () })
}
_ => {
log::error!(
"file open error (path={:?}): {} ",
path.as_ref().display(),
err
);
reject::not_found()
reject::known(FileOpenError { _p: () })
}
};
Either::Right(future::err(rej))
Expand Down Expand Up @@ -450,6 +454,16 @@ fn get_block_size(_metadata: &Metadata) -> usize {
DEFAULT_READ_BUF_SIZE
}

// ===== Rejections =====

unit_error! {
pub(crate) FileOpenError: "file open error"
}

unit_error! {
pub(crate) FilePermissionError: "file perimission error"
}

#[cfg(test)]
mod tests {
use super::sanitize_path;
Expand Down
10 changes: 6 additions & 4 deletions src/reject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ enum_known! {
LengthRequired(LengthRequired),
PayloadTooLarge(PayloadTooLarge),
UnsupportedMediaType(UnsupportedMediaType),
FileOpenError(crate::fs::FileOpenError),
FilePermissionError(crate::fs::FilePermissionError),
BodyReadError(crate::body::BodyReadError),
BodyDeserializeError(crate::body::BodyDeserializeError),
CorsForbidden(crate::cors::CorsForbidden),
Expand Down Expand Up @@ -394,10 +396,10 @@ impl Rejections {
Known::LengthRequired(_) => StatusCode::LENGTH_REQUIRED,
Known::PayloadTooLarge(_) => StatusCode::PAYLOAD_TOO_LARGE,
Known::UnsupportedMediaType(_) => StatusCode::UNSUPPORTED_MEDIA_TYPE,
Known::CorsForbidden(_) => StatusCode::FORBIDDEN,
Known::MissingExtension(_) | Known::BodyConsumedMultipleTimes(_) => {
StatusCode::INTERNAL_SERVER_ERROR
}
Known::FilePermissionError(_) | Known::CorsForbidden(_) => StatusCode::FORBIDDEN,
Known::FileOpenError(_)
| Known::MissingExtension(_)
| Known::BodyConsumedMultipleTimes(_) => StatusCode::INTERNAL_SERVER_ERROR,
},
Rejections::Custom(..) => StatusCode::INTERNAL_SERVER_ERROR,
Rejections::Combined(ref a, ref b) => preferred(a, b).status(),
Expand Down

0 comments on commit d6ac489

Please sign in to comment.