parlov-elicit 0.4.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::{NormativeStrength, OracleClass, 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;

fn metadata() -> StrategyMetadata {
    StrategyMetadata {
        strategy_id: "rate-limit-burst-elicit",
        strategy_name: "Rate Limit Burst Elicitation",
        risk: RiskLevel::OperationDestructive,
    }
}

fn technique() -> Technique {
    Technique {
        id: "rate-limit-burst",
        name: "Rate-limit burst volume probe",
        oracle_class: OracleClass::Existence,
        vector: Vector::StatusCodeDiff,
        strength: NormativeStrength::May,
    }
}

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

impl Strategy for RateLimitBurstElicitation {
    fn id(&self) -> &'static str {
        "rate-limit-burst-elicit"
    }

    fn name(&self) -> &'static str {
        "Rate Limit Burst Elicitation"
    }

    fn risk(&self) -> RiskLevel {
        RiskLevel::OperationDestructive
    }

    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(), technique(),
            );
            specs.push(ProbeSpec::Burst(BurstSpec {
                baseline: pair.baseline,
                probe: pair.probe,
                burst_count: BURST_COUNT,
                metadata: pair.metadata,
                technique: pair.technique,
            }));
        }
        specs
    }
}

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

    fn make_ctx() -> ScanContext {
        ScanContext {
            target: "https://api.example.com/users/{id}".to_string(),
            baseline_id: "1001".to_string(),
            probe_id: "9999".to_string(),
            headers: HeaderMap::new(),
            max_risk: RiskLevel::OperationDestructive,
            known_duplicate: None,
            state_field: None,
            alt_credential: None,
            body_template: None,
        }
    }

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

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

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

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

    #[test]
    fn baseline_url_uses_baseline_id() {
        let ctx = make_ctx();
        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(&make_ctx());
        assert_eq!(specs[0].technique().strength, NormativeStrength::May);
    }
}