parlov-elicit 0.1.2

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 between responses reveal
//! session or authorization state through header-level leakage.

use http::Method;

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

fn metadata() -> StrategyMetadata {
    StrategyMetadata {
        strategy_id: "rate-limit-headers-elicit",
        strategy_name: "Rate Limit Headers Elicitation",
        risk: RiskLevel::Safe,
    }
}

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

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

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

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

    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(),
            );
            specs.push(ProbeSpec::HeaderDiff(pair));
        }
        specs
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use http::{HeaderMap, HeaderValue};

    fn make_ctx_no_auth() -> 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::Safe,
            known_duplicate: None,
            state_field: None,
            alt_credential: None,
            body_template: None,
        }
    }

    fn make_ctx_with_auth() -> ScanContext {
        let mut headers = HeaderMap::new();
        headers.insert(
            http::header::AUTHORIZATION,
            HeaderValue::from_static("Bearer token123"),
        );
        ScanContext { headers, ..make_ctx_no_auth() }
    }

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

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

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

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

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

    #[test]
    fn baseline_and_probe_use_ctx_headers_unmodified() {
        let ctx = make_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),
        );
    }
}