parlov-elicit 0.1.2

Elicitation engine: strategy selection and probe plan generation for parlov.
Documentation
//! `ScopeManipulationElicitation` — probes using an under-scoped alternate credential.
//!
//! Sends the probe request with `ctx.alt_credential` headers while the baseline
//! uses `ctx.headers` unmodified. A server that returns different status codes
//! for the under-scoped credential (e.g. 403 vs 404) reveals resource existence
//! through scope-differentiated authorization behavior.

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: "scope-manipulation-elicit",
        strategy_name: "Scope Manipulation Elicitation",
        risk: RiskLevel::Safe,
    }
}

/// Elicits existence differentials by probing with an under-scoped alternate credential.
pub struct ScopeManipulationElicitation;

impl Strategy for ScopeManipulationElicitation {
    fn id(&self) -> &'static str {
        "scope-manipulation-elicit"
    }

    fn name(&self) -> &'static str {
        "Scope Manipulation Elicitation"
    }

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

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

    fn is_applicable(&self, ctx: &ScanContext) -> bool {
        ctx.alt_credential.is_some()
    }

    fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
        let alt = match &ctx.alt_credential {
            Some(h) => h.clone(),
            None => return vec![],
        };
        let mut specs = Vec::with_capacity(2);
        for method in [Method::GET, Method::HEAD] {
            let pair = build_pair(
                ctx,
                method,
                ctx.headers.clone(),
                alt.clone(),
                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() }
    }

    fn make_ctx_with_alt_credential() -> ScanContext {
        let mut alt = HeaderMap::new();
        alt.insert(
            http::header::AUTHORIZATION,
            HeaderValue::from_static("Bearer under-scoped-token"),
        );
        ScanContext { alt_credential: Some(alt), ..make_ctx_with_auth() }
    }

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

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

    #[test]
    fn is_applicable_true_when_alt_credential_some() {
        assert!(ScopeManipulationElicitation.is_applicable(&make_ctx_with_alt_credential()));
    }

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

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

    #[test]
    fn probe_uses_alt_credential_headers() {
        let ctx = make_ctx_with_alt_credential();
        let specs = ScopeManipulationElicitation.generate(&ctx);
        let ProbeSpec::Pair(pair) = &specs[0] else { panic!("expected Pair") };
        assert_eq!(
            pair.probe.headers.get(http::header::AUTHORIZATION).unwrap(),
            "Bearer under-scoped-token"
        );
    }

    #[test]
    fn baseline_uses_ctx_headers() {
        let ctx = make_ctx_with_alt_credential();
        let specs = ScopeManipulationElicitation.generate(&ctx);
        let ProbeSpec::Pair(pair) = &specs[0] else { panic!("expected Pair") };
        assert_eq!(
            pair.baseline.headers.get(http::header::AUTHORIZATION).unwrap(),
            "Bearer token123"
        );
    }

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