parlov-elicit 0.4.0

Elicitation engine: strategy selection and probe plan generation for parlov.
Documentation
//! `CpIfNoneMatch` -- conditional retrieval via `If-None-Match: *`.
//!
//! Sends `If-None-Match: *` on both baseline and probe requests. Per RFC 9110
//! S13.1.2 a server MUST respond 304 when a matching representation exists.
//! For a nonexistent resource the server returns 404, producing the differential.

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, clone_headers_with};

fn metadata() -> StrategyMetadata {
    StrategyMetadata {
        strategy_id: "cp-if-none-match",
        strategy_name: "Cache Probe: If-None-Match",
        risk: RiskLevel::Safe,
    }
}

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

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

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

    fn name(&self) -> &'static str {
        "Cache Probe: If-None-Match"
    }

    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 hdrs = clone_headers_with(&ctx.headers, "if-none-match", "*");
            let pair = build_pair(
                ctx,
                method,
                hdrs.clone(),
                hdrs,
                None,
                metadata(),
                technique(),
            );
            specs.push(ProbeSpec::Pair(pair));
        }
        specs
    }
}

#[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 = CpIfNoneMatch.generate(&make_ctx());
        assert_eq!(specs[0].technique().vector, Vector::CacheProbing);
    }

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

    #[test]
    fn generates_probe_with_correct_header() {
        let specs = CpIfNoneMatch.generate(&make_ctx());
        let ProbeSpec::Pair(pair) = &specs[0] else {
            panic!("expected Pair variant")
        };
        assert_eq!(pair.probe.headers.get("if-none-match").unwrap(), "*");
        assert_eq!(pair.baseline.headers.get("if-none-match").unwrap(), "*");
    }

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

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