Skip to main content

Crate towershield

Crate towershield 

Source
Expand description

Tower HTTP adapter for the portable path denylist in towershield_core.

ShieldLayer compiles a RuleSet once and wraps any Tower Service. On each request ShieldService builds an towershield_core::InspectionPath from uri.path(), evaluates the compiled rules, and either forwards the request unchanged or returns a generic empty blocked response without polling the inner service.

§Domain map

ConceptTypes
Layer configurationShieldBuilder, BlockedResponse, OnBlock
MiddlewareShieldLayer, ShieldService
Rule authoring (re-exported)Rule, RuleSet, PathMatcher, DEFAULT_RULES
Decision surface (re-exported)ShieldDecision, ShieldMatch, CompiledRuleSet

Rule compilation errors surface as towershield_core::ruleset::CompileError from ShieldBuilder::try_build. Prefer that at process startup when rules come from external config; ShieldBuilder::build panics on failure.

§Placement (critical for Axum)

Wrap the complete axum::Router with Layer::layer so the shield runs before route matching. router.layer(ShieldLayer::…) runs after matching, so unmatched scanner probes never hit the denylist.

use tower::Layer;
use towershield::ShieldLayer;
// let app = ShieldLayer::default().layer(router);

§Middleware ordering

Keep the shield outermost (applied last → runs first):

ShieldLayer               ← reject probes before the rest of the stack
  TracingLayer
    RequestBodyLimitLayer
      AuthLayer
        Router
let app = ServiceBuilder::new()
    .layer(ShieldLayer::default())
    .service(router);

Core rule types are re-exported so application crates can depend only on towershield for common configuration. Offline Cloudflare export lives in the separate towershield-cloudflare crate and does not run on the request path.

§Features

FeatureEffect
tracing (default)Emit a debug log line on each block
regex (default)Regex matchers and the broader built-in rule tier
rayonForwarded to towershield-core for parallel rule compile

Structs§

BlockedResponse
Client-facing response shape for blocked scanner probes.
CompiledRuleSet
Startup-compiled rules ready for per-request evaluation.
Rule
One declarative path rule: identity, disposition, matcher, and flags.
RuleId
Stable, opaque rule identifier suitable for metrics and export reports.
RuleSchemaVersion
Schema version embedded in serialised rule files.
RuleSet
Versioned, ordered collection of declarative Rule values.
ShieldBuilder
Fluent configuration for a ShieldLayer before rule compilation.
ShieldLayer
Tower Layer that evaluates the path denylist before the inner service.
ShieldMatch
Identity of the deny rule that produced a ShieldDecision::Block.
ShieldService
Tower Service that evaluates path rules, then either blocks or forwards.

Enums§

CaseSensitivity
Per-rule case-handling policy applied during compile and evaluate.
MatchKind
Discriminant of a PathMatcher without the pattern payload.
PathMatcher
How a rule compares against the decoded inspection path.
RuleDisposition
Whether a matching path is blocked or explicitly permitted.
RuleGroup
Logical category for filtering, metrics labels, and export organisation.
ShieldDecision
Outcome of evaluating an inspection path against a CompiledRuleSet.

Statics§

DEFAULT_RULES
Process-wide handle for the built-in scanner-probe denylist.

Type Aliases§

OnBlock
Observability hook invoked after a path is blocked, before the response is returned.