parlov-elicit 0.3.0

Elicitation engine: strategy selection and probe plan generation for parlov.
Documentation
//! `IfNoneMatchElicitation` -- probes with `If-None-Match: *`.
//!
//! Sends `If-None-Match: *` on the probe request. Per RFC 9110 S13.1.2 a server
//! MUST respond 304 when the condition matches (resource exists), but cannot do
//! so for a nonexistent resource (404 or 412). The differential reveals existence
//! without transmitting the resource body.

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: "if-none-match-elicit",
        strategy_name: "If-None-Match Elicitation",
        risk: RiskLevel::Safe,
    }
}

fn technique() -> Technique {
    Technique {
        id: "if-none-match",
        name: "If-None-Match conditional request",
        oracle_class: OracleClass::Existence,
        vector: Vector::StatusCodeDiff,
        strength: NormativeStrength::Must,
    }
}

/// Elicits existence differentials via the `If-None-Match: *` conditional header.
pub struct IfNoneMatchElicitation;

impl Strategy for IfNoneMatchElicitation {
    fn id(&self) -> &'static str {
        "if-none-match-elicit"
    }

    fn name(&self) -> &'static str {
        "If-None-Match 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, "if-none-match", "*");
            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!(IfNoneMatchElicitation.risk(), RiskLevel::Safe);
    }

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

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

    #[test]
    fn probe_has_if_none_match_star() {
        let specs = IfNoneMatchElicitation.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("if-none-match").unwrap(), "*");
    }

    #[test]
    fn baseline_lacks_if_none_match_header() {
        let specs = IfNoneMatchElicitation.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("if-none-match").is_none());
    }

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