Skip to main content

Module request_limits

Module request_limits 

Source
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 empty content-length header passes (if not content_length in the reference). A content-length that is not an integer raises ValueError in the reference, which the pipeline answers with the fail-secure 500 "Security check failed"; decide mirrors that with an Err return and a negative content-length passes (-1 <= limit), like Python’s signed int().
  • 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, so application/json; charset=utf-8 matches application/json and application/json ; charset=utf-8 does not (trailing space), exactly as in Python. A missing content-type header 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, never 415.

§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§

ContentBlock
The block answer of a failed size or type check: the status and default message body the reference’s create_error_response answers with, plus the log_activity reason string.
ContentLengthError
A content-length header value that is not an integer: the error decide fails with.
ContentLimits
The route-level content limits (the reference RouteConfig.max_request_size and RouteConfig.allowed_content_types).

Enums§

ContentViolation
Which sub-check blocked the request (the reference’s violation_type event 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-type header value: the exact prefix before the first ;, with no trim (the reference split(";")[0]). A missing header reads as the empty string.