parlov-elicit 0.5.0

Elicitation engine: strategy selection and probe plan generation for parlov.
Documentation
//! `RateLimitHeadersElicitation` -- probes for rate-limit header differentials.
//!
//! Sends both the baseline and probe requests with `ctx.headers` unmodified and
//! compares the full response header sets. Differences in `X-RateLimit-*`,
//! `Retry-After`, `Cache-Control`, or `Vary` headers reveal session or
//! authorization state through header-level leakage.

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

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

static METADATA: StrategyMetadata = StrategyMetadata {
    strategy_id: "rate-limit-headers-elicit",
    strategy_name: "Rate Limit Headers Elicitation",
    risk: RiskLevel::Safe,
};

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

/// Elicits state differentials by diffing response header sets across baseline and probe.
pub struct RateLimitHeadersElicitation;

impl Strategy for RateLimitHeadersElicitation {
    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::HeaderDiff(pair));
        }
        specs
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::{ctx_with_auth, minimal_ctx};

    #[test]
    fn risk_is_safe() {
        assert_eq!(RateLimitHeadersElicitation.risk(), RiskLevel::Safe);
    }

    #[test]
    fn is_applicable_always_true() {
        assert!(RateLimitHeadersElicitation.is_applicable(&minimal_ctx()));
    }

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

    #[test]
    fn all_items_are_header_diff_variants() {
        for spec in &RateLimitHeadersElicitation.generate(&minimal_ctx()) {
            assert!(matches!(spec, ProbeSpec::HeaderDiff(_)));
        }
    }

    #[test]
    fn baseline_and_probe_use_ctx_headers_unmodified() {
        let ctx = ctx_with_auth();
        let specs = RateLimitHeadersElicitation.generate(&ctx);
        let ProbeSpec::HeaderDiff(pair) = &specs[0] else {
            panic!("expected HeaderDiff")
        };
        assert_eq!(
            pair.baseline.headers.get(http::header::AUTHORIZATION),
            pair.probe.headers.get(http::header::AUTHORIZATION),
        );
    }

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

    #[test]
    fn normalization_weight_is_none() {
        assert_eq!(TECHNIQUE.normalization_weight, None);
    }

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