Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/handlers/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ fn parse_weighted_codes(codes_str: &str) -> Result<Vec<WeightedChoice>, String>
};

let code = code_str.parse::<u16>().map_err(|_| "Invalid status code")?;
// Only 100-999 can be sent as a real status line; anything else would
// otherwise fall back to 200 further down.
if StatusCode::from_u16(code).is_err() {
return Err("Invalid status code".to_string());
}

if weight < 0.0 {
return Err("Weight cannot be negative".to_string());
Expand Down
22 changes: 22 additions & 0 deletions src/tests/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,25 @@ async fn response_headers_post_sets_headers() {
"bar"
);
}

/// Codes outside 100-999 can't be sent as a real status, so they are
/// rejected like any other invalid code instead of falling back to 200.
#[actix_web::test]
async fn status_out_of_range_code_is_bad_request() {
let app = test::init_service(create_app(cfg())).await;
for uri in [
"/status/99",
"/status/1000",
"/status/0",
"/status/200,1000",
] {
for method in [actix_web::http::Method::GET, actix_web::http::Method::POST] {
let req = test::TestRequest::default()
.method(method.clone())
.uri(uri)
.to_request();
let resp = test::call_service(&app, req).await;
assert_eq!(resp.status(), StatusCode::BAD_REQUEST, "{method} {uri}");
}
}
}