parlov-elicit 0.4.0

Elicitation engine: strategy selection and probe plan generation for parlov.
Documentation
//! `EmgBola` -- BOLA body content differential.
//!
//! Sends identical GET requests to baseline and probe URLs with no header
//! manipulation. The oracle signal is a body-level difference: the server
//! returns different JSON error messages (e.g. "access denied" vs "not found")
//! for existing vs nonexistent resources while keeping the status code the same.

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

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

fn metadata() -> StrategyMetadata {
    StrategyMetadata {
        strategy_id: "emg-bola",
        strategy_name: "EMG: BOLA Body Diff",
        risk: RiskLevel::Safe,
    }
}

fn technique() -> Technique {
    Technique {
        id: "emg-bola",
        name: "BOLA body content differential",
        oracle_class: OracleClass::Existence,
        vector: Vector::ErrorMessageGranularity,
        strength: NormativeStrength::May,
    }
}

/// Elicits existence differentials via body content differences on same-status responses.
pub struct EmgBola;

impl Strategy for EmgBola {
    fn id(&self) -> &'static str {
        "emg-bola"
    }

    fn name(&self) -> &'static str {
        "EMG: BOLA Body Diff"
    }

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

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

    fn is_applicable(&self, _ctx: &ScanContext) -> bool {
        true
    }

    fn generate(&self, ctx: &ScanContext) -> Vec<ProbeSpec> {
        let pair = build_pair(
            ctx,
            Method::GET,
            ctx.headers.clone(),
            ctx.headers.clone(),
            None,
            metadata(),
            technique(),
        );
        vec![ProbeSpec::Pair(pair)]
    }
}

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

    fn make_ctx() -> 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,
        }
    }

    #[test]
    fn generates_correct_technique_vector() {
        let specs = EmgBola.generate(&make_ctx());
        assert_eq!(specs[0].technique().vector, Vector::ErrorMessageGranularity);
    }

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

    #[test]
    fn is_always_applicable() {
        assert!(EmgBola.is_applicable(&make_ctx()));
    }

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

    #[test]
    fn generates_get_method() {
        let specs = EmgBola.generate(&make_ctx());
        let ProbeSpec::Pair(pair) = &specs[0] else {
            panic!("expected Pair variant")
        };
        assert_eq!(pair.baseline.method, Method::GET);
        assert_eq!(pair.probe.method, Method::GET);
    }
}