Skip to main content

Module custom_checks

Module custom_checks 

Source
Expand description

The custom function seams: the reference pipeline’s ninth and seventeenth checks.

This is the Rust family’s port of the two checks the reference pipeline runs host-supplied functions through:

  • guard_core/core/checks/implementations/custom_validators.py: a route’s custom_validators list, run in order, first truthy response wins.
  • guard_core/core/checks/implementations/custom_request.py: the global custom_request_check function.

The reference seams are async callables over the request; the Rust family’s are sync boxed functions over a CustomRequestContext (the request facts the reference’s checks can see). Both checks share the same shapes:

custom_validators (route-scoped, in order, first truthy wins):
  validator returns None:            continue
  validator returns a response:      Failed (block with the validator's
                                     own response, the reference returns
                                     it as-is, never create_error_response)
  validator returns a truthy
  non-response:                      Failed with no block (the reference
                                     logs and emits but does not block)
custom_request (global):
  check returns None:                Allowed
  check returns a response:          Blocked with it (the reference
                                     applies the response modifier;
                                     passive mode logs only)

Names are explicit on the seam (the reference reads __name__, falling back to "anonymous" for unnamed callables): a Rust closure carries no name, so hosts name them at registration.

§Example

use std::sync::Arc;

use guard_core_engine::custom_checks::{
    decide_custom_request, CustomRequestContext, CustomRequestFn, CustomRequestVerdict,
    CustomResponse,
};

let check: CustomRequestFn = Arc::new(|_ctx: &CustomRequestContext<'_>| {
    Some(CustomResponse { status: Some(418), body: None })
});
let ctx = CustomRequestContext { method: "GET", path: "/", client_ip: None, body: None };
let verdict = decide_custom_request(&check, "block_maintenance", &ctx);
assert_eq!(
    verdict,
    CustomRequestVerdict::Blocked { status: Some(418), function: "block_maintenance".to_owned() }
);

Structs§

CustomRequestContext
The request facts a custom function sees (the pieces of GuardRequest the reference’s checks read; everything else stays with the host).
CustomResponse
A response a custom function hands back.

Enums§

CustomRequestVerdict
What decide_custom_request decided.
CustomValidatorsVerdict
What decide_custom_validators decided.
ValidatorAnswer
What a route validator returned: the reference’s truthy response (blocks with its own shape) versus a truthy non-response value (logs and emits, never blocks).

Constants§

CUSTOM_REQUEST_CHECK_NAME
The custom_request check’s stable name (check_name).
CUSTOM_VALIDATORS_CHECK_NAME
The custom_validators check’s stable name (check_name).

Functions§

anonymous_name
The "anonymous" name the reference falls back to for unnamed callables.
decide_custom_request
The custom_request check body: the function runs once; a returned response blocks with it.
decide_custom_validators
The custom_validators check body: run in order, first truthy answer wins (the reference stops at the first truthy response, never running the rest).

Type Aliases§

CustomRequestFn
The host-supplied custom_request_check function.
CustomValidatorFn
The host-supplied route validator function.