fix(http): separate transport and protocol limits

This commit is contained in:
2026-10-02 02:50:56 +09:00
parent 65c51006ad
commit df006ee706
12 changed files with 643 additions and 336 deletions
+72 -106
View File
@@ -5,15 +5,15 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::time::{Duration, Instant};
use http::header::ALLOW;
use http::{Request, Response, StatusCode};
use wip_host::{ExecutionContext, ObjectProvider};
use wip_http::{
BatchRequestKind, BatchResponse, BatchResponseItem, BatchSuccess, CodecError, EncodeHttpError,
Endpoint, RequestMetadataError, ResourceLimitKind, Route, decode_batch_request,
decode_call_operation_metadata, decode_fetch_interface_request, decode_observe_request,
encode_batch_error_response, encode_batch_resource_limit_error_response, encode_batch_response,
encode_call_operation_response, encode_fetch_interface_response, encode_observe_response,
encode_protocol_error_response, encode_resource_limit_error_response, validate_http_request,
Endpoint, RequestMetadataError, Route, decode_batch_request, decode_call_operation_metadata,
decode_fetch_interface_request, decode_observe_request, encode_batch_error_response,
encode_batch_response, encode_call_operation_response, encode_fetch_interface_response,
encode_observe_response, encode_protocol_error_response, validate_http_request,
};
use wip_protocol::{ProtocolError, ProtocolErrorCode, ProtocolInteraction};
@@ -68,20 +68,25 @@ impl WipFsServer {
request: Request<Vec<u8>>,
deadline: Instant,
) -> Response<Vec<u8>> {
let Some(route) = self.route(request.uri().path()) else {
let Some(route) = self.route(request.uri()) else {
return empty_response(StatusCode::NOT_FOUND);
};
if let Err(error) = validate_http_request(&self.endpoint, route, &request) {
return self.metadata_error(route, error);
return self.metadata_error(error);
}
let context = ExecutionContext::new()
.with_deadline(deadline)
.with_resource_budget(u64::from(self.config.limits.max_observation_nodes));
match route {
let response = match route {
Route::Observe => self.observe(request.body(), &context),
Route::FetchInterface => self.fetch_interface(request.body()),
Route::CallOperation => self.call_operation(request.body(), &context),
Route::Batch => self.batch(request.body(), &context),
};
if Instant::now() >= deadline {
empty_response(StatusCode::GATEWAY_TIMEOUT)
} else {
response
}
}
@@ -106,7 +111,10 @@ impl WipFsServer {
})
}
fn route(&self, path: &str) -> Option<Route> {
fn route(&self, uri: &http::Uri) -> Option<Route> {
if uri.query().is_some() {
return None;
}
[
Route::Observe,
Route::FetchInterface,
@@ -114,7 +122,7 @@ impl WipFsServer {
Route::Batch,
]
.into_iter()
.find(|route| self.endpoint.route(*route).path() == path)
.find(|route| self.endpoint.route(*route).path() == uri.path())
}
fn observe(&self, body: &[u8], context: &ExecutionContext) -> Response<Vec<u8>> {
@@ -127,7 +135,7 @@ impl WipFsServer {
.unwrap_or_else(|error| {
self.read_encode_error(ProtocolInteraction::Observe, error)
}),
Err(error) => self.protocol_error(ProtocolInteraction::Observe, &error, None),
Err(error) => self.protocol_error(ProtocolInteraction::Observe, &error),
}
}
@@ -143,7 +151,7 @@ impl WipFsServer {
self.read_encode_error(ProtocolInteraction::FetchInterface, error)
})
}
Err(error) => self.protocol_error(ProtocolInteraction::FetchInterface, &error, None),
Err(error) => self.protocol_error(ProtocolInteraction::FetchInterface, &error),
}
}
@@ -181,7 +189,7 @@ impl WipFsServer {
},
message: "operation completed without response metadata".into(),
};
return self.protocol_error(ProtocolInteraction::CallOperation, &error, None);
return self.protocol_error(ProtocolInteraction::CallOperation, &error);
};
encode_call_operation_response(
&request,
@@ -190,6 +198,12 @@ impl WipFsServer {
self.config.limits.wire,
)
.unwrap_or_else(|encode_error| {
if matches!(
&encode_error,
EncodeHttpError::Codec(codec) if is_transport_limit(codec)
) {
return empty_response(StatusCode::INTERNAL_SERVER_ERROR);
}
if !may_commit {
return self
.read_encode_error(ProtocolInteraction::CallOperation, encode_error);
@@ -200,10 +214,10 @@ impl WipFsServer {
code: ProtocolErrorCode::OperationOutcomeUnknown,
message: "operation completed but its response could not be encoded".into(),
};
self.protocol_error(ProtocolInteraction::CallOperation, &error, None)
self.protocol_error(ProtocolInteraction::CallOperation, &error)
})
}
Err(error) => self.protocol_error(ProtocolInteraction::CallOperation, &error, None),
Err(error) => self.protocol_error(ProtocolInteraction::CallOperation, &error),
}
}
@@ -236,22 +250,20 @@ impl WipFsServer {
let response = BatchResponse { responses };
encode_batch_response(&request, &response, self.config.limits.wire).unwrap_or_else(
|error| match error {
EncodeHttpError::Codec(codec) => self.batch_codec_error(codec),
EncodeHttpError::Http(_) => self.batch_top_error(&internal_error(), None),
EncodeHttpError::Codec(codec) => self.batch_encode_error(&codec),
EncodeHttpError::Http(_) => self.batch_top_error(&internal_error()),
},
)
}
fn metadata_error(&self, route: Route, error: RequestMetadataError) -> Response<Vec<u8>> {
fn metadata_error(&self, error: RequestMetadataError) -> Response<Vec<u8>> {
match error {
RequestMetadataError::InvalidRequest(_) => self.protocol_error(
interaction(route),
&ProtocolError {
code: ProtocolErrorCode::InvalidRequest,
message: "invalid HTTP request metadata".into(),
},
None,
),
RequestMetadataError::NotFound => empty_response(StatusCode::NOT_FOUND),
RequestMetadataError::MethodNotAllowed => Response::builder()
.status(StatusCode::METHOD_NOT_ALLOWED)
.header(ALLOW, "POST")
.body(Vec::new())
.expect("static method response is valid"),
RequestMetadataError::UnsupportedMediaType => {
empty_response(StatusCode::UNSUPPORTED_MEDIA_TYPE)
}
@@ -264,8 +276,8 @@ impl WipFsServer {
interaction: ProtocolInteraction,
error: CodecError,
) -> Response<Vec<u8>> {
if let Some(kind) = error.resource_limit_kind() {
self.protocol_error(interaction, &resource_error(), Some(kind))
if is_transport_limit(&error) {
empty_response(StatusCode::PAYLOAD_TOO_LARGE)
} else {
self.protocol_error(
interaction,
@@ -273,22 +285,18 @@ impl WipFsServer {
code: ProtocolErrorCode::InvalidRequest,
message: "malformed WIP request".into(),
},
None,
)
}
}
fn batch_codec_error(&self, error: CodecError) -> Response<Vec<u8>> {
if let Some(kind) = error.resource_limit_kind() {
self.batch_top_error(&resource_error(), Some(kind))
if is_transport_limit(&error) {
empty_response(StatusCode::PAYLOAD_TOO_LARGE)
} else {
self.batch_top_error(
&ProtocolError {
code: ProtocolErrorCode::InvalidRequest,
message: "malformed WIP batch request".into(),
},
None,
)
self.batch_top_error(&ProtocolError {
code: ProtocolErrorCode::InvalidRequest,
message: "malformed WIP batch request".into(),
})
}
}
@@ -298,10 +306,18 @@ impl WipFsServer {
error: EncodeHttpError,
) -> Response<Vec<u8>> {
match error {
EncodeHttpError::Codec(codec) if codec.resource_limit_kind().is_some() => {
self.protocol_error(interaction, &resource_error(), codec.resource_limit_kind())
EncodeHttpError::Codec(codec) if is_transport_limit(&codec) => {
empty_response(StatusCode::INTERNAL_SERVER_ERROR)
}
_ => self.protocol_error(interaction, &internal_error(), None),
_ => self.protocol_error(interaction, &internal_error()),
}
}
fn batch_encode_error(&self, error: &CodecError) -> Response<Vec<u8>> {
if is_transport_limit(error) {
empty_response(StatusCode::INTERNAL_SERVER_ERROR)
} else {
self.batch_top_error(&internal_error())
}
}
@@ -309,32 +325,14 @@ impl WipFsServer {
&self,
interaction: ProtocolInteraction,
error: &ProtocolError,
resource_kind: Option<ResourceLimitKind>,
) -> Response<Vec<u8>> {
let encoded = match resource_kind {
Some(kind) => encode_resource_limit_error_response(
interaction,
error,
kind,
self.config.limits.wire,
),
None => encode_protocol_error_response(interaction, error, self.config.limits.wire),
};
encoded.unwrap_or_else(|_| empty_response(StatusCode::INTERNAL_SERVER_ERROR))
encode_protocol_error_response(interaction, error, self.config.limits.wire)
.unwrap_or_else(|_| empty_response(StatusCode::INTERNAL_SERVER_ERROR))
}
fn batch_top_error(
&self,
error: &ProtocolError,
resource_kind: Option<ResourceLimitKind>,
) -> Response<Vec<u8>> {
let encoded = match resource_kind {
Some(kind) => {
encode_batch_resource_limit_error_response(error, kind, self.config.limits.wire)
}
None => encode_batch_error_response(error, self.config.limits.wire),
};
encoded.unwrap_or_else(|_| empty_response(StatusCode::INTERNAL_SERVER_ERROR))
fn batch_top_error(&self, error: &ProtocolError) -> Response<Vec<u8>> {
encode_batch_error_response(error, self.config.limits.wire)
.unwrap_or_else(|_| empty_response(StatusCode::INTERNAL_SERVER_ERROR))
}
}
@@ -404,7 +402,7 @@ struct ParsedRequestHead {
enum IntakeError {
Timeout,
HeaderTooLarge,
PayloadTooLarge { target: String },
PayloadTooLarge,
BadRequest,
Socket,
}
@@ -425,9 +423,7 @@ fn serve_connection(service: &WipFsServer, mut stream: TcpStream) {
Err(IntakeError::HeaderTooLarge) => {
Some(empty_response(StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE))
}
Err(IntakeError::PayloadTooLarge { target }) => {
Some(payload_too_large_response(service, &target))
}
Err(IntakeError::PayloadTooLarge) => Some(empty_response(StatusCode::PAYLOAD_TOO_LARGE)),
Err(IntakeError::BadRequest) => Some(empty_response(StatusCode::BAD_REQUEST)),
Err(IntakeError::Timeout | IntakeError::Socket) => None,
};
@@ -466,9 +462,7 @@ fn read_http_request(
let parsed = parse_request_head(&received[..header_end])?;
if parsed.content_length > maximum_body {
return Err(IntakeError::PayloadTooLarge {
target: parsed.target,
});
return Err(IntakeError::PayloadTooLarge);
}
let mut body = Vec::with_capacity(parsed.content_length);
let buffered = &received[header_end..];
@@ -567,27 +561,6 @@ fn read_with_deadline(
})
}
fn payload_too_large_response(service: &WipFsServer, target: &str) -> Response<Vec<u8>> {
let route = service.route(target.split('?').next().unwrap_or_default());
route.map_or_else(
|| empty_response(StatusCode::PAYLOAD_TOO_LARGE),
|route| {
if route == Route::Batch {
service.batch_top_error(
&resource_error(),
Some(ResourceLimitKind::RepresentationBytes),
)
} else {
service.protocol_error(
interaction(route),
&resource_error(),
Some(ResourceLimitKind::RepresentationBytes),
)
}
},
)
}
fn write_http_response(
stream: &mut TcpStream,
response: &Response<Vec<u8>>,
@@ -648,20 +621,13 @@ fn write_all_with_deadline(
stream.flush()
}
fn interaction(route: Route) -> ProtocolInteraction {
match route {
Route::Observe => ProtocolInteraction::Observe,
Route::FetchInterface => ProtocolInteraction::FetchInterface,
Route::CallOperation => ProtocolInteraction::CallOperation,
Route::Batch => ProtocolInteraction::Observe,
}
}
fn resource_error() -> ProtocolError {
ProtocolError {
code: ProtocolErrorCode::ResourceLimitExceeded,
message: "configured resource limit exceeded".into(),
}
fn is_transport_limit(error: &CodecError) -> bool {
matches!(
error,
CodecError::BodyTooLarge { .. }
| CodecError::NestingTooDeep { .. }
| CodecError::TooManyBatchItems { .. }
)
}
fn internal_error() -> ProtocolError {
+184 -12
View File
@@ -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