Expand description
The request size and content-type gate: max_request_size and
allowed_content_types.
This is the Rust family’s port of the reference engine’s
request_size_content check
(guard_core/core/checks/implementations/request_size_content.py). One
call, decide, carries a request’s size and content-type header values
through the reference’s two sub-checks in its order:
no max_request_size: skip the size check
no content-length header: pass the size check
content-length <= max_request_size: pass
content-length > max_request_size: 413 "Request too large"
no allowed_content_types: skip the type check
media type (before ";") in the allowed list: pass
anything else (a missing header included): 415 "Unsupported content type"Both limits are route-scoped in the reference (RouteConfig.max_request_size,
RouteConfig.allowed_content_types); there is no global knob, so the
config value is Option-shaped on both fields and the all-None default
is inert: decide passes everything.
§Details that mirror the reference exactly
- The size comparison is
int(content_length) <= max_request_size: a request at exactly the limit passes, one byte over is blocked, and a missing or emptycontent-lengthheader passes (if not content_lengthin the reference). A content-length that is not an integer raisesValueErrorin the reference, which the pipeline answers with the fail-secure500 "Security check failed";decidemirrors that with anErrreturn and a negative content-length passes (-1 <= limit), like Python’s signedint(). - The media type is
content_type.split(";")[0]with no trim: the exact prefix before the first;is compared case-sensitively against the configured list, soapplication/json; charset=utf-8matchesapplication/jsonandapplication/json ; charset=utf-8does not (trailing space), exactly as in Python. A missingcontent-typeheader compares as the empty string and is blocked by any non-empty allowed list. - The size check runs first and wins: a request over the size limit with
a disallowed content type is answered
413, never415.
§Example
use guard_core_engine::request_limits::{decide, ContentLimits};
let limits = ContentLimits {
max_request_size: Some(1024),
allowed_content_types: Some(vec!["application/json".to_owned()]),
};
// At the limit passes, one byte over blocks with the 413 shape.
assert!(decide(&limits, Some("1024"), Some("application/json"))
.unwrap()
.is_none());
let block = decide(&limits, Some("1025"), Some("application/json"))
.unwrap()
.expect("over the limit");
assert_eq!(block.status, 413);
assert_eq!(block.body, "Request too large");
// The media type is the prefix before ";", matched exactly.
assert!(decide(&limits, Some("10"), Some("application/json; charset=utf-8"))
.unwrap()
.is_none());
let block = decide(&limits, Some("10"), Some("text/plain"))
.unwrap()
.expect("disallowed type");
assert_eq!(block.status, 415);
assert_eq!(block.body, "Unsupported content type");
// The default is inert.
assert!(decide(&ContentLimits::default(), Some("999999999"), Some("text/plain"))
.unwrap()
.is_none());Structs§
- Content
Block - The block answer of a failed size or type check: the status and default
message body the reference’s
create_error_responseanswers with, plus thelog_activityreason string. - Content
Length Error - A
content-lengthheader value that is not an integer: the errordecidefails with. - Content
Limits - The route-level content limits (the reference
RouteConfig.max_request_sizeandRouteConfig.allowed_content_types).
Enums§
- Content
Violation - Which sub-check blocked the request (the reference’s
violation_typeevent fields, carried for observability).
Constants§
- BODY_
READ_ TIMEOUT_ MESSAGE - The body of the request-body read timeout answer.
- BODY_
READ_ TIMEOUT_ STATUS - The status of a request body that did not arrive within
body_read_timeout(SecurityConfig::body_read_timeout): the adapter answers it instead of forwarding a body it could not read in full.
Functions§
- decide
- Carry one request’s size and content-type header values through the reference’s two sub-checks.
- media_
type - The media type of a
content-typeheader value: the exact prefix before the first;, with no trim (the referencesplit(";")[0]). A missing header reads as the empty string.