fix(http): separate transport and protocol limits
This commit is contained in:
@@ -9,8 +9,9 @@ use std::time::{Duration, Instant};
|
||||
use tempfile::tempdir;
|
||||
use wip_fs_server::{AccessMode, ServerConfig, WipFsServer};
|
||||
use wip_http::{
|
||||
BatchRequest, BatchRequestItem, BatchRequestKind, BatchResponse, BatchSuccess, DecodedResponse,
|
||||
Endpoint, JSON_CONTENT_TYPE, Limits, Route, decode_batch_request, decode_batch_response,
|
||||
BatchRequest, BatchRequestItem, BatchRequestKind, BatchResponse, BatchSuccess,
|
||||
ClientResponseError, DecodedResponse, Endpoint, JSON_CONTENT_TYPE, Limits, Route,
|
||||
TransportFailureKind, decode_batch_request, decode_batch_response,
|
||||
decode_call_operation_response, decode_fetch_interface_response, decode_observe_response,
|
||||
encode_batch_request, encode_call_operation_request, encode_fetch_interface_request,
|
||||
encode_observe_request,
|
||||
@@ -210,6 +211,166 @@ fn real_listener_serves_three_core_interactions_batch_and_write() {
|
||||
.expect("listener exits cleanly");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn real_listener_keeps_route_method_and_malformed_body_boundaries_distinct() {
|
||||
let temporary = tempdir().expect("temporary root");
|
||||
let mut config = ServerConfig::for_root(temporary.path());
|
||||
config.limits.wire = Limits::new(64, 4_096, 64, 4).expect("wire limits");
|
||||
let bound = Arc::new(
|
||||
WipFsServer::new(config)
|
||||
.expect("build server")
|
||||
.bind()
|
||||
.expect("bind"),
|
||||
);
|
||||
let address = bound.local_addr();
|
||||
let shutdown = Arc::new(AtomicBool::new(false));
|
||||
let worker = {
|
||||
let bound = Arc::clone(&bound);
|
||||
let shutdown = Arc::clone(&shutdown);
|
||||
thread::spawn(move || bound.serve_until(shutdown))
|
||||
};
|
||||
|
||||
for uri in [
|
||||
"/wip/v1/unknown",
|
||||
"/wip/v1/observe?unexpected=true",
|
||||
"/wip/v1/observe/extra",
|
||||
] {
|
||||
let response = exchange(address, json_request(http::Method::POST, uri, b"{}"));
|
||||
assert_eq!(response.status(), http::StatusCode::NOT_FOUND);
|
||||
assert!(response.body().is_empty());
|
||||
assert!(!response.headers().contains_key(http::header::CONTENT_TYPE));
|
||||
}
|
||||
|
||||
let wrong_method = exchange(
|
||||
address,
|
||||
json_request(http::Method::GET, "/wip/v1/observe", b""),
|
||||
);
|
||||
assert_eq!(wrong_method.status(), http::StatusCode::METHOD_NOT_ALLOWED);
|
||||
assert_eq!(wrong_method.headers()[http::header::ALLOW], "POST");
|
||||
assert!(wrong_method.body().is_empty());
|
||||
assert!(
|
||||
!wrong_method
|
||||
.headers()
|
||||
.contains_key(http::header::CONTENT_TYPE)
|
||||
);
|
||||
|
||||
let mut unsupported_coding = json_request(
|
||||
http::Method::POST,
|
||||
"/wip/v1/observe",
|
||||
br#"{"path":"/","depth":0}"#,
|
||||
);
|
||||
unsupported_coding
|
||||
.headers_mut()
|
||||
.insert(http::header::CONTENT_ENCODING, "gzip".parse().unwrap());
|
||||
let unsupported_coding = exchange(address, unsupported_coding);
|
||||
assert_eq!(
|
||||
unsupported_coding.status(),
|
||||
http::StatusCode::UNSUPPORTED_MEDIA_TYPE
|
||||
);
|
||||
assert!(unsupported_coding.body().is_empty());
|
||||
assert!(
|
||||
!unsupported_coding
|
||||
.headers()
|
||||
.contains_key(http::header::CONTENT_TYPE)
|
||||
);
|
||||
|
||||
let malformed = exchange(
|
||||
address,
|
||||
json_request(http::Method::POST, "/wip/v1/observe", b"{"),
|
||||
);
|
||||
assert_eq!(malformed.status(), http::StatusCode::BAD_REQUEST);
|
||||
assert_eq!(
|
||||
malformed.headers()[http::header::CONTENT_TYPE],
|
||||
JSON_CONTENT_TYPE
|
||||
);
|
||||
assert!(
|
||||
std::str::from_utf8(malformed.body())
|
||||
.expect("UTF-8 error envelope")
|
||||
.contains("\"invalid_request\"")
|
||||
);
|
||||
|
||||
let oversized = exchange(
|
||||
address,
|
||||
json_request(http::Method::POST, "/wip/v1/observe", &[b'x'; 65]),
|
||||
);
|
||||
assert_eq!(oversized.status(), http::StatusCode::PAYLOAD_TOO_LARGE);
|
||||
assert!(oversized.body().is_empty());
|
||||
assert!(!oversized.headers().contains_key(http::header::CONTENT_TYPE));
|
||||
|
||||
let mut header_only = TcpStream::connect(address).expect("connect header-only client");
|
||||
header_only
|
||||
.set_read_timeout(Some(Duration::from_secs(1)))
|
||||
.expect("set header-only timeout");
|
||||
write!(
|
||||
header_only,
|
||||
"POST /wip/v1/observe HTTP/1.1\r\nHost: localhost\r\nContent-Type: application/json\r\nContent-Length: 65\r\n\r\n"
|
||||
)
|
||||
.expect("write oversized request head");
|
||||
header_only.flush().expect("flush oversized request head");
|
||||
let mut raw = Vec::new();
|
||||
header_only
|
||||
.read_to_end(&mut raw)
|
||||
.expect("read immediate limit response");
|
||||
assert!(raw.starts_with(b"HTTP/1.1 413 "));
|
||||
assert!(raw.ends_with(b"\r\n\r\n"));
|
||||
assert!(
|
||||
!raw.windows(b"resource_limit_exceeded".len())
|
||||
.any(|window| { window == b"resource_limit_exceeded" })
|
||||
);
|
||||
|
||||
shutdown.store(true, Ordering::Release);
|
||||
worker
|
||||
.join()
|
||||
.expect("join listener")
|
||||
.expect("listener exits cleanly");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_decoder_and_batch_packaging_limits_are_transport_failures() {
|
||||
let temporary = tempdir().expect("temporary root");
|
||||
let mut config = ServerConfig::for_root(temporary.path());
|
||||
config.limits.wire = Limits::new(4_096, 4_096, 8, 1).expect("wire limits");
|
||||
let server = WipFsServer::new(config).expect("build server");
|
||||
|
||||
let nested = br#"{"path":[[[[[[[[["/"]]]]]]]]],"depth":0}"#;
|
||||
let response = server.handle_http(json_request(http::Method::POST, "/wip/v1/observe", nested));
|
||||
assert_eq!(response.status(), http::StatusCode::PAYLOAD_TOO_LARGE);
|
||||
assert!(response.body().is_empty());
|
||||
assert!(!response.headers().contains_key(http::header::CONTENT_TYPE));
|
||||
|
||||
let batch = br#"{"requests":[{"id":"one","method":"observe","arguments":{"path":"/","depth":0}},{"id":"two","method":"observe","arguments":{"path":"/","depth":0}}]}"#;
|
||||
let response = server.handle_http(json_request(http::Method::POST, "/wip/v1/batch", batch));
|
||||
assert_eq!(response.status(), http::StatusCode::PAYLOAD_TOO_LARGE);
|
||||
assert!(response.body().is_empty());
|
||||
assert!(!response.headers().contains_key(http::header::CONTENT_TYPE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn logical_observation_limit_remains_protocol_resource_failure() {
|
||||
let temporary = tempdir().expect("temporary root");
|
||||
let mut config = ServerConfig::for_root(temporary.path());
|
||||
config.limits.max_observation_depth = 0;
|
||||
let limits = config.limits.wire;
|
||||
let server = WipFsServer::new(config).expect("build server");
|
||||
let endpoint = Endpoint::parse("http://localhost/wip").expect("endpoint");
|
||||
let request = ObserveRequest {
|
||||
path: "/".into(),
|
||||
depth: 1,
|
||||
};
|
||||
|
||||
let response = server.handle_http(
|
||||
encode_observe_request(&endpoint, &request, limits).expect("encode observation"),
|
||||
);
|
||||
assert_eq!(response.status(), http::StatusCode::UNPROCESSABLE_ENTITY);
|
||||
assert!(matches!(
|
||||
decode_observe_response(&request, &response, limits).expect("decode logical limit"),
|
||||
DecodedResponse::ProtocolFailure {
|
||||
error,
|
||||
status_mismatch: None,
|
||||
} if error.code == ProtocolErrorCode::ResourceLimitExceeded
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stalled_request_body_times_out_and_releases_the_only_worker() {
|
||||
let temporary = tempdir().expect("temporary root");
|
||||
@@ -359,7 +520,7 @@ fn backend_invalid_unicode_path_is_not_found_not_internal() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_response_size_failure_preserves_non_commit_guarantee() {
|
||||
fn read_response_size_failure_is_transport_local() {
|
||||
let temporary = tempdir().expect("temporary root");
|
||||
std::fs::write(temporary.path().join("large"), vec![b'x'; 2_000]).expect("write file");
|
||||
let mut config = ServerConfig::for_root(temporary.path());
|
||||
@@ -406,15 +567,16 @@ fn read_response_size_failure_preserves_non_commit_guarantee() {
|
||||
encode_call_operation_request(&endpoint, &call, &interface.descriptor, limits)
|
||||
.expect("encode read call"),
|
||||
);
|
||||
assert_eq!(response.status(), http::StatusCode::PAYLOAD_TOO_LARGE);
|
||||
match decode_call_operation_response(&call, &interface.descriptor, &response, limits)
|
||||
.expect("decode resource failure")
|
||||
{
|
||||
DecodedResponse::ProtocolFailure { error, .. } => {
|
||||
assert_eq!(error.code, ProtocolErrorCode::ResourceLimitExceeded);
|
||||
}
|
||||
DecodedResponse::Success(_) => panic!("oversized read response must fail"),
|
||||
}
|
||||
assert_eq!(response.status(), http::StatusCode::INTERNAL_SERVER_ERROR);
|
||||
assert!(response.body().is_empty());
|
||||
assert!(!response.headers().contains_key(http::header::CONTENT_TYPE));
|
||||
assert!(matches!(
|
||||
decode_call_operation_response(&call, &interface.descriptor, &response, limits)
|
||||
.expect_err("oversized representation is not a protocol failure"),
|
||||
ClientResponseError::TransportBinding(failure)
|
||||
if failure.status == http::StatusCode::INTERNAL_SERVER_ERROR
|
||||
&& failure.kind == TransportFailureKind::HttpStatus
|
||||
));
|
||||
}
|
||||
|
||||
fn assert_batch_successes(batch: &BatchResponse) {
|
||||
@@ -440,6 +602,16 @@ fn success<T>(response: DecodedResponse<T>) -> T {
|
||||
}
|
||||
}
|
||||
|
||||
fn json_request(method: http::Method, uri: &str, body: &[u8]) -> http::Request<Vec<u8>> {
|
||||
http::Request::builder()
|
||||
.method(method)
|
||||
.uri(uri)
|
||||
.header(http::header::CONTENT_TYPE, JSON_CONTENT_TYPE)
|
||||
.header(http::header::ACCEPT, "application/json")
|
||||
.body(body.to_vec())
|
||||
.expect("build JSON request")
|
||||
}
|
||||
|
||||
fn exchange(address: SocketAddr, request: http::Request<Vec<u8>>) -> http::Response<Vec<u8>> {
|
||||
let mut stream = TcpStream::connect(address).expect("connect listener");
|
||||
stream
|
||||
|
||||
Reference in New Issue
Block a user