parlov-elicit 0.5.0

Elicitation engine: strategy selection and probe plan generation for parlov.
Documentation
//! `RateLimitBurstElicitation` -- collects burst samples to detect rate-limit differentials.
//!
//! Sends `burst_count` requests to the baseline URL then `burst_count` to the probe
//! URL with `ctx.headers` unmodified and no body. Differences in rate-limit response
//! behavior across a burst reveal whether the server applies per-resource or
//! per-session rate limits.

use http::Method;
use parlov_core::{
    always_applicable, NormativeStrength, OracleClass, SignalSurface, Technique, Vector,
};

use crate::strategy::Strategy;
use crate::types::{BurstSpec, ProbeSpec, RiskLevel, StrategyMetadata};
use crate::util::build_pair;
use crate::ScanContext;

/// Number of requests to send per side for rate-limit burst analysis.
const BURST_COUNT: usize = 100;

static METADATA: StrategyMetadata = StrategyMetadata {
    strategy_id: "rate-limit-burst-elicit",
    strategy_name: "Rate Limit Burst Elicitation",
    risk: RiskLevel::OperationDestructive,
};

static TECHNIQUE: Technique = Technique {
    id: "rate-limit-burst",
    name: "Rate-limit burst volume probe",
    oracle_class: OracleClass::Existence,
    vector: Vector::StatusCodeDiff,
    strength: NormativeStrength::May,
    normalization_weight: Some(0.02),
    inverted_signal_weight: Some(0.02),
    method_relevant: false,
    parser_relevant: false,
    applicability: always_applicable,
    contradiction_surface: SignalSurface::Status,
};

/// Elicits rate-limit state differentials by sending burst-volume requests to baseline and probe.
pub struct RateLimitBurstElicitation;

impl Strategy for RateLimitBurstElicitation {
    fn metadata(&self) -> &'static StrategyMetadata {
        &METADATA
    }

    fn technique_def(&self) -> &'static Technique {
        &TECHNIQUE
    }

    fn methods(&self) -> &[Method] {
        &[Method::GET, Method::HEAD]
    }

    fn is_applicable(&self, _ctx: &ScanContext) -> bool {
        true
    }

    fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
        let mut specs = Vec::with_capacity(2);
        for method in [Method::GET, Method::HEAD] {
            let pair = build_pair(
                ctx,
                method,
                ctx.headers.clone(),
                ctx.headers.clone(),
                None,
                METADATA.clone(),
                TECHNIQUE,
            );
            specs.push(ProbeSpec::Burst(BurstSpec {
                baseline: pair.baseline,
                probe: pair.probe,
                burst_count: BURST_COUNT,
                metadata: pair.metadata,
                technique: pair.technique,
                chain_provenance: None,
            }));
        }
        specs
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::ctx_operation_destructive;
    use crate::util::substitute_url;

    #[test]
    fn risk_is_operation_destructive() {
        assert_eq!(
            RateLimitBurstElicitation.risk(),
            RiskLevel::OperationDestructive
        );
    }

    #[test]
    fn generate_returns_two_items() {
        assert_eq!(
            RateLimitBurstElicitation
                .generate(&ctx_operation_destructive())
                .len(),
            2
        );
    }

    #[test]
    fn all_items_are_burst_variants() {
        for spec in &RateLimitBurstElicitation.generate(&ctx_operation_destructive()) {
            assert!(matches!(spec, ProbeSpec::Burst(_)));
        }
    }

    #[test]
    fn burst_count_is_100() {
        for spec in &RateLimitBurstElicitation.generate(&ctx_operation_destructive()) {
            let ProbeSpec::Burst(burst) = spec else {
                panic!("expected Burst")
            };
            assert_eq!(burst.burst_count, 100);
        }
    }

    #[test]
    fn baseline_url_uses_baseline_id() {
        let ctx = ctx_operation_destructive();
        let specs = RateLimitBurstElicitation.generate(&ctx);
        let ProbeSpec::Burst(burst) = &specs[0] else {
            panic!("expected Burst")
        };
        assert_eq!(
            burst.baseline.url,
            substitute_url(&ctx.target, &ctx.baseline_id)
        );
    }

    #[test]
    fn technique_strength_is_may() {
        let specs = RateLimitBurstElicitation.generate(&ctx_operation_destructive());
        assert_eq!(specs[0].technique().strength, NormativeStrength::May);
    }

    #[test]
    fn normalization_weight_is_0_02() {
        assert_eq!(TECHNIQUE.normalization_weight, Some(0.02));
    }

    #[test]
    fn inverted_signal_weight_is_0_02() {
        assert_eq!(TECHNIQUE.inverted_signal_weight, Some(0.02));
    }
}