parlov-elicit 0.1.2

Elicitation engine: strategy selection and probe plan generation for parlov.
Documentation
//! `AuthStripElicitation` — probes with the `Authorization` header removed.
//!
//! Sends the probe request without an `Authorization` header while the baseline
//! uses `ctx.headers` unmodified. A server that returns 403 for authenticated
//! requests to existing resources but 404 for unauthenticated requests reveals
//! resource existence through authorization-gated responses.

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: "auth-strip-elicit",
        strategy_name: "Auth Strip Elicitation",
        risk: RiskLevel::Safe,
    }
}

/// Elicits existence differentials by stripping the `Authorization` header from probe requests.
pub struct AuthStripElicitation;

impl Strategy for AuthStripElicitation {
    fn id(&self) -> &'static str {
        "auth-strip-elicit"
    }

    fn name(&self) -> &'static str {
        "Auth Strip Elicitation"
    }

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

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

    fn is_applicable(&self, ctx: &ScanContext) -> bool {
        ctx.headers.contains_key(http::header::AUTHORIZATION)
    }

    fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
        let mut specs = Vec::with_capacity(2);
        for method in [Method::GET, Method::HEAD] {
            let baseline_headers = ctx.headers.clone();
            let mut probe_headers = ctx.headers.clone();
            probe_headers.remove(http::header::AUTHORIZATION);
            let pair = build_pair(ctx, method, baseline_headers, probe_headers, None, metadata());
            specs.push(ProbeSpec::Pair(pair));
        }
        specs
    }
}

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

    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!(AuthStripElicitation.risk(), RiskLevel::Safe);
    }

    #[test]
    fn methods_contains_get_and_head() {
        let methods = AuthStripElicitation.methods();
        assert_eq!(methods.len(), 2);
        assert!(methods.contains(&Method::GET));
        assert!(methods.contains(&Method::HEAD));
    }

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

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

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

    #[test]
    fn all_items_are_pair_variants() {
        let specs = AuthStripElicitation.generate(&make_ctx_with_auth());
        for spec in &specs {
            assert!(matches!(spec, ProbeSpec::Pair(_)));
        }
    }

    #[test]
    fn probe_lacks_authorization_header() {
        let specs = AuthStripElicitation.generate(&make_ctx_with_auth());
        let ProbeSpec::Pair(pair) = &specs[0] else { panic!("expected Pair") };
        assert!(pair.probe.headers.get(http::header::AUTHORIZATION).is_none());
    }

    #[test]
    fn baseline_has_authorization_header() {
        let specs = AuthStripElicitation.generate(&make_ctx_with_auth());
        let ProbeSpec::Pair(pair) = &specs[0] else { panic!("expected Pair") };
        assert!(pair.baseline.headers.get(http::header::AUTHORIZATION).is_some());
    }
}