parlov-elicit 0.3.0

Elicitation engine: strategy selection and probe plan generation for parlov.
Documentation
//! `LowPrivilegeElicitation` -- probes using the operator's own credential as-is.
//!
//! Sends both the baseline and probe requests with `ctx.headers` unmodified. The
//! oracle signal is the server returning 403 for existing resources the caller
//! cannot access versus 404 for nonexistent ones.

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

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

fn metadata() -> StrategyMetadata {
    StrategyMetadata {
        strategy_id: "low-privilege-elicit",
        strategy_name: "Low Privilege Elicitation",
        risk: RiskLevel::Safe,
    }
}

fn technique() -> Technique {
    Technique {
        id: "low-privilege",
        name: "Low-privilege credential probe",
        oracle_class: OracleClass::Existence,
        vector: Vector::StatusCodeDiff,
        strength: NormativeStrength::Should,
    }
}

/// Elicits existence differentials using the caller's own credential on both sides.
pub struct LowPrivilegeElicitation;

impl Strategy for LowPrivilegeElicitation {
    fn id(&self) -> &'static str {
        "low-privilege-elicit"
    }

    fn name(&self) -> &'static str {
        "Low Privilege 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 pair = build_pair(
                ctx, method,
                ctx.headers.clone(), ctx.headers.clone(),
                None, metadata(), technique(),
            );
            specs.push(ProbeSpec::Pair(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!(LowPrivilegeElicitation.risk(), RiskLevel::Safe);
    }

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

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

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

    #[test]
    fn both_sides_carry_authorization_header() {
        let specs = LowPrivilegeElicitation.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());
        assert!(pair.probe.headers.get(http::header::AUTHORIZATION).is_some());
    }

    #[test]
    fn technique_strength_is_should() {
        let specs = LowPrivilegeElicitation.generate(&make_ctx_with_auth());
        assert_eq!(specs[0].technique().strength, NormativeStrength::Should);
    }
}