parlov-elicit 0.4.0

Elicitation engine: strategy selection and probe plan generation for parlov.
Documentation
//! `AcceptElicitation` -- probes with an unsupported `Accept` media type.
//!
//! Sends `Accept: application/x-nonexistent` on the probe request while the
//! baseline uses the caller-supplied headers unmodified. A server that responds
//! differently (e.g. 406 vs 200, or distinct body) reveals resource existence
//! through content-negotiation handling.

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, clone_headers_with};
use crate::ScanContext;

fn metadata() -> StrategyMetadata {
    StrategyMetadata {
        strategy_id: "accept-elicit",
        strategy_name: "Accept Elicitation",
        risk: RiskLevel::Safe,
    }
}

fn technique() -> Technique {
    Technique {
        id: "accept",
        name: "Accept content negotiation",
        oracle_class: OracleClass::Existence,
        vector: Vector::StatusCodeDiff,
        strength: NormativeStrength::Should,
    }
}

/// Elicits existence differentials via an unsupported `Accept` header on probe requests.
pub struct AcceptElicitation;

impl Strategy for AcceptElicitation {
    fn id(&self) -> &'static str {
        "accept-elicit"
    }

    fn name(&self) -> &'static str {
        "Accept 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 baseline_headers = ctx.headers.clone();
            let probe_headers =
                clone_headers_with(&ctx.headers, "accept", "application/x-nonexistent");
            let pair = build_pair(
                ctx,
                method,
                baseline_headers,
                probe_headers,
                None,
                metadata(),
                technique(),
            );
            specs.push(ProbeSpec::Pair(pair));
        }
        specs
    }
}

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

    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 risk_is_safe() {
        assert_eq!(AcceptElicitation.risk(), RiskLevel::Safe);
    }

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

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

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

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

    #[test]
    fn get_pair_probe_has_accept_header() {
        let specs = AcceptElicitation.generate(&make_ctx());
        let pair = specs.iter().find_map(|s| {
            if let ProbeSpec::Pair(p) = s {
                if p.probe.method == Method::GET {
                    return Some(p);
                }
            }
            None
        });
        let pair = pair.expect("GET pair must exist");
        assert_eq!(
            pair.probe.headers.get("accept").unwrap(),
            "application/x-nonexistent"
        );
    }

    #[test]
    fn get_pair_baseline_lacks_accept_header() {
        let specs = AcceptElicitation.generate(&make_ctx());
        let pair = specs.iter().find_map(|s| {
            if let ProbeSpec::Pair(p) = s {
                if p.baseline.method == Method::GET {
                    return Some(p);
                }
            }
            None
        });
        let pair = pair.expect("GET pair must exist");
        assert!(pair.baseline.headers.get("accept").is_none());
    }

    #[test]
    fn head_pair_probe_method_is_head() {
        let specs = AcceptElicitation.generate(&make_ctx());
        let pair = specs.iter().find_map(|s| {
            if let ProbeSpec::Pair(p) = s {
                if p.probe.method == Method::HEAD {
                    return Some(p);
                }
            }
            None
        });
        let pair = pair.expect("HEAD pair must exist");
        assert_eq!(pair.probe.method, Method::HEAD);
    }

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

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